logo CodeStepByStep logo

variableScope

Language/Type: Java expressions for %

A variable's scope is the part of a program in which it exists. In Java, the scope of a variable starts when it is declared and ends when the closing curly brace for the block that contains it is reached. A variable is said to be in scope where it is accessible.

Consider the following program:

public class Example {
    public static void main(String[] args) {
        performTest();
    }

    public static void performTest() {
        int count = 12;
        for (int i = 1; i <= 12; i++) {
            runSample();
            System.out.print(count);   
        }
    }

    public static void runSample() {
        System.out.print("sample");
    }
} 
In which of these blocks is the variable count in scope for the entirety of the block?
(order shuffled)
In which of these blocks is the variable i in scope for the entirety of the block?
(order shuffled)

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.