0% found this document useful (0 votes)
134 views9 pages

Multiple Choice Questions Based On Switch Construct

The document contains multiple choice questions related to the switch construct in Java, covering syntax, data types, behavior of break statements, and other functionalities. Each question is followed by the correct answer, providing insights into how switch statements work in Java programming. The questions also explore various scenarios and edge cases, enhancing understanding of the switch statement's behavior.

Uploaded by

radhikadaksh1983
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views9 pages

Multiple Choice Questions Based On Switch Construct

The document contains multiple choice questions related to the switch construct in Java, covering syntax, data types, behavior of break statements, and other functionalities. Each question is followed by the correct answer, providing insights into how switch statements work in Java programming. The questions also explore various scenarios and edge cases, enhancing understanding of the switch statement's behavior.

Uploaded by

radhikadaksh1983
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Multiple Choice Questions based on switch construct C) break

1. What is the correct syntax of a switch statement in Java? D) return


A) switch (expression) { case value: statements; break; } Answer: C) break
B) switch { case expression: statements; break; }
C) switch (expression) case value: statements; break; 8. Can a switch statement work with an enum in Java?
D) case (expression) switch { statements; break; } A) Yes
Answer: A) switch (expression) { case value: statements; B) No
break; } C) Only with int values
D) Only with String values
2. What type of values can be used in a switch statement in Answer: A) Yes
Java?
A) int, char, byte, short, String, enum 9. Can multiple case labels have the same value?
B) float, double, boolean A) Yes
C) Arrays B) No
D) Objects of any class C) Only if break is used
Answer: A) int, char, byte, short, String, enum D) Only in Java 8 and above
Answer: B) No
3. What happens if the break statement is omitted in a switch
case? 10. What will happen if there is no default case in a switch
A) Compilation error statement?
B) Only the matched case executes A) Compilation error
C) Fall-through occurs, executing subsequent cases B) The switch will not execute
D) The program crashes C) Nothing, it is optional
Answer: C) Fall-through occurs, executing subsequent cases D) The program crashes
Answer: C) Nothing, it is optional
4. What is the purpose of the default case in a switch
statement? 11. What is the maximum number of case labels allowed in a
A) It must be included in all switch statements switch statement?
B) It executes when no case matches the expression A) 100
C) It terminates the switch statement B) 255
D) It is optional and only runs if break is missing C) No fixed limit
Answer: B) It executes when no case matches the expression D) 10
Answer: C) No fixed limit
5. Which data type is NOT allowed in a switch statement?
A) char 12. Can a switch statement contain duplicate case values?
B) String A) Yes
C) boolean B) No
D) int C) Only with String values
Answer: C) boolean D) Only with int values
Answer: B) No
6. What will be the output of the following code?
int x = 2; 13. What is the output of this code?
switch(x) int a = 5;
{ switch(a)
case 1: {
System.out.println("One"); case 5:
case 2: case 6:
System.out.println("Two"); System.out.println("Match found");
case 3: break;
System.out.println("Three"); default:
} System.out.println("No match");
A) Two }
B) Two Three A) Match found
C) Two Three One B) No match
D) Compilation error C) Compilation error
Answer: B) Two Three (fall-through occurs) D) Runtime error
Answer: A) Match found
7. Which keyword is used to exit a switch case?
A) continue 14. What happens if the break statement is missing in all case
B) exit blocks?
A) Compilation error 21. What will be the output of the following code?
B) Fall-through behavior int x = 3;
C) Only the first matching case executes switch(x)
D) The program stops immediately {
Answer: B) Fall-through behavior case 1:
System.out.println("One");
15. What will be the output of this code? case 2:
char grade = 'B'; System.out.println("Two");
switch(grade) case 3:
{ System.out.println("Three");
case 'A': default:
System.out.println("Excellent"); System.out.println("Default");
case 'B': }
System.out.println("Good"); A) Three
case 'C': B) Three Default
System.out.println("Average"); C) Three Two One
} D) Compilation Error
A) Good Answer: B) Three Default (fall-through occurs because there
B) Good Average are no break statements)
C) Excellent Good Average
D) Compilation error 22. What will be the output of this code?
Answer: B) Good Average (fall-through occurs) int num = 2;
switch(num)
16. Can we use a long data type in a switch statement? {
A) Yes case 1:
B) No System.out.println("One");
C) Only with break break;
D) Only with default case 2:
Answer: B) No System.out.println("Two");
case 3:
17. What happens if a case contains no statements and no System.out.println("Three");
break? break;
A) Compilation error }
B) The next case executes A) Two
C) Only the default case runs B) Two Three
D) The program crashes C) Compilation Error
Answer: B) The next case executes D) No Output
Answer: B) Two Three (fall-through due to missing break after
18. Can case values be variables? case 2)
A) Yes
B) No 23. What will be the output of this code?
C) Only if they are final constants
D) Only for String type int a = 5;
Answer: C) Only if they are final constants switch(a + 2)
{
19. What is the scope of a variable declared inside a switch case 6:
case? System.out.println("Six");
A) Global break;
B) Local to that case block case 7:
C) It depends on the default case System.out.println("Seven");
D) Can be accessed outside switch break;
Answer: B) Local to that case block default:
System.out.println("Default");
20. Can a switch statement be nested inside another switch? }
A) Yes A) Six
B) No B) Seven
C) Only with int values C) Default
D) Only in Java 11 and above D) Compilation Error
Answer: A) Yes Answer: B) Seven (since a + 2 = 7 matches case 7)

