wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Mastering Java Control Statements

Total questions: 20

Worksheet time: 2mins

Name
Class
Date
1.

What is the main difference between a while loop and a do-while loop?

a)

A while loop checks the condition before execution; a do-while loop checks after execution.

b)

A while loop can only be used with integers; a do-while loop can be used with any data type.

c)

A while loop is used for infinite loops; a do-while loop is for conditional loops.

d)

A while loop executes at least once; a do-while loop may not execute at all.

2.

Write a for loop that prints numbers 1 to 10.

a)

for i in range(1, 9): print(i)

b)

for i in range(1, 11): print(i)

c)

for i in range(10): print(i)

d)

for i in range(1, 12): print(i)

3.

What will happen if the condition in a while loop is never true?

a)

The program will crash immediately.

b)

The while loop will execute indefinitely.

c)

The loop will execute once and then stop.

d)

The while loop will not execute.

4.

Can you use a for loop to iterate over an array? If so, how?

a)

For loops can only iterate over objects, not arrays.

b)

You can only use a while loop to iterate over an array.

c)

Yes, you can use a for loop to iterate over an array.

d)

No, a for loop cannot be used with arrays.

5.

What is the output of the following code: 'for(int i=0; i<5; i++) { System.out.print(i); }'?

a)

01234

b)

0123

c)

12345

d)

56789

6.

Explain how a do-while loop guarantees at least one execution of the loop body.

a)

A do-while loop executes the body only if the condition is true.

b)

A do-while loop checks the condition before executing the loop body.

c)

A do-while loop may not execute the body if the condition is false initially.

d)

A do-while loop guarantees at least one execution of the loop body by checking the loop condition after executing the body.

7.

How can you create an infinite loop using a while loop?

a)

while (true) {}

b)

while (x < 10) {}

c)

while (1) {}

d)

while (false) {}

8.

What does 'final int myNum = 15;' declare?

a)

A constant variable

b)

A variable that can be changed

c)

A string variable

d)

A double variable

9.

What will 'System.out.println(x + y);' output if x = 5 and y = 6?

a)

11

b)

56

c)

5 + 6

d)

Error

10.

What does the ternary operator do?

a)

Replaces simple if-else statements

b)

Declares a variable

c)

Loops through an array

d)

Defines a method

11.

What will 'if (myNum % 2 == 0) {

System.out.println(myNum + " is even");

} else {

System.out.println(myNum + " is odd");

}

"); ' output if myNum = 5?

a)

5 is even

b)

5 is odd

c)

Error

d)

No output

12.

What will be the output of 'int[] ages = {22, 35, 18, 20};

int lowestAge = ages[0];

for (int i = 1; i < ages.length; i++) {

if (ages[i] < lowestAge) {

lowestAge = ages[i];

}

}

System.out.println("The lowest age in the array is: " + lowestAge); '?

a)

The lowest age in the array is: 18

b)

The lowest age in the array is: 20

c)

The lowest age in the array is: 22

d)

The lowest age in the array is: 35

13.

What is the output of this program?

a)

a=10

b)

a=20

c)

Compilation Error

d)

No Error, but no output

14.

A keyword used to initialize an object.

a)

OLD

b)

CLONE

c)

METHOD

d)

NEW

15.

Below are the list of three characteristics of an Object EXCEPT ONE. What is it?

a)

Behavior

b)

Attribute

c)

State

d)

Identity

16.

Represents the state of an object

a)

Class

b)

Object

c)

Attribute

d)

Method

17.

Blueprint for individual object, attribute and method.

a)

Class

b)

Object

c)

Attribute

d)

Method

18.

Which keyword allows a variable or method to be accessed by subclasses even if they are in a different package?

a)

Public

b)

Private

c)

Protected

d)

Default

19.

Which of the following statements is true about static variables in Java?

a)

They are created each time an object is created

b)

They are shared among all objects of the class

c)

They cannot be accessed without an object

d)

They are destroyed when the object is destroyed

20.

Which is NOT true about static methods?

a)

They belong to the class rather than an object

b)

They can be called using the class name

c)

They can use this keyword

d)

They can access static variables directly