Java Ch. 4 Quiz

24 July 2022
4.7 (114 reviews)
30 test answers

Unlock all answers in this set

Unlock answers (26)
question
This variable controls the number of times that the loop iterates. a. Counter variable b. Loop control variable c. Running total d. Decrement variable
answer
Loop control variable
question
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
answer
Running total
question
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
answer
Set the accumulator where the total will be kept to an initial value, usually zero
question
The increment operator is: a. ++ b. -- c. -= d. *=
answer
++
question
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
answer
Sentinel
question
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
answer
Braces
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.integer(); b. int number = inputFile.next(); c. int number = inputFile.readInt(); d. int number = inputFile.nextInt();
answer
int number = inputFile.nextInt();
question
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");
answer
diskOut.println("Calvin");
question
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
answer
User controlled loop
question
This is a control structure that causes a statement or group of statements to repeat. a. Body b. Block c. Prefix mode d. Loop
answer
Loop
question
In all but rare cases, loops must contain within themselves: a. nested loops b. if statements c. arithmetic statements d. a way to terminate
answer
a way to terminate
question
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
answer
conditional loop
question
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
answer
The boolean condition can never be true.
question
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
answer
counter-controlled loop
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. 1 b. 11 c. 10 d. 0
answer
11
question
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
answer
post-test loop
question
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
answer
Numbers in the range 100 - 500
question
This is an item that separates other items. a. Partition b. Controller c. Doorway d. Delimiter
answer
Delimiter
question
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.
answer
5, 8, 11, 14,
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.hasNext()) { ... } b. while (inputFile.nextLine == " ") { ... } c. while (!inputFile.EOF) { ... } d. while (inputFile != null) { ... }
answer
while (inputFile.hasNext()) { ... }
question
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
answer
do-while loop
question
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
answer
1
question
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
answer
for loop
question
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
answer
All of these
question
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
answer
is a special value that cannot be mistaken as a member of the list
question
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
answer
5
question
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.
answer
100
question
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
answer
infinite loop
question
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
answer
28
question
Each repetition of a loop is known as what? a. An iteration b. A cycle c. An execution d. A Lap
answer
An iteration