✕ Close

Week 16 · 16.4 · Thu, Nov 19 · Do Now

Step 1: Start fresh

  1. Close every tab, one at a time. Closing the whole window can bring the old tabs back.
  2. Open Chrome.
  3. Go to the calendar: muggsofcompsci.net/cs1/calendar
  4. Press Ctrl + Shift + R on the calendar page.
  5. Open today from the calendar: Do Now first, then type your ID.

Step 2: the Do Now below.

Do Now

Work every question in your notebook. Do it on your Chromebook with your ID.

  1. Trace row

    Trace these lines. What do the boxes hold after line 3?

    int x = 7;
    double y = x / 2.0;
    x = x / 2;
    1. x = 3, y = 3.5
    2. x = 3.5, y = 3.5
    3. x = 7, y = 3.5
    4. x = 3, y = 3
  2. Predict

    What is printed? The first line printed is first.

    System.out.println(2 + 2 + "2");
    System.out.println("2" + 2 + 2);
    1. 42, then 222
    2. 6, then 6
    3. 222, then 42
    4. 42, then 24
  3. Error analysis

    A student wants the average of 9 and 10, which is 9.5. The code prints 9.0. What mistake did the student make?

    int sum = 9 + 10;
    double avg = sum / 2;
    System.out.println(avg);
    1. sum and 2 are both ints, so sum / 2 keeps only the whole part before it is stored.
    2. avg should be an int.
    3. 9 + 10 is not 19.
    4. println always drops the decimal part.
  4. Pick the type

    A program stores a city name, a count of 12 riders, a fare of 1.25 and whether the bus is full. Which set of declarations compiles?

    1. String city = "NOLA"; int riders = 12; double fare = 1.25; boolean full = false;
    2. String city = "NOLA"; int riders = 12; int fare = 1.25; boolean full = false;
    3. int city = "NOLA"; int riders = 12; double fare = 1.25; boolean full = false;
    4. String city = "NOLA"; int riders = 12; double fare = 1.25; String full = false;
  5. Output

    Trace these lines. Write exactly what is printed, one line for each println.

    int bags = 5;
    double each = 1.75;
    double cost = bags * each;
    System.out.println(bags / 2);
    System.out.println("cost " + cost);