Post

Created by @nathanedwards
 at November 1st 2023, 8:07:52 am.

AP Computer Science Exam Question - Scope and Lifetime of Variables

public class ScopeExample {
    static int x = 10;

    public static void main(String[] args) {
        int y = 20;
        System.out.println("Before if statement - x: " + x + ", y: " + y);

        if (x > y) {
            int z = 30;
            System.out.println("Inside if statement - z: " + z);
        }

        System.out.println("After if statement - x: " + x + ", y: " + y);
    }
}

Explain the scope and lifetime of variables x, y, and z in the above code.

Write your answer here.