CS160 Exam 2 Questions With Correct
Answers
How many iterations (if any)will the following loop make?
| | | | | | | | |
for(int a = 0; a < 4; a++) {} - CORRECT ANSWER✔✔-4
| | | | | | | | | | |
How many iterations (if any)will the following loop make?
| | | | | | | |
for(int b = 5; b >= 0; b--) {} - CORRECT ANSWER✔✔-6
| | | | | | | | | | |
How many iterations (if any)will the following loop make?
| | | | | | | |
for(int c = 25; c > 45; c+= 5) {} - CORRECT ANSWER✔✔-0
| | | | | | | | | | | |
How many iterations (if any)will the following loop make?
| | | | | | | |
for (int d = 0; d <= 17; d += 3) {} - CORRECT ANSWER✔✔-6
| | | | | | | | | | | | | |
while (true) {} - CORRECT ANSWER✔✔-infinite
| | | | |
When is it better or preferable to use a while loop instead of a for loop? -
| | | | | | | | | | | | | | | | |
CORRECT ANSWER✔✔-It is better to use a while loop when the number of
| | | | | | | | | | | | |
iterations is not known in advance, such as when dealing with random numbers
| | | | | | | | | | | | |
or type safe user input.
| | | |
, When is it better or preferable to use a for loop instead of a while loop? -
| | | | | | | | | | | | | | | | |
CORRECT ANSWER✔✔-It is better to use a for loop when the number of
| | | | | | | | | | | | |
iterations is known in advance, such as when processing Strings, lists, or dealing
| | | | | | | | | | | | |
with input of a fixed size.
| | | | |
Write some selection statements for a number guessing game that can help
| | | | | | | | | | | |
determine how a user inputted guess is related to the hidden number. As a part
| | | | | | | | | | | | | | |
of your solution you can use two int variables with identifiers guess and
| | | | | | | | | | | | |
secretNumber, which you can assume have been previously declared and | | | | | | | | | |
initialized. Your code should print out the message "Too high", "Too low", or
| | | | | | | | | | | |
|"Correct" based on how the guess is related to the secret number. You do not
| | | | | | | | | | | | | | |
need to worry about reading input or handling multiple guesses. - CORRECT
| | | | | | | | | | | |
ANSWER✔✔-if(guess > secretNumber) { | | |
| System.out.println("Too high"); | |
}
else if (guess < secretNumber) {
| | | | |
| System.out.println("Too low"); | |
}
else { |
| System.out.rpintln("Correct"); |
}
Write a boolean expression involving a variable with the identifier speed that
| | | | | | | | | | | |
evaluates to true when speed is greater than 75 or less than or equal to 44 and
| | | | | | | | | | | | | | | | |
assign that result to a boolean variable that you declare. - CORRECT ANSWER✔✔-
| | | | | | | | | | | |
boolean getsTicket = speed > 75 || speed <= 44;
| | | | | | | | |
Answers
How many iterations (if any)will the following loop make?
| | | | | | | | |
for(int a = 0; a < 4; a++) {} - CORRECT ANSWER✔✔-4
| | | | | | | | | | |
How many iterations (if any)will the following loop make?
| | | | | | | |
for(int b = 5; b >= 0; b--) {} - CORRECT ANSWER✔✔-6
| | | | | | | | | | |
How many iterations (if any)will the following loop make?
| | | | | | | |
for(int c = 25; c > 45; c+= 5) {} - CORRECT ANSWER✔✔-0
| | | | | | | | | | | |
How many iterations (if any)will the following loop make?
| | | | | | | |
for (int d = 0; d <= 17; d += 3) {} - CORRECT ANSWER✔✔-6
| | | | | | | | | | | | | |
while (true) {} - CORRECT ANSWER✔✔-infinite
| | | | |
When is it better or preferable to use a while loop instead of a for loop? -
| | | | | | | | | | | | | | | | |
CORRECT ANSWER✔✔-It is better to use a while loop when the number of
| | | | | | | | | | | | |
iterations is not known in advance, such as when dealing with random numbers
| | | | | | | | | | | | |
or type safe user input.
| | | |
, When is it better or preferable to use a for loop instead of a while loop? -
| | | | | | | | | | | | | | | | |
CORRECT ANSWER✔✔-It is better to use a for loop when the number of
| | | | | | | | | | | | |
iterations is known in advance, such as when processing Strings, lists, or dealing
| | | | | | | | | | | | |
with input of a fixed size.
| | | | |
Write some selection statements for a number guessing game that can help
| | | | | | | | | | | |
determine how a user inputted guess is related to the hidden number. As a part
| | | | | | | | | | | | | | |
of your solution you can use two int variables with identifiers guess and
| | | | | | | | | | | | |
secretNumber, which you can assume have been previously declared and | | | | | | | | | |
initialized. Your code should print out the message "Too high", "Too low", or
| | | | | | | | | | | |
|"Correct" based on how the guess is related to the secret number. You do not
| | | | | | | | | | | | | | |
need to worry about reading input or handling multiple guesses. - CORRECT
| | | | | | | | | | | |
ANSWER✔✔-if(guess > secretNumber) { | | |
| System.out.println("Too high"); | |
}
else if (guess < secretNumber) {
| | | | |
| System.out.println("Too low"); | |
}
else { |
| System.out.rpintln("Correct"); |
}
Write a boolean expression involving a variable with the identifier speed that
| | | | | | | | | | | |
evaluates to true when speed is greater than 75 or less than or equal to 44 and
| | | | | | | | | | | | | | | | |
assign that result to a boolean variable that you declare. - CORRECT ANSWER✔✔-
| | | | | | | | | | | |
boolean getsTicket = speed > 75 || speed <= 44;
| | | | | | | | |