This session covers:

1. Some reviews for 6.1

2. Sentinel Values

3. Common Loop Algorithm

4. Random

  1. (1pt)

    The code snippet below checks whether a given number is a prime number. What will be the result of executing it?

    public static void main(String[] args) {

            System.out.print("Please enter an integer greater than 1:  ");
            int number = new Scanner(System.in).nextInt();
            if (number <= 1) {
                System.out.println(number + " is not greater than 1.");
                return;
            }
            
            int j = 2;
            boolean isPrime = true;

            while (j <= number/2 && isPrime) {      // better: while (j*j <= number 
    && isPrime)

                if (number % j == 0) isPrime = false;
                j++;
            }

            String isItPrime = "";
            if (!isPrime) isItPrime = "not ";
            System.out.println(number + " is " + isItPrime + "prime.");
        }

    The code snippet will not compile.
    The code snippet will display the desired result.
    The code snippet will display an incorrect result. // incorrect if number is 1
    The code snippet will loop forever.
  2. (1pt)

    How many times will the output line be printed in the following code snippet?

     

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

    {

    for (int num1 = 0; num1 <= 2; num1++)

    {

    System.out.println("" + num2 + " " + num1);

    }

    }

    3
    6
    9
    12
  3. (1pt)

    What is the sentinel value in the following code snippet?

     

    public static void main(String[] args) {

            System.out.print("Enter an age (-1 to stop): ");
            Scanner reader = new Scanner(System.in);
            int sumOfAges = 0;

            for (int age = reader.nextInt();  age != -1;  age = reader.nextInt()) {
                sumOfAges = sumOfAges + age;
                System.out.print("Enter an age (-1 to stop): ");
            }
            System.out.println("Sum of ages " + sumOfAges);

    }

    -1
    0
    1
    2
  4. (1pt)

    Which of the following code snippets will generate a random number between 0 and 79?

    int val = (int) Math.random() % 80;
    int val = (int) Math.random() * 80 - 1;
    int val = (int) Math.random() % 79;
    int val = (int) Math.random() * 80;
  5. (1pt)

    Which code snippet produces the sum of the first n positive even numbers?


     

    a)

    int sum = 0;
    for (int i = 1; i <= n; i++) 
         if (i % 2 == 0) sum +=  i

    b)

    int sum = 0;
    for (int i = 1; i <= n; i++)  sum +=  i * 2;

    c)

    int sum = 0;
    for (int i = 0; i < n; i++) 
        if (i % 2 == 0) sum += i;

    d)

    int sum;
    for (int i = 1; i < n; i++)  sum += i * 2;

     

    a
    b
    c
    d
  6. (1pt)

    How many times does the following loop execute?

    do {
    double x = Math.random() * 100;
    System.out.println(x);
    while (x > 0);
    Exactly once
    On average 50 times
    Can't be determined
    Always an infinite loop
  7. (1pt)

    Assume the following variable has been declared and given a value as shown.

    Random rand = new Random();
     
    Which of the following will generate a random integer in the range – 20 to 20, inclusive, where each value has an equal chance of being generated?
    rand.nextInt (-20, 20)
    rand.nextInt(20) - 41
    rand.nextInt (-20) + 40
    rand.nextInt(41) - 20
  8. (1pt)

    Assume the following variable has been declared and given a value as shown:

    Random rand = new Random();
    double number = rand.nextDouble() * 2 + 3;
     
    What is the range of values number may be assigned?
    From 3.0 up to but not including 5.0
    From 0.0 up to but not including 6.0
    From -3.0 up to and including 3.0
    From 3.0 up to and including 5.0
  9. (1pt)

    What does the method below return?

     

    public int findSomething (String str) {

    int position = 0;

    while (position < str.length()  && (str.charAt(position) != 'e')) position++;

    return position;

    }

    The position of the first 'e' in the string or the length of the string if there is no 'e'
    The position of the last 'e' in the string or the length of the string if there is no 'e'
    The position of the first character that is not an 'e' in the string or the length of the string if there is no character that is not an 'e'
    The method won't compile.
  10. (1pt)

    What does the following loop compute?

     

    Scanner in = new Scanner (System.in);

    int sum = 0;

    int count = 0;

    while (in.hasNextInt())

    {

    int value = in.nextInt();

    if (value > 0)

    {

    sum += value;

    count++;

    }

    }

    double result = (sum * 1.0)/count;

    The average of all the integers in the input
    The sum of all the positive integers in the input divided by the number of integers in the input
    The average of all the positive integers in the input
    The second smallest value in the input