this Keyword MCQ Practice

Question 1 of 10

A library hold slip stores a patron name with this. What text does this Java code display?

class HoldSlip {
    private String name;
    public HoldSlip(String name) {
        this.name = name;
    }
    public String label() {
        return this.name;
    }
}

HoldSlip novel = new HoldSlip("Ada");
System.out.print(novel.label());
  1. A. Ada
  2. B. null
  3. C. name
  4. D. The code does not compile.

Question 2 of 10

Two lockers use this in spin. What text does this Java code display?

class Locker {
    private int combo;
    public Locker(int combo) {
        this.combo = combo;
    }
    public void spin() {
        this.combo += 1;
    }
    public int code() {
        return this.combo;
    }
}

Locker gym = new Locker(20);
Locker art = new Locker(20);
gym.spin();
gym.spin();
System.out.print(gym.code() + "," + art.code());
  1. A. 22,20
  2. B. 22,22
  3. C. 21,20
  4. D. 20,20

Question 3 of 10

A greenhouse tray constructor names the parameter seeds but does not use this. What text does this Java code display?

class Tray {
    private int seeds;
    public Tray(int seeds) {
        seeds = seeds;
    }
    public int count() {
        return seeds;
    }
}

Tray flat = new Tray(18);
System.out.print(flat.count());
  1. A. 18
  2. B. 0
  3. C. 36
  4. D. The code does not compile.

Question 4 of 10

A ferry pass uses this.fare versus a parameter named fare. What text does this Java code display?

class FerryPass {
    private int fare;
    public FerryPass(int fare) {
        this.fare = fare;
    }
    public void surcharge(int fare) {
        this.fare += fare;
    }
    public int cost() {
        return this.fare;
    }
}

FerryPass dawn = new FerryPass(15);
dawn.surcharge(4);
System.out.print(dawn.cost());
  1. A. 4
  2. B. 15
  3. C. 19
  4. D. 0

Question 5 of 10

A weather buoy passes this to a harbor log. What text does this Java code display?

class Buoy {
    private int signal;
    public Buoy(int signal) {
        this.signal = signal;
    }
    public int reading() {
        return this.signal;
    }
    public void radio(Harbor h) {
        h.log(this);
    }
}
class Harbor {
    public void log(Buoy b) {
        System.out.print(b.reading());
    }
}

Buoy north = new Buoy(27);
Harbor dock = new Harbor();
north.radio(dock);
  1. A. 0
  2. B. 27
  3. C. this
  4. D. The code does not compile.

Question 6 of 10

A cabin radio tries to retune from a class method that uses this. What happens when this Java code is compiled and run?

class Radio {
    private int channel;
    public Radio(int channel) {
        this.channel = channel;
    }
    public static void retune(int channel) {
        this.channel = channel;
    }
    public int band() {
        return this.channel;
    }
}

Radio cabin = new Radio(4);
Radio.retune(7);
System.out.print(cabin.band());
  1. A. 7
  2. B. 4
  3. C. 0
  4. D. The code does not compile.

Question 7 of 10

A trail marker class mixes an instance method and a class method. Which pair correctly fills the table?

class TrailMark {
    private int miles;
    public TrailMark(int miles) {
        this.miles = miles;
    }
    public void hike(int n) {
        this.miles += n;
    }
    public static int parkTotal() {
        return 50;
    }
}
Method Kind May use this?
hike instance ?
parkTotal class ?
  1. A. yes, then yes
  2. B. yes, then no
  3. C. no, then yes
  4. D. no, then no

Question 8 of 10

A pottery wheel passes this to a porter that adds clay. What text does this Java code display?

class Wheel {
    private int clay;
    public Wheel(int clay) {
        this.clay = clay;
    }
    public void add(int n) {
        this.clay += n;
    }
    public int mass() {
        return this.clay;
    }
    public void request(Porter p) {
        p.deliver(this);
    }
}
class Porter {
    public void deliver(Wheel w) {
        w.add(8);
    }
}

Wheel left = new Wheel(11);
Wheel right = new Wheel(11);
Porter aide = new Porter();
left.request(aide);
System.out.print(left.mass() + "," + right.mass());
  1. A. 19,11
  2. B. 19,19
  3. C. 11,19
  4. D. 11,11

Question 9 of 10

A hold slip method updates both a parameter named days and this.days. What text does this Java code display?

class HoldSlip {
    private int days;
    public HoldSlip(int days) {
        this.days = days;
    }
    public void extend(int days) {
        days = days + 2;
        this.days = this.days + days;
    }
    public int left() {
        return this.days;
    }
}

HoldSlip novel = new HoldSlip(3);
novel.extend(4);
System.out.print(novel.left());
  1. A. 5
  2. B. 6
  3. C. 7
  4. D. 9

Question 10 of 10

A kiln passes this to an inspector that fires only the received kiln. What text does this Java code display?

class Kiln {
    private int temp;
    public Kiln(int temp) {
        this.temp = temp;
    }
    public void fire(int temp) {
        this.temp += temp;
    }
    public int heat() {
        return this.temp;
    }
    public void check(Inspector i) {
        i.visit(this);
    }
}
class Inspector {
    public void visit(Kiln k) {
        k.fire(20);
        System.out.print(k.heat());
    }
}

Kiln front = new Kiln(400);
Kiln back = new Kiln(250);
Inspector lab = new Inspector();
front.check(lab);
System.out.print("," + back.heat());
  1. A. 400,250
  2. B. 420,250
  3. C. 420,270
  4. D. 400,270