Java Questions & Answers – Random Number

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Random Number”.

1. Which class is used to generate random number?
a) java.lang.Object
b) java.util.randomNumber
c) java.util.Random
d) java.util.Object
View Answer

Answer: c
Explanation: The java.util.Random class is used to generate random numbers in Java. It provides methods such as nextInt(), nextDouble(), and nextFloat() to generate different types of random values. The other options are either incorrect class names or unrelated to random number generation.

2. Which method is used to generate boolean random values in java?
a) nextBoolean()
b) randomBoolean()
c) previousBoolean()
d) generateBoolean()
View Answer

Answer: a
Explanation: nextBoolean() method of java.util.Random class is used to generate random numbers.

3. What is the return type of Math.random() method?
a) Integer
b) Double
c) String
d) Boolean
View Answer

Answer: b
Explanation: Math.random() method returns floating point number or precisely a double.
advertisement

4. Random is a final class?
a) True
b) False
View Answer

Answer: b
Explanation: Random is not a final class and can be extended to implement the algorithm as per requirement.

5. What is the range of numbers returned by Math.random() method?
a) -1.0 to 1.0
b) -1 to 1
c) 0 to 100
d) 0.0 to 1.0
View Answer

Answer: d
Explanation: Math.random() returns only double value greater than or equal to 0.0 and less than 1.0.
Free 30-Day Python Certification Bootcamp is Live. Join Now!

6. How many bits are used for generating random numbers?
a) 32
b) 64
c) 48
d) 8
View Answer

Answer: c
Explanation: Random number can accept 64 bits but it only uses 48 bits for generating random numbers.

7. What will be the output of the following Java code snippet?

int a = random.nextInt(15) + 1;

a) Random number between 1 to 15, including 1 and 15
b) Random number between 1 to 15, excluding 15
c) Random number between 1 to 15, excluding 1
d) Random number between 1 to 15, excluding 1 and 15
View Answer

Answer: a
Explanation: random.nextInt(15) + 1; returns random numbers between 1 to 15 including 1 and 15.

8. What will be the output of the following Java code snippet?

int a = random.nextInt(7) + 4;
advertisement

a) Random number between 4 to 7, including 4 and 7
b) Random number between 4 to 7, excluding 4 and 7
c) Random number between 4 to 10, excluding 4 and 10
d) Random number between 4 to 10, including 4 and 10
View Answer

Answer: d
Explanation: random.nextInd(7) + 4; returns random numbers between 4 to 10 including 4 and 10. it follows “nextInt(max – min +1) + min” formula.

9. Math.random() guarantees uniqueness?
a) True
b) False
View Answer

Answer: b
Explanation: Math.random() doesn’t guarantee uniqueness. To guarantee uniqueness we must store the generated value in the database and compare against already generated values.

10. What is the signature of Math.random() method?
a) public static double random()
b) public void double random()
c) public static int random()
d) public void int random()
View Answer

Answer: a
Explanation: public static double random() is the utility method provided by Math class which returns double.

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.