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
Q

When should you use a do-while loop?

A

When the loop must execute at least once before testing the condition.

26
Q

When should you use a for loop?

A

When there is a known iteration count or a counting variable.

27
Q

What is a sentinel value, and how is it used in loops?

A

A special value (e.g., -1) used to signal the end of user input.

28
Q

Why is it bad practice to update the control variable in a loop body?

A

It complicates code readability and debugging.

29
Q

What is the code to create a Scanner instance for reading keyboard input?

A

Scanner keyboard = new Scanner(System.in);

30
Q

How do you prompt a user to enter a filename and read it as a string?

A

System.out.print(“Enter the filename: “);
String filename = keyboard.nextLine();

31
Q

How do you create a File object to represent a file using the filename input by the user?

A

File file = new File(filename);

32
Q

What code creates a Scanner to read data from a file?

A

Scanner inputFile = new Scanner(file);

33
Q

What method is used to check if there is more data to read in a file?

A

The hasNext() method. Example:

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

34
Q

How do you close a file after reading from it?

A

inputFile.close();

35
Q

What exception must be handled or declared when working with files using Scanner?

A

IOException. Add a throws IOException clause in the method header:

java
Copy code
public static void main(String[] args) throws IOException {
// Code here
}

36
Q

What import statement is used to include the Random class in your program?

A

import java.util.Random;

37
Q

How do you create an instance of the Random class?

A

Random randomNumbers = new Random();

38
Q

What method generates a random integer within a specific range?

A

int number = randomNumbers.nextInt(bound);
This generates a number between 0 (inclusive) and bound (exclusive).

39
Q

What method generates a random floating-point number between 0.0 and 1.0?

A

double number = randomNumbers.nextDouble();

40
Q

What method generates a random boolean value (true or false)?

A

boolean flag = randomNumbers.nextBoolean();

41
Q

What is a full example of reading from a file using Scanner?

A

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
Q

Why is it important to close a file after reading?

A

To free system resources and avoid resource leaks.

43
Q

What happens if a file doesn’t exist when creating a Scanner?

A

An IOException is thrown, and the program must handle it with try-catch or a throws clause.