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


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


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


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


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


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


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


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


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


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


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


12. _______ is not a flow control statement in Java.

A. break
B. continue
C. exit()
D. return

View Answer


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


14. Which of the following is an iteration statement?

A. switch
B. if-else
C. if
D. do-while

View Answer


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





Also check :

Discussion


* You must be logged in to add comment.