Chapter 4 Flashcards
What are the shortcut operators for incrementing or decrementing a variable?
++ (increment) and – (decrement).
What is the difference between prefix and postfix notation in increment/decrement?
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.
What is the syntax of a while loop in Java?
while (condition) {
statements;
}
When does a while loop stop executing?
When the condition becomes false.
What is a common mistake that causes infinite loops?
Failing to update the condition variable (e.g., forgetting to decrement x in while (x > 0)).
How can a while loop be used for input validation?
while (number < 1 || number > 100) {
System.out.println(“Invalid input!”);
number = keyboard.nextInt();
}
What is the syntax of a do-while loop?
do {
statements;
} while (condition);
How is a do-while loop different from a while loop?
A do-while loop always executes the body at least once before testing the condition.
What is the syntax of a for loop in Java?
for (initialization; test; update) {
statements;
}
What are the three sections of a for loop?
Initialization: Initializes the loop control variable(s).
Test: Condition that determines whether the loop runs.
Update: Updates the loop control variable(s).
Why should you avoid modifying the control variable inside the body of a for loop?
It makes the code harder to debug and maintain.
What happens if you omit all parts of a for loop (e.g., for(;;))?
It creates an infinite loop.
Can a for loop initialize and update multiple variables?
Yes, for example:
java
Copy code
for (int i = 0, j = 10; i < 5 && j > 5; i++, j–) {
// Loop body
}
What does the break statement do in a loop?
It terminates the loop immediately.
What does the continue statement do in a loop?
It skips the current iteration and moves to the next iteration.
Why should break and continue be avoided?
They make code harder to read and debug.
How does a nested loop execute?
The inner loop runs completely for each iteration of the outer loop
If there is a nested loop with 10 iterations for both inner and outer loops, how many total iterations occur?
100 iterations.
What are the steps to handle files in Java?
Open the file.
Write or read data.
Close the file.
How do you write to a file using PrintWriter?
PrintWriter outFile = new PrintWriter(“output.txt”);
outFile.println(“Hello, world!”);
outFile.close();
How do you append data to a file without overwriting it?
FileWriter fw = new FileWriter(“output.txt”, true);
PrintWriter outFile = new PrintWriter(fw);
outFile.println(“Additional data”);
outFile.close();
How do you handle file paths with backslashes in Windows?
Use \ in string literals:
java
Copy code
PrintWriter outFile = new PrintWriter(“C:\folder\file.txt”);
How do you avoid exceptions when writing to a file?
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();
}
When should you use a while loop?
When the number of iterations is unknown and the loop may not execute at all.
When should you use a do-while loop?
When the loop must execute at least once before testing the condition.
When should you use a for loop?
When there is a known iteration count or a counting variable.
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.
Why is it bad practice to update the control variable in a loop body?
It complicates code readability and debugging.
What is the code to create a Scanner instance for reading keyboard input?
Scanner keyboard = new Scanner(System.in);
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();
How do you create a File object to represent a file using the filename input by the user?
File file = new File(filename);
What code creates a Scanner to read data from a file?
Scanner inputFile = new Scanner(file);
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);
}
How do you close a file after reading from it?
inputFile.close();
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
}
What import statement is used to include the Random class in your program?
import java.util.Random;
How do you create an instance of the Random class?
Random randomNumbers = new Random();
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).
What method generates a random floating-point number between 0.0 and 1.0?
double number = randomNumbers.nextDouble();
What method generates a random boolean value (true or false)?
boolean flag = randomNumbers.nextBoolean();
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();
Why is it important to close a file after reading?
To free system resources and avoid resource leaks.
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.