Deck 5: Loops and Files

Full screen (f)
exit full mode
Question
Which of the following statements opens a file named MyFile.txt and allows 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");
Use Space or
up arrow
down arrow
to flip the card.
Question
A ________ is a value that signals when the end of a list of values has been reached.

A) terminal
B) sentinel
C) token
D) delimiter
Question
What will be the value of x after the following code is executed?
Int x, y = 15;
X = y--;

A) 14
B) 16
C) 0
D) 15
Question
The ________ loop is ideal in situations where you always want the loop to iterate at least once.

A) for
B) while
C) do-while
D) pretest
Question
The ________ is ideal in situations where the exact number of iterations is known.

A) for loop
B) while loop
C) posttest loop
D) do-while loop
Question
A loop that repeats a specific number of times is known as a(n) ________ loop.

A) count-controlled
B) infinite
C) conditional
D) pretest
Question
This type of loop will always be executed at least once.

A) pretest
B) posttest
C) infinite
D) conditional
Question
When working with the PrintWriter class, which of the following import statements should you have near the top of your program?

A) import javax.swing.*;
B) import javac.io.*;
C) import java.io.*;
D) import java.file.*;
Question
What will be the values of x and y as a result of the following code?
Int x = 25, y = 8;
X += y++;

A) x = 34, y = 9
B) x = 25, y = 8
C) x = 33, y = 8
D) x = 33, y = 9
Question
Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached?

A) while (inputFile.nextLine == " ")
B) while (inputFile != null)
C) while (!inputFile.EOF)
D) while (inputFile.hasNext())
Question
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);

A) It writes to a file named filename.
B) It allows the user to enter the name of the file that data will to be written to.
C) It establishes a connection with a file named filename.
D) Nothing. The code contains a syntax error.
Question
How many times will the following do-while loop be executed?
Int x = 11;
Do
{
X += 20;
} while (x > 100);

A) 0
B) 1
C) 5
D) 4
Question
A for loop normally performs which of these steps?

A) update the control variable during each iteration
B) test the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value
C) initialize a control variable to a starting value
D) All of the above
Question
Before entering a loop to compute a running total, the program should first

A) set the accumulator variable to an initial value, usually zero.
B) set all variables to zero.
C) read all the values into main memory.
D) know exactly how many values there are to total.
Question
How many times will the following do-while loop be executed?
Int x = 11;
Do
{
X += 20;
} while (x <= 100);

A) 5
B) 4
C) 3
D) 1
Question
What will be the value of x after the following code is executed?
Int x = 10;
While (x < 100)
{
X += 10;
}

A) 100
B) 90
C) 110
D) 10
Question
This is an item that separates other items.

A) partition
B) delimiter
C) sentinel
D) terminator
Question
What will be the value of x after the following code is executed?
Int x = 10;
For (int y = 5; y < 20; y +=5)
X += y;

A) 25
B) 30
C) 50
D) 40
Question
How many times will the following for loop be executed?
For (int count = 10; count <= 21; count++)
System.out.println("Java is great!");

A) 0
B) 12
C) 10
D) 11
Question
You can use this method to determine whether a file exists.

A) the File class's canOpen method
B) the Scanner class's exists method
C) the File class's exists method
D) the PrintWriter class's fileExists method
Question
The for loop is a posttest loop that has built-in expressions for initializing, testing, and updating.
Question
The variable used to keep the running total is called a(n)

A) sentinel.
B) integer.
C) summation.
D) accumulator.
Question
When you open a file with the PrintWriter class, the class can potentially throw an IOException.
Question
You can use the PrintWriter class to open a file and write data to it.
Question
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();
Question
The while loop is ideal in situations where the exact number of iterations is known.
Question
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;
}

A) 130
B) 210
C) 110
D) 90
Question
In all but rare cases, loops must contain within themselves

A) nested loops.
B) a way to terminate.
C) arithmetic operators.
D) if statements.
Question
When you pass the name of a file 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.
Question
Given the following statement, which statement will write the string "Calvin" to the file DiskFile.txt?
PrintWriter diskOut = new PrintWriter("DiskFile.txt");

A) System.out.println(diskOut, "Calvin");
B) PrintWriter.println("Calvin");
C) DiskFile.println("Calvin");
D) diskOut.println("Calvin");
Question
In a for loop, the control variable can only be incremented.
Question
In general, there are two types of files:

A) text and binary.
B) encrypted and unencrypted.
C) delimited and unlimited.
D) static and dynamic.
Question
A loop that executes as long as a particular condition exists is called a(n) ________ loop.

A) infinite
B) count-controlled
C) conditional
D) relational
Question
When the continue 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.
Question
Which of the following statements opens a file named MyFile.txt and allows you to read data from it?

A) Scanner inputFile = new Scanner("MyFile.txt");
B) File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
C) File file = new File("MyFile.txt");
D) PrintWriter inputFile = new PrintWriter("MyFile.txt");
Question
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 from the list to be processed.

A) accumulator
B) sentinel
C) delimiter
D) terminator
Question
A file must always be opened before using it and closed when the program is finished using it.
Question
The do-while loop is ideal in situations where you always want the loop to iterate at least once.
Question
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.
Question
If a loop does not contain within itself a way to terminate, it is called

