Scope and Access MCQ Practice

Question 1 of 10

A clerk tries to print a bonus. Which statement about this code is true?

int n = 4;
if (n > 0)
{
    int bonus = 2;
}
System.out.print(bonus);
  1. A. The program displays 2
  2. B. The program displays 0
  3. C. The program does not compile because bonus exists only in the if block
  4. D. The program displays 4

Question 2 of 10

A ticket helper updates a parameter. What text does this Java code display?

public class Ticket
{
    public static int bump(int n)
    {
        n = n + 5;
        return n;
    }
}

int wait = 3;
int shown = Ticket.bump(wait);
System.out.print(wait + " " + shown);
  1. A. 3 8
  2. B. 8 8
  3. C. 3 3
  4. D. 5 8

Question 3 of 10

Which declaration inside a method is not allowed in Java?

public void mark()
{
    public int stamp = 1;
}
  1. A. Local variables cannot be declared public or private
  2. B. stamp must be static
  3. C. mark must return int
  4. D. stamp must be double

Question 4 of 10

A poster class declares a local height. What text does this Java code display?

public class Poster
{
    private int height = 12;
    public int crop()
    {
        int height = 5;
        return height;
    }
}

Poster p = new Poster();
System.out.print(p.crop());
  1. A. 12
  2. B. 5
  3. C. 17
  4. D. 0

Question 5 of 10

A meter method uses a parameter named reading. What text does this Java code display?

public class Meter
{
    private int reading = 9;
    public void reset(int reading)
    {
        reading = 0;
    }
    public int getReading()
    {
        return reading;
    }
}

Meter m = new Meter();
m.reset(4);
System.out.print(m.getReading());
  1. A. 0
  2. B. 4
  3. C. 9
  4. D. 13

Question 6 of 10

Which statement about this loop is true?

int total = 0;
for (int k = 1; k <= 3; k++)
{
    total += k;
}
System.out.print(total + k);
  1. A. The program displays 9
  2. B. The program displays 6
  3. C. The program does not compile because k exists only in the for header and body
  4. D. The program displays 3

Question 7 of 10

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

int a = 2;
if (a > 0)
{
    int b = 5;
    if (b > 1)
    {
        int c = a + b;
        a = c;
    }
}
System.out.print(a);
  1. A. 2
  2. B. 5
  3. C. 7
  4. D. The program does not compile

Question 8 of 10

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

public class Cart
{
    private int load = 8;
    public int pack()
    {
        int load = 3;
        return load;
    }
    public int peek()
    {
        return load;
    }
}

Cart c = new Cart();
System.out.print(c.pack() + " " + c.peek());
  1. A. 3 8
  2. B. 8 8
  3. C. 3 3
  4. D. 11 8

Question 9 of 10

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

public class Pump
{
    private int flow = 10;
    public int drain()
    {
        int flow = 0;
        int step = 3;
        while (step > 0)
        {
            flow += 2;
            step--;
        }
        return flow;
    }
    public int getFlow()
    {
        return flow;
    }
}

Pump p = new Pump();
System.out.print(p.drain() + " " + p.getFlow());
  1. A. 6 10
  2. B. 10 10
  3. C. 6 6
  4. D. 0 10

Question 10 of 10

A desk method shadows ink. The table lists names at the return statement. What text is displayed, and which names are in scope there?

public class Desk
{
    private int ink = 7;
    public int refill(int ink)
    {
        int extra = 2;
        if (ink > 0)
        {
            int boost = 1;
            extra += boost;
        }
        return ink + extra;
    }
}

Desk d = new Desk();
System.out.print(d.refill(4));
Name Kind In scope at return?
ink parameter yes
extra local in method yes
boost local in if block no
instance ink instance variable shadowed by the parameter
  1. A. 7; ink and extra only among the locals
  2. B. 7; ink, extra, and boost
  3. C. 5; ink and extra only among the locals
  4. D. 11; the instance ink and extra