Math.random()

Exposure Java 2014 for Teaching
AP® Computer Science
DYRT Quiz 06.01-05
Take out a piece of paper and PEN.
The quiz starts TWO minutes after
the tardy bell rings.
You will have 30 seconds per question.
Title the quiz as shown below
The quiz starts in ONE minute.
Name
Period
Date
Quiz 06.01-05
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
EC.
Question 01
In the statement
What is int ?
(a) a variable
(b) a data type
(c) a class
(d) an object
int x;
Question 02
In the statement
What is x ?
(a) a variable
(b) a data type
(c) a class
(d) an object
int x;
Question 03
In the statement Bank tom = new Bank(5000.0);
Which is the class?
(a) Bank
(b) tom
(c) new
(d) 5000.0
Question 04
In the statement Bank tom = new Bank(5000.0);
Which is the object?
(a) Bank
(b) tom
(c) new
(d) 5000.0
Question 05
How many classes
are being shown in
this example?
(a)
(b)
(c)
(d)
(e)
(f)
(g)
1
2
3
4
5
10
13
Question 06
How many objects
are being shown in
this example?
(a)
(b)
(c)
(d)
(e)
(f)
(g)
1
2
3
4
5
10
13
Question 07
Which of these statements will cause a different set
of random numbers to be generated every time the
program is executed?
(a) Random rand = new Random();
(b) Random rand = new Random(40);
(c) Random rand = new Random(12345);
(d) All of the above
Question 08
Assume rand is an object of the Random class.
What is the output of this statement?
System.out.println(rand.nextInt(1000));
(a) 1000
(b) a random number from 1 to 1000
(c) a random number from 0 to 1000
(d) a random number from 1 to 999
(e) a random number from 0 to 999
Question 09
Assume rand is an object of the Random class.
What is the output of this statement?
System.out.println(rand.nextInt(1001));
(a) 1001
(b) a random number from 1 to 1000
(c) a random number from 0 to 1000
(d) a random number from 1 to 999
(e) a random number from 0 to 999
Question 10
Assume rand is an object of the Random class.
What is the output of this program segment?
int x = rand.nextInt(1000) + 1;
System.out.println(x);
(a) 1001
(b) a random number from 1 to 1000
(c) a random number from 0 to 1000
(d) a random number from 1 to 999
(e) a random number from 0 to 999
Question 11
Which of the following are methods of the Random
class?
(a) nextInt
(b) nextDouble
(c) setSeed
(d) All of the above
Question 12
The random method of the Math class behaves
Just like the _____ method of the _____ class.
(a) nextRnd
(b) nextDouble
(c) nextRandom
(d) nextInt
Random
Random
Dice
Integer
Question 13
The Math.random() method generates a random
number x , such that
(a) 0 < x < 1
(b) 0 <= x < 1
(c) 0 <= x <= 1
(d) 0 < x <= 1
Question 14
What is the output of the following statement?
System.out.println( (int) (100 * Math.random() );
(a) An integer in the [1..100] range
(b) An integer in the [0..100] range
(c) An integer in the [1..99] range
(d) An integer in the [0..99] range
Question 15
Assume rand is an object of the Random class.
Which of the following statements will cause x to
have a random value between 100 and 500?
(a)
(b)
(c)
(d)
(e)
int x = rand.nextInt(500) + 100;
int x = rand.nextInt(100) + 500;
int x = rand.nextInt(400) + 100;
int x = rand.nextInt(401) + 100;
int x = rand.nextInt(100) + 401;
Extra Credit
Assume rand is an object of the Random class.
Which of the following statements will cause x to
have a random value in the same range as this statement:
int x = rand.nextInt(1001) + 500;
(a)
(b)
(c)
(d)
(e)
int x = Math.random(1001) + 500;
int x = (int) (Math.random() * 1001 + 500);
int x = Math.random(1600) + 500;
int x = Math.random() * 1001 + 500;
int x = (int) (Math.random() * 500 + 1001);