1/32
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
When is the condition in a while loop evaluated?
Before each iteration of the loop
Which of the following is the correct syntax for a while loop in Java?
What would happen if you forget to increment the index variable in a while loop that processes a string?
The loop would process only the first character of the string repeatedly, resulting in an infinite loop.
In the "Reversing a String" example, why does the index start at original.length() - 1
Because array indices are zero-based, so the last character's index is length-1
What will be returned by the following code?
String text = "Programming in Java"; System.out.println(text.substring(12, 16));
"
"in J"
If you have the string String str = "Hello World";, what would str.indexOf('o') return?
4
Which of these statements is true about the equals() and == operators when comparing strings in Java?
equals() compares the content while == compares memory references
What will happen if the condition in a while loop never becomes false?
The program will run into an infinite loop and continue executing the loop body indefinitely
How does the program flow differ between an if-else if-else structure and multiple separate if statements?
In an if-else if-else structure, once a condition is true and its code block executes, the remaining conditions are skipped. With separate if statements, each condition is evaluated regardless of previous results.
In a while loop with the condition (x < 100), what must happen to the variable x within the loop to avoid an infinite loop?
The value of x must increase until it reaches or exceeds 100
Select all of code snippets below that will print Loops are awesome! exactly 10 times.
for (int i = 0; i <= 9; i++) { System.out.println("Loops are awesome!"); }
for (int i = 0; i < 10; i++) { System.out.println("Loops are awesome!"); }
Which of the following for loop headers will loop exactly 100 times? Ignore the value of the iterating variable and focus on the number of times the loop body will be executed.
for (int i = 0; i < 100; i++)
for (int i = 1; i <= 100; i++)
Which of the following for loop declarations is syntactically correct?
for (int i = 0; i < 10; i++) { System.out.println(i); }
What happens when you try to access a for loop's control variable outside of the loop?
for (int i = 0; i < 5; i++) { System.out.println(i); } System.out.println("Final value: " + i);
The program generates a compile-time error because i is not accessible outside the loop
Consider the following code:
for (int i = 0; i < 5; i++) { System.out.println(i); } System.out.println("After loop: " + i);
What happens when this code is executed?
The code prints 0, 1, 2, 3, 4, then causes a compilation error
What would happen if you used continue in a while loop where the increment statement comes after the continue?
int i = 1; while (i <= 5) { if (i == 3) { continue; // Skip when i equals 3 } System.out.println("Number: " + i); i++; // Increment statement after continue }
It would create an infinite loop because the increment statement would never be reached when the condition is true
Why might using System.exit() in the middle of a program be considered problematic in terms of program design and resource management? Select all that apply.
It prevents proper cleanup of resources like open files or database connections
It bypasses finally blocks that might contain important cleanup code
It makes the program flow difficult to predict and debug
What happens if you forget to include the increment part of a for loop?
The loop will create an infinite loop because the loop variable never changes
How would you modify a for loop to process every other element in an array of 20 items?
for (int i = 0; i < 20; i += 2) { // process array[i] }
What is the correct order of execution for the parts of a for loop?
for (initialization; condition; update) { // loop body }
initialization → condition → loop body → update → condition → loop body → update...
In the input validation example, what would happen if the user never enters valid input?
The program would run indefinitely in an infinite loop
What method would you use to read an integer from the Scanner class?
nextInt()
Why is input validation important in programs that accept user input?
To prevent program crashes from invalid data
To ensure data meets expected format and requirements
To improve security by preventing malicious input
To provide better user experience with helpful error messages
What are the three parts of a for loop header, and what does each part control?
Initialization (sets starting value), condition (controls when loop continues), increment/decrement (updates loop variable)
What is the difference between the javac and java commands?
javac compiles Java source code into bytecode, while java runs the compiled bytecode
If you have a Java file named Calculator.java, what two terminal commands would you need to compile and run it?
javac Calculator.java and java Calculator
Why might a programmer choose to use the terminal instead of an IDE for compiling and running Java programs?
Lighter resource usage on the system
Better understanding of the compilation process
More direct control over compiler flags and options
What data type are all command line arguments stored as in the args array?
String
If you run a Java program with the command java MyProgram apple banana cherry, what would args[1] contain?
banana
Why should you check args.length before accessing elements in the args array?
To prevent ArrayIndexOutOfBoundsException when the user doesn't provide enough command line arguments
Which definition best describes a nested loop?
A nested loop is when a loop exists inside another loop.
What is the correct method signature for the Math.pow method in Java?
public static double pow(double base, double exponent)
If you want to print numbers from 1 to 100, which loop type would be most appropriate and why?
For loop, because we know exactly how many iterations we need (100)