public static void main(String[] args) {
char colorCode = 'y';
switch (colorCode) {
case 'r':
int color = 100;
break;
case 'b':
color = 10;
break;
case 'y':
color = 1;
break;
}
System.out.println(color);
}
The correct answer is the letter A, it is the typical case of differentiating between local variable and instance variable, as the local variable "color" was instantiated inside the switch, the code inside the switch compiles normally, but when exiting siwtch and trying printing the variable inside the class cannot because there is no variable created with that name, as "color" is a local variable, its instantiation is only valid locally inside the switch and not outside it.
upvoted 1 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
jackymak
7 months, 1 week agoRavisai_7
3 months agoRavisai_7
3 months agoiSnover
1 year, 3 months ago