wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java switch/for/while

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

A SWITCH case statement in Java is a ___ control statement.

a)

Iteration

b)

Looping

c)

Selection

d)

Jump

2.

Which is the alternative to SWITCH in Java language?

a)

break, continue

b)

for, while

c)

if-else

d)

goto, exit

3.

What are the keywords used to implement a SWITCH case in Java language?

a)

switch, case

b)

default

c)

break

d)

All of above

4.

A SWITCH statement accepts ___ type of data as input.

a)

byte

b)

short

c)

int

d)

String (case sensitive)

e)

All of above

5.

What is the output of Java program with SWITCH below?


int a=10;

switch(a)

{

case 2: System.out.println("TWO");

break;

case 10: System.out.println("TEN");

break;

}

a)

No Output

b)

TEN

c)

Compiler error as there is no break

d)

None

6.

What is the output of the Java program below?


int b=20;

switch(b)

{

default: System.out.println("LION");

}

a)

No Output

b)

LION

c)

Compiler error as there are no CASE statements.

d)

None

7.

What is the output of Java program with SWITCH?


int num=20;

switch(num)

{

case 10: System.out.println("TEN"); break;

case 20: System.out.println("TWENTY"); break;

case 30: System.out.println("THIRTY");

}

a)

TEN

b)

TWENTY

c)

THIRTY

d)

TEN TWENTY

8.

What is the output of Java program below?


int num=40;

switch(num)

{

case 5: System.out.println("FIVE"); break;

case 35+5: System.out.println("FORTY"); break;

case 20+30: System.out.println("FIFTY");

}

a)

FIVE

b)

FORTY

c)

FIFTY

d)

Error

9.

Single Line Comments are initiated with (a)  

10.

What is the output of Java program with SWITCH?


int num=20;

switch(num)

{

case 10: System.out.println("TEN"); break;

case 20: System.out.println("TWENTY");

case 30: System.out.println("THIRTY");

}

a)

TEN

b)

TWENTY

c)

THIRTY

d)

TEN

TWENTY

e)

TWENTY

THIRTY

11.

A SWITCH fall through (keeps going onto the next case) occurs in Java only in the absence of a ___.

a)

Keyword "case"

b)

Keyword "break"

c)

Keyword "default"

d)

None of these

12.

Which way is a correct way to write an if statement in Java?

a)

if (1 > 0)

{

System.out.println("1 is greater than 0");

}

b)

if (1 > 0) ;

{

System.out.println("1 is greater than 0");

}

c)

if (1 > 0)

[

System.out.println("1 is greater than 0");

]

d)

If (1 > 0)

{

System.out.println("1 is greater than 0");

};

13.

What does the below mean?

while(guess != 8)

a)

Keep executing code if guess IS equal to 8

b)
c)

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,...

d)

Keep executing code if guess IS NOT equal to 8

14.

What code would print out the contents of array1 correctly?

String array1[] = {"Hulk", "Iron Man", "Spiderman", "Wolverine", "NightCrawler", "Cap Murica"};

a)

for(int i = 0; i < array1.length; i++)

{

System.out.println(array1[i]);

}

b)

int i = 0;

while(i<array1.length)

{

System.out.println(array1[i]);

i++;

}

c)

for(int i = 0; i < 6; i++)

{

System.out.println(array1[i]);

}

d)

int i = 0;

while(i < 6)

{

System.out.println(array1[i]);

i++;

}

15.

What language do you prefer?

a)

Java

b)

Python