Scope and Access MCQ Practice

Question 1 of 10

A spark lives in a loop body. Which statement about this code is true?

int t = 1;
while (t < 3)
{
    int spark = t;
    t++;
}
System.out.print(spark);
  1. A. The program displays 1
  2. B. The program displays 2
  3. C. The program does not compile because spark exists only in the while block
  4. D. The program displays 3

Question 2 of 10

A dye vat updates a parameter named cloth. What text does this Java code display?

public class DyeVat
{
    public static void stain(int cloth)
    {
        cloth = 9;
    }
}

int cloth = 2;
DyeVat.stain(cloth);
System.out.print(cloth);
  1. A. 9
  2. B. 2
  3. C. 11
  4. D. 0

Question 3 of 10

Which statement about this method is true?

public int label()
{
    private int tag = 5;
    return tag;
}
  1. A. The method compiles and returns 5
  2. B. The method does not compile because a local variable cannot be private
  3. C. tag is a class variable
  4. D. tag must be final

Question 4 of 10

A muffin recipe shadows sugar. What text does this Java code display?

public class Muffin
{
    private int sugar = 8;
    public int dust()
    {
        int sugar = 3;
        return sugar;
    }
    public int getSugar()
    {
        return sugar;
    }
}

Muffin m = new Muffin();
System.out.print(m.dust() + " " + m.getSugar());
  1. A. 3 8
  2. B. 8 8
  3. C. 3 3
  4. D. 11 8

Question 5 of 10

An arcade method takes a parameter named tokens. What text does this Java code display?

public class Arcade
{
    private int tokens = 20;
    public void play(int tokens)
    {
        tokens = tokens - 5;
    }
    public int getTokens()
    {
        return tokens;
    }
}

Arcade a = new Arcade();
a.play(7);
System.out.print(a.getTokens());
  1. A. 15
  2. B. 7
  3. C. 20
  4. D. 2

Question 6 of 10

Which statement is true after this loop finishes compiling?

int sum = 1;
for (int n = 2; n <= 4; n++)
{
    sum *= n;
}
  1. A. sum is 24 and n is 5
  2. B. sum is 24 and n is not in scope
  3. C. sum is 9 and n is 4
  4. D. The fragment does not compile because n is local

Question 7 of 10

Nested if blocks update p. What text does this Java code display?

int p = 6;
if (p > 4)
{
    int q = 1;
    p = p - q;
    if (p > 0)
    {
        int r = 2;
        p = p * r;
    }
}
System.out.print(p);
  1. A. 6
  2. B. 5
  3. C. 10
  4. D. The program does not compile

Question 8 of 10

Two cabin methods use the name logs. What text does this Java code display?

public class Cabin
{
    private int logs = 9;
    public int stack()
    {
        int logs = 2;
        return logs;
    }
    public int peek()
    {
        return logs;
    }
}

Cabin c = new Cabin();
System.out.print(c.stack() + " " + c.peek());
  1. A. 2 9
  2. B. 9 9
  3. C. 2 2
  4. D. 11 9

Question 9 of 10

A vault method shadows code and uses a while loop. What text does this Java code display?

public class Vault
{
    private int code = 15;
    public int scramble(int code)
    {
        int spin = 0;
        while (code > 10)
        {
            spin++;
            code -= 3;
        }
        return spin;
    }
    public int getCode()
    {
        return code;
    }
}

Vault v = new Vault();
System.out.print(v.scramble(16) + " " + v.getCode());
  1. A. 2 15
  2. B. 2 10
  3. C. 1 15
  4. D. 0 15

Question 10 of 10

A folio method shadows pages. What text does this Java code display?

public class Folio
{
    private int pages = 18;
    public int trim(int pages)
    {
        int keep = 0;
        if (pages > 10)
        {
            int cut = 4;
            keep = pages - cut;
        }
        return keep;
    }
    public int getPages()
    {
        return pages;
    }
}

Folio f = new Folio();
System.out.print(f.trim(12));
  1. A. 8
  2. B. 14
  3. C. 12
  4. D. 18