Class Variables and Methods - AP Computer Science A
Card 1 of 30
Identify the order of execution for static blocks and class variables.
Identify the order of execution for static blocks and class variables.
Tap to reveal answer
Static variables first, then static blocks. Static initialization follows declaration order in source code.
Static variables first, then static blocks. Static initialization follows declaration order in source code.
← Didn't Know|Knew It →
What is the scope of a class variable?
What is the scope of a class variable?
Tap to reveal answer
Entire class. Class variables exist at the class level globally.
Entire class. Class variables exist at the class level globally.
← Didn't Know|Knew It →
What is the main purpose of a static block?
What is the main purpose of a static block?
Tap to reveal answer
Initialize static variables. Static blocks run once when class is first loaded.
Initialize static variables. Static blocks run once when class is first loaded.
← Didn't Know|Knew It →
What is the default value of a class variable of type boolean?
What is the default value of a class variable of type boolean?
Tap to reveal answer
false. Boolean primitive type defaults to false.
false. Boolean primitive type defaults to false.
← Didn't Know|Knew It →
Can a static method call another static method in the same class?
Can a static method call another static method in the same class?
Tap to reveal answer
Yes. Static methods can call other static members directly.
Yes. Static methods can call other static members directly.
← Didn't Know|Knew It →
Can class methods be overridden in subclasses?
Can class methods be overridden in subclasses?
Tap to reveal answer
No. Static methods belong to class, cannot be overridden.
No. Static methods belong to class, cannot be overridden.
← Didn't Know|Knew It →
What is the effect of declaring a method as static?
What is the effect of declaring a method as static?
Tap to reveal answer
Belongs to the class, not instances. Static members are associated with the class itself.
Belongs to the class, not instances. Static members are associated with the class itself.
← Didn't Know|Knew It →
What is a class variable in Java?
What is a class variable in Java?
Tap to reveal answer
A variable declared with the static keyword, shared by all instances. Belongs to the class itself, not individual objects.
A variable declared with the static keyword, shared by all instances. Belongs to the class itself, not individual objects.
← Didn't Know|Knew It →
What is a class method in Java?
What is a class method in Java?
Tap to reveal answer
A method declared with the static keyword, can be called without an instance. Belongs to the class, accessible without creating objects.
A method declared with the static keyword, can be called without an instance. Belongs to the class, accessible without creating objects.
← Didn't Know|Knew It →
Identify the keyword used to declare a class variable.
Identify the keyword used to declare a class variable.
Tap to reveal answer
static. Makes the variable belong to the class, not instances.
static. Makes the variable belong to the class, not instances.
← Didn't Know|Knew It →
What is the visibility modifier for a class variable visible only within its package?
What is the visibility modifier for a class variable visible only within its package?
Tap to reveal answer
default. Default means package-private visibility.
default. Default means package-private visibility.
← Didn't Know|Knew It →
State whether class methods can be abstract.
State whether class methods can be abstract.
Tap to reveal answer
No. Abstract requires inheritance; static methods cannot be overridden.
No. Abstract requires inheritance; static methods cannot be overridden.
← Didn't Know|Knew It →
What is the primary advantage of using class variables?
What is the primary advantage of using class variables?
Tap to reveal answer
Shared data among all instances. One copy exists for all objects of the class.
Shared data among all instances. One copy exists for all objects of the class.
← Didn't Know|Knew It →
Identify the visibility modifier for a class variable accessible to all classes.
Identify the visibility modifier for a class variable accessible to all classes.
Tap to reveal answer
public. Public modifier allows access from any class.
public. Public modifier allows access from any class.
← Didn't Know|Knew It →
Can class methods be overloaded in Java?
Can class methods be overloaded in Java?
Tap to reveal answer
Yes. Multiple methods with same name but different parameters.
Yes. Multiple methods with same name but different parameters.
← Didn't Know|Knew It →
What is the result of calling a non-static method from a static context?
What is the result of calling a non-static method from a static context?
Tap to reveal answer
Compilation error. Static context cannot access non-static members.
Compilation error. Static context cannot access non-static members.
← Didn't Know|Knew It →
Which keyword should be used to ensure a class variable cannot be changed?
Which keyword should be used to ensure a class variable cannot be changed?
Tap to reveal answer
final. Final prevents reassignment after initialization.
final. Final prevents reassignment after initialization.
← Didn't Know|Knew It →
What is the main disadvantage of using class variables extensively?
What is the main disadvantage of using class variables extensively?
Tap to reveal answer
Increased coupling. Global state creates dependencies between classes.
Increased coupling. Global state creates dependencies between classes.
← Didn't Know|Knew It →
Determine if class methods can access static variables.
Determine if class methods can access static variables.
Tap to reveal answer
Yes. Static methods can access other static members.
Yes. Static methods can access other static members.
← Didn't Know|Knew It →
Find the error: accessing instance variable from a static method.
Find the error: accessing instance variable from a static method.
Tap to reveal answer
Static methods cannot access instance variables directly. Static methods need object reference for instance variables.
Static methods cannot access instance variables directly. Static methods need object reference for instance variables.
← Didn't Know|Knew It →
Identify the order of execution for static blocks and class variables.
Identify the order of execution for static blocks and class variables.
Tap to reveal answer
Static variables first, then static blocks. Static initialization follows declaration order in source code.
Static variables first, then static blocks. Static initialization follows declaration order in source code.
← Didn't Know|Knew It →
Can a class variable be private?
Can a class variable be private?
Tap to reveal answer
Yes. Private restricts access to the declaring class only.
Yes. Private restricts access to the declaring class only.
← Didn't Know|Knew It →
Determine the visibility of a class method declared with no modifier.
Determine the visibility of a class method declared with no modifier.
Tap to reveal answer
Package-private. No access modifier means package-private visibility.
Package-private. No access modifier means package-private visibility.
← Didn't Know|Knew It →
State the keyword used to prevent inheritance of a class method.
State the keyword used to prevent inheritance of a class method.
Tap to reveal answer
final. Final prevents method from being overridden in subclasses.
final. Final prevents method from being overridden in subclasses.
← Didn't Know|Knew It →
Which keyword is used to synchronize a class method?
Which keyword is used to synchronize a class method?
Tap to reveal answer
synchronized. Synchronized ensures thread-safe access to the method.
synchronized. Synchronized ensures thread-safe access to the method.
← Didn't Know|Knew It →
Identify the error: modifying a final class variable.
Identify the error: modifying a final class variable.
Tap to reveal answer
Final variables cannot be reassigned. Final variables are constants after initialization.
Final variables cannot be reassigned. Final variables are constants after initialization.
← Didn't Know|Knew It →
What happens if a class variable is not initialized?
What happens if a class variable is not initialized?
Tap to reveal answer
Default value is assigned. Java automatically assigns default values to static variables.
Default value is assigned. Java automatically assigns default values to static variables.
← Didn't Know|Knew It →
Determine if a class method can be called using an instance.
Determine if a class method can be called using an instance.
Tap to reveal answer
Yes, but not recommended. Compiler allows but style guides discourage this practice.
Yes, but not recommended. Compiler allows but style guides discourage this practice.
← Didn't Know|Knew It →
What is the primary characteristic of a static nested class?
What is the primary characteristic of a static nested class?
Tap to reveal answer
Does not require an instance of the outer class. Static nested classes don't need outer class instances.
Does not require an instance of the outer class. Static nested classes don't need outer class instances.
← Didn't Know|Knew It →
Can a static method be final?
Can a static method be final?
Tap to reveal answer
Yes. Final prevents method from being overridden.
Yes. Final prevents method from being overridden.
← Didn't Know|Knew It →