24. What is the output of this code?


int n = 10; case "Red":
switch(n) System.out.println("Red");
{ case "Green":
default: System.out.println("Green");
System.out.println("Default case"); }
case 5: A) Red Green
System.out.println("Five"); B) Red
case 10: C) Red Green Blue
System.out.println("Ten"); D) Compilation Error
break; Answer: A) Red Green (fall-through occurs because there is no
} break after case "Red")
A) Default case Five Ten
B) Ten 28. What will be the output of this code?
C) Default case Ten int x = 4;
D) Compilation Error switch(x)
Answer: C) Default case Ten (fall-through from default to case {
10 since there's no break before it) case 2:
System.out.println("Two");
25. What will be the output of this code? break;
char ch = 'B'; case 4:
switch(ch) System.out.println("Four");
{ default:
case 'A': System.out.println("Default");
System.out.println("Apple"); }
case 'B': A) Four Default
System.out.println("Banana"); B) Four
break; C) Default
case 'C': D) Compilation Error
System.out.println("Cherry"); Answer: A) Four Default (fall-through occurs since there's no
} break after case 4)
A) Banana
B) Apple Banana 29. What will be the output of this code?
C) Banana Cherry int number = 5;
D) Compilation Error switch(number)
Answer: A) Banana (executes case 'B' and stops due to break) {
case 2:
26. What will be the output of this code? case 4:
int val = 3; System.out.println("Even");
switch(val * 2) break;
{ case 3:
case 4: case 5:
System.out.println("Four"); System.out.println("Odd");
break; break;
case 6: default:
System.out.println("Six"); System.out.println("Unknown");
break; }
default: A) Odd
System.out.println("Default"); B) Even
} C) Unknown
A) Four D) Compilation Error
B) Six Answer: A) Odd (case 5 matches and prints Odd)
C) Default
D) Compilation Error 30. What will be the output of this code?
Answer: B) Six (since val * 2 = 6 matches case 6) int num = 7;
switch(num)
27. What is the output of this code? {
String color = "Red"; case 5:
switch(color) System.out.println("Five");
{ case 6:
case "Blue": System.out.println("Six");
System.out.println("Blue"); default:
System.out.println("Default"); System.out.println("Cherry");
} default:
A) Five Six System.out.println("Default");
B) Six }
C) Default A) Cherry Default
D) Compilation Error B) Cherry
Answer: C) Default (case 7 is missing, so default executes) C) Apple Banana Cherry
D) Compilation Error
31. What will be the output of this code? Answer: A) Cherry Default (fall-through occurs since there is
int x = 2; no break after case 'C')
switch(x)
{ 34. What will be the output of this code?
case 1: int z = 5;
System.out.println("One"); switch(z)
case 2: {
System.out.println("Two"); default:
case 3: System.out.println("Default");
System.out.println("Three"); break;
case 4: case 5:
System.out.println("Four"); System.out.println("Five");
break; }
default: A) Five
System.out.println("Default"); B) Default
} C) Default Five
A) Two Three Four D) Compilation Error
B) Two Three Four Default Answer: A) Five (The default case executes only if no matching
C) Two case is found, but case 5 matches and executes instead.)
D) Compilation Error
Answer: A) Two Three Four (fall-through happens from case 2 35. What will be the output of this code?
to case 4, then break stops execution) String fruit = "Mango";
switch(fruit)
32. What will be the output of this code? {
int y = 3; case "Apple":
switch(y + 1) System.out.println("Apple");
{ break;
case 2: case "Mango":
System.out.println("Two"); System.out.println("Mango");
case 3: case "Banana":
System.out.println("Three"); System.out.println("Banana");
case 4: break;
System.out.println("Four"); default:
break; System.out.println("Default");
default: }
System.out.println("Default"); A) Mango
} B) Mango Banana
A) Three Four C) Mango Banana Default
B) Four D) Compilation Error
C) Default Answer: B) Mango Banana (fall-through occurs from case
D) Compilation Error "Mango" to case "Banana" since break is missing after case
Answer: B) Four (since y + 1 = 4, it starts execution from case 4 "Mango")
and stops due to break)
36. What will be the output of this code?
33. What is the output of this code? int num = 8;
char ch = 'C'; switch(num)
switch(ch) {
{ case 8:
case 'A': System.out.println("Eight");
System.out.println("Apple"); case 9:
case 'B': System.out.println("Nine");
System.out.println("Banana"); default:
case 'C': System.out.println("Default");
} {
A) Eight case true:
B) Eight Nine System.out.println("True");
C) Eight Nine Default break;
D) Default case false:
Answer: C) Eight Nine Default (fall-through occurs since there System.out.println("False");
is no break after case 8 or case 9) break;
default:
37. What will be the output of this code? System.out.println("Invalid");
int day = 4; }
switch(day) A) True
{ B) False
case 1: C) Invalid
case 2: D) Compilation Error
case 3: Answer: D) Compilation Error (switch cannot be used with
System.out.println("Weekday"); boolean values.)
break;
case 4: 40. What will be the output of this code?
case 5: int num = 2;
System.out.println("Almost Weekend"); switch(num)
break; {
case 6: case 1:
case 7: System.out.println("One");
System.out.println("Weekend"); case 2:
break; System.out.println("Two");
default: case 3:
System.out.println("Invalid Day"); System.out.println("Three");
} break;
A) Weekday case 4:
B) Almost Weekend System.out.println("Four");
C) Weekend }
D) Invalid Day A) Two Three
Answer: B) Almost Weekend (since day = 4, it executes case 4 B) Two
and case 5, but stops at break) C) Two Three Four
D) Compilation Error
38. What will be the output of this code? Answer: A) Two Three (fall-through occurs since case 2 has no
int score = 75; break before case 3.)
switch(score / 10)
{
case 9: 41. What will be the output of this code?
System.out.println("A Grade"); int a = 10, b = 20;
break; switch(a + b)
case 8: {
System.out.println("B Grade"); case 30:
break; System.out.println("Thirty");
case 7: break;
System.out.println("C Grade"); case 40:
break; System.out.println("Forty");
default: break;
System.out.println("Fail"); default:
} System.out.println("Default");
A) A Grade }
B) B Grade A) Thirty
C) C Grade B) Forty
D) Fail C) Default
Answer: C) C Grade (since score / 10 = 7, it executes case 7 and D) Compilation Error
prints C Grade.) Answer: A) Thirty (since a + b = 30, case 30 executes.)

