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

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

Prepare for your Java exam with our ultimate quiz based on 'Thinking in Java'. Engage with expertly crafted questions that enhance your learning process. Perfect for Java enthusiasts looking to solidify their knowledge!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

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).