Chapter 4

25 July 2022
4.7 (114 reviews)
37 test answers

Unlock all answers in this set

Unlock answers (33)
question
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.
answer
False
question
The do-while loop is ideal in situations where you always want the loop to iterate at least once
answer
True
question
The while loop is always the best choice in situations where the exact number of iterations is known
answer
False
question
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.
answer
True
question
In a for loop, the control variable cannot be initialized to a constant value and tested against a constant value.
answer
True
question
A loop that repeats a specific number of times is known as a(n) __________ loop
answer
Count-controlled
question
A __________ loop will always be executed at least once
answer
posttestloop
question
A __________ is a value that signals when the end of a list of values has been reached.
answer
sentinel
question
The while loop is always the best choice in situations where the exact number of iterations is known.
answer
False
question
Java provides a set of simple unary operators designed just for incrementing and decrementing variables.
answer
T
question
Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops is the correct way to read data from the file until the end of the file is reached?
answer
d. while (inputFile.hasNext()) { ... }
question
Which of the following is the method you can use to determine wether a file exist?
answer
the File class exists method
question
int x = 10; for (int y = 5; y < 20; y +=5) x += y; What's the value of X
answer
40
question
What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; }
answer
infinite loop
question
What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++;
answer
x= 33, y = 9
question
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100);
answer
once because //X is never over 100
question
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x <= 100);
answer
5
question
int x = 11; do { x += 20; } while (x <= 100);
answer
5
question
How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!");
answer
12// because 10 is printed and 21
question
What will be the value of x after the following code is executed? int x, y = 15; x = y--;
answer
15. // y-- or y++ does nothing unless in the for loop or before th y
question
What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z);
answer
28
question
What will be printed after the following code is executed? for (int number = 5; number <= 15; number +=3) System.out.print(number + ", ");
answer
5, 8, 11, 14
question
What will be the values of x aand y after the following code is executed? int x = 12, y = 5; x += y--;
answer
x = 17, y = 4
question
What will be the values of x and y after the following code is executed? int x, y = 15, z = 3; x = (y--) / (++z);
answer
3
question
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; }
answer
b
question
Which is a control structure that causes a statement or group of statements to repeat?
answer
loop
question
Each repetition of a loop is known as a(n) _______
answer
iteration
question
Which of the following are pre-test loops?
answer
While, for
question
The variable that controls the number of times a loop iterates is known as a(n) _____
answer
loop control variable
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();
answer
Impossible - the boolean condition can never be true//
question
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();
answer
Numbers 100- 500
question
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();
answer
Numbers in the range 100 - 500
question
Which of the following expressions will generate a random number in the range of 1 through 10?
answer
myNumber = randomNumbers.nextInt(10) + 1;
question
Which of the following statements will create an object from the Random class?
answer
Random myNumber = new Random();
question
A random number, created as an object of the Random class, is always a(n) __________.
answer
object
question
Select all that apply. Which of the following steps is normally performed by a for loop? a. update the control variable during each iteration b. test the control variable by comparing it to a maximum or minimum value c. terminate when the control variable reaches its maximum or minimum value d. initialize the control variable to a starting value
answer
ALL
question
Select all that apply. Which method of the Random class will return a random number within the range of 0.0 and 1.0? a. nextDouble() c. nextFloat() b. nextLong() d. nextInt(int n)
answer
A, C