Class Variables and Methods - AP Computer Science A
- State whether class methods can be abstract.
- No. Abstract requires inheritance; static methods cannot be overridden.
- Can a static method be final?
- Yes. Final prevents method from being overridden.
- Can class methods be overloaded in Java?
- Yes. Multiple methods with same name but different parameters.
- Which keyword should be used to ensure a class variable cannot be changed?
- final. Final prevents reassignment after initialization.
- Identify the error: modifying a final class variable.
- Final variables cannot be reassigned. Final variables are constants after initialization.
- Can class methods access instance variables directly?
- No. Static context cannot access non-static members directly.
- Which keyword is used to synchronize a class method?
- synchronized. Synchronized ensures thread-safe access to the method.
- What is the result of calling a non-static method from a static context?
- Compilation error. Static context cannot access non-static members.
- Identify the error: static method trying to override an instance method.
- Static methods cannot override instance methods. Static methods cannot participate in polymorphism.
- What is the primary advantage of using class variables?
- Shared data among all instances. One copy exists for all objects of the class.
- Can a static method call another static method in the same class?
- Yes. Static methods can call other static members directly.
- Can class methods be overridden in subclasses?
- No. Static methods belong to class, cannot be overridden.
- Identify the keyword used to declare a class method.
- static. Makes the method belong to the class, not instances.
- Choose the correct way to call a class method 'compute' from class 'MathUtils'.
- MathUtils.compute(). Use ClassName.methodName() syntax for static methods.
- Find the error: accessing instance variable from a static method.
- Static methods cannot access instance variables directly. Static methods need object reference for instance variables.
- What is a class variable in Java?
- A variable declared with the static keyword, shared by all instances. Belongs to the class itself, not individual objects.
- What is the main purpose of a static block?
- Initialize static variables. Static blocks run once when class is first loaded.
- Identify the order of execution for static blocks and class variables.
- Static variables first, then static blocks. Static initialization follows declaration order in source code.
- What is the primary characteristic of a static nested class?
- Does not require an instance of the outer class. Static nested classes don't need outer class instances.
- What is the visibility modifier for a class variable visible only within its package?
- default. Default means package-private visibility.
- What is the scope of a class variable?
- Entire class. Class variables exist at the class level globally.
- Identify the keyword used to declare a class variable.
- static. Makes the variable belong to the class, not instances.
- What is the effect of declaring a method as static?
- Belongs to the class, not instances. Static members are associated with the class itself.
- What happens if a class variable is not initialized?
- Default value is assigned. Java automatically assigns default values to static variables.
- Identify the visibility modifier for a class variable accessible to all classes.
- public. Public modifier allows access from any class.
- Determine if a class method can be called using an instance.
- Yes, but not recommended. Compiler allows but style guides discourage this practice.
- What is the default value of a class variable of type int?
- 0. Numeric primitive types default to zero.
- Can a class variable be private?
- Yes. Private restricts access to the declaring class only.
- What is the main disadvantage of using class variables extensively?
- Increased coupling. Global state creates dependencies between classes.
- Determine the visibility of a class method declared with no modifier.
- Package-private. No access modifier means package-private visibility.
- What is a class method in Java?
- A method declared with the static keyword, can be called without an instance. Belongs to the class, accessible without creating objects.
- What is the default value of a class variable of type boolean?
- false. Boolean primitive type defaults to false.
- State the default value of a class variable of type double.
- 0.0. Floating-point primitive types default to 0.0.
- Determine if class methods can access static variables.
- Yes. Static methods can access other static members.
- What is the result of accessing a static variable with an instance?
- Valid but discouraged. Works but using class name is clearer style.
- In which memory area are class variables stored?
- Method Area. Static variables are stored in the method area of memory.
- State the keyword used to prevent inheritance of a class method.
- final. Final prevents method from being overridden in subclasses.
- What is the primary use of static import?
- Access static members without class name. Allows direct access to static members without qualification.