In my previous article, I have published how to prepare ArrayList topic for OCAJP exam. I am writing a series of articles on how to prepare for OCAJP certification exam. Also, read about the sample questions on Lambda Expressions.
Are you looking for mock exam questions to prepare for OCAJP 8 exam, please try these free mock exam practice questions for OCAJP exam?
OCAJP – Switch statement practice questions
1. Given
switch (a) { case 1: System.out.println("1"); break; case 4: System.out.println("4"); break; case 2: System.out.println("2"); break; case 5: System.out.println("5"); break; }
Which of the following statements can be inserted in place of //insert code here So that the above code compiles and runs?
- A. int a = 1;
- B. long a = 1;
- C. Byte a = 1;
- D. float a =1;
- E. Integer a = new Integer(“1”);
- F. compile time error.
2. Which of the following are valid statements about switch ?
- A. switch expression of type int and case label value of type char are correct combination.
- B. switch expression of type byte and case label value of type float are correct combination.
- C. switch expression of type char and case label value of type byte are correct combination.
- D. All combinations are wrong.
3. What is the output?
public class SwitchDemo { public static void main(String args[]) { boolean b = true; switch (b) { case true: System.out.println("true"); break; case false: System.out.println("false"); default: System.out.println("true false"); } } }
- A. true
- B. false
- C. true false
- D. Compile time error
- E. false
4. What data type of “value” is allowed so that it compiles and runs?
XXXX value = 50 ; switch (value) { case 32: System.out.println("32"); break; case 50: System.out.println("50"); break; case 150: System.out.println("50"); break; case 250: System.out.println("50"); }
- A. int value = 50;
- B. long value = 50;
- C. char value = 50;
- D. byte value = 50;
- E. short value = 50;
5. What changes can be done to make this program compile and run successfully ?
public class SwitchDemo { public static void main(String args[]) { boolean value = true; //line 1 switch (value) { case true: // line 2 System.out.println("true"); break; default: System.out.println("default"); } } }
- A. No change required.
- B. Replace line 1 with String value = “true”;
- C. Replace line 2 with the case “true” :
- D. Replace both line 1 with String value = “true”; and line 2 with the case “true” :
6. What is the output?
public class SwitchDemo { public static void main(String args[]) { final int a = 5; final int b = 6; final int c; c = 8; switch (a) { case a: System.out.println("a"); break; case b: System.out.println("b"); break; case c: System.out.println("c"); break; default: System.out.println("none"); } } }
- A.
- B. b
- C. c
- D. none
- E. Compile time error.
7. What is the output?
String day = "Sun"; switch (day) { case "Mon": System.out.println("Monday"); case "Tue": System.out.println("Tuesday"); break; case "Sun": System.out.println("Sunday"); default: System.out.println("Friday"); }
- A. Monday
- B. Tuesday
- C. Sunday
- D. Friday
- E. Compile time error because String is not allowed.
Answers
1. Correct options : A,C,E. A, C, E are correct because int, byte, short, char and their wrapper classes are allowed as a data type of switch parameter data type. B, D are wrong options because long, float, double, boolean are not allowed by a switch statement.
2. Correct option: A. To answer this question you need to remember one rule about the switch.
Switch variable must able hold to all the case constants. For example, byte ranges from -128 to 127. So all case values must be within that range. A is correct because char values are in the range of it.
3. Correct option: D. D is correct because boolean data type is not allowed by the switch.
4. Correct options: A,C,E. A, C, E are correct because int, char, short can hold all case label values. B is wrong because long is not allowed. D is also wrong because byte range is -128 to 127.
5. Correct option: D. boolean data type is not allowed by the switch. So you need to change at line 1 and line 2.
6. Correct option: E. Because c is a final variable but it is initialized in the next line. So it is evaluated at runtime. so the whole expression evaluated at runtime. So it is not compiled time constant. It gives compile time error.
7. Correct option: D. Once case constant is matched, JVM will execute the associated case branch and subsequent code blocks if there is no break statement after first matched case.
References
If you are preparing for the OCAJP certification exam, then the following resources will be useful to you.
- JavaBeat’s OCAJP Guide
- OCAJP Mock Exams (Java Basics)
- Lambda Expressions Practice Questions
- How to prepare for OCAJP Certification Exam?
- What are the good books for OCAJP Exam?