Oracle Certified Professional Java Se 11 Developer · Free Practice Question Easy
Question 68
Question ID: UKOCP37160
Given code:
- package com.udayankhattry.ocp;
- public class Test {
- public static void main(String[] args) {
- /*INSERT*/
- switch(var) {
- case 10:
- System.out.println("TEN");
- break;
- default:
- System.out.println("DEFAULT");
- }
- }
- }
For the class Test, which options, if used to replace /*INSERT*/, will print TEN on to the console?
Select 4 options.
-
A
byte var = 10; -
B
long var = 10; -
C
Short var = 10; -
D
Integer var = 10; -
E
char var = 10; -
F
double var = 10;
Reveal correct answers
Correct answers: A, C, D, E
Explanation
UKOCP37160:
switch can accept primitive types: byte, short, int, char; wrapper types: Byte, Short, Integer, Character; String and enums. In this case long and double are invalid values to be passed in switch expression. char uses 16 bits (2 Bytes) and its range is 0 to 65535 (no signed bit reserved) so it can easily store value 10.
Please note that 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.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
