CSC 120 Practice Exam Chaper 4
The increment operator is:
++
The Decrement operator is:
--
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. 12 c. 0 d. 11
12
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
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
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 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
A special value that indicates the end of a set of data or of a process.
If you are using a block of statements, don't forget to enclose all of the statements in a set of:
Braces
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
D
Given the following statement, which statement will write "Calvin" to the file DiskFile.txt?
DiskFile.println("Calvin")
True or False: In the for loop, the control variable cannot be initialized to a constant value and tested against a constant value.
False
________ is the process of inspecting data given to the program by the user and determining if it is valid.
Input validation
This is a control structure that causes a statement or group of statements to repeat.
Loop
This variable controls the number of times that the loop iterate
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(); } 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.
Running Total
This is a value that signals when the end of a list of values has been reached.
Sentinel
Before entering a loop to compute a running total, the program should first do this.
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.
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.
This is an infinite loop.
True or False: 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
True or False:Java provides a set of simple unary operators designed just for incrementing and decrementing variables.
True
True or false: The do- while loop must be terminated with a semicolon
True
This type of loop allows the user to decide the number of iterations.
User controlled loop
True
When the continued statement is encountered in a loop, all the statements in the body of the loop that appeared after it are are ignored, and the loop prepare itself for the next iteration
In all but rare cases, loops must contain within themselves:
a way to terminate
Each repetition of a loop is known as what?
an iteration
A loop that executes as long as a particular condition exists is called a(n):
count controlled
A loop that repeats a specific number of times is known as a(n)
counter-controlled loop
This is an item that separates other items.
delimiter
This type of loop is ideal in situations where you always want the loop to iterate at least once.
do-while loop
True or 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.
false
True or False:In a for statement, the control variable can only be incremented.
false
This type of loop is ideal in situations where the exact number of iterations is known.
for loop
If a loop does not contain within itself a way to terminate, it is called a(n)
infinite
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.nextInt();
This type of loop will always be executed at least once.
post-test loop
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())
Which of the following are pre-test loops?
while, for