39. What is the output of this code? 42. What will be the output of this code?
boolean flag = true; int x = 5;
switch(flag) switch(x)
{ System.out.println("One or Two");
case 5: break;
System.out.println("Five"); case 3:
case 10: System.out.println("Three");
System.out.println("Ten"); break;
break; default:
case 15: System.out.println("Default");
System.out.println("Fifteen"); }
} A) One or Two
A) Five B) Three
B) Five Ten C) Default
C) Five Ten Fifteen D) Compilation Error
D) Compilation Error Answer: A) One or Two (both case 1 and case 2 execute the
Answer: B) Five Ten (fall-through occurs since case 5 has no same statement.)
break, so case 10 executes too.)
46. What will be the output of this code?
43. What will be the output of this code? int num = 6;
String str = "Hello"; switch(num % 3)
switch(str) {
{ case 0:
case "Hi": System.out.println("Divisible by 3");
System.out.println("Hi"); break;
case "Hello": case 1:
System.out.println("Hello"); System.out.println("Remainder 1");
case "Hey": break;
System.out.println("Hey"); default:
break; System.out.println("Default");
default: }
System.out.println("Default"); A) Divisible by 3
} B) Remainder 1
A) Hello C) Default
B) Hello Hey D) Compilation Error
C) Hello Hey Default Answer: A) Divisible by 3 (since 6 % 3 = 0, case 0 executes.)
D) Compilation Error
Answer: B) Hello Hey (fall-through occurs from case "Hello" to 47. What will be the output of this code?
case "Hey".) int num = 5;
switch(num)
44. What will be the output of this code? {
int num = 9; default:
switch(num) System.out.println("Default");
{ break;
case 5: case 5:
System.out.println("Five"); System.out.println("Five");
case 10: }
System.out.println("Ten"); A) Five
default: B) Default
System.out.println("Default"); C) Default Five
} D) Compilation Error
A) Five Answer: A) Five (default only executes if no case matches, but
B) Ten case 5 matches.)
C) Default
D) Compilation Error 48. What will be the output of this code?
Answer: C) Default (num = 9 does not match any case, so int num = 4;
default executes.) switch(num)
{
45. What will be the output of this code? case 2:
int num = 1; System.out.println("Two");
switch(num) case 4:
{ System.out.println("Four");
case 1: break;
case 2: case 6:
System.out.println("Six"); break;
} default:
A) Four System.out.println("Default");
B) Two Four }
C) Four Six A) Seven
D) Compilation Error B) Seven Eight
Answer: A) Four (case 4 matches and executes, stopping at C) Seven Eight Default
break.) D) Compilation Error
Answer: B) Seven Eight (fall-through occurs from case 7 to
49. What will be the output of this code? case 8, then break stops execution.)
int num = 3;
switch(num + 2) 52. What will be the output of this code?
{ int num = 10;
case 4: switch(num % 4)
System.out.println("Four"); {
break; case 0:
case 5: System.out.println("Divisible by 4");
System.out.println("Five"); case 2:
default: System.out.println("Remainder 2");
System.out.println("Default"); break;
} default:
A) Five System.out.println("Default");
B) Five Default }
C) Four A) Divisible by 4
D) Compilation Error B) Remainder 2
Answer: B) Five Default (fall-through occurs from case 5 to C) Divisible by 4 Remainder 2
default.) D) Default
Answer: C) Divisible by 4 Remainder 2 (fall-through occurs
50. What will be the output of this code? from case 0 to case 2.)
int num = 1;
switch(num) 53. What will be the output of this code?
{ int num = 5;
case 1: switch(num)
System.out.println("Case 1"); {
case 2: default:
System.out.println("Case 2"); System.out.println("Default");
default: case 5:
System.out.println("Default"); System.out.println("Five");
} break;
A) Case 1 }
B) Case 1 Case 2 A) Default
C) Case 1 Case 2 Default B) Five
D) Compilation Error C) Default Five
Answer: C) Case 1 Case 2 Default (fall-through occurs since D) Compilation Error
there are no break statements.) Answer: B) Five (default does not execute because case 5 is
matched first.)

