Java MCQ Questions on Control Flow Statements
This section focuses on the "Control Flow Statements" in Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.
1. What will be the output of the following code snippet?
int a=15;
int b=25;
if ((a < b ) || ( a = 5)>15)
system.out.println(a);
else
system.out.println(b);
A. Error
B. 15
C. 25
D. No output
View Answer
Ans : B
Explanation: No explanation
2. What will be the output of the program?
int x, y;
x=15; y=20;
if (x>15)
if(y>15)
{
system.ptintln("y is "+y);
}
else
system.out.ptintln("x is "+x);
A. Error
B. y is 20
C. x is 15
D. No output
View Answer
Ans : C
Explanation: No explanation.
3. Which two are acceptable types for x?
switch(x)
{
default:
System.out.println("Hello");
}
A. short
B. char
C. long
D. float
View Answer
Ans : A
Explanation: Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to an integer, these can also be used. Also shorts can be used. Short and Long are wrapper classes and reference types can not be used as variables.
4. Which statement is true?
public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}
A. Compilation fails.
B. "odd" will always be output.
C. "even" will always be output.
D. "odd" will be output for odd values of x, and "even" for even values.
View Answer
Ans : A
Explanation: The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.
5. Which statement is true?
public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}
A. There is a syntax error on line 1.
B. There are syntax errors on lines 1 and 6.
C. There are syntax errors on lines 1, 6, and 8.
D. There is a syntax error on line 6.
View Answer
Ans : D
Explanation: Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java.
A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown.
6. 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);
}
}
A. 1
B. 2
C. 3
D. 4
View Answer
Ans : B
Explanation: var2 is initialised to 1. The conditional statement returns false and the else part gets executed.
7. What is the output of this program?
class comma_operator
{
public static void main(String args[])
{
int sum = 0;
for(int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}
A. 5
B. 6
C. 14
D. compilation error
View Answer
Ans : B
Explanation: Using comma operator, we can include more than one statement in the initialization and iteration portion of the for loop. Therefore both ++i and j = i + 1 is executed i gets the value 0,1,2,3,4 & j gets the values -0,1,2,3,4,5.
8. What will be the output of the program?
int i = l, j = -1;
switch (i)
{
case 0, 1: j = 1; /* Line 4 */
case 2: j = 2;
default: j = 0;
}
System.out.println("j = " + j);
A. j = -1
B. j = 0
C. j = 1
D. Compilation fails.
View Answer
Ans : D
Explanation: The case statement takes only a single argument. The case statement on line 4 is given two arguments so the compiler complains.
9. In the following code snippet, which lines of code contain error?
int j=0;
while (j < 10)
{
j++;
if (j==5) continue loop;
system.out.ptintln("j is " +j);
}
A. Line 2
B. Line 3
C. Line 4
D. Line 5
View Answer
Ans : A
Explanation: No explanation.
10. What is the output of this program?
int i = 1, j = 10;
do
{ if(i > j)
{
break;
}
j--;
} while (++i < 5);
System.out.println("i = " + i + " and j = " + j);
A. i = 6 and j = 5
B. i = 5 and j = 5
C. i = 6 and j = 4
D. i = 5 and j = 6
View Answer
Ans : D
Explanation: This loop is a do-while loop, which always executes the code block within the block at least once, due to the testing condition being at the end of the loop, rather than at the beginning. This particular loop is exited prematurely if i becomes greater than j.The order is, test i against j, if bigger, it breaks from the loop, decrements j by one, and then tests the loop condition, where a pre-incremented by one i is tested for being lower than 5.
11. Which of the following for loop declaration is not valid?
A. for ( int i = 99; i >= 0; i / 9 )
B. for ( int i = 7; i <= 77; i += 7 )
C. for ( int i = 20; i >= 2; - -i )
D. for ( int i = 2; i <= 20; i = 2* i )
View Answer
Ans : A
Explanation: The first option is not a valid declaration as i/9 is not declared correctly.
12. _______ is not a flow control statement in Java.
A. break
B. continue
C. exit()
D. return
View Answer
Ans : C
Explanation: exit() is not a flow control statement in Java. exit() terminates the currently running JVM.
13. The break statement causes an exit from ______ loop.
A. innermost
B. outermost
C. break statement causes an exit from program
D. Depends on program
View Answer
Ans : A
Explanation: The break statement causes an exit from innermost loop or switch.
14. Which of the following is an iteration statement?
A. switch
B. if-else
C. if
D. do-while
View Answer
Ans : D
Explanation: do-while is an iteration statement. Others are decision making statements.
15. What is the valid data type for variable "a" to print "Hello World"?
switch(a)
{
System.out.println("Hello World");
}
A. int and float
B. byte and short
C. char and long
D. byte and char
View Answer
Ans : D
Explanation: The switch condition would only meet if variable "a" is of type byte or char.
Also check :
Discussion