This set of Java Multiple Choice Questions & Answers (MCQs) on “Random Number”.
1. Which class is used to generate random number? Answer: c 2. Which method is used to generate boolean random values in java? Answer: a 3. What is the return type of Math.random() method? Answer: b 4. Random is a final class? Answer: b 5. What is the range of numbers returned by Math.random() method? Answer: d 6. How many bits are used for generating random numbers? Answer: c 7. What will be the output of the following Java code snippet? a) Random number between 1 to 15, including 1 and 15 Answer: a 8. What will be the output of the following Java code snippet? a) Random number between 4 to 7, including 4 and 7 Answer: d 9. Math.random() guarantees uniqueness? Answer: b 10. What is the signature of Math.random() method? Answer: a
a) java.lang.Object
b) java.util.randomNumber
c) java.util.Random
d) java.util.Object
Clarification: java.util.random class is used to generate random numbers in java program.
a) nextBoolean()
b) randomBoolean()
c) previousBoolean()
d) generateBoolean()
Clarification: nextBoolean() method of java.util.Random class is used to generate random numbers.
a) Integer
b) Double
c) String
d) Boolean
Clarification: Math.random() method returns floating point number or precisely a double.
a) True
b) False
Clarification: Random is not a final class and can be extended to implement the algorithm as per requirement.
a) -1.0 to 1.0
b) -1 to 1
c) 0 to 100
d) 0.0 to 1.0
Clarification: Math.random() returns only double value greater than or equal to 0.0 and less than 1.0.
a) 32
b) 64
c) 48
d) 8
Clarification: Random number can accept 64 bits but it only uses 48 bits for generating random numbers.int a = random.nextInt(15) + 1;
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
Clarification: random.nextInt(15) + 1; returns random numbers between 1 to 15 including 1 and 15.int a = random.nextInt(7) + 4;
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
Clarification: random.nextInd(7) + 4; returns random numbers between 4 to 10 including 4 and 10. it follows “nextInt(max – min +1) + min” formula.
a) True
b) False
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.
a) public static double random()
b) public void double random()
c) public static int random()
d) public void int random()
Clarification: public static double random() is the utility method provided by Math class which returns double.