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

Question 97

Question ID: UKOCP75077


Consider below code of TestSquare.java file:


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

  • A It prints 0 on to the console
  • B It prints 5 on to the console
  • C It prints 10 on to the console
  • D It prints null on to the console
  • E Compilation error
  • F An exception is thrown at runtime
Reveal correct answer

Correct answer: B

Explanation

UKOCP75077:

As both the classes: Square and TestSquare are in the same file, hence variables 'length' and 'sq' can be accessed using dot operator. Given code compiles successfully.


Line n1 creates an instance of Square class and 'sq1' refers to it. sq1.length = 10 and sq1.sq = null.

Line n2 creates an instance of Square class and 'sq2' refers to it. sq2.length = 5 and sq2.sq = null.


On execution of Line n3, sq1.sq = sq2.


Line n4: System.out.println(sq1.sq.length); => System.out.println(sq2.length); => Prints 5 on to the console.

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