175k views
1 vote
Question 1

Errors can be syntax errors or logic errors (the code works, but not as intended).

In the Guess My Number game, there is a lower limit and an upper limit for the range of possible numbers to guess. If the lower limit were exclusive and the upper limit exclusive, which expression would properly generate values for the secret number?

(int)(Math.random() * (upper − lower) ) + lower
(int)(Math.random() * (upper − lower + 1) ) + lower
(int)(Math.random() * (upper − lower) ) + lower + 1
(int)(Math.random() * (upper − 1 − lower) ) + lower + 1
(int)(Math.random() * (upper − lower + 1) ) + lower − 1




Question 2
Errors can be syntax errors or logic errors (the code works, but not as intended).

Which of the following is equivalent to while( !(userGuess == secretNumber) )?


while( userGuess != secretNumber )
while(userGuess < secretNumber || userGuess > secretNumber)
while( userGuess < secretNumber && userGuess > secretNumber)
I only
II only
III only
I and II only
I and III only




Question 3
Errors can be syntax errors or logic errors (the code works, but not as intended).

After execution of the following code segment, what will be displayed?

int x = 15;
while(x > 0)
{
x −= 6;
}
System.out.println(x);

−6
−3
0
3
15




Question 4
Errors can be syntax errors or logic errors (the code works, but not as intended).

What conclusion can be made about the state of the program when the while loop terminates? Assume answer is a declared and initialized String.

while(!answer.equals( "N"))
{
// code not shown
}

The value of answer is N
The value of answer is n or N
The value of answer is not N
The value of answer is Y
Nothing can be determined





Question 5
Errors can be syntax errors or logic errors (the code works, but not as intended).

In the Guess My Number program, the user continues guessing numbers until the secret number is matched. Assuming numGuesses is initialized to 1, how would the while statement be modified to include an extra criterion limiting the number of guesses to 15?

while( (userGuess == secretNumber) || numGuesses < 14 )
while( (userGuess== secretNumber) && numGuesses <= 15 )
while( !(userGuess == secretNumber) || numGuesses < 15 )
while( !(userGuess == secretNumber) && numGuesses <= 15 )
while( !(userGuess == secretNumber) && numGuesses < 15 )

User Benjamin T
by
5.1k points

1 Answer

4 votes

Answer:

Question 1: (int)(Math.random() * (upper − lower) ) + lower

This function will generate a value for secret number. As given that lower limit and upper limit are exclusive so this function will generate a value between upper bound and lower bound but not exceed the bounds.

Question 2: I only

The given while condition says that the loop will continue until the userGuess is not equal to the secretNumber. The similar condition is depicted in the 1st option that says unless userGuess not becomes equal to secretNumber continue performing the do block.

Question 3: -3

According to given scenario the while block operates until value of x becomes less than zero.

  • As x is initiated as 15, it is greater than one so 6 will be subtracted from it as depicted.
  • The answer will be 9 that is again greater than 0 so 6 will be subtracted again which gives 3.
  • Checking for while condition it again is greater than 0 so 6 will be subtracted from 0 giving -3.
  • As now while condition = false so the last value of x which is -3 is printed.

Question 4: The value of answer is N

In this question we have to determine the answer when the while loop terminates. As the while loop terminates when the condition inside the while brackets is true. So the given condition inside the while brackets depicts that answer will be true when it will be equal to N. So when while loop will be terminated, the answer would be N.

Question 5: while( !(userGuess == secretNumber) || numGuesses < 15 )

In this statement, while loop will be terminated only in two conditions that are:

  1. When the number guessed by user is equal to the secret number. The loop will terminate ending the game.
  2. When the number of guesses from the user reaches 15 the while loop will terminate and end the game.

I hope it will help you!

User ArtisticPhoenix
by
5.7k points