wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Control Structures

Total questions: 39

Worksheet time: 20mins

Name
Class
Date
1.

allows selection of specific sections of code to be executed

a)

decision control structures(if,else,switch)

b)

repetition control structures(while,do-while,for)

c)

branching statements(break,continue,return)

2.

allow executing specific sections of code a number of times

a)

decision control structures(if,else,switch)

b)

repetition control structures(while,do-while,for)

c)

branching statements(break,continue,return)

3.

allows redirection of program flow

a)

decision control structures(if,else,switch)

b)

repetition control structures(while,do-while,for)

c)

branching statements(break,continue,return)

4.

allows us to change the ordering of how the statements in our programs are executed

a)

decision control structures

b)

repetition control structures

c)

control structures

d)

branching statements

5.

specifies that a statement(or block of code) will be executed if and only if a certain boolean statement is true.

a)

if-else statement

b)

if statement

c)

else if statement

d)

nested if-else statement

6.

What is the output?

int grade = 68;

if(grade > 60) {

System.out.println("Congratulations!");

}

a)

"Congratulations!"

b)

Congratulations!

c)

Syntax Error

d)

Compilation Error

7.

What is the output?

int grade = 68;

if(grade > 60) {

System.out.println("Congratulations!");

System.out.println("You passed!");

}

a)

Congratulations!

You passed!

b)

"Congratulations!"

"You passed!"

c)

Congratulations

d)

You passed

8.

its a part of a statement that should evaluate a boolean value that means that the execution of the condition should either result to a value of true or a fulse

a)

if-else statement

b)

boolean_expression

c)

statement 1

statement 2

d)

condition

9.

used when we want to execute a certain statement if a condition is true, and a different statement if the condition is false

a)

if statement

b)

nested if-else statement

c)

if-else statement

d)

else if statement

10.

What is the output?

int grade = 68;

if(grade > 60)

System.out.println("Congratulations!");

else

System.out.println("Sorry you failed");

a)

"Congratulations!"

"Sorry you failed"

b)

Congratulations!

c)

Sorry you failed

d)

Congratulations!

Sorry you failed

11.

What is the output?

int grade = 68;

if(grade > 60) {

System.out.println("Congratulations!");

System.out.println("You passed!");

}

else {

System.out.println("Sorry you failed");

}

a)

Sorry you failed

b)

Syntax Error

c)

Congratulations!

You passed!

d)

Compile Error

12.

this means that you can have other if-else inside another if-else block

a)

if-else statement

b)

else-if statement

c)

if-statement

d)

nested if-else blocks

13.

the cascading of structures allows us to make more complex selections

a)

if-else-else if statement

b)

else-if

c)

if statement

d)

else statement

14.

What is the output?

int grade = 105;

if(grade<75) {

System.out.println("Failed");

}

else if(grade>74||grade<=100) {

System.out.println("passed!");

}

else {

System.out.println("invalid");

}

a)

Failed

b)

passed

c)

invalid

d)

error

15.

What is the output?

double grade = 92.0;

if(grade>=90) {

System.out.println("Excellent");

}

else if(grade<90&&grade>=100) {

System.out.println("Good job!");

}

else if(grade<80&&grade>=60) {

System.out.println("Study harder!");

}

else {

System.out.println("Sorry you failed");

}

a)

Excellent

b)

Good job!

c)

Study harder!

d)

Sorry you failed

16.

allows branching on multiple outcomes

a)

switch

b)

while

c)

do-while

17.

is an integer or character expression

a)

switch_expression

b)

case_selector1,case_selector2 and so on

c)

switch_statement

18.

What is the output?

int grade = 92;

switch(grade) {

case 100:

System.out.println("Excellent!");

break;

case 90:

System.out.println("Good job!");

break;

case 80:

System.out.println("Study harder!");

break;

default:

System.out.println("Sorry you failed");

a)

Excellent!

b)

Good job!

c)

Study harder!

d)

Sorry you failed

19.

What is the output?

int x = 0;

while(x<10) {

System.out.println(x);

x++;

}

a)

0

1

2

3

4

5

6

7

8

9

b)

0123456789

c)

012345678910

d)

0 10

20.

what is the output?

while(true) {

System.out.println("hello");

}

a)

hello hello hello

b)

hello

c)

compile error

d)

infinite loop hello

21.

what is the output?

while(false) {

System.out.println("hello");

}

a)

hello

b)

false

c)

compile time error

d)

false hello

22.

what is the output?

int x = 0;

do {

System.out.println(x);

x++;

}while(x<10);

a)

0123456789

b)

0

1

