int[] data = {23, 41, 8, 62, 15, 39};int m = data[0];for (int i = 1; i < data.length; i++) { if (data[i] > m) { m = data[i]; }}System.out.println(m);
What is printed?
int[] vals = {4, 7, 2, 9};int t = 0;for (int v : vals) { if (v > 4) { t += v; }}System.out.println(t);
What is printed?
int a = 47;int b = 5;System.out.println(a / b + " " + a % b);
What is printed?
int[] data = {4, 0, 8};int i = 0, hits = 0;while (i < data.length && data[i] != 0) { hits++; i++;}System.out.println(hits);
System.out.println(Math.abs(-6) + (int) Math.pow(2, 3) + (int) Math.sqrt(49));String email = "user@site.org";int at = email.indexOf("@");System.out.println(email.substring(at + 1));
public class Meter { private int count; public Meter() { } public int getCount() { return count; }}What is printed?
Meter m = new Meter();System.out.println(m.getCount());
What is the value of r after this segment runs?
int x = 15;int r = 0;if (x > 5) r += 1;else if (x > 10) r += 2;if (x % 3 == 0) r += 4;System.out.println(r);
public class Coin { private int cents; public Coin(int c) { cents = c; } public String toString() { return cents + "c"; }}What is printed?
Coin q = new Coin(25);System.out.println(q);
How many times does the loop body execute (final value of steps)?
int n = 40;int steps = 0;while (n > 1) { if (n % 2 == 0) n = n / 2; else n = 3 * n + 1; steps++;}System.out.println(steps);
A city parking app is modeled by the ParkingMeter class below. A meter stores the number of minutes of paid time remaining. The constructor, getMinutes, and expired methods are already written for you.
public class ParkingMeter { private int minutes; // minutes of paid time remaining public ParkingMeter(int m) { minutes = m; } /** Returns the minutes of paid time remaining. */ public int getMinutes() { return minutes; } /** Returns true if no paid time remains. */ public boolean expired() { return minutes <= 0; } // Part (a): write tick // Part (b): write plateState}
Part (a) Write the method tick, which simulates n minutes passing. For each of the n minutes, if the meter is not already expired, one minute of remaining time is used up. The method returns the number of minutes that were actually counted down (which may be fewer than n if the time runs out). Your method must call expired. For example, a meter created with 5 minutes on which tick(3) is called returns 3 and has 2 minutes left; a meter created with 2 minutes on which tick(5) is called returns 2 and has 0 minutes left.
/** Uses up one minute for each of n passing minutes while time remains; * returns how many minutes were counted down. */public int tick(int n)
Part (b) Write the method plateState, which is given a license-plate string in the form state-abbreviation, a hyphen, then the plate characters, such as "TX-ABC123". The method returns the part of the string before the hyphen (for example "TX"). If the string contains no hyphen, the method returns "??".
/** Returns the state prefix (text before the first '-'), or "??" if no '-'. */public String plateState(String plate)