Chapter 4 Practice Questions Flashcards

1
Q

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.

A

B
Explanation: Prefix (++variable) increments the value before it is used in an expression, while postfix (variable++) increments the value after it is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The condition in a while loop is evaluated after the body of the loop is executed.

A. True
B. False

A

B
(False, because a while loop is a pretest loop.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A

B
Explanation: The loop will run infinitely because x is not decremented inside the loop, so the condition x > 0 will always remain true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In a for loop, the control variable can be updated in the loop body instead of the update section.

A. True
B. False

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A

B
Explanation: The continue statement skips the rest of the current iteration and moves directly to the next iteration of the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

B
Explanation: When i == 1, the continue statement skips the System.out.println(i) statement, so only 0 and 2 are printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

D
Explanation: All these loops have conditions that will never become false, so they result in infinite loops.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A throws IOException clause is required when working with PrintWriter.

A. True
B. False

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A
Explanation: The break statement terminates the loop immediately and execution continues with the next statement after the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the following line do in Java?

java
Copy code
File file = new File(filename);
Options:
A. Creates a new file on the disk.
B. Creates a File object that represents the file.
C. Deletes the specified file.
D. Reads data directly from the file.

A

B
Explanation: This line creates a File object that represents the file specified by filename. It doesn’t create or modify the file on the disk.

17
Q

Quiz on File Handling and Random Numbers in Java
Question 1: File Handling Basics
What is the purpose of the following code?

java
Copy code
Scanner keyboard = new Scanner(System.in);
System.out.print(“Enter the filename: “);
String filename = keyboard.nextLine();
Options:
A. To create a file and write data to it.
B. To read data from the file directly.
C. To prompt the user to enter a filename and store it in a variable.
D. To close the file after reading data.

A

Answer: C
Explanation: The code prompts the user to enter a filename and stores it in the variable filename as a string. It doesn’t read or write to a file directly.

18
Q

The hasNext() method of the Scanner class checks if there is more data to read from a file.
Options:
A. True
B. False

A

A
Explanation: The hasNext() method returns true if the file has more data to read.

19
Q

What will the following code do?

java
Copy code
File file = new File(“data.txt”);
Scanner inputFile = new Scanner(file);

while (inputFile.hasNext()) {
String str = inputFile.nextLine();
System.out.println(str);
}

inputFile.close();
Options:
A. Read and print all lines from data.txt.
B. Throw a FileNotFoundException at runtime if data.txt doesn’t exist.
C. Compile successfully but do nothing.
D. Both A and B.

A

D
Explanation: The code reads and prints all lines from data.txt if it exists (A). If the file does not exist, it throws a FileNotFoundException at runtime (B).

20
Q

Random Number Generation
Which of the following is the correct way to generate a random integer between 0 and 9 (inclusive)?
Options:
A. int num = randomNumbers.nextInt();
B. int num = randomNumbers.nextInt(10);
C. int num = randomNumbers.nextInt(1, 10);
D. int num = randomNumbers.randomInt(10);

A

B
Explanation: The method nextInt(bound) generates a random integer from 0 (inclusive) to bound (exclusive). To get a number between 0 and 9, you pass 10 as the bound.

21
Q

The nextDouble() method of the Random class generates a random number between 0.0 (inclusive) and 1.0 (exclusive).
Options:
A. True
B. False

A

A
Explanation: The nextDouble() method always generates a floating-point number in the range [0.0, 1.0).

22
Q

Why is it important to close a file after reading from it in Java?
Options:
A. To delete the file automatically.
B. To ensure data is not lost.
C. To free system resources.
D. To allow the file to be reopened later.

A

C
Explanation: Closing a file frees up system resources such as memory and file handles, preventing resource leaks.

23
Q

What will the following code do if the file “example.txt” exists and contains multiple lines of text?

java
Copy code
File file = new File(“example.txt”);
Scanner inputFile = new Scanner(file);

while (inputFile.hasNext()) {
System.out.println(inputFile.nextLine());
}

inputFile.close();
Options:
A. Print all lines in the file.
B. Throw a runtime error.
C. Only print the first line of the file.
D. Compile successfully but do nothing.

A

A
Explanation: The code uses a while loop with hasNext() and nextLine() to read and print all lines in the file until the end.

24
Q

Which of the following code snippets will compile successfully?

java
Copy code
public static void main(String[] args) throws IOException {
File file = new File(“data.txt”);
Scanner inputFile = new Scanner(file);
}
Options:
A. Code as shown.
B. Remove throws IOException and compile.
C. Add a try-catch block around the file code.
D. Both A and C.

A

D
Explanation: The code can compile as-is with the throws IOException clause (A), or you can use a try-catch block to handle the exception (C).

25
Q

What is the output range of the following code?

java
Copy code
Random random = new Random();
int num = random.nextInt(5) + 10;
System.out.println(num);
Options:
A. 5 to 14
B. 10 to 14
C. 5 to 10
D. 10 to 15

A

B
Explanation: random.nextInt(5) generates numbers between 0 and 4. Adding 10 shifts the range to 10 through 14.

26
Q

What does the following line do in Java?
java
Copy code
File file = new File(filename);
Options:
A. Creates a new file on the disk.
B. Creates a File object that represents the file.
C. Deletes the specified file.
D. Reads data directly from the file.

A

Answer: B
Explanation: This line creates a File object that represents the file specified by filename. It doesn’t create or modify the file on the disk.

27
Q

What happens if the file specified in the following code doesn’t exist?
java
Copy code
File file = new File(“data.txt”);
Scanner inputFile = new Scanner(file);
Options:
A. The file is created automatically.
B. The program throws an IOException.
C. The program continues execution without error.
D. A warning is displayed, but the program continues.

A

B
Explanation: If the file does not exist, the Scanner object will throw a FileNotFoundException, which is a subclass of IOException.