wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Control Flow statements

Total questions: 10

Worksheet time: 8mins

Name
Class
Date
1.

Which of these are selection statements in Java?

a)

if()

b)

for

c)

continue

d)

break

2.

Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?

a)

for

b)

while

c)

do..while

d)

None of the above

3.

What will be the output of the following Java 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

4.

What will be the output of the following Java 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)

None of the above

5.

Predict the output?


class jump_statments

{

public static void main(String args[])

{

int x = 2;

int y = 0;

for ( ; y < 10; ++y)

{

if (y % x == 0)

continue;

else if (y == 8)

break;

else

System.out.print(y + " ");

} } }

a)

1 3 5 7

b)

2 4 6 8

c)

1 3 5 7 9

d)

1 2 3 4 5 6 7 8 9

6.

Which of the statement test only for equality?

a)

if

b)

switch

c)

if & switch

d)

None of the above

7.

What will be the output of the following code?

int x,y;

x=15;

y=20;

if(x>15)

{

if(y>15)

{

System.out.println("y is "+y);

}

}

else

{

System.out.println("X is "+x);

}

a)

Error

b)

Y is 20

c)

X is 15

d)

No output

8.

What will be the output of the following program.

class WeekDays

{

public static void main(String s[])

{

int day = 4;

switch(day)

{

case 1:

System.out.println("Monday");

case 2:

System.out.println("Tuesday");

case 3:

System.out.println("Wednesday");

case 4:

System.out.println("Thursday");

case 5:

System.out.println("Friday");

default:

System.out.println("Weekend");

break;

}

}

}

a)

Thursday

b)

Compilation Error

c)

Thursday

Friday

Weekend

d)

Runtime error

9.

What will be the output of the following program.

class WeekDays

{

public static void main(String s[])

{

int day = 7;


switch(day)

{

case 1:

System.out.println("Monday");

case 2:

System.out.println("Tuesday");

case 3:

System.out.println("Wednesday");

case 4:

System.out.println("Thursday");

case 5:

System.out.println("Friday");

}

}

}

a)

No Output

b)

Compilation Error

c)

Sunday

d)

Runtime error

10.

What will be the output of the following program?

class ForSample

{


public static void main(String s[])

{

int i = 0;

for(;i <= 5; i++ )

{

System.out.println("i = " + i );

}


System.out.println("i after the loop = " + i );

}

}

a)

i = 0

i = 1

i = 2

i = 3

i = 4

i = 5

i after the loop = 6

b)

i = 0

i = 1

i = 2

i = 3

i = 4

i = 5

i = 6

c)

Compilation Errors

d)

i = 0

i = 1

i = 2

i = 3

i = 4

i = 5