250+ TOP MCQs on Random Number and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) 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

Answer: c
Clarification: 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()

Answer: a
Clarification: 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

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

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

Answer: b
Clarification: 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

Answer: d
Clarification: 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

Answer: c
Clarification: 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

Answer: a
Clarification: 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;

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

Answer: d
Clarification: 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

Answer: b
Clarification: 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()

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

contest

Leave a Reply

Your email address will not be published. Required fields are marked *