ICSE Class 10 Computer Applications
Question 16 of 43
Conditional Constructs in Java — Question 16
Back to all questions 16
Question Question 16
Find the errors in the following code and rewrite the correct version:
char m="A";
Switch ("A");
{
Case 'a';
System.out.println("A");
break;
Case 'b';
System.out.println("B");
break;
Default:
System.out.println("Not a valid option");
}The code has the following errors:
- char variable m is assigned a String literal.
- S in Switch is capital.
- There should be no semicolon (;) after switch statement.
- Expression of switch statement is a String literal "A".
- C in Case is capital.
- Semicolon (;) of case statements should be replaced with colon (:).
- D in Default is capital.
Corrected Version:
char m = 'A';
switch (m) {
case 'a':
System.out.println("A");
break;
case 'b':
System.out.println("B");
break;
default:
System.out.println("Not a valid option");
}