Chapter 4 Flashcards

1
Q

What are the shortcut operators for incrementing or decrementing a variable?

A

++ (increment) and – (decrement).

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

What is the difference between prefix and postfix notation in increment/decrement?

A

Prefix (++number): Increment/decrement happens before the rest of the expression is evaluated.

Postfix (number++): Increment/decrement happens after the rest of the expression is evaluated.

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

What is the syntax of a while loop in Java?

A

while (condition) {
statements;
}

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

When does a while loop stop executing?

A

When the condition becomes false.

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

What is a common mistake that causes infinite loops?

A

Failing to update the condition variable (e.g., forgetting to decrement x in while (x > 0)).

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

How can a while loop be used for input validation?

A

while (number < 1 || number > 100) {
System.out.println(“Invalid input!”);
number = keyboard.nextInt();
}

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

What is the syntax of a do-while loop?

A

do {
statements;
} while (condition);

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

How is a do-while loop different from a while loop?

A

A do-while loop always executes the body at least once before testing the condition.

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

What is the syntax of a for loop in Java?

A

for (initialization; test; update) {
statements;
}

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

What are the three sections of a for loop?

A

Initialization: Initializes the loop control variable(s).

Test: Condition that determines whether the loop runs.

Update: Updates the loop control variable(s).

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

Why should you avoid modifying the control variable inside the body of a for loop?

A

It makes the code harder to debug and maintain.

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

What happens if you omit all parts of a for loop (e.g., for(;;))?

A

It creates an infinite loop.

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

Can a for loop initialize and update multiple variables?

A

Yes, for example:

java
Copy code
for (int i = 0, j = 10; i < 5 && j > 5; i++, j–) {
// Loop body
}

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

What does the break statement do in a loop?

A

It terminates the loop immediately.

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

What does the continue statement do in a loop?

A

It skips the current iteration and moves to the next iteration.

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

Why should break and continue be avoided?

A

They make code harder to read and debug.

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

How does a nested loop execute?

A

The inner loop runs completely for each iteration of the outer loop

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

If there is a nested loop with 10 iterations for both inner and outer loops, how many total iterations occur?

A

100 iterations.

19
Q

What are the steps to handle files in Java?

A

Open the file.
Write or read data.
Close the file.

20
Q

How do you write to a file using PrintWriter?

A

PrintWriter outFile = new PrintWriter(“output.txt”);
outFile.println(“Hello, world!”);
outFile.close();

21
Q

How do you append data to a file without overwriting it?

A

FileWriter fw = new FileWriter(“output.txt”, true);
PrintWriter outFile = new PrintWriter(fw);
outFile.println(“Additional data”);
outFile.close();

22
Q

How do you handle file paths with backslashes in Windows?

A

Use \ in string literals:

java
Copy code
PrintWriter outFile = new PrintWriter(“C:\folder\file.txt”);

23
Q

How do you avoid exceptions when writing to a file?

A

Add throws IOException to the method header:

java
Copy code
public static void main(String[] args) throws IOException {
PrintWriter outFile = new PrintWriter(“output.txt”);
outFile.close();
}

24
Q

When should you use a while loop?

A

When the number of iterations is unknown and the loop may not execute at all.

25
When should you use a do-while loop?
When the loop must execute at least once before testing the condition.
26
When should you use a for loop?
When there is a known iteration count or a counting variable.
27
What is a sentinel value, and how is it used in loops?
A special value (e.g., -1) used to signal the end of user input.
28
Why is it bad practice to update the control variable in a loop body?
It complicates code readability and debugging.
29
What is the code to create a Scanner instance for reading keyboard input?
Scanner keyboard = new Scanner(System.in);
30
How do you prompt a user to enter a filename and read it as a string?
System.out.print("Enter the filename: "); String filename = keyboard.nextLine();
31
How do you create a File object to represent a file using the filename input by the user?
File file = new File(filename);
32
What code creates a Scanner to read data from a file?
Scanner inputFile = new Scanner(file);
33
What method is used to check if there is more data to read in a file?
The hasNext() method. Example: java Copy code while (inputFile.hasNext()) { String str = inputFile.nextLine(); System.out.println(str); }
34
How do you close a file after reading from it?
inputFile.close();
35
What exception must be handled or declared when working with files using Scanner?
IOException. Add a throws IOException clause in the method header: java Copy code public static void main(String[] args) throws IOException { // Code here }
36
What import statement is used to include the Random class in your program?
import java.util.Random;
37
How do you create an instance of the Random class?
Random randomNumbers = new Random();
38
What method generates a random integer within a specific range?
int number = randomNumbers.nextInt(bound); This generates a number between 0 (inclusive) and bound (exclusive).
39
What method generates a random floating-point number between 0.0 and 1.0?
double number = randomNumbers.nextDouble();
40
What method generates a random boolean value (true or false)?
boolean flag = randomNumbers.nextBoolean();
41
What is a full example of reading from a file using Scanner?
File file = new File("data.txt"); Scanner inputFile = new Scanner(file); while (inputFile.hasNext()) { String str = inputFile.nextLine(); System.out.println(str); } inputFile.close();
42
Why is it important to close a file after reading?
To free system resources and avoid resource leaks.
43
What happens if a file doesn't exist when creating a Scanner?
An IOException is thrown, and the program must handle it with try-catch or a throws clause.