The switch variable can be of

int type only
char type only
both int as well as char type
float type only E. None of the above

The correct answer is: C. both int as well as char type.

A switch statement is a control flow statement that allows you to execute a different block of code for each value of a variable. The switch variable can be of any integral type, including int, char, and short. It can also be of enum type.

Here is an example of a switch statement that uses an int variable:

“`
int x = 5;

switch (x) {
case 1:
System.out.println(“x is 1”);
break;
case 2:
System.out.println(“x is 2”);
break;
case 3:
System.out.println(“x is 3”);
break;
default:
System.out.println(“x is not 1, 2, or 3”);
}
“`

Here is an example of a switch statement that uses a char variable:

“`
char c = ‘a’;

switch (c) {
case ‘a’:
System.out.println(“c is ‘a'”);
break;
case ‘b’:
System.out.println(“c is ‘b'”);
break;
case ‘c’:
System.out.println(“c is ‘c'”);
break;
default:
System.out.println(“c is not ‘a’, ‘b’, or ‘c'”);
}
“`

Here is an example of a switch statement that uses an enum type:

“`
enum Color {
RED, GREEN, BLUE
}

Color c = Color.RED;

switch (c) {
case RED:
System.out.println(“c is RED”);
break;
case GREEN:
System.out.println(“c is GREEN”);
break;
case BLUE:
System.out.println(“c is BLUE”);
break;
default:
System.out.println(“c is not RED, GREEN, or BLUE”);
}
“`

Exit mobile version