Class Variables and Methods MCQ Practice

Question 1 of 10

Class Beacon stores a glow value on each object. Which statement about pulse is true?

public class Beacon
{
    private int glow;
    public Beacon(int glow)
    {
        this.glow = glow;
    }
    public static void pulse()
    {
        glow++;
    }
}
  1. A. pulse compiles because glow is an int
  2. B. pulse does not compile because a class method cannot use the instance variable glow unless an instance is supplied
  3. C. pulse compiles only after a Beacon object exists at runtime
  4. D. pulse does not compile because constructors cannot assign glow

Question 2 of 10

A ticket booth keeps a shared count. What text does this Java code display?

public class Kiosk
{
    private static int tickets = 4;
    public static int extra()
    {
        return 3;
    }
    public static int total()
    {
        return tickets + extra();
    }
}

System.out.print(Kiosk.total());
  1. A. 3
  2. B. 4
  3. C. 7
  4. D. 12

Question 3 of 10

Class Lantern is shown below. Which class method compiles?

public class Lantern
{
    private int watts;
    public Lantern(int watts)
    {
        this.watts = watts;
    }
    public int getWatts()
    {
        return watts;
    }
    public void dim()
    {
        watts--;
    }
}
  1. A. public static int report() { return watts; }
  2. B. public static void fade() { dim(); }
  3. C. public static int report(Lantern lamp) { return lamp.getWatts(); }
  4. D. public static int report() { return this.watts; }

Question 4 of 10

Two ferries share a loading helper. What text does this Java code display?

public class Ferry
{
    private int cargo;
    public Ferry(int cargo)
    {
        this.cargo = cargo;
    }
    public int getCargo()
    {
        return cargo;
    }
    public static int combined(Ferry a, Ferry b)
    {
        return a.getCargo() + b.getCargo();
    }
}

Ferry left = new Ferry(8);
Ferry right = new Ferry(5);
System.out.print(Ferry.combined(left, right));
  1. A. 8
  2. B. 5
  3. C. 13
  4. D. 0

Question 5 of 10

A greenhouse helper mixes class data and object data. Which statement about estimate is true?

public class Greenhouse
{
    private int trays;
    private static int sheds = 2;
    public Greenhouse(int trays)
    {
        this.trays = trays;
    }
    public int getTrays()
    {
        return trays;
    }
    public static int shedCount()
    {
        return sheds;
    }
    public static int estimate()
    {
        return trays * shedCount();
    }
}
  1. A. estimate compiles because shedCount is static
  2. B. estimate does not compile because it uses instance variable trays without an object
  3. C. estimate compiles and always returns 0
  4. D. estimate fails only at runtime if no Greenhouse object exists

Question 6 of 10

A locker shop counts keys for every new locker. What text does this Java code display?

public class Locker
{
    public static int keys = 0;
    public Locker()
    {
        keys++;
    }
}

Locker first = new Locker();
Locker second = new Locker();
System.out.print(Locker.keys);
  1. A. 0
  2. B. 1
  3. C. 2
  4. D. 3

Question 7 of 10

StampPad declares a public class variable. Which statement from another class displays 9?

public class StampPad
{
    public static int ink = 9;
}
  1. A. System.out.print(StampPad.ink);
  2. B. System.out.print(ink.StampPad);
  3. C. System.out.print(new ink);
  4. D. System.out.print(StampPad->ink);

Question 8 of 10

A harbor records a fixed pier count. Which statement about expand is true?

public class Harbor
{
    public static final int PIERS = 4;
    public static void expand()
    {
        PIERS = PIERS + 1;
    }
}
  1. A. expand stores 5 in PIERS
  2. B. expand does not compile because a final class variable cannot be modified
  3. C. expand leaves PIERS as 4 and compiles
  4. D. expand throws an exception when it runs

Question 9 of 10

A quilt studio shares one square count. What text does this Java code display?

public class Quilt
{
    public static int squares = 1;
    private int border;
    public Quilt(int border)
    {
        this.border = border;
        squares += border;
    }
    public int getBorder()
    {
        return border;
    }
}

Quilt first = new Quilt(2);
Quilt second = new Quilt(3);
System.out.print(Quilt.squares + " " + first.getBorder() + " " + second.getBorder());
  1. A. 6 2 3
  2. B. 5 2 3
  3. C. 3 2 3
  4. D. 6 5 5

Question 10 of 10

A studio class is shown, then two objects are constructed. The table names each field.

public class Studio
{
    public static int lights = 5;
    public static final int ROOMS = 2;
    private int chairs;
    public Studio(int chairs)
    {
        this.chairs = chairs;
        lights--;
    }
    public int getChairs()
    {
        return chairs;
    }
}

Studio north = new Studio(8);
Studio south = new Studio(3);
Field Belongs to Modifiable after initialization
lights the class yes
ROOMS
chairs one object yes

Which pair correctly completes the ROOMS row, and what is Studio.lights after both constructors?

  1. A. the class / no; lights is 3
  2. B. one object / no; lights is 3
  3. C. the class / yes; lights is 5
  4. D. one object / yes; lights is 4