CH5 MC
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) { x += 100; }
110
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, y = 15; x = y--;
15
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 the value of x after the following statements are executed? int x = 10; for (int y = 5; y < 20; y +=5) x += y;
40
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x <= 100);
5
Which of the following statements opens a file named MyFile.txt and allows you to read data from it?
File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
Which of the following statements opens a file named MyFile.txt and allows you to append data to its existing contents?
FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter);
What does the following code do? Scanner keyboard = new Scanner(System.in); String filename; System.out.print("Enter the filename: "); filename = keyboard.readString(); PrintWriter outFile = new PrintWriter(filename);
It allows the user to enter the name of the file that data will be written to.
In all but very rare cases, loops must contain, within themselves
a way to terminate.
The variable used to keep a running total in a loop is called a(n)
accumulator
A loop that executes as long as a particular condition exists is called a(n) ________ loop.
conditional
A loop that repeats a specific number of times is known as a(n)
count-controlled
An item that separates other items is known as a
delimiter
Given the following statement, which statement will write the string "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt");
diskOut.println("Calvin");
The ________ loop is ideal in situations where you always want the loop to iterate at least once.
do-while
The ________ loop is ideal in situations where the exact number of iterations is known.
for
When working with the PrintWriter class, which of the following import statements should you have near the top of your program?
import java.io.*;
If a loop does not contain, within itself, a valid way to terminate, it is called a(n) ________ loop
infinite
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?
int number = inputFile.nextInt();
A ________ loop will always be executed at least once.
posttest
A ________ is a value that signals when the end of a list of values has been reached.
sentinel
A(n) ________ is a special value that cannot be mistaken as a member of a list of data items and signals that there are no more data items to be processed.
sentinel
Before entering a loop to compute a running total, the program should first
set the accumulator variable to an initial value, often zero.
In general, there are two types of files which are
text and binary
Which of the following is the method you can use to determine whether a file exists?
the File class's exists method
Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops is the correct way to read data from the file until the end of the file is reached?
while (inputFile.hasNext())
What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++;
x = 33, y = 9