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