Scope and Access - AP Computer Science A

  1. Find the error: 'int x = 5; System.out.println(y);'
    Variable 'y' is not declared. Variable y was never declared before being used.
  2. What is the default value of a class-level int variable?
    0. Numeric instance variables are automatically initialized to zero.
  3. Which keyword is used to declare a constant in Java?
    final. Makes variables immutable after initialization.
  4. Identify the error: 'int x; x++;'
    Variable 'x' might not have been initialized. Local variables must be explicitly initialized before use.
  5. Identify the error: 'class A { private int x; } public class B extends A { x = 5; }'
    Cannot access private member 'x'. Private members are not inherited by subclasses.
  6. Identify the error in: 'public static void main() { int x = 0; }'
    Missing 'String[] args' in main method. Main method requires String array parameter to be valid entry point.
  7. What is the scope of a variable declared in a class but outside methods?
    Class-level scope. Instance variables are accessible throughout the entire class.
  8. What is the lifetime of a local variable?
    Until the method finishes. Local variables are destroyed when method execution ends.
  9. What is the purpose of the 'this' keyword?
    Refers to the current object. Distinguishes instance variables from parameters with same names.
  10. Which keyword is used to prevent method overriding?
    final. Prevents subclasses from overriding the method.
  11. Identify the error: 'int x; for(int i; i < 10; i++) { x++; }'
    Variable 'x' might not have been initialized. Loop variable i is declared but not initialized.
  12. Identify the error: 'class C { void method() { this = new C(); } }'
    Cannot assign to 'this'. 'this' is a reference and cannot be reassigned.
  13. What is the effect of the 'final' keyword on a class?
    Class cannot be subclassed. Prevents the class from being extended by other classes.
  14. Which access level allows visibility across different packages?
    Public. Only access level that crosses package boundaries freely.
  15. Identify the error: 'public class Test { private int x; x = 10; }'
    Assignment outside method. Direct assignment statements must be inside methods or constructors.
  16. Which access modifier allows subclass and package access?
    protected. Provides inheritance access while maintaining some encapsulation.
  17. What is the scope of a method parameter?
    Local to the method. Parameters are local variables passed to the method.
  18. What is the scope of a public class?
    Accessible from any package. Public classes can be imported and used anywhere.
  19. What is the scope of a static method?
    Accessible by class name. Static methods can be called using the class name directly.
  20. What is the default access level for interface methods?
    Public. Interface methods are implicitly public and abstract.
  21. What scope is limited to a method or constructor?
    Local variable scope. Variables accessible only within their declaring method or constructor.
  22. What is the scope of a class variable?
    Throughout the class. Instance variables are accessible from all methods in the class.
  23. Which keyword is used to inherit a class in Java?
    extends. Establishes inheritance relationship between classes.
  24. What is the effect of using a static import?
    Allows static members to be used without class name. Enables direct use of static members without qualification.
  25. What is the access level of a variable declared inside a block?
    Local to the block. Block variables exist only within their enclosing braces.
  26. What keyword is used to create an instance of a class?
    new. Allocates memory and creates object instances.
  27. State the access level for a protected modifier.
    Class, package, and subclasses. More restrictive than public, less than private.
  28. What type of variable is shared among all instances of a class?
    Static variable. Static variables belong to the class, not individual objects.
  29. Identify the access level if no modifier is specified in Java.
    Package-private. Default access when no explicit modifier is provided.
  30. What is the scope of a variable declared as static?
    Shared across all instances. One copy exists for the entire class, not per instance.
  31. Identify the error in: 'public void method() { return 0; }'
    Void method cannot return a value. Void methods cannot use return statements with values.
  32. Identify the error: 'public class A { private int x; x = 5; }'
    Assignment outside method or constructor. Assignment statements must be inside methods or constructors.
  33. What does the static keyword imply for a method?
    Belongs to the class, not instances. Can be called without creating an instance of the class.
  34. Which keyword restricts inheritance of a method?
    final. Prevents methods from being overridden in subclasses.
  35. Which keyword is used to access superclass methods?
    super. References the parent class in inheritance hierarchy.
  36. Which access modifier allows visibility only within its own class?
    private. Most restrictive access level in Java.
  37. What is the scope of a variable declared inside a method in Java?
    Local to the method. Method variables are only accessible within that specific method.

Study these flashcards