Sample questions
Question 1Medium
Which of these are selection statements in Java?
if()for()continuebreak
Answer: A. if()
Explanation
continue and break are jump statements, and for is an looping statement.
Step-by-step solution
- if() checks a condition and executes code based on it.
- for() is used to loop through a block of code repeatedly.
- Continue skips the current iteration in a loop.
- Break exits the loop or switch statement immediately.
- Only if() is a selection statement as it makes decisions.
- Final answer: A.
Why the other options are wrong
- B. for() is a loop, not a selection statement, since it repeats code instead of choosing a branch.
- C. continue skips to the next loop iteration; it is a jump statement, not a selection statement.
- D. break exits a loop or switch; it is a jump statement, not a selection statement.
Tip: Remember: if() is the only selection statement in Java.
Question 2Medium
5) What will be the output of the program?
int I = 0;
outer:
while (true)
{
I++;
inner:
for (int j = 0; j < 10; j++)
{
I += j;
if (j == 3)
continue inner;
break outer;
}
continue outer;
}
System.out.println(I);
- 1
- 2
- 3
- 4
Answer: A. 1
Explanation
The program flows as follows: I will be incremented after the while loop is entered, then I will be incremented (by zero) when the for loop is entered. The if statement evaluates to false, and the continue statement is never reached. The break statement tells the JVM to break out of the outer loop, at which point I is printed and the fragment is done.
Step-by-step solution
- I starts at 0; the while(true) loop runs once and I++ makes I equal to 1.
- The for loop starts with j = 0, so I += j adds 0 and I stays 1.
- J is 0, not 3, so the continue is skipped and break outer runs.
- Break outer leaves the while loop, so the print shows 1. The answer is option A.
Why the other options are wrong
- B. I would need one more increment, but the loop breaks on the first pass.
- C. I would need two increments, but only the first I++ runs before the break.
- D. I would need three increments, but the loop stops after the first pass.
Tip: Trace a labelled loop one pass at a time; break outer ends the whole outer loop at once.
Question 3Medium
What will be the output of the program?
boolean bool = true;
if(bool = false) /* Line 2 */
{
System.out.println("a");
}
else if(bool) /* Line 6 */
{
System.out.println("b");
}
else if(!bool) /* Line 10 */
{
System.out.println("c"); /* Line 12 */
}
else
{
System.out.println("d");
}
- a
- b
- c
- d
Answer: C. c
Explanation
Look closely at line 2, is this an equality check (==) or an assignment (=). The condition at line 2 evaluates to false and also assigns false to bool. bool is now false so the condition at line 6 is not true. The condition at line 10 checks to see if bool is not true ( if !(bool == true) ), it isn't so line 12 is executed.
Step-by-step solution
- Line 2 uses a single = (assignment), not ==, so bool becomes false.
- The if condition is the assigned value, false, so the first block is skipped.
- Line 6 tests bool, which is now false, so the else if block is skipped.
- Line 10 tests !bool, which is true, so c is printed; option C is the correct answer.
Why the other options are wrong
- A. The first if is false because = assigns false, so a is never printed.
- B. The else if at line 6 tests bool, which is now false, so b is not printed.
- D. The final else only runs if every earlier test is false, but line 10 is true.
Tip: In an if condition, a single = assigns a value and that value decides the test; use == to compare.
Question 4Medium
What is the output of this program?
class selection_statements {
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
}
}
- 1
- 2
- 3
- 4
Answer: B. 2
Explanation
In the if condition, var2 is assigned 1. Since 1 is not equal to var1 (which is 5), the else block executes. The ++var2 increments var2 from 1 to 2, so the output is 2.
Step-by-step solution
- The if test assigns var2 = 1, so var2 becomes 1.
- The test checks 1 == var1, which is 1 == 5, so it is false.
- The else branch runs: ++var2 raises var2 from 1 to 2.
- It prints 2, so the answer is option B.
Why the other options are wrong
- A. var2 is 1 but the else branch prints the value after ++, which is 2.
- C. The value is incremented only once, from 1 to 2, not to 3.
- D. The increment happens once, so the value is 2, not 4.
Tip: Assignment inside an if test changes the variable before the test is checked.
Question 5Medium
What will be the output of the program?
public class If2
{
static boolean b1, b2;
public static void main(String [] args)
{
int x = 0;
if ( !b1 ) /* Line 7 */
{
if ( !b2 ) /* Line 9 */
{
b1 = true;
x++;
if ( 5 > 6 )
{
x++;
}
if ( !b1 )
x = x + 10;
else if ( b2 = true ) /* Line 19 */
x = x + 100;
else if ( b1 | b2 ) /* Line 21 */
x = x + 1000;
}
}
System.out.println(x);
}
}
- 0
- 1
- 101
- 111
Answer: C. 101
Explanation
As instance variables, b1 and b2 are initialized to false. The if tests on lines 7 and 9 are successful so b1 is set to true and x is incremented. The next if test to succeed is on line 19 (note that the code is not testing to see if b2 is true, it is setting b2 to be true). Since line 19 was successful, subsequent else-if's (line 21) will be skipped.
Step-by-step solution
- B1 and b2 are static booleans, so they start as false.
- The tests on lines 7 and 9 are true, so b1 becomes true and x becomes 1.
- 5 > 6 is false, so x stays 1.
- !b1 is false, so the else-if on line 19 runs: b2 = true sets b2 and the condition is true, so x = 1 + 100 = 101.
- The else-if on line 21 is skipped, so the output is 101, option C.
Why the other options are wrong
- A. X is incremented on line 12, so it cannot end at 0.
- B. X is set to 1 by the increment, but the line 19 branch adds 100.
- D. The x + 10 branch needs !b1 to be true, but b1 is true here, so 10 is not added and 1000 is skipped.
Tip: Assignments inside an if condition, such as b2 = true, set the value and also give that value as the test result.
Question 6Medium
What will be the output of the program?
int I = 0;
label:
if (I < 2) {
System.out.print("I is " + I);
I++;
continue label;
}
- I is 0
- I is 0 I is 1
- Compilation fails.
- None of the above
Answer: C. Compilation fails.
Explanation
The code will not compile because a continue statement can only occur in a looping construct. If this syntax were legal, the combination of the continue and the if statements would create a kludgey kind of loop, but the compiler will force you to write cleaner code than this.
Step-by-step solution
- Continue label; is used outside any loop structure which is invalid syntax.
- Java requires continue to be inside a loop like for, while or do-while.
- The code will fail to compile due to incorrect use of continue statement.
- Compilation error occurs because the label is not part of a loop construct.
- The program cannot run as it contains a syntax error in the continue statement.
- Final answer: Compilation fails.
Why the other options are wrong
- A. The code does not run, since continue label outside a loop is a compile error.
- B. Output needs the code to compile, and it does not, because continue label is used outside any loop.
- D. None is wrong because the code fails to compile, which is listed as option C.
Tip: Always use continue inside loops to avoid compilation errors.
Question 7Medium
Which of the following statements are incorrect?
- static methods can call other static methods only.
- static methods must only access static data.
- static methods can not refer to this or super in any way.
- when object of class is declared, each object contains its own copy of static variables.
Answer: D. when object of class is declared, each object contains its own copy of static variables.
Explanation
All objects of class share same static variable, when object of a class are declared, all the objects share same copy of static members, no copy of static variables are made.
Step-by-step solution
- Static methods can call other static methods and access static data directly, but they cannot use this or super.
- Static variables are shared by all objects; there is only one copy, not one per object.
- So option D, which says each object has its own copy, is the incorrect statement.
Why the other options are wrong
- A. A static method calling other static methods is a correct statement.
- B. A static method accessing static data is a correct statement.
- C. A static method cannot refer to this or super, so this statement is correct.
Tip: Static members belong to the class, so there is one copy shared by all objects.
Question 8Medium
What will be the output of the program?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3; z++)
{
switch (z)
{
case x: System.out.print("0 ");
case x-1: System.out.print("1 ");
case x-2: System.out.print("2 ");
}
}
}
}
- 0 1 2
- 0 1 2 1 2 2
- 2 1 0 1 0 0
- 2 1 2 0 1 2
Answer: D. 2 1 2 0 1 2
Explanation
The case expressions are all legal because x is marked final, which means the expressions can be evaluated at compile time. In the first iteration of the for loop case x-2 matches, so 2 is printed. In the second iteration, x-1 is matched so 1 and 2 are printed (remember, once a match is found all remaining statements are executed until a break statement is encountered). In the third iteration, x is matched. So 0 1 and 2 are printed.
Step-by-step solution
- X is a final short with value 2, so the case labels x, x-1 and x-2 are constants 2, 1 and 0.
- z = 0: only case x-2 (0) matches. It prints '2 ' and, with no break, nothing else runs.
- z = 1: case x-1 (1) matches. It prints '1 ' and falls through to case x-2, which prints '2 '.
- z = 2: case x (2) matches. It prints '0 ', falls through to '1 ' and then '2 '.
- Full output is '2 1 2 0 1 2', which is option D.
Why the other options are wrong
- A. Shows only one pass of the loop; the loop runs three times and the first pass prints just 2.
- B. Wrong order and count; the first pass prints only 2, and the third pass prints 0 1 2.
- C. The third pass prints 0 1 2 because the fall-through goes down the list, not 0 1 0.
Tip: In a switch with no break, once a case matches every later case runs too; trace each loop value on its own.
Question 9Medium
What will be the output of the program?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3; z++)
{
switch (z)
{
case y: System.out.print("0 "); /* Line 11 */
case x-1: System.out.print("1 "); /* Line 12 */
case x: System.out.print("2 "); /* Line 13 */
}
}
}
}
- 0 1 2
- 0 1 2 1 2 2
- Compilation fails at line 11.
- Compilation fails at line 12.
Answer: C. Compilation fails at line 11.
Explanation
Case expressions must be constant expressions. Since x is marked final, lines 12 and 13 are legal; however y is not a final so the compiler will fail at line 11.
Step-by-step solution
- Case labels must be compile-time constants.
- X is a final static short with a constant value, so x and x-1 are fine.
- Y is public static but not final, so case y is not a constant.
- So compilation fails at line 11 and option C is correct.
Why the other options are wrong
- A. The code does not compile, so nothing runs.
- B. The code does not compile, so nothing runs.
- D. Line 12 uses x-1, which is a valid constant.
Tip: Case labels must be constants, so the variable needs final.
Question 10Medium
What will be the output of the program?
for(int i = 0; i < 3; i++)
{
switch(i)
{
case 0: break;
case 1: System.out.print("one ");
case 2: System.out.print("two ");
case 3: System.out.print("three ");
}
}
System.out.println("done");
- done
- one two done
- one two three done
- one two three two three done
Answer: D. one two three two three done
Explanation
The variable i will have the values 0, 1 and 2.
When i is 0, nothing will be printed because of the break in case 0.
When i is 1, "one two three" will be output because case 1, case 2 and case 3 will be executed (they don't have break statements).
When i is 2, "two three" will be output because case 2 and case 3 will be executed (again no break statements).
Finally, when the for loop finishes "done" will be output.
Step-by-step solution
- The loop variable i takes the values 0, 1 and 2.
- When i is 0, the break in case 0 runs, so nothing is printed.
- When i is 1, it prints one, then falls through to case 2 and case 3, printing two and three.
- When i is 2, it prints two and three, then done prints after the loop. The output is option D.
Why the other options are wrong
- A. Only done is printed; the loop prints text for i equal to 1 and 2 before that.
- B. It misses the fall-through: case 3 also prints three for i equal to 1.
- C. It misses the second two three, which comes from i equal to 2 falling through.
Tip: Without a break, a switch falls through to the next case; count the printed text for each loop pass.
Practise all 24 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →