Class Variables and Methods MCQ Practice

Question 1 of 10

A mill class starts grinding from a class method. Which statement about start is true?

public class Mill
{
    private int grain;
    public void grind()
    {
        grain--;
    }
    public static void start()
    {
        grind();
    }
}
  1. A. start compiles because grind has no parameters
  2. B. start does not compile because a class method cannot call instance method grind unless an instance is supplied
  3. C. start compiles and grind uses a default mill
  4. D. start fails only at runtime

Question 2 of 10

A banner shop mixes dye with class helpers. What text does this Java code display?

public class Banner
{
    private static int dye = 6;
    public static int mix()
    {
        return dye + 2;
    }
    public static int batch()
    {
        return mix() + mix();
    }
}

System.out.print(Banner.batch());
  1. A. 6
  2. B. 8
  3. C. 16
  4. D. 12

Question 3 of 10

Class Canoe is shown below. Which class method compiles?

public class Canoe
{
    private int depth;
    public Canoe(int depth)
    {
        this.depth = depth;
    }
    public int getDepth()
    {
        return depth;
    }
    public void splash()
    {
        depth++;
    }
}
  1. A. public static int draft() { return depth; }
  2. B. public static void rock() { splash(); }
  3. C. public static int draft(Canoe boat) { return boat.getDepth(); }
  4. D. public static int draft() { return this.depth; }

Question 4 of 10

An orchard helper groups trees four at a time. What text does this Java code display?

public class Orchard
{
    private int trees;
    public Orchard(int trees)
    {
        this.trees = trees;
    }
    public int getTrees()
    {
        return trees;
    }
    public static int rows(Orchard plot)
    {
        int n = plot.getTrees();
        int r = 0;
        while (n > 0)
        {
            r++;
            n -= 4;
        }
        return r;
    }
}

Orchard grove = new Orchard(9);
System.out.print(Orchard.rows(grove));
  1. A. 1
  2. B. 2
  3. C. 3
  4. D. 9

Question 5 of 10

A thread shop mixes leftover class data with one spool's length. Which statement about waste is true?

public class Spool
{
    private int length;
    private static int leftover = 3;
    public Spool(int length)
    {
        this.length = length;
    }
    public static int scrap()
    {
        return leftover;
    }
    public static int waste()
    {
        return length + scrap();
    }
}
  1. A. waste compiles and returns leftover only
  2. B. waste does not compile because length is an instance variable
  3. C. waste does not compile because scrap is static
  4. D. waste compiles whenever a Spool object exists at runtime

Question 6 of 10

A theater marquee shares one bulb count. What text does this Java code display?

public class Marquee
{
    public static int bulbs = 10;
    public Marquee()
    {
        bulbs -= 2;
    }
}

Marquee left = new Marquee();
Marquee right = new Marquee();
System.out.print(Marquee.bulbs);
  1. A. 10
  2. B. 8
  3. C. 6
  4. D. 4

Question 7 of 10

Kiln declares a public class variable. Which statement from another class displays 14?

public class Kiln
{
    public static int heat = 14;
}
  1. A. System.out.print(Kiln.heat);
  2. B. System.out.print(heat.Kiln);
  3. C. System.out.print(Kiln:heat);
  4. D. System.out.print(this.heat);

Question 8 of 10

A barge has a fixed hold size. Which statement about fill is true?

public class Barge
{
    public static final int HOLDS = 8;
    public static int crates = 3;
    public static void fill()
    {
        crates = HOLDS;
        HOLDS = crates;
    }
}
  1. A. fill stores 8 in crates and in HOLDS
  2. B. fill does not compile because HOLDS cannot be assigned
  3. C. fill throws an exception after crates becomes 8
  4. D. fill leaves HOLDS as 8 and crates as 3

Question 9 of 10

An attic inventory shares one box count. What text does this Java code display?

public class Attic
{
    public static int boxes = 4;
    private int labels;
    public Attic(int labels)
    {
        this.labels = labels;
        boxes += labels;
    }
    public int getLabels()
    {
        return labels;
    }
}

Attic east = new Attic(1);
Attic west = new Attic(2);
System.out.print(Attic.boxes + " " + east.getLabels() + " " + west.getLabels());
  1. A. 7 1 2
  2. B. 6 1 2
  3. C. 4 1 2
  4. D. 7 3 3

Question 10 of 10

A loom shares thread and keeps a constant frame count. After both constructors, what is Loom.thread + Loom.FRAMES + first.getPattern() + second.getPattern()?

public class Loom
{
    public static int thread = 20;
    public static final int FRAMES = 3;
    private int pattern;
    public Loom(int pattern)
    {
        this.pattern = pattern;
        thread -= pattern;
    }
    public int getPattern()
    {
        return pattern;
    }
}

Loom first = new Loom(5);
Loom second = new Loom(4);
  1. A. 23
  2. B. 20
  3. C. 12
  4. D. 32