Deck 4: 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
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/57
Play
Full screen (f)
Deck 4: Loops and Files
1
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 = 25, y = 8
B) x = 33, y = 8
C) x = 33, y = 9
D) x = 34, y = 9
X += y++;
A) x = 25, y = 8
B) x = 33, y = 8
C) x = 33, y = 9
D) x = 34, y = 9
C
2
____________ is the process of inspecting data given to the program by the user and determining if it is valid.
A) Data parsing
B) Input validation
C) User authentication
D) Defensive coding
A) Data parsing
B) Input validation
C) User authentication
D) Defensive coding
B
3
What will be the value of x after the following code is executed? int x = 10;
While (x < 100)
{
X += 10;
}
A) 90
B) 100
C) 110
D) This is an infinite loop
While (x < 100)
{
X += 10;
}
A) 90
B) 100
C) 110
D) This is an infinite loop
B
4
Each repetition of a loop is known as what?
A) An iteration
B) A cycle
C) An execution
D) A Lap
A) An iteration
B) A cycle
C) An execution
D) A Lap
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
5
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) 4
D) 5
Do
{
X += 20;
} while (x > 100);
A) 0
B) 1
C) 4
D) 5
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
6
This type of loop will always be executed at least once.
A) pre-test loop
B) post-test loop
C) sentinel loop
D) for loop
A) pre-test loop
B) post-test loop
C) sentinel loop
D) for loop
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
7
What will be the value of x after the following code is executed? int x, y = 4, z = 6;
X = (y++) * (++z);
A) 24
B) 28
C) 30
D) 35
X = (y++) * (++z);
A) 24
B) 28
C) 30
D) 35
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
8
If a loop does not contain within itself a way to terminate, it is called a(n)
A) while loop
B) do-while loop
C) for loop
D) infinite loop
A) while loop
B) do-while loop
C) for loop
D) infinite loop
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
9
What will be the value of x after the following code is executed? int x = 10;
Do
{
X *= 20;
}
While (x > 5);
A) 10
B) 200
C) This is an infinite loop.
D) The loop will not be executed, the initial value of x > 5.
Do
{
X *= 20;
}
While (x > 5);
A) 10
B) 200
C) This is an infinite loop.
D) The loop will not be executed, the initial value of x > 5.
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
10
This is a control structure that causes a statement or group of statements to repeat.
A) Block
B) Loop
C) Prefix mode
D) Body
A) Block
B) Loop
C) Prefix mode
D) Body
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
11
A loop that repeats a specific number of times is known as a(n)
A) sentinel loop
B) conditional loop
C) counter-controlled loop
D) infinite loop
A) sentinel loop
B) conditional loop
C) counter-controlled loop
D) infinite loop
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
12
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();
}
A) Numbers less than 100 or greater than 500
B) Numbers in the range 100 - 499
C) Numbers in the range 100 - 500
D) The boolean condition can never be true
System.out.print("Enter a number: ");
Int number = keyboard.nextInt();
While (number < 100 && number > 500)
{
System.out.print("Enter another number: ");
Number = keyboard.nextInt();
}
A) Numbers less than 100 or greater than 500
B) Numbers in the range 100 - 499
C) Numbers in the range 100 - 500
D) The boolean condition can never be true
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
13
This type of loop allows the user to decide the number of iterations.
A) Counter-controlled loop
B) Dynamically executed loop
C) User controlled loop
D) Infinite loop
A) Counter-controlled loop
B) Dynamically executed loop
C) User controlled loop
D) Infinite loop
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
14
What will be the value of x after the following code is executed? int x = 10, y = 20;
While (y < 100)
{
X += y;
}
A) 90
B) 110
C) 210
D) This is an infinite loop
While (y < 100)
{
X += y;
}
A) 90
B) 110
C) 210
D) This is an infinite loop
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
15
How many times will the following for loop be executed? for (int count = 10; count <= 21; count++)
System.out.println("Java is great!!!");
A) 1
B) 10
C) 12
D) 0
System.out.println("Java is great!!!");
A) 1
B) 10
C) 12
D) 0
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
16
This variable controls the number of times that the loop iterates.
A) Counter variable
B) Loop control variable
C) Running total
D) Decrement variable
A) Counter variable
B) Loop control variable
C) Running total
D) Decrement variable
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
17
The increment operator is:
A) ++
B) --
C) *=
D) -=
A) ++
B) --
C) *=
D) -=
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
18
This is a value that signals when the end of a list of values has been reached.
A) Terminal value
B) Final value
C) End value
D) Sentinel
A) Terminal value
B) Final value
C) End value
D) Sentinel
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
19
If you are using a block of statements, don't forget to enclose all of the statements in a set of:
A) Braces
B) Double quotes
C) Semicolons
D) Parentheses
A) Braces
B) Double quotes
C) Semicolons
D) Parentheses
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
20
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) 40
B) 25
C) 30
D) Invalid for statement
For (int y = 5; y < 20; y +=5)
X += y;
A) 40
B) 25
C) 30
D) Invalid for statement
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
21
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", true);
PrintWriter outFile = new PrintWriter(fwriter);
B) FileWriter fwriter = new FileWriter("MyFile.txt");
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", true);
PrintWriter outFile = new PrintWriter(fwriter);
B) FileWriter fwriter = new FileWriter("MyFile.txt");
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 57 flashcards in this deck.
Unlock Deck
k this deck
22
When using the PrintWriter class, which of the following import statements would you write near the top of your program?
A) import javax.swing.*;
B) import java.io.*;
C) import PrintWriter;
D) import java.file.*;
A) import javax.swing.*;
B) import java.io.*;
C) import PrintWriter;
D) import java.file.*;
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
23
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();
}
A) Numbers less than 100
B) Numbers greater than 500
C) Numbers in the range 100 - 499
D) Numbers in the range 100 - 500
System.out.print("Enter a number: ");
Int number = keyboard.nextInt();
While (number < 100 || number > 500)
{
System.out.print("Enter another number: ");
Number = keyboard.nextInt();
}
A) Numbers less than 100
B) Numbers greater than 500
C) Numbers in the range 100 - 499
D) Numbers in the range 100 - 500
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
24
What will be the value of x after the following code is executed? int x, y = 15, z = 3;
X = (y--) / (++z);
A) 3
B) 4
C) 5
D) 6
X = (y--) / (++z);
A) 3
B) 4
C) 5
D) 6
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
25
Before entering a loop to compute a running total, the program should first do this.
A) Read all the values into main memory
B) Set the accumulator where the total will be kept to an initial value, usually zero
C) Know exactly how many values there are to total
D) Set all variables to zero
A) Read all the values into main memory
B) Set the accumulator where the total will be kept to an initial value, usually zero
C) Know exactly how many values there are to total
D) Set all variables to zero
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
26
This type of loop is ideal in situations where the exact number of iterations is known.
A) while loop
B) do-while loop
C) for loop
D) if statement
A) while loop
B) do-while loop
C) for loop
D) if statement
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
27
Which of the following are pre-test loops?
A) while, for, do-while
B) while, do-while
C) while, for
D) for, do-while
A) while, for, do-while
B) while, do-while
C) while, for
D) for, do-while
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
28
A for loop normally performs which of these steps?
A) initializes a control variable to a starting value
B) tests the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value
C) updates the control variable during each iteration
D) all of the above
E) None of the above
A) initializes a control variable to a starting value
B) tests the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value
C) updates the control variable during each iteration
D) all of the above
E) None of the above
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
29
What will be the value of x after the following code is executed? int x = 10;
While (x < 100);
{
X += 10;
}
A) 90
B) 100
C) 110
D) This is an infinite loop
While (x < 100);
{
X += 10;
}
A) 90
B) 100
C) 110
D) This is an infinite loop
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
30
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) 90
B) 110
C) 130
D) 210
While (y < 100)
{
X += y;
Y += 20;
}
A) 90
B) 110
C) 130
D) 210
Unlock Deck
Unlock for access to all 57 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) 1
B) 3
C) 4
D) 5
Do
{
X += 20;
}
While (x <= 100);
A) 1
B) 3
C) 4
D) 5
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
32
Given the following statement, which statement will write "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt");
A) System.out.println(diskOut, "Calvin");
B) DiskFile.println("Calvin");
C) PrintWriter.println("Calvin");
D) diskOut.println("Calvin");
A) System.out.println(diskOut, "Calvin");
B) DiskFile.println("Calvin");
C) PrintWriter.println("Calvin");
D) diskOut.println("Calvin");
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
33
A sentinel value _________ and signals that there are no more values to be entered.
A) is a different data type than the values being processed
B) is a special value that cannot be mistaken as a member of the list
C) indicates the start of a list
D) guards the list
A) is a different data type than the values being processed
B) is a special value that cannot be mistaken as a member of the list
C) indicates the start of a list
D) guards the list
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
34
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 != null)
{ … }
B) while (!inputFile.EOF)
{ … }
C) while (inputFile.hasNext())
{ … }
D) while (inputFile.nextLine == " ")
{ … }
A) while (inputFile != null)
{ … }
B) while (!inputFile.EOF)
{ … }
C) while (inputFile.hasNext())
{ … }
D) while (inputFile.nextLine == " ")
{ … }
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
35
In all but rare cases, loops must contain within themselves
A) arithmetic statements
B) if statements
C) a way to terminate
D) nested loops
A) arithmetic statements
B) if statements
C) a way to terminate
D) nested loops
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
36
What will be the values of x and y as a result of the following code? int x = 12, y = 5;
X += y--;
A) x = 12, y = 5
B) x = 16, y = 4
C) x = 17, y = 5
D) x = 17, y = 4
X += y--;
A) x = 12, y = 5
B) x = 16, y = 4
C) x = 17, y = 5
D) x = 17, y = 4
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
37
What will be the value of x after the following code is executed? int x = 10;
Do
{
X *= 20;
}
While (x < 5);
A) 10
B) 200
C) This is an infinite loop.
D) The loop will not be executed, the initial value of x > 5.
Do
{
X *= 20;
}
While (x < 5);
A) 10
B) 200
C) This is an infinite loop.
D) The loop will not be executed, the initial value of x > 5.
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
38
This is a sum of numbers that accumulates with each iteration of a loop.
A) Running total
B) Final total
C) Grand finale
D) Galloping total
A) Running total
B) Final total
C) Grand finale
D) Galloping total
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
39
What will be printed after the following code is executed? for (int number = 5; number <= 15; number +=3)
System.out.print(number + ", ");
A) 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
B) 5, 8, 11, 14, 17,
C) 5, 8, 11, 14,
D) This is an invalid for statement
System.out.print(number + ", ");
A) 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
B) 5, 8, 11, 14, 17,
C) 5, 8, 11, 14,
D) This is an invalid for statement
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
40
A loop that executes as long as a particular condition exists is called a(n)
A) sentinel loop
B) conditional loop
C) count-controlled loop
D) infinite loop
A) sentinel loop
B) conditional loop
C) count-controlled loop
D) infinite loop
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
41
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.nextInt();
B) int number = inputFile.next();
C) int number = inputFile.readInt();
D) int number = inputFile.integer();
A) int number = inputFile.nextInt();
B) int number = inputFile.next();
C) int number = inputFile.readInt();
D) int number = inputFile.integer();
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
42
This is an item that separates other items.
A) Controller
B) Partition
C) Doorway
D) Delimiter
A) Controller
B) Partition
C) Doorway
D) Delimiter
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
43
This type of loop is ideal in situations where you always want the loop to iterate at least once.
A) while loop
B) do-while loop
C) for loop
D) if statement
A) while loop
B) do-while loop
C) for loop
D) if statement
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
44
Which of the following will open a file named MyFile.txt and allow you to read data from it?
A) File file = new File("MyFile.txt");
B) Scanner inputFile = new Scanner("MyFile.txt");
C) File file = new File("MyFile.txt");
Scanner inputFile = new Scanner(file);
D) PrintWriter inputFile = new PrintWriter("MyFile.txt");
A) File file = new File("MyFile.txt");
B) Scanner inputFile = new Scanner("MyFile.txt");
C) File file = new File("MyFile.txt");
Scanner inputFile = new Scanner(file);
D) PrintWriter inputFile = new PrintWriter("MyFile.txt");
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
45
Java provides a set of simple unary operators designed just for incrementing and decrementing variables.
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
46
When you open a file with the PrintWriter class, the class can potentially throw an IOException.
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
47
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 57 flashcards in this deck.
Unlock Deck
k this deck
48
The do-while loop must be terminated with a semicolon.
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
49
The while loop has two important parts: (1) a boolean expression that is tested for a true or false value, and (2) a statement or block of statements that is repeated as long as the expression is true.
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
50
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 57 flashcards in this deck.
Unlock Deck
k this deck
51
The do-while loop is a pre-test loop.
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
52
You can use the PrintWriter class to open a file for writing and write data to it.
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
53
In the for loop, the control variable cannot be initialized to a constant value and tested against a constant value.
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
54
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 57 flashcards in this deck.
Unlock Deck
k this deck
55
In a for statement, the control variable can only be incremented.
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
56
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.
exists, it will be erased and a new empty file with the same name will be created.
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck
57
You can use this method to determine whether a file exists.
A) The Scanner class's exists method
B) The File class's exists method
C) The File class's canOpen method
D) The PrintWriter class's fileExists method
A) The Scanner class's exists method
B) The File class's exists method
C) The File class's canOpen method
D) The PrintWriter class's fileExists method
Unlock Deck
Unlock for access to all 57 flashcards in this deck.
Unlock Deck
k this deck