Deck 5: Loops and Files
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
Play
Full screen (f)
Deck 5: Loops and Files
1
The for loop is a posttest loop that has built-in expressions for initializing, testing, and updating.
False
2
The while loop is always the best choice in situations where the exact number of iterations is known.
False
3
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
4
A __________ loop will always be executed at least once.
A) pretest
B) posttest
C) conditional
D) user-controlled
A) pretest
B) posttest
C) conditional
D) user-controlled
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
A loop that repeats a specific number of times is known as a(n)
A) count-controlled
B) infinite
C) conditional
D) pretest
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
6
In a for loop, the control variable is always incremented.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
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
8
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
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
9
In general, there are two types of files which are
A) encrypted and unencrypted
B) delimited and unlimited
C) text and binary
D) static and dynamic
A) encrypted and unencrypted
B) delimited and unlimited
C) text and binary
D) static and dynamic
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
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
A) terminal
B) sentinel
C) token
D) delimiter
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
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
12
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
13
Before entering a loop to compute a running total, the program should first
A) set the accumulator variable to an initial value, often 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
A) set the accumulator variable to an initial value, often 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
14
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
15
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
16
In all but very rare cases, loops must contain, within themselves
A) nested loops
B) a way to terminate
C) arithmetic operators
D) nested decision strucures
A) nested loops
B) a way to terminate
C) arithmetic operators
D) nested decision strucures
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
The variable used to keep a running total in a loop is called a(n) __________.
A) accumulator
B) sentinel
C) summation
D) integer
A) accumulator
B) sentinel
C) summation
D) integer
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
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.
A) accumulator
B) sentinel
C) delimiter
D) terminator
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
19
An item that separates other items is known as a
A) partition
B) delimiter
C) sentinel
D) terminator
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
20
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
21
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");
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");
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
What will be the value of x after the following code is executed? int x = 10;
While (x < 100)
{
X += 100;
}
A) 100
B) 90
C) 110
D) 10
While (x < 100)
{
X += 100;
}
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
23
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;
A) 25
B) 30
C) 50
D) 40
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
24
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");
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
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();
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
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
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
27
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.*;
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
28
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 be written to.
C) It establishes a connection with a file named filename.
D) Nothing; the code contains a syntax error.
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 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
29
Which of the following is the method you can use 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
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
30
The __________ loop is ideal in situations where the exact number of iterations is known.
A) for
B) while
C) do-while
D) posttest
A) for
B) while
C) do-while
D) posttest
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
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
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
32
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
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
33
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
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
34
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) posttest
A) for
B) while
C) do-while
D) posttest
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 Scanner = new File("MyFile.txt");
D) PrintWriter inputFile = new PrintWriter("MyFile.txt");
A) Scanner inputFile = new Scanner("MyFile.txt");
B) File file = new File("MyFile.txt");
Scanner inputFile = new Scanner(file);
C) File Scanner = 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
Select all that apply. Which of the following steps is normally performed by a for loop?
A) update the control variable during each iteration
B) test the control variable by comparing it to a maximum or minimum value
C) terminate when the control variable reaches its maximum or minimum value
D) initialize the control variable to a starting value
A) update the control variable during each iteration
B) test the control variable by comparing it to a maximum or minimum value
C) terminate when the control variable reaches its maximum or minimum value
D) initialize the control variable to a starting value
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
If a loop does not contain, within itself, a valid way to terminate, it is called a(n) __________ loop
A) for
B) while
C) do-while
D) infinite
A) for
B) while
C) do-while
D) infinite
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
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?
A) while (inputFile.nextLine == " ")
B) while (inputFile != null)
C) while (!inputFile.EOF)
D) while (inputFile.hasNext())
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
39
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
X = y--;
A) 14
B) 16
C) 0
D) 15
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
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
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