this Keyword MCQ Practice

Question 1 of 10

A prize booth stores leftover ink with this. What text does this Java code display?

class StampPad {
    private int ink;
    public StampPad(int ink) {
        this.ink = ink;
    }
    public int remaining() {
        return this.ink;
    }
}

StampPad desk = new StampPad(10);
System.out.print(desk.remaining());
  1. A. 0
  2. B. 10
  3. C. ink
  4. D. null

Question 2 of 10

Two stamp pads start with the same ink. stamp uses this. What text does this Java code display?

class StampPad {
    private int ink;
    public StampPad(int ink) {
        this.ink = ink;
    }
    public void stamp() {
        this.ink -= 2;
    }
    public int remaining() {
        return this.ink;
    }
}

StampPad desk = new StampPad(10);
StampPad hall = new StampPad(10);
desk.stamp();
System.out.print(desk.remaining() + "," + hall.remaining());
  1. A. 8,8
  2. B. 8,10
  3. C. 10,8
  4. D. 10,10

Question 3 of 10

A clerk writes a constructor that names the parameter the same as the field but omits this. What text does this Java code display?

class StampPad {
    private int ink;
    public StampPad(int ink) {
        ink = ink;
    }
    public int remaining() {
        return ink;
    }
}

StampPad desk = new StampPad(10);
System.out.print(desk.remaining());
  1. A. 10
  2. B. 0
  3. C. 20
  4. D. The code does not compile.

Question 4 of 10

A ride ticket uses this to keep the field distinct from a same-named parameter. What text does this Java code display?

class Ticket {
    private int price;
    public Ticket(int price) {
        this.price = price;
    }
    public void markup(int price) {
        this.price += price;
    }
    public int getPrice() {
        return this.price;
    }
}

Ticket ride = new Ticket(12);
ride.markup(5);
System.out.print(ride.getPrice());
  1. A. 5
  2. B. 12
  3. C. 17
  4. D. 0

Question 5 of 10

A pad reports itself by passing this to a clerk. What text does this Java code display?

class StampPad {
    private int ink;
    public StampPad(int ink) {
        this.ink = ink;
    }
    public int remaining() {
        return this.ink;
    }
    public void report(Clerk clerk) {
        clerk.log(this);
    }
}
class Clerk {
    public void log(StampPad pad) {
        System.out.print(pad.remaining());
    }
}

StampPad desk = new StampPad(14);
Clerk booth = new Clerk();
desk.report(booth);
  1. A. 0
  2. B. 14
  3. C. this
  4. D. The code does not compile.

Question 6 of 10

A warehouse method is static and tries to read this. What happens when this Java code is compiled and run?

class Hopper {
    private int grain;
    public Hopper(int grain) {
        this.grain = grain;
    }
    public static int leftover() {
        return this.grain;
    }
}

Hopper bin = new Hopper(9);
System.out.print(Hopper.leftover());
  1. A. 9
  2. B. 0
  3. C. The code does not compile.
  4. D. 100

Question 7 of 10

A hopper has an instance method and a class method. Which statement about this is correct?

class Hopper {
    private int grain;
    public Hopper(int grain) {
        this.grain = grain;
    }
    public void pour(int n) {
        this.grain -= n;
    }
    public static int warehouse() {
        return 100;
    }
}
  1. A. warehouse may use this.grain because Hopper objects can exist.
  2. B. pour may use this because it is an instance method; warehouse may not.
  3. C. Both pour and warehouse may use this.
  4. D. Neither pour nor warehouse may use this.

Question 8 of 10

A crate passes this to a worker that adds apples. What text does this Java code display?

class Crate {
    private int apples;
    public Crate(int apples) {
        this.apples = apples;
    }
    public void add(int n) {
        this.apples += n;
    }
    public int count() {
        return this.apples;
    }
    public void restock(Worker w) {
        w.fill(this);
    }
}
class Worker {
    public void fill(Crate box) {
        box.add(4);
    }
}

Crate red = new Crate(6);
Crate green = new Crate(6);
Worker crew = new Worker();
red.restock(crew);
System.out.print(red.count() + "," + green.count());
  1. A. 10,6
  2. B. 10,10
  3. C. 6,10
  4. D. 6,6

Question 9 of 10

A scoreboard method uses both a parameter named score and this.score. What text does this Java code display?

class Scoreboard {
    private int score;
    public Scoreboard(int score) {
        this.score = score;
    }
    public void bump(int score) {
        score = score + 3;
        this.score = this.score + 1;
    }
    public int getScore() {
        return this.score;
    }
}

Scoreboard board = new Scoreboard(10);
board.bump(5);
System.out.print(board.getScore());
  1. A. 8
  2. B. 11
  3. C. 13
  4. D. 15

Question 10 of 10

A beacon passes this to a tower that boosts only the received object. What text does this Java code display?

class Beacon {
    private int watts;
    public Beacon(int watts) {
        this.watts = watts;
    }
    public void boost(int watts) {
        this.watts += watts;
    }
    public int power() {
        return this.watts;
    }
    public void ping(Tower t) {
        t.echo(this);
    }
}
class Tower {
    public void echo(Beacon b) {
        b.boost(5);
        System.out.print(b.power());
    }
}

Beacon north = new Beacon(8);
Beacon south = new Beacon(3);
Tower hill = new Tower();
north.ping(hill);
System.out.print("," + south.power());
  1. A. 8,3
  2. B. 13,3
  3. C. 13,8
  4. D. 8,8