131k views
4 votes
Looking at the code below, what are the possible values that could be generated? (Assume Random has been imported with a new Random object).

int randomInt = rand.nextInt(2);
randomInt = randomInt + 1;
System.out.print(randomInt);

User Cstrutton
by
4.7k points

1 Answer

2 votes

Answer:

1 or 2

Step-by-step explanation:

rand.nextInt(2) generats a number between 0 and 2 ( not including 2). randomInt = randomInt + 1 adds 1 to that number.

User Jon Kartago Lamida
by
4.9k points