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: 3Given the following piece of code:
public class Test
{
public static void main(String args[])
{
int i=0,j=5;
for(;(i<3)&&(j++<10);i++)
{
System.out.print(“ “+i+” “+j);
}
System.out.print(“ “+i+” “+j);
}
}
What will be the result?
Option A
In Java, a for loop consists of three parts, initialization, condition, and update. In this question, the loop uses the post-increment operator (j++) and the logical AND operator (&&).
The post-increment operator uses the current value of a variable in the expression and increments it afterward. The && operator evaluates the second condition only when the first condition is true.
Q: 4The break statement in Java is used to _________.
Option A
In Java, the break statement is a control-flow statement used to immediately terminate the execution of the nearest loop or switch statement.
When the break statement is executed inside a loop, the program comes out of that loop and continues executing the statement written after the loop.
E.g.:
class Example
{
public static void main(String args[])
{
for (int i=1; i<=5; i++)
{
if (i==3)
{
break;
}
System.out.println(i);
}
System.out.println("Loop Ended.");
}
}
OUTPUT
1
2
Loop Ended.
| Statement | Used For |
|---|---|
| break | Terminates the loop or switch. |
| continue | Skips the remaining part of the current loop iteration. |
| return | Exits from the current method. |
Q: 5What 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.
Q: 6In Java, which loop construct guarantees that the loop body is executed at least once?
Option B
In Java, the do-while loop guarantees that its loop body is executed at least once, because the condition is checked after the loop body is executed. Therefore, even if the condition is false initially, the statements inside the loop will execute one time.
Syntax:
do
{
// statements
}
while (condition);
E.g.:
class Example
{
public static void main(String args[])
{
int i=10;
do
{
System.out.println("Loop Executed");
}
while (i<5);
}
}
OUTPUT
Loop Executed
| Loop Type | Description | Condition Checked | Syntax |
|---|---|---|---|
| for loop | Used when the number of iterations is known or can be controlled using initialization, condition, and update expressions. | Before the loop body. | for (initialization; condition; update) { } |
| while loop | Used when the number of iterations may not be known in advance and execution depends on a condition. | Before the loop body. | while (condition) { } |
| do-while loop | Used when the loop body must execute before checking the condition. | After the loop body. | do { } while (condition); |
| Enhanced for loop (for-each) | Used to easily traverse elements of an array or collection without manually managing an index. | Automatically for each element. | for (Type variable : array/collection) { } |
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.