University of Ottawa

School of IT and Engineering

CSI1102

 

Quiz 1

 

 

Q1. [1 mark]

                                          

In the application Password.java the password you generated was composed of four characters and four digits, the first being a character, the second a digit, then a character, a digit, a character, a digit, a character, and a digit. (Example: e5c8a0b6).

 

Which of the following is the best design decision?  (circle one)

 

A.  To use a while loop

B.  To use a do loop

C.  To use a for loop with 4 iterations

D.  To use a for loop with 8 iterations

E.  To use no loops at all

 

Answer: C

 

 

Q2. [2 marks]

 

Write a for loop with three iterations. In each iteration the body of the for loop prints a value, so that after the loop finishes the following values were printed: 1  2  3

 

 

Answer:

 

for (int i=1; i<=3; i++)

    {   System.out.print(i + "  " );  }

 

 

 

 

 

Q3. [2 marks]

 

Write a short code that generates one random integer with the values 1, 2, or 3.

 

Answer:

 

int j = (int)(Math.random() * 3) + 1;

 

or

 

Random gen = new Random();

int k = gen.nextInt(3) + 1;