Methods: How to Write Them MCQ Practice

Question 1 of 10

An oven class contains the methods below. Which statement is true?

public class Oven {
    private int temp;
    public Oven() {
        temp = 350;
    }
    public void preheat() {
        temp += 25;
    }
    public int getTemp() {
        return temp;
    }
}
  1. A. preheat returns 375
  2. B. getTemp is void because it only looks at temp
  3. C. preheat is void, and getTemp is non-void
  4. D. Both methods must include a return value

Question 2 of 10

A ferry ticket window returns a fare. What text does this Java code display?

public class Ferry {
    private int base;
    public Ferry() {
        base = 12;
    }
    public int fare(int extra) {
        return base + extra;
    }
}

Ferry boat = new Ferry();
System.out.print(boat.fare(4));
  1. A. 4
  2. B. 12
  3. C. 16
  4. D. 8

Question 3 of 10

A radio method returns early, then the caller prints again. What text does this Java code display?

public class Radio {
    private int band;
    public Radio() {
        band = 88;
    }
    public int tune(boolean jump) {
        if (jump) {
            return band + 2;
        }
        band = 90;
        return band;
    }
    public int getBand() {
        return band;
    }
}

Radio r = new Radio();
int shown = r.tune(true);
System.out.print(shown + "," + r.getBand());
  1. A. 90,90
  2. B. 88,88
  3. C. 88,90
  4. D. 90,88

Question 4 of 10

A hatch method returns from an if. What text does this Java code display?

public class Hatch {
    public int bolts(int wind) {
        if (wind > 20) {
            return 6;
        }
        return 2;
    }
}

Hatch h = new Hatch();
System.out.print(h.bolts(25) + "," + h.bolts(20));
  1. A. 6,2
  2. B. 6,6
  3. C. 2,2
  4. D. 2,6

Question 5 of 10

A probe adds values until a return inside the loop fires. What text does this Java code display?

public class Probe {
    public int climb() {
        int p = 0;
        int i = 1;
        while (i <= 6) {
            p += i;
            if (p > 6) {
                return p;
            }
            i++;
        }
        return p;
    }
}

Probe p = new Probe();
System.out.print(p.climb());
  1. A. 6
  2. B. 10
  3. C. 21
  4. D. 0

Question 6 of 10

A bakery tries to change a primitive count in a parameter. What text does this Java code display?

public class Tally {
    public void addDozen(int n) {
        n += 12;
    }
}

int rolls = 5;
Tally t = new Tally();
t.addDozen(rolls);
System.out.print(rolls);
  1. A. 5
  2. B. 12
  3. C. 17
  4. D. 0

Question 7 of 10

A kettle class uses the methods in the table. Which pair is one accessor and one mutator?

Method Role of the body
fill(int n) sets cups to n
getCups() returns cups
label() returns "tea"
spill() sets cups to 0
  1. A. fill and spill
  2. B. getCups and fill
  3. C. label and getCups
  4. D. spill and fill

Question 8 of 10

A brewer returns a new primitive without changing the argument. What text does this Java code display?

public class Brewer {
    public int wrap(int n) {
        n += 3;
        return n;
    }
}

int a = 4;
Brewer b = new Brewer();
int mid = b.wrap(a);
int last = b.wrap(mid);
System.out.print(a + "," + mid + "," + last);
  1. A. 4,7,10
  2. B. 7,7,10
  3. C. 4,4,4
  4. D. 10,10,10

Question 9 of 10

A ranger uses nested if returns. What text does this Java code display?

public class Ranger {
    public int rank(int n) {
        if (n >= 10) {
            if (n >= 20) {
                return 3;
            }
            return 2;
        }
        return 1;
    }
}

Ranger r = new Ranger();
System.out.print(r.rank(15) + "," + r.rank(20) + "," + r.rank(9));
  1. A. 2,3,1
  2. B. 3,3,1
  3. C. 2,2,1
  4. D. 1,3,2

Question 10 of 10

A dial method returns from a loop, then a later call finishes the loop. What text does this Java code display?

public class Dial {
    public int firstAtLeast(int start, int goal) {
        int n = start;
        while (n < start + 8) {
            if (n >= goal) {
                return n;
            }
            n += 2;
        }
        return -1;
    }
}

Dial d = new Dial();
System.out.print(d.firstAtLeast(1, 5) + "," + d.firstAtLeast(1, 20));
  1. A. 5,-1
  2. B. 5,20
  3. C. 1,-1
  4. D. -1,-1