A) a for loop.
B) an infinite loop.
C) a while loop.
D) a do-while loop.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 5: Loops and Files
1
Which of the following statements opens a file named MyFile.txt and allows 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
2
A ________ is a value that signals when the end of a list of values has been reached.

A) terminal
B) sentinel
C) token
D) delimiter
B
3
What will be the value of x after the following code is executed?
Int x, y = 15;
X = y--;

A) 14
B) 16
C) 0
D) 15
D
4
The ________ loop is ideal in situations where you always want the loop to iterate at least once.

A) for
B) while
C) do-while
D) pretest
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
The ________ is ideal in situations where the exact number of iterations is known.

A) for loop
B) while loop
C) posttest loop
D) do-while loop
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
A loop that repeats a specific number of times is known as a(n) ________ loop.

A) count-controlled
B) infinite
C) conditional
D) pretest
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
This type of loop will always be executed at least once.

A) pretest
B) posttest
C) infinite
D) conditional
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
When working with the PrintWriter class, which of the following import statements should you have near the top of your program?

A) import javax.swing.*;
B) import javac.io.*;
C) import java.io.*;
D) import java.file.*;
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
What will be the values of x and y as a result of the following code?
Int x = 25, y = 8;
X += y++;

A) x = 34, y = 9
B) x = 25, y = 8
C) x = 33, y = 8
D) x = 33, y = 9
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached?

A) while (inputFile.nextLine == " ")
B) while (inputFile != null)
C) while (!inputFile.EOF)
D) while (inputFile.hasNext())
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
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);

A) It writes to a file named filename.
B) It allows the user to enter the name of the file that data will to be written to.
C) It establishes a connection with a file named filename.
D) Nothing. The code contains a syntax error.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
How many times will the following do-while loop be executed?
Int x = 11;
Do
{
X += 20;
} while (x > 100);

A) 0
B) 1
C) 5
D) 4
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
A for loop normally performs which of these steps?

A) update the control variable during each iteration
B) test the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value
C) initialize a control variable to a starting value
D) All of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
Before entering a loop to compute a running total, the program should first

A) set the accumulator variable to an initial value, usually zero.
B) set all variables to zero.
C) read all the values into main memory.
D) know exactly how many values there are to total.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
How many times will the following do-while loop be executed?
Int x = 11;
Do
{
X += 20;
} while (x <= 100);

A) 5
B) 4
C) 3
D) 1
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
What will be the value of x after the following code is executed?
Int x = 10;
While (x < 100)
{
X += 10;
}

A) 100
B) 90
C) 110
D) 10
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
This is an item that separates other items.

A) partition
B) delimiter
C) sentinel
D) terminator
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
What will be the value of x after the following code is executed?
Int x = 10;
For (int y = 5; y < 20; y +=5)
X += y;

A) 25
B) 30
C) 50
D) 40
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
How many times will the following for loop be executed?
For (int count = 10; count <= 21; count++)
System.out.println("Java is great!");

A) 0
B) 12
C) 10
D) 11
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
You can use this method to determine whether a file exists.

A) the File class's canOpen method
B) the Scanner class's exists method
C) the File class's exists method
D) the PrintWriter class's fileExists method
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
The for loop is a posttest loop that has built-in expressions for initializing, testing, and updating.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
The variable used to keep the running total is called a(n)

A) sentinel.
B) integer.
C) summation.
D) accumulator.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
When you open a file with the PrintWriter class, the class can potentially throw an IOException.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
You can use the PrintWriter class to open a file and write data to it.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
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();
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
The while loop is ideal in situations where the exact number of iterations is known.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
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;
}

A) 130
B) 210
C) 110
D) 90
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
In all but rare cases, loops must contain within themselves

A) nested loops.
B) a way to terminate.
C) arithmetic operators.
D) if statements.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
When you pass the name of a file 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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
Given the following statement, which statement will write the string "Calvin" to the file DiskFile.txt?
PrintWriter diskOut = new PrintWriter("DiskFile.txt");

A) System.out.println(diskOut, "Calvin");
B) PrintWriter.println("Calvin");
C) DiskFile.println("Calvin");
D) diskOut.println("Calvin");
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
In a for loop, the control variable can only be incremented.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
In general, there are two types of files:

A) text and binary.
B) encrypted and unencrypted.
C) delimited and unlimited.
D) static and dynamic.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
A loop that executes as long as a particular condition exists is called a(n) ________ loop.

A) infinite
B) count-controlled
C) conditional
D) relational
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
When the continue 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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
Which of the following statements opens a file named MyFile.txt and allows you to read data from it?

A) Scanner inputFile = new Scanner("MyFile.txt");
B) File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
C) File file = new File("MyFile.txt");
D) PrintWriter inputFile = new PrintWriter("MyFile.txt");
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
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 from the list to be processed.

A) accumulator
B) sentinel
C) delimiter
D) terminator
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
A file must always be opened before using it and closed when the program is finished using it.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
The do-while loop is ideal in situations where you always want the loop to iterate at least once.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
If a loop does not contain within itself a way to terminate, it is called

A) a for loop.
B) an infinite loop.
C) a while loop.
D) a do-while loop.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 40 flashcards in this deck.