generate random number + java

generate random number + java

Professional Development

72 Qs

quiz-placeholder

Similar activities

JavaScript

JavaScript

Professional Development

70 Qs

Python1-9

Python1-9

Professional Development

68 Qs

แบบทดสอบ การเขียนโปรแกรม Java

แบบทดสอบ การเขียนโปรแกรม Java

Professional Development

67 Qs

python10AB

python10AB

Professional Development

67 Qs

MortalKombyte

MortalKombyte

University - Professional Development

76 Qs

Repaso final Python

Repaso final Python

Professional Development

71 Qs

Java Fundamentals

Java Fundamentals

Professional Development

70 Qs

Ôn tập Python

Ôn tập Python

Professional Development

67 Qs

generate random number + java

generate random number + java

Assessment

Quiz

Computers

Professional Development

Hard

Created by

ANIL KUMAR

FREE Resource

72 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

20 sec • 1 pt

Media Image
What is the code in the image?
import java.util.Random;
class applent
public static void main(String[] args) {
Random dice = new Random();

Answer explanation

The answer is (C). The code in the image is for a Java program that uses a random number generator to generate numbers between 1 and 6. The main method of the program first creates a random number generator object, and then loops 10 times. In each iteration of the loop, the program generates a random number and prints it to the console.

2.

MULTIPLE CHOICE QUESTION

20 sec • 1 pt

Media Image
What is the line of code that generates a random number between 1 and 100?
import java.util.Random;
public class RandomNumber {
int answer = rand.nextInt(MAX);
System.out.println("Random number: " + answer);

Answer explanation

The answer is (C). The line of code int answer = rand.nextInt(MAX); generates a random number between 1 and 100, where rand is an object of the Random class and MAX is a constant with the value of 100.

3.

MULTIPLE CHOICE QUESTION

20 sec • 1 pt

Media Image
What is the purpose of the setSeed() method in the code in the image?
To generate a random number between 1 and 364.
To ensure that the same random numbers are generated each time the program is run.
To prevent the program from generating the same random numbers each time the program is run.
To generate a random number between 0 and 363.

Answer explanation

The answer is (B). The setSeed() method in the code in the image is used to set the seed for the random number generator. The seed is a number that is used to initialize the random number generator, and it affects the sequence of random numbers that are generated. By setting the seed, the program can be guaranteed to generate the same random numbers each time it is run.

4.

MULTIPLE CHOICE QUESTION

20 sec • 1 pt

Media Image
What is the range of random numbers generated by the code in the image?
0 to 49
1 to 50
7 to 42
0 to 9

Answer explanation

The answer is (A). The code in the image uses the nextInt() method of the Random class to generate random numbers. The nextInt() method generates a random number between 0 and the specified upper bound, which in this case is 50. Therefore, the range of random numbers generated by the code in the image is 0 to 49.

5.

MULTIPLE CHOICE QUESTION

20 sec • 1 pt

Media Image
What is the name of the class that contains the code in the image?
RandomNumbersDemo
RandomNumbers
Numbers
Demo

Answer explanation

The answer is (A). The code in the image is contained in the RandomNumbersDemo class. The RandomNumbersDemo class is declared on line 1 of the image.

6.

MULTIPLE CHOICE QUESTION

20 sec • 1 pt

Media Image
What is the value of the variable randomnum after the code in the image is executed?
0.09112701489
0.123456789
1
100

Answer explanation

The answer is (A). The value of the variable randomnum after the code in the image is executed is 0.09112701489411879. This is because the code in the image uses the nextDouble() method of the Random class to generate a random double number between 0.0 and 1.0. The value of randomnum will be different each time the code is executed.

7.

MULTIPLE CHOICE QUESTION

20 sec • 1 pt

Media Image
What is the purpose of the randomNum() method in the image?
To generate a random number between 188 and 2008.
To print a random number between 188 and 2008 to the console.
To generate a random number between 0 and 2008, inclusive.
To generate a random number between 188 and 2008, exclusive.

Answer explanation

The answer is (A). The purpose of the randomNum() method in the image is to generate a random number between 188 and 2008, inclusive. The method first creates a random number generator object, and then uses the nextInt() method to generate a random number between 0 and 2008, inclusive. The method then adds 188 to the result of the nextInt() method to ensure that the random number is between 188 and 2008, inclusive.

Create a free account and access millions of resources

Create resources
Host any resource
Get auto-graded reports
or continue with
Microsoft
Apple
Others
By signing up, you agree to our Terms of Service & Privacy Policy
Already have an account?