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

Question 43

Question ID: UKOCP63462


Consider below code of Test.java file:


What will be the result of compiling and executing Test class?

  • A 0
  • B 1
  • C 3
  • D 5
  • E 6
  • F Compilation error at Line n1
  • G

    Compilation error at Line n2

  • H

    Compilation error at Line n3

  • I

    Compilation error at Line n4

Reveal correct answer

Correct answer: H

Explanation

UKOCP63462:

Line n1 correctly declares an Object array. Line n1 doesn't cause any compilation error.


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, package name or loop's label but it cannot be used as a class or interface name.

At Line n2, variable 'obj' infers to Object type, so no issues at Line n2.


if-else block uses break; and continue; statements. break; will exit the loop and will take the control to Line n4 on the other hand continue; will take the control to Line n2. In both the cases Line n3 will never be executed.

As Compiler knows about it, hence it tags Line n3 as unreachable, which causes compilation error.

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