This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Which type cannot traditionally be used as a switch expression in standard Java switch statements?
Option D
In traditional Java switch statements, the switch expression can use integral types such as byte, short, char, and int, as well as their corresponding wrapper types. Java also supports String and enum values in switch. However, double is not supported as a switch expression type.
E.g.: 1
int x = 2;
switch (x)
{
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
}
E.g.: 2
char grade = 'A';
switch (grade)
{
case 'A':
System.out.println("Excellent");
break;
}
E.g.: 3
String day = "Monday";
switch (day)
{
case "Monday":
System.out.println("Start of week");
break;
}
E.g.: 4
double value = 2.5;
switch (value) // Compile-Time Error
{
case 2.5:
System.out.println("Two point five");
break;
}
Q: 2What is the output of the following given code?
int a=2;
switch (a)
{
case 1:
System.out.print("A");
case 2:
System.out.print("B");
case 3:
System.out.print("C");
}
Option B
In Java, the switch statement is used to select one block of code based on the value of an expression. When the value of the expression matches a case, execution starts from that case and continues with the following cases until a break statement or the end of the switch block is reached.
In the given code, the value of a is 2. Java first checks the cases. case 1 does not match because a is 2. Therefore, execution starts from case 2, where B is printed.
There is no break statement after case 2, so Java does not stop there. It continues directly to case 3 and prints C.
Therefore, the output is BC.
Q: 3What is the output of the below given code?
for (int i=0; i<5; i++)
{
if (i==2)
continue;
System.out.print(i);
}
Option B
In Java, the continue statement is used inside a loop to skip the remaining statements of the current iteration and move to the next iteration of the loop. It does not terminate the entire loop, it only skips one iteration.
In the given code, the loop starts with i=0 and continues while i<5.
Therefore, the output is 0134.
Note:
The important difference is that continue skips the current iteration, whereas break terminates the loop completely.
For example, if continue were replaced with break:
for (int i=0; i<5; i++)
{
if (i==2)
break;
System.out.print(i);
}
The output would be 01 because the loop would terminate as soon as i becomes 2.
You have reached the end of this topic. Continue learning with the next topic below.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.