2

3

4

5

6

7

8

9

c)

10

9

8

7

6

5

4

3

2

1

d)

012345678910

23.

what is the output?

do {

System.out.println("hello");

}while(true);

a)

infinite loop hello

b)

hello

c)

hello hello hello

d)

true

24.

what is the output?

do {

System.out.println("hello");

}while(false);

a)

hello

b)

false

c)

infinite loop hello

d)

compile time error

25.

allows execution of the same code a number of times

a)

for loop

b)

while loop

c)

do-while loop

26.

what is the output?

int i;

for(i=0;i<10;i++) {

System.out.println(i);

}

a)

0

1

2

3

4

5

6

7

8

9

b)

0123456789

c)

10

d)

1

27.

What is the output?

int i=0;

while(i<10) {

System.out.println(i);

i++;

}

a)

012310

b)

0

1

2

3

4

5

6

7

8

9

c)

0123456789

d)

error

28.

terminates the enclosing switch statement, and flow control transfers to the statement immediately following the switch

a)

break

b)

continue

c)

return

d)

unlabeled break

29.

what is output?

String names[] = {"Beah","Bianca","Lance","Belle","Nico",

"Yza","Gem","Ethan"};

String searchName = "Yza";

boolean foundName = false;

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

if(names[i].equals(searchName)) {

foundName = true;

break;

}

}

if(foundName)

System.out.println(searchName + " found!");

else System.out.println(searchName + " not found");

a)

searchName found

b)

Yza found!

c)

Yza not found!

d)

searchName not found

30.

terminates an outer statements

a)

labeled break statement

b)

unlabeled break statement

c)

break

d)

continue

31.

int[][] numbers = {{1,2,3},{4,5,6},{7,8,9}};

int searchNum = 5;

boolean foundNum = false;

searchLabel:

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

for(int j=0;j<numbers[i].length;j++) {

if(searchNum == numbers[i][j]) {

foundNum = true;

break searchLabel;

}

}

}

if(foundNum)System.out.println(searchNum + " found!");

else System.out.println(searchNum + " notfound!");

a)

compile time error

b)

5 found!

c)

5 not found!

d)

searchNum

32.

what is the output?

int count;

for(count=1;count<=10;count++) {

if(count==5)

break;

System.out.println(" " +count);

}

System.out.println("broke out of loop at " + count);

a)

1

2

3

4

broke out of loop at 5

b)

1

2

3

4

5

c)

broke out of loop at 5

d)

error

33.

what is the output?

boolean t = true;

first:{

second:{

third:{

System.out.println("Before the break;");

if(t)break second;

System.out.println("This won't execute");

}

System.out.println("This won't execute");

}

System.out.println("This is after second block");

}

a)

before the break

this is after the second block

b)

this wont execute

c)

syntax error

d)

compile time error

34.

what is the output?

for(int count=1;count<=10;count++) {

if(count==5)

continue;

System.out.println(""+count);

}

System.out.println("used to continue to skip printing 5");

a)

1

2

3

4

6

7

8

9

10

used to continue to skip printing 5

b)

5

c)

1

2

3

4

5

used to skip printing 5

d)

error

35.

begin:for(int count=1;count<=10;count++) {

if(count==5)

continue begin;

System.out.println(""+count);

}

System.out.println("used to continue to skip printing 5");

a)

1234678910 used to skip printing 5

b)

5 used to continue to skip printing 5

c)

1

2

3

4

6

7

8

9

10

used to continue to skip printing 5

d)

5

used to continue to skip counting 5

36.

what is the output?

String names[] = {"Bianca","Lance","Beah","Beah"};

int count = 0;

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

if(!names[i].equals("Beah")) {

continue;

}

count++;

}

System.out.println("There are "+count+" Beahs in the list");

a)

There are 2 Beahs in the list

b)

syntax error

c)

compile error

d)

there are

e)

beahs in the list

37.

outerLoop:

for(int i=0; i<5; i++) {

for(int j=0;j<5;j++) {

System.out.println("Inside for(j) loop");

if(j==2) continue outerLoop;

}

System.out.println("Inside for(i) loop");

}

a)

Inside for(i) loop

Inside for(i) loop

Inside for(i) loop

Inside for(i) loop

Inside for(i) loop

Inside for(i) loop

b)

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

Inside for(j) loop

c)

Inside for(j) loop

d)

Inside for(i) loop

38.

used to exit from the current method

a)

return

b)

continue

c)

break

d)

labeled break

39.

flow of control returns to the statement that follows the original method call

a)

return

b)

break

c)

continue

d)

unlabeled break