Oracle Certified Professional Java Se 11 Developer · Free Practice Question Easy
Question 105
Question ID: UKOCP42141
Given code of Test.java file:
- package com.udayankhattry.ocp;
- public class Test {
- static String str = "KEEP IT "; //Line n1
- public static void main(String[] args) {
- String str = str + "SIMPLE"; //Line n2
- System.out.println(str);
- }
- }
What will be the result of compiling and executing Test class?
- A KEEP IT
- B KEEP IT SIMPLE
- C SIMPLE
- D Compilation error
Reveal correct answer
Correct answer: D
Explanation
UKOCP42141:
At Line n2, local variable 'str' shadows the static variable 'str' created at Line n1. Hence, for the expression str + "SIMPLE", Java compiler complains as local variable 'str' is not initialized.
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.
