CS 116 Proficiency Exam / Lab 01

Instructions

We will be going over parts I-III together in class — your only submission for this lab will be your solutions to part IV, which should be committed and pushed to your BitBucket repository by the lab due date.

Unlike future labs, there is no grading rubric for the programming problems on this exam — we're just interested in seeing how you approach the questions, and will use submissions as a gauge for how/what material is covered in class (the machine problems will be given a nominal point value of 5 points each, however, contingent on submission and reasonable effort).

I. Definitions / Short answer

  1. Give an example of a reference type.
  2. What does it mean to say that method arguments are passed-by-value?
  3. Why would you declare a method static? Why would you not?
  4. Give an example of some static methods you've used from the Java library.
  5. What keyword is used to allocate an instance of a class (aka an object)?
  6. What is the purpose of an equals method?
  7. What is the primary difference between a while loop and do-while loop?
  8. Under what circumstances are for loops best used?
  9. Is a switch statement always a suitable replacement for if/else statements? Why or why not?
  10. What distinguishes "service" classes from "client" classes?

II. Evaluation

The questions in this section are based on the following declarations:

int x=5, y=10, z=15;

What do the following expressions evaluate to?

  1. x + 2 * z - y
  2. (x / y) * y
  3. y % (z / x)
  4. ((x < y) && (x >= z)) || (y < z)
  5. x++ + y
  6. ++z

III. Multiple Choice

  1. Which of the following is a syntactically correct constructor declaration for a class named Foo?

    a. public new() { ... }
    b. public Foo() { ... }
    c. public void Foo() { ... }
    d. public int Foo() { ... }

Questions 8 and 9 are based on the following code:

class Container {
    int val;

    public Container(int v) {
        val = v;
    }
}

public class Swapper {
    public static void swapInts(int x, int y) {
        int tmp = x;
        x = y;
        y = tmp;
    }

    public static void swapObjs(Container c1, Container c2) {
        int tmp = c1.val;
        c1.val = c2.val;
        c2.val = tmp;
    }

    public static void main(String[] args) {
        int a = 5, b = 10;

        Container c = new Container(5),
                  d = new Container(10);

        swapInts(a, b); // L1
        swapObjs(c, d); // L2
    }
}
  1. Following the execution of the line marked L1, what are the values of the variables a and b (in that order)?

    a. 5 and 10
    b. 10 and 5
    c. 5 and 5
    d. 10 and 10

  2. Following the execution of the line marked L2, what are the values of c.val and d.val (in that order)?

    a. 5 and 10
    b. 10 and 5
    c. 5 and 5
    d. 10 and 10

  3. Which of the following lines correctly declares and allocates an array of 10 integers?

    a. int iarr = new [10] int;
    b. int iarr[] = new int(10);
    c. int[] iarr = new int[10];
    d. int iarr[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

IV. Machine Problems

  1. Write a program that implements a "guessing game" — it should work as follows:
    • when started, it will generate and save a "secret" number between 0 and 100 (you should use java.util.Random's nextInt method to generate a random number)
    • the user will then be prompted to enter a guess; if the guess is incorrect, one of the following will be output by the program:
      • "Nope!", if this is the player's first guess
      • "Warmer", if the guess is closer to the secret than the previous guess
      • "Colder", if the guess is further away from the secret than the previous guess

      if the guess is correct, the program should just print out a congratulatory message and terminate.

    Your program should be saved in a file named "GuessingGame.java".

  2. Write a program that reads in 10 separate positive integers and prints out their mode — i.e., the most frequently occuring number. You can assume that there will never be a tie. E.g., with input 1 2 5 1 2 2 3 5 2 3, the output should be 2, as it appears the most times. Your program should be saved in a file named "Mode.java".