Scope and Access - AP Computer Science A
Card 1 of 30
Which access modifier allows subclass and package access?
Which access modifier allows subclass and package access?
Tap to reveal answer
protected. Provides inheritance access while maintaining some encapsulation.
protected. Provides inheritance access while maintaining some encapsulation.
← Didn't Know|Knew It →
What keyword is used to create an instance of a class?
What keyword is used to create an instance of a class?
Tap to reveal answer
new. Allocates memory and creates object instances.
new. Allocates memory and creates object instances.
← Didn't Know|Knew It →
What is the scope of a variable declared inside a method in Java?
What is the scope of a variable declared inside a method in Java?
Tap to reveal answer
Local to the method. Method variables are only accessible within that specific method.
Local to the method. Method variables are only accessible within that specific method.
← Didn't Know|Knew It →
Which keyword is used to declare a constant in Java?
Which keyword is used to declare a constant in Java?
Tap to reveal answer
final. Makes variables immutable after initialization.
final. Makes variables immutable after initialization.
← Didn't Know|Knew It →
Identify the access level if no modifier is specified in Java.
Identify the access level if no modifier is specified in Java.
Tap to reveal answer
Package-private. Default access when no explicit modifier is provided.
Package-private. Default access when no explicit modifier is provided.
← Didn't Know|Knew It →
Which access modifier allows visibility only within its own class?
Which access modifier allows visibility only within its own class?
Tap to reveal answer
private. Most restrictive access level in Java.
private. Most restrictive access level in Java.
← Didn't Know|Knew It →
State the access level for a protected modifier.
State the access level for a protected modifier.
Tap to reveal answer
Class, package, and subclasses. More restrictive than public, less than private.
Class, package, and subclasses. More restrictive than public, less than private.
← Didn't Know|Knew It →
Find the error: 'int x = 5; System.out.println(y);'
Find the error: 'int x = 5; System.out.println(y);'
Tap to reveal answer
Variable 'y' is not declared. Variable y was never declared before being used.
Variable 'y' is not declared. Variable y was never declared before being used.
← Didn't Know|Knew It →
Identify the error: 'public class Test { private int x; x = 10; }'
Identify the error: 'public class Test { private int x; x = 10; }'
Tap to reveal answer
Assignment outside method. Direct assignment statements must be inside methods or constructors.
Assignment outside method. Direct assignment statements must be inside methods or constructors.
← Didn't Know|Knew It →
What is the default access level for interface methods?
What is the default access level for interface methods?
Tap to reveal answer
Public. Interface methods are implicitly public and abstract.
Public. Interface methods are implicitly public and abstract.
← Didn't Know|Knew It →
Which access level allows visibility across different packages?
Which access level allows visibility across different packages?
Tap to reveal answer
Public. Only access level that crosses package boundaries freely.
Public. Only access level that crosses package boundaries freely.
← Didn't Know|Knew It →
Identify the error in: 'public static void main() { int x = 0; }'
Identify the error in: 'public static void main() { int x = 0; }'
Tap to reveal answer
Missing 'String[] args' in main method. Main method requires String array parameter to be valid entry point.
Missing 'String[] args' in main method. Main method requires String array parameter to be valid entry point.
← Didn't Know|Knew It →
What does the static keyword imply for a method?
What does the static keyword imply for a method?
Tap to reveal answer
Belongs to the class, not instances. Can be called without creating an instance of the class.
Belongs to the class, not instances. Can be called without creating an instance of the class.
← Didn't Know|Knew It →
Which keyword is used to prevent method overriding?
Which keyword is used to prevent method overriding?
Tap to reveal answer
final. Prevents subclasses from overriding the method.
final. Prevents subclasses from overriding the method.
← Didn't Know|Knew It →
What is the scope of a variable declared as static?
What is the scope of a variable declared as static?
Tap to reveal answer
Shared across all instances. One copy exists for the entire class, not per instance.
Shared across all instances. One copy exists for the entire class, not per instance.
← Didn't Know|Knew It →
Identify the error: 'class A { private int x; } public class B extends A { x = 5; }'
Identify the error: 'class A { private int x; } public class B extends A { x = 5; }'
Tap to reveal answer
Cannot access private member 'x'. Private members are not inherited by subclasses.
Cannot access private member 'x'. Private members are not inherited by subclasses.
← Didn't Know|Knew It →
Which keyword is used to access superclass methods?
Which keyword is used to access superclass methods?
Tap to reveal answer
super. References the parent class in inheritance hierarchy.
super. References the parent class in inheritance hierarchy.
← Didn't Know|Knew It →
What is the effect of the 'final' keyword on a class?
What is the effect of the 'final' keyword on a class?
Tap to reveal answer
Class cannot be subclassed. Prevents the class from being extended by other classes.
Class cannot be subclassed. Prevents the class from being extended by other classes.
← Didn't Know|Knew It →
Identify the error: 'int x; x++;'
Identify the error: 'int x; x++;'
Tap to reveal answer
Variable 'x' might not have been initialized. Local variables must be explicitly initialized before use.
Variable 'x' might not have been initialized. Local variables must be explicitly initialized before use.
← Didn't Know|Knew It →
What is the access level of a variable declared inside a block?
What is the access level of a variable declared inside a block?
Tap to reveal answer
Local to the block. Block variables exist only within their enclosing braces.
Local to the block. Block variables exist only within their enclosing braces.
← Didn't Know|Knew It →
What type of variable is shared among all instances of a class?
What type of variable is shared among all instances of a class?
Tap to reveal answer
Static variable. Static variables belong to the class, not individual objects.
Static variable. Static variables belong to the class, not individual objects.
← Didn't Know|Knew It →
What is the scope of a static method?
What is the scope of a static method?
Tap to reveal answer
Accessible by class name. Static methods can be called using the class name directly.
Accessible by class name. Static methods can be called using the class name directly.
← Didn't Know|Knew It →
Identify the error in: 'public void method() { return 0; }'
Identify the error in: 'public void method() { return 0; }'
Tap to reveal answer
Void method cannot return a value. Void methods cannot use return statements with values.
Void method cannot return a value. Void methods cannot use return statements with values.
← Didn't Know|Knew It →
What is the scope of a public class?
What is the scope of a public class?
Tap to reveal answer
Accessible from any package. Public classes can be imported and used anywhere.
Accessible from any package. Public classes can be imported and used anywhere.
← Didn't Know|Knew It →
What is the lifetime of a local variable?
What is the lifetime of a local variable?
Tap to reveal answer
Until the method finishes. Local variables are destroyed when method execution ends.
Until the method finishes. Local variables are destroyed when method execution ends.
← Didn't Know|Knew It →
Identify the error: 'public class A { private int x; x = 5; }'
Identify the error: 'public class A { private int x; x = 5; }'
Tap to reveal answer
Assignment outside method or constructor. Assignment statements must be inside methods or constructors.
Assignment outside method or constructor. Assignment statements must be inside methods or constructors.
← Didn't Know|Knew It →
Which keyword restricts inheritance of a method?
Which keyword restricts inheritance of a method?
Tap to reveal answer
final. Prevents methods from being overridden in subclasses.
final. Prevents methods from being overridden in subclasses.
← Didn't Know|Knew It →
What is the scope of a variable declared in a class but outside methods?
What is the scope of a variable declared in a class but outside methods?
Tap to reveal answer
Class-level scope. Instance variables are accessible throughout the entire class.
Class-level scope. Instance variables are accessible throughout the entire class.
← Didn't Know|Knew It →
What is the default value of a class-level int variable?
What is the default value of a class-level int variable?
Tap to reveal answer
- Numeric instance variables are automatically initialized to zero.
- Numeric instance variables are automatically initialized to zero.
← Didn't Know|Knew It →
Identify the error: 'int x; for(int i; i < 10; i++) { x++; }'
Identify the error: 'int x; for(int i; i < 10; i++) { x++; }'
Tap to reveal answer
Variable 'x' might not have been initialized. Loop variable i is declared but not initialized.
Variable 'x' might not have been initialized. Loop variable i is declared but not initialized.
← Didn't Know|Knew It →