Java Chapter 5
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100);
1
What will be the value of x after the following code is executed? int x = 10; while (x < 100)
100
What will be the value of x after the following code is executed? int x, y = 15; x = y--;
15
In all but rare cases, loops must contain within themselves
A way to terminate
How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!!!");
12
What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x < 5);
200
What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; y += 20; }
210
What will be printed after the following code is executed? for (int number = 5; number <= 15; number +=3) System.out.print(number + ", ");
5, 8, 11, 14
This type of loop will always be executed at least once.
Post-test loop
This is a value that signals when the end of a list of values has been reached.
Sentinel
If a loop does not contain within itself a way to terminate, it is called
An infinite loop
Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents? A) FileWriter fwriter = new FileWriter("MyFile.txt"); PrintWriter outFile = new PrintWriter(fwriter); B) FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter); C) PrintWriter outfile = new PrintWriter("MyFile.txt", true); D) PrintWriter outfile = new PrintWriter(true, "MyFile.txt");
B) FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter);
A loop that executes as long as a particular condition exists is called a(n) _____.
Conditional loop
A loop that repeats a specific number of times is known as a(n) _____________.
Count-controlled loop
Assuming that inputFile references a Scanner object that was used to open a file, which of the following statements will read an int from the file? A) int number = inputFile.next(); B) int number = inputFile.integer(); C) int number = inputFile.readInt(); D) int number = inputFile.nextInt();
D) int number = inputFile.nextInt();
This is an item that separates other items.
Delimiter
True or False The do-while loop is a pre-test loop.
False
True or False In a for loop, the control variable can only be incremented.
False
True or False The do-while loop is a pre-test loop.
False
True or False\ When the break statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.
False
A sentinel value ____________ and signals that there are no more values to be entered.
Is a special value that cannot be mistaken as a member of the list
In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number: "); int number = keyboard.nextInt(); while (number < 100 || number > 500) { System.out.print("Enter another number: "); number = keyboard.nextInt(); }
Numbers in the range 100 through 500
This is a sum of numbers that accumulates with each iteration of a loop.
Running total
You can use this method to determine whether a file exists.
The File class's exists method
In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number: "); int number = keyboard.nextInt(); while (number < 100 && number > 500) { System.out.print("Enter another number: "); number = keyboard.nextInt(); }
The boolean condition can never be true
True or False When you pass the name of a fi le to the PrintWriter constructor, and the file already exists, it will be erased and a new empty file with the same name will be created.
True
True or False The do-while loop must be terminated with a semicolon.
True
When you open a file with the PrintWriter class, the class can potentially throw an IOException True or False
True
This type of loop is ideal in situations where the exact number of iterations is known.
for loop
Which of the following are pre-test loops:
while, for