51. What will be the output of this code? 54. What will be the output of this code?
int num = 7; int num = 3;
switch(num) switch(num)
{ {
case 1: case 1:
System.out.println("One"); System.out.println("One");
break; case 3:
case 3: System.out.println("Three");
System.out.println("Three"); case 5:
break; System.out.println("Five");
case 7: default:
System.out.println("Seven"); System.out.println("Default");
case 8: }
System.out.println("Eight");
A) Three default:
B) Three Five System.out.println("Default");
C) Three Five Default }
D) Compilation Error A) Number is 1, 2, or 3
Answer: C) Three Five Default (fall-through occurs from case 3 B) Number is 1, 2, or 3 Default
onward.) C) Four
D) Compilation Error
55. What will be the output of this code? Answer: A) Number is 1, 2, or 3 (since case 1, case 2, and case
int num = 9; 3 share a statement, it prints once and then break stops
switch(num % 3) execution.)
{
case 0: 58. What will be the output of this code?
System.out.println("Divisible by 3"); int num = 4;
break; switch(num)
case 1: {
System.out.println("Remainder 1"); case 4:
break; System.out.println("Four");
case 2: break;
System.out.println("Remainder 2"); default:
break; System.out.println("Default");
default: case 5:
System.out.println("Default"); System.out.println("Five");
} }
A) Divisible by 3 A) Four
B) Remainder 1 B) Four Default
C) Remainder 2 C) Four Five
D) Default D) Compilation Error
Answer: A) Divisible by 3 (since 9 % 3 == 0, it executes case 0.) Answer: A) Four (case 4 executes, but break prevents fall-
through.)
56. What will be the output of this code?
char ch = 'B'; 59. What will be the output of this code?
switch(ch) String word = "Code";
{ switch(word)
case 'A': {
System.out.println("Apple"); case "Code":
break; System.out.println("Programming");
case 'B': break;
System.out.println("Banana"); case "Debug":
case 'C': System.out.println("Testing");
System.out.println("Cherry"); default:
default: System.out.println("Default");
System.out.println("Default"); }
} A) Programming
A) Banana B) Programming Testing
B) Banana Cherry C) Programming Default
C) Banana Cherry Default D) Compilation Error
D) Compilation Error Answer: A) Programming (case "Code" executes, then break
Answer: C) Banana Cherry Default (fall-through occurs from stops further execution.)
case 'B' onward.)
60. What will be the output of this code?
57. What will be the output of this code? int num = 2;
int num = 1; switch(num * 2)
switch(num) {
{ case 4:
case 1: System.out.println("Four");
case 2: case 6:
case 3: System.out.println("Six");
System.out.println("Number is 1, 2, or 3"); default:
break; System.out.println("Default");
case 4: }
System.out.println("Four");
A) Four
B) Four Six
C) Four Six Default
D) Compilation Error
Answer: C) Four Six Default (fall-through occurs from case 4
onward.)

You might also like