Computer Applications
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");
}
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");
}
Answer
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");
}
Related Questions
What will be the value of 'n' after the execution of the code given below?
int x=2, m=1, c=-1; int n = x + c; n = n - c + x; System.out.println(n);
What will be the output of the following code?
int x=2,y=5,a=0; a=x; x=y; y=a; System.out.println("x=" + x + " y=" + y);
Create a program to find out if the number entered by the user is a two, three or four digits number.
Sample input: 1023
Sample output: 1023 is a 4 digit number.Write a program to find the number of and sum of all integers greater than 500 and less than 1000 that are divisible by 17