Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


What will be the output range of 'rand.nextInt(100) + 1' in Java?

  1. 0 to 100

  2. 1 to 100

  3. 1 to 101

  4. 0 to 99

The correct answer is: 1 to 100

This is because when using rand.nextInt(100), the numbers generated are between 0 (inclusive) to 100 (exclusive), so adding 1 to the result will give a range of 1 to 100. Options A and D are incorrect because the lower bound is not inclusive in rand.nextInt(), therefore the range cannot include 0. Option C is incorrect because adding 1 to the result will give a range of 1 to 101, which is not correct based on the parameters of rand.nextInt(100).