NEW
Font size
WorksheetsMastering Java Control Statements
Total questions: 20
Worksheet time: 2mins
What is the main difference between a while loop and a do-while loop?
A while loop checks the condition before execution; a do-while loop checks after execution.
A while loop can only be used with integers; a do-while loop can be used with any data type.
A while loop is used for infinite loops; a do-while loop is for conditional loops.
A while loop executes at least once; a do-while loop may not execute at all.
Write a for loop that prints numbers 1 to 10.
for i in range(1, 9): print(i)
for i in range(1, 11): print(i)
for i in range(10): print(i)
for i in range(1, 12): print(i)
What will happen if the condition in a while loop is never true?
The program will crash immediately.
The while loop will execute indefinitely.
The loop will execute once and then stop.
The while loop will not execute.
Can you use a for loop to iterate over an array? If so, how?
For loops can only iterate over objects, not arrays.
You can only use a while loop to iterate over an array.
Yes, you can use a for loop to iterate over an array.
No, a for loop cannot be used with arrays.
What is the output of the following code: 'for(int i=0; i<5; i++) { System.out.print(i); }'?
01234
0123
12345
56789
Explain how a do-while loop guarantees at least one execution of the loop body.
A do-while loop executes the body only if the condition is true.
A do-while loop checks the condition before executing the loop body.
A do-while loop may not execute the body if the condition is false initially.
A do-while loop guarantees at least one execution of the loop body by checking the loop condition after executing the body.
How can you create an infinite loop using a while loop?
while (true) {}
while (x < 10) {}
while (1) {}
while (false) {}
What does 'final int myNum = 15;' declare?
A constant variable
A variable that can be changed
A string variable
A double variable
What will 'System.out.println(x + y);' output if x = 5 and y = 6?
11
56
5 + 6
Error
What does the ternary operator do?
Replaces simple if-else statements
Declares a variable
Loops through an array
Defines a method
What will 'if (myNum % 2 == 0) {
System.out.println(myNum + " is even");
} else {
System.out.println(myNum + " is odd");
}
"); ' output if myNum = 5?
5 is even
5 is odd
Error
No output
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); '?
The lowest age in the array is: 18
The lowest age in the array is: 20
The lowest age in the array is: 22
The lowest age in the array is: 35
What is the output of this program?
a=10
a=20
Compilation Error
No Error, but no output
A keyword used to initialize an object.
OLD
CLONE
METHOD
NEW
Below are the list of three characteristics of an Object EXCEPT ONE. What is it?
Behavior
Attribute
State
Identity
Represents the state of an object
Class
Object
Attribute
Method
Blueprint for individual object, attribute and method.
Class
Object
Attribute
Method
Which keyword allows a variable or method to be accessed by subclasses even if they are in a different package?
Public
Private
Protected
Default
Which of the following statements is true about static variables in Java?
They are created each time an object is created
They are shared among all objects of the class
They cannot be accessed without an object
They are destroyed when the object is destroyed
Which is NOT true about static methods?
They belong to the class rather than an object
They can be called using the class name
They can use this keyword
They can access static variables directly
