Oracle Certified Professional Java Se 11 Developer · Free Practice Question Easy

Question 2

Question ID: UKOCP21636


Consider below code of Test.java file:


What is the result of compiling and executing Test class?

  • A Only Line n1 causes compilation error
  • B Only Line n2 causes compilation error
  • C Only Line n3 causes compilation error
  • D Only Line n1 and Line n3 cause compilation error
  • E Line n1, Line n2 and Line n3 cause compilation error
  • F Code compiles successfully and prints 200 on to the console
  • G

    Code compiles successfully and prints 100100 on to the console

Reveal correct answer

Correct answer: G

Explanation

UKOCP21636:

Local variable Type inference was added in JDK 10.

Reserved type name var is allowed in JDK 10 onwards for local variable declarations with initializers, enhanced for-loop indexes, and index variables declared in traditional for loops. For example,

var x = "Java"; //x infers to String

var m = 10; //m infers to int


The identifier var is not a keyword, hence var can still be used as variable name, method name or package name but it cannot be used as a class or interface name.


For Line n1, x infers to String type.

For Line n2, y infers to int type.

As, x is of String type and y is of int type, hence x + y results in "100100".


For more information on local variable type inference, please check the URL: http://openjdk.java.net/jeps/286

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need