Java Ch. 4 Quiz
The increment operator is: a. ++ b. -- c. -= d. *=
++
How many times will the following do-while loop be executed? int x = 11; do { x+=20; } while (x> 100); a. 4 b. 0 c. 1 d. 5
1
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. 11 c. 10 d. 0
11
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. 34 c. 28 d. 30
28
This is a control structure that causes a statement or group of statements to repeat. a. Body b. Block c. Prefix mode d. Loop
Loop
This variable controls the number of times that the loop iterates. a. Counter variable b. Loop control variable c. Running total d. Decrement variable
Loop control variable
This type of loop allows the user to decide the number of iterations. a. User controlled loop b. Infinite loop c. Dynamically executed loop d. Counter-controlled loop
User controlled loop
Given the following statement, which statement will write "Calvin" to the file DiskFile.txt? a. System.out.println(diskOut, "Calvin"); b. DiskFile.println("Calvin"); c. diskOut.println("Calvin"); d. PrintWriter.println("Calvin");
diskOut.println("Calvin");
This type of loop is ideal in situations where you always want the loop to iterate at least once. a. for loop b. while loop c. if statement d. do-while loop
do-while loop
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
post-test loop
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
5
A loop that repeats a specific number of times is known as a(n): a. conditional loop b. infinite loop c. counter-controlled loop d. sentinel loop
counter-controlled loop
This is an item that separates other items. a. Partition b. Controller c. Doorway d. Delimiter
Delimiter
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.
100
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.
5, 8, 11, 14,
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 these e. None of these
All of these
Each repetition of a loop is known as what? a. An iteration b. A cycle c. An execution d. A Lap
An iteration
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. Parentheses d. Semicolons
Braces
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 in the range 100 - 500 c. Numbers in the range 100 - 499 d. Numbers greater than 500
Numbers in the range 100 - 500
This is a sum of numbers that accumulates with each iteration of a loop. a. Final total b. Galloping total c. Running total d. Grand finale
Running total
This is a value that signals when the end of a list of values has been reached. a. Sentinel b. Terminal value c. Final value d. End value
Sentinel
Before entering a loop to compute a running total, the program should first do this. a. Set the accumulator where the total will be kept to an initial value, usually zero b. Read all the values into main memory c. Set all variables to zero d. Know exactly how many values there are to total
Set the accumulator where the total will be kept to an initial value, usually zero
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 in the range 100 - 499 b. The boolean condition can never be true. c. Numbers in the range 100 - 500 d. Numbers less than 100 or greater than 500
The boolean condition can never be true.
In all but rare cases, loops must contain within themselves: a. nested loops b. if statements c. arithmetic statements d. a way to terminate
a way to terminate
A loop that executes as long as a particular condition exists is called a(n): a. conditional loop b. count-controlled loop c. sentinel loop d. infinite loop
conditional loop
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
for loop
If a loop does not contain within itself a way to terminate, it is called a(n): a. while loop b. infinite loop c. do-while loop d. for 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? a. int number = inputFile.integer(); b. int number = inputFile.next(); c. int number = inputFile.readInt(); d. int number = inputFile.nextInt();
int number = inputFile.nextInt();
A sentinel value ________ and signals that there are no more values to be entered. a. guards the list b. indicates the start of a list c. is a different data type than the values being processed d. 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? a. while (inputFile.hasNext()) { ... } b. while (inputFile.nextLine == " ") { ... } c. while (!inputFile.EOF) { ... } d. while (inputFile != null) { ... }
while (inputFile.hasNext()) { ... }