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: java.util.random class is used to generate random numbers in java program.

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

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;
advertisement

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.