Scope and Access - AP Computer Science A
- Find the error: 'int x = 5; System.out.println(y);'
- Variable 'y' is not declared. Variable y was never declared before being used.
- What is the default value of a class-level int variable?
- 0. Numeric instance variables are automatically initialized to zero.
- Which keyword is used to declare a constant in Java?
- final. Makes variables immutable after initialization.
- Identify the error: 'int x; x++;'
- Variable 'x' might not have been initialized. Local variables must be explicitly initialized before use.
- 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.
- 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.
- 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.
- What is the lifetime of a local variable?
- Until the method finishes. Local variables are destroyed when method execution ends.
- What is the purpose of the 'this' keyword?
- Refers to the current object. Distinguishes instance variables from parameters with same names.
- Which keyword is used to prevent method overriding?
- final. Prevents subclasses from overriding the method.
- 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.
- Identify the error: 'class C { void method() { this = new C(); } }'
- Cannot assign to 'this'. 'this' is a reference and cannot be reassigned.
- What is the effect of the 'final' keyword on a class?
- Class cannot be subclassed. Prevents the class from being extended by other classes.
- Which access level allows visibility across different packages?
- Public. Only access level that crosses package boundaries freely.
- Identify the error: 'public class Test { private int x; x = 10; }'
- Assignment outside method. Direct assignment statements must be inside methods or constructors.
- Which access modifier allows subclass and package access?
- protected. Provides inheritance access while maintaining some encapsulation.
- What is the scope of a method parameter?
- Local to the method. Parameters are local variables passed to the method.
- What is the scope of a public class?
- Accessible from any package. Public classes can be imported and used anywhere.
- What is the scope of a static method?
- Accessible by class name. Static methods can be called using the class name directly.
- What is the default access level for interface methods?
- Public. Interface methods are implicitly public and abstract.
- What scope is limited to a method or constructor?
- Local variable scope. Variables accessible only within their declaring method or constructor.
- What is the scope of a class variable?
- Throughout the class. Instance variables are accessible from all methods in the class.
- Which keyword is used to inherit a class in Java?
- extends. Establishes inheritance relationship between classes.
- 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.
- What is the access level of a variable declared inside a block?
- Local to the block. Block variables exist only within their enclosing braces.
- What keyword is used to create an instance of a class?
- new. Allocates memory and creates object instances.
- State the access level for a protected modifier.
- Class, package, and subclasses. More restrictive than public, less than private.
- What type of variable is shared among all instances of a class?
- Static variable. Static variables belong to the class, not individual objects.
- Identify the access level if no modifier is specified in Java.
- Package-private. Default access when no explicit modifier is provided.
- What is the scope of a variable declared as static?
- Shared across all instances. One copy exists for the entire class, not per instance.
- Identify the error in: 'public void method() { return 0; }'
- Void method cannot return a value. Void methods cannot use return statements with values.
- Identify the error: 'public class A { private int x; x = 5; }'
- Assignment outside method or constructor. Assignment statements must be inside methods or constructors.
- 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.
- Which keyword restricts inheritance of a method?
- final. Prevents methods from being overridden in subclasses.
- Which keyword is used to access superclass methods?
- super. References the parent class in inheritance hierarchy.
- Which access modifier allows visibility only within its own class?
- private. Most restrictive access level in Java.
- 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.