Chapter 4 Practice Questions Flashcards
What is the difference between prefix and postfix increment in Java?
A. Prefix increments after the expression is evaluated; postfix increments before.
B. Prefix increments before the expression is evaluated; postfix increments after.
C. Both prefix and postfix increment before the expression is evaluated.
D. Both prefix and postfix increment after the expression is evaluated.
B
Explanation: Prefix (++variable) increments the value before it is used in an expression, while postfix (variable++) increments the value after it is used.
The condition in a while loop is evaluated after the body of the loop is executed.
A. True
B. False
B
(False, because a while loop is a pretest loop.)
What is the key difference between a while loop and a do-while loop?
A. A do-while loop executes at least once, while a while loop may not execute at all.
B. A do-while loop runs faster than a while loop.
C. A do-while loop cannot validate input.
D. There is no difference; they are the same.
A
Explanation: A do-while loop is a post-test loop, so the body is executed at least once before the condition is checked, unlike a while loop, which checks the condition first.
What will the following code do?
java
Copy code
int x = 10;
while (x > 0) {
System.out.println(x);
}
A. Print the numbers 10 to 1 and stop.
B. Print the number 10 infinitely.
C. Print nothing because the condition is false.
D. Will not compile due to syntax error.
B
Explanation: The loop will run infinitely because x is not decremented inside the loop, so the condition x > 0 will always remain true.
In a for loop, the control variable can be updated in the loop body instead of the update section.
A. True
B. False
A
Explanation: While it is technically possible to update the control variable in the loop body, it is considered bad practice as it can make the code harder to understand and debug.
How many times will the following code execute the innermost statement?
java
Copy code
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
System.out.println(“Hello”);
}
}
A. 5 times
B. 20 times
C. 9 times
D. The code will not compile.
B
Explanation: The outer loop runs 5 times, and for each iteration of the outer loop, the inner loop runs 4 times.
5
×
4
=
20
5×4=20.
What does the following code do?
java
Copy code
FileWriter fw = new FileWriter(“output.txt”, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(“Hello, world!”);
pw.close();
A. Writes “Hello, world!” to output.txt and appends if the file exists.
B. Overwrites output.txt with “Hello, world!”.
C. Throws an exception because FileWriter cannot append.
D. Will not compile due to syntax error.
A
Explanation: Using FileWriter(“output.txt”, true) opens the file in append mode, so new data is added to the file without erasing its existing content.
A sentinel value is used to notify a program to terminate input collection when the exact number of inputs is unknown.
A. True
B. False
A
Explanation: A sentinel value is a special value that indicates the end of input, such as -1 when processing positive numbers. It helps terminate loops when input size is unknown.
What does the continue statement do in a loop?
A. Ends the loop completely.
B. Skips the remaining statements in the loop body and moves to the next iteration.
C. Causes a syntax error.
D. Executes the loop body twice before proceeding to the next iteration.
B
Explanation: The continue statement skips the rest of the current iteration and moves directly to the next iteration of the loop.
What will the following code output?
java
Copy code
for (int i = 0; i < 3; i++) {
if (i == 1) {
continue;
}
System.out.println(i);
}
A. 0, 1, 2
B. 0, 2
C. 1, 2
D. The code will not compile.
B
Explanation: When i == 1, the continue statement skips the System.out.println(i) statement, so only 0 and 2 are printed.
Which of the following creates an infinite loop?
A.
java
Copy code
for (;;) {
System.out.println(“Looping”);
}
B.
java
Copy code
while (true) {
System.out.println(“Looping”);
}
C.
java
Copy code
do {
System.out.println(“Looping”);
} while (true);
D. All of the above.
D
Explanation: All these loops have conditions that will never become false, so they result in infinite loops.
A throws IOException clause is required when working with PrintWriter.
A. True
B. False
A
Explanation: Methods that involve file handling, such as using PrintWriter, may throw an IOException. The throws IOException clause is required in the method declaration.
What will happen if the following code is executed?
java
Copy code
for (int i = 0, j = 5; i < 3 && j < 10; i++, j++) {
System.out.println(i + “, “ + j);
}
A. Prints 0, 5, 1, 6, 2, 7.
B. Prints 0, 5 only.
C. Will not compile due to syntax error.
D. Throws a runtime exception.
A
Explanation: The loop executes while i < 3 && j < 10. Both i and j are incremented each iteration. This results in 0, 5, 1, 6, and 2, 7 being printed.
What does the break statement do when used inside a loop?
A. Exits the loop and continues with the next statement after the loop.
B. Skips the current iteration and moves to the next iteration.
C. Ends the program completely.
D. Causes a syntax error.
A
Explanation: The break statement terminates the loop immediately and execution continues with the next statement after the loop.
What is the output of the following code?
java
Copy code
int x = 5;
do {
System.out.println(x);
x–;
} while (x > 5);
A. 5
B. No output
C. Infinite loop
D. The code will not compile.
A
Explanation: A do-while loop executes the body at least once. The condition x > 5 is false after the first iteration, so the loop stops after printing 5.