Java 9 - Interview Questions



Dear readers, these Java 9 Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Java 9. As per my experience good interviewers hardly plan to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer −

JAVA 9 (aka jdk 1.9) is a major release of JAVA programming language development. Its initial version was released on 21 Sep 2017. The main goals of Java 9 release are −

  • To make JDK and Java Standard Edition platform modular based in the sense that it can be scalled down to small computing devices well.

  • To improve the overall security of the JDK and Java Implementations.

  • To make build process and maintainance of java code libraries and large applications easy for for JAVA SE and EE platforms.

  • To design and implement a standard module system for the Java Platform which can be applied on both Platform and JDK easily.

There are 90+ enhancements added to Java 8, the most significant ones are mentioned below −

  • Module − A new kind of Java programing component introduced as module, which is a named, self-describing collection of code and data.

  • REPL (JShell) − Read-Eval-Print Loop (REPL) capability added to the Java platform.

  • HTTP 2 Client − new HTTPClient API supporting websockets and HTTP 2 streams and server push features.

  • Improved JavaDocs − Supports HTML5 output generation. Provides a search box to generated API documentation.

  • Multirelease JAR − Enhances the JAR format so that multiple, Java release-specific versions of class files can coexist in a single archive.

In Java 9, a new kind of programming component called module has been introduced. A module is a self-describing collection of code and data and has a name to identify it.

With the Modules component, following enhancements has been added in Java 9 −

  • A new optional phase,link time, is introduced. This phase is in-between compile time and run time. During this phase, a set of modules can be assembled and optimized, making a custom runtime image using jlink tool.

  • javac, jlink, and java have additional options to specify module paths, which further locate definitions of modules.

  • JAR format updated as modular JAR, which contains module-info.class file in its root directory.

  • JMOD format introduced, a packaging format (similar to JAR) which can include native code and configuration files.

By convention, the source code of a module to lie in same directory which is the name of the module.

REPL stands for Read-Eval-Print Loop.

With JShell, java has REPL capability. Using JShell, we can code and test java based logic without compiling using javac and see the result of calculations directly.

Run the javadoc tool of jdk 9 with -html5 flag to generate new type of documentation.

In java 9, a new feature is introduced where a jar format has been enhanced to have different versions of java class or resources can be maintained and used as per the platform.

Yes! Following command will create a multi-release jar for java 7 and java 9 version.

$ jar -c -f test.jar -C java7 . --release 9 -C java9 .

Syntax is same on both java versions, result will be different. Run with JDK 7.

C:\JAVA > java -cp test.jar com.tutorialspoint.Tester
Inside Java 7

Run with JDK 9.

C:\JAVA > java -cp test.jar com.tutorialspoint.Tester
Inside Java 9

With Java 9, new factory methods are added to List, Set and Map interfaces to create immutable instances. These factory methods are convenience factory methods to create a collection in less verbose and in concise way.

With java 9, following methods are added to List, Set and Map interfaces along with their overloaded counterparts.

static <E> List<E> of(E e1, E e2, E e3);
static <E> Set<E>  of(E e1, E e2, E e3);
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3);
static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)

Points to Note

  • For List and Set interfaces, of(...) method is overloaded to have 0 to 10 parameters and one with var args parameter.

  • For Map interface, of(...) method is overloaded to have 0 to 10 parameters.

  • In case of more than 10 paramters for Map interface, ofEntries(...) method can be used accepting var args parameter.

The following is the default project structure:

  • The database scripts are stored in the db folder.

  • The java source code is stored in the src folder.

  • The images, js, META-INF, styles (css) are stored in the war folder.

  • The JSPs are stored in the jsp folder.

  • The third party jar files are stored in the lib folder.

  • The java class files are stored in the WEB-INF\classes folder.

With Java 9 interfaces can have following type of variables/methods.

  • Constant variables
  • Abstract methods
  • Default methods
  • Static methods
  • Private methods
  • Private Static methods

In Java 9 Process API which is responsible to control and manage operating system processes has been improved considerably. ProcessHandle Class now provides process's native process ID, start time, accumulated CPU time, arguments, command, user, parent process, and descendants.

ProcessHandle class provides method to check processes' liveness and to destroy processes. It has onExit method, the CompletableFuture class can perform action asynchronously when process exits.

Streams were introduced in Java to help developers perform aggregate operations from a sequence of objects. With Java 9, few more methods are added to make streams better.

  • takeWhile
  • dropWhile
  • iterate
  • ofNullable

takeWhile method takes all the values until the predicate returns false. It returns, in case of ordered stream, a stream consisting of the longest prefix of elements taken from this stream matching the given predicate.

dropWhile method throw away all the values at the start until the predicate returns true. It returns, in case of ordered stream, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements matching the given predicate.

iterate method now has hasNext predicate as paremeter which stops the loop once hasNext predicate returns false.

ofNullable method is introduced to prevent NullPointerExceptions and to avoid null checks for streams. This method returns a sequential Stream containing single element, if non-null, otherwise returns an empty Stream.

The try-with-resources statement is a try statement with one or more resources duly declared. Here resource is an object which should be closed once it is no more required. The try-with-resources statement ensures that each resource is closed after the requirement finishes. Any object implementing java.lang.AutoCloseable or java.io.Closeable, interface can be used as a resource.

With Java 9, two new enhancements are made to @Deprecated annotation.

  • forRemoval − Indicates whether the annotated element is subject to removal in a future version. The default value is false.

  • since − Returns the version in which the annotated element became deprecated. The default value is the empty string.

In java 9, it can be used with annonymous class as well to simplify code and improves readability.

Optional Class was introduced in Java 8 to avoid null checks and NullPointerException issues. In java 9, three new methods are added to improve its functionality.

  • stream()
  • ifPresentOrElse()
  • or()

With Java 9, a new multi-resolution image API has been introduced which supports multiple images with different resolution variants. This API allows a set of images with different resolution to be used as a single multi-resolution image. Following are major operations of multi-resolution image.

  • Image getResolutionVariant(double destImageWidth, double destImageHeight) − Gets a specific image which is best variant to represent this logical image at the indicated size.

  • List<Image> getResolutionVariants() − Gets a readable list of all resolution variants.

CompletableFuture class was introduced in Java 8 to represent the Future which can be completed by setting its value and status explicity. It can be used as java.util.concurrent.CompletionStage. It supports dependent functions and actions which got triggered upon the future's completion. In java 9 CompletableFuture API has been enhanced further. Following are the relevant changes done to the API.

  • Support for delays and timeouts.
  • Improved support for subclassing.
  • New factory methods added.

What is Next?

Further, you can go through your past assignments you have done with the subject and make sure you are able to speak confidently on them. If you are fresher then interviewer does not expect you will answer very complex questions, rather you have to make your basics concepts very strong.

Second it really doesn't matter much if you could not answer few questions but it matters that whatever you answered, you must have answered with confidence. So just feel confident during your interview. We at tutorialspoint wish you best luck to have a good interviewer and all the very best for your future endeavor. Cheers :-)

java9_questions_answers.htm
Advertisements