Methods: How to Write Them MCQ Practice

Question 1 of 10

A kiln class contains the methods below. Which statement is true?

public class Kiln {
    private int heat;
    public Kiln() {
        heat = 200;
    }
    public void boost() {
        heat += 25;
    }
    public int getHeat() {
        return heat;
    }
}
  1. A. boost returns 25 each time it is called
  2. B. getHeat is a void method
  3. C. boost is void, and getHeat is non-void
  4. D. Both methods return the current heat

Question 2 of 10

A locker tag stores a code. What text does this Java code display?

public class Tag {
    private int code;
    public Tag() {
        code = 41;
    }
    public int copyCode() {
        return code;
    }
}

Tag t = new Tag();
System.out.print(t.copyCode());
  1. A. 0
  2. B. 41
  3. C. code
  4. D. nothing, because copyCode is void

Question 3 of 10

A hose reel reports leftover hose. What text does this Java code display?

public class Reel {
    private int meters;
    public Reel() {
        meters = 8;
    }
    public int leftover(int used) {
        return meters - used;
    }
}

Reel r = new Reel();
int n = r.leftover(3);
System.out.print(n);
  1. A. 3
  2. B. 5
  3. C. 8
  4. D. 11

Question 4 of 10

A chime returns a count, then the caller continues. What text does this Java code display?

public class Chime {
    private int notes;
    public Chime() {
        notes = 3;
    }
    public int play(boolean loud) {
        if (loud) {
            return notes + 4;
        }
        notes = 0;
        return notes;
    }
    public int getNotes() {
        return notes;
    }
}

Chime c = new Chime();
int n = c.play(true);
System.out.print(n + "," + c.getNotes());
  1. A. 7,3
  2. B. 7,0
  3. C. 3,0
  4. D. 0,0

Question 5 of 10

A gate fee method returns from inside an if. What text does this Java code display?

public class Gate {
    public int fee(int age) {
        if (age < 12) {
            return 0;
        }
        return 8;
    }
}

Gate g = new Gate();
System.out.print(g.fee(10) + "," + g.fee(12));
  1. A. 0,0
  2. B. 0,8
  3. C. 8,8
  4. D. 8,0

Question 6 of 10

A scout method returns from inside a loop. What text does this Java code display?

public class Scout {
    public int firstOdd(int start) {
        int n = start;
        while (n < start + 6) {
            if (n % 2 == 1) {
                return n;
            }
            n++;
        }
        return -1;
    }
}

Scout s = new Scout();
System.out.print(s.firstOdd(4));
  1. A. 4
  2. B. 5
  3. C. 9
  4. D. -1

Question 7 of 10

A lantern class uses the methods in the table. Which method is an accessor?

Method header Body
public void tint() shade += 2;
public int getShade() return shade;
public void dim(int n) shade -= n;
public void reset() shade = 0;
  1. A. tint
  2. B. getShade
  3. C. dim
  4. D. reset

Question 8 of 10

A booth tries to raise a score through a parameter. What text does this Java code display?

public class Counter {
    public void bump(int n) {
        n += 5;
    }
}

int score = 10;
Counter c = new Counter();
c.bump(score);
System.out.print(score);
  1. A. 5
  2. B. 10
  3. C. 15
  4. D. 0

Question 9 of 10

A mixer returns an updated copy of a primitive parameter. What text does this Java code display?

public class Mixer {
    public int stir(int cups) {
        cups += 2;
        return cups;
    }
}

int batch = 6;
Mixer m = new Mixer();
int out = m.stir(batch);
System.out.print(batch + "," + out);
  1. A. 8,8
  2. B. 6,6
  3. C. 6,8
  4. D. 8,6

Question 10 of 10

A finder returns from a loop when a square matches. What text does this Java code display?

public class Finder {
    public int hunt(int x) {
        int k = 1;
        while (k <= 4) {
            if (k * k == x) {
                return k;
            }
            k++;
        }
        return 0;
    }
}

Finder f = new Finder();
System.out.print(f.hunt(9) + "," + f.hunt(7));
  1. A. 3,0
  2. B. 9,0
  3. C. 3,4
  4. D. 0,0