Class Variables and Methods - AP Computer Science A

  1. State whether class methods can be abstract.
    No. Abstract requires inheritance; static methods cannot be overridden.
  2. Can a static method be final?
    Yes. Final prevents method from being overridden.
  3. Can class methods be overloaded in Java?
    Yes. Multiple methods with same name but different parameters.
  4. Which keyword should be used to ensure a class variable cannot be changed?
    final. Final prevents reassignment after initialization.
  5. Identify the error: modifying a final class variable.
    Final variables cannot be reassigned. Final variables are constants after initialization.
  6. Can class methods access instance variables directly?
    No. Static context cannot access non-static members directly.
  7. Which keyword is used to synchronize a class method?
    synchronized. Synchronized ensures thread-safe access to the method.
  8. What is the result of calling a non-static method from a static context?
    Compilation error. Static context cannot access non-static members.
  9. Identify the error: static method trying to override an instance method.
    Static methods cannot override instance methods. Static methods cannot participate in polymorphism.
  10. What is the primary advantage of using class variables?
    Shared data among all instances. One copy exists for all objects of the class.
  11. Can a static method call another static method in the same class?
    Yes. Static methods can call other static members directly.
  12. Can class methods be overridden in subclasses?
    No. Static methods belong to class, cannot be overridden.
  13. Identify the keyword used to declare a class method.
    static. Makes the method belong to the class, not instances.
  14. Choose the correct way to call a class method 'compute' from class 'MathUtils'.
    MathUtils.compute(). Use ClassName.methodName() syntax for static methods.
  15. 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.
  16. 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.
  17. What is the main purpose of a static block?
    Initialize static variables. Static blocks run once when class is first loaded.
  18. 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.
  19. 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.
  20. What is the visibility modifier for a class variable visible only within its package?
    default. Default means package-private visibility.
  21. What is the scope of a class variable?
    Entire class. Class variables exist at the class level globally.
  22. Identify the keyword used to declare a class variable.
    static. Makes the variable belong to the class, not instances.
  23. What is the effect of declaring a method as static?
    Belongs to the class, not instances. Static members are associated with the class itself.
  24. What happens if a class variable is not initialized?
    Default value is assigned. Java automatically assigns default values to static variables.
  25. Identify the visibility modifier for a class variable accessible to all classes.
    public. Public modifier allows access from any class.
  26. Determine if a class method can be called using an instance.
    Yes, but not recommended. Compiler allows but style guides discourage this practice.
  27. What is the default value of a class variable of type int?
    0. Numeric primitive types default to zero.
  28. Can a class variable be private?
    Yes. Private restricts access to the declaring class only.
  29. What is the main disadvantage of using class variables extensively?
    Increased coupling. Global state creates dependencies between classes.
  30. Determine the visibility of a class method declared with no modifier.
    Package-private. No access modifier means package-private visibility.
  31. 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.
  32. What is the default value of a class variable of type boolean?
    false. Boolean primitive type defaults to false.
  33. State the default value of a class variable of type double.
    0.0. Floating-point primitive types default to 0.0.
  34. Determine if class methods can access static variables.
    Yes. Static methods can access other static members.
  35. What is the result of accessing a static variable with an instance?
    Valid but discouraged. Works but using class name is clearer style.
  36. In which memory area are class variables stored?
    Method Area. Static variables are stored in the method area of memory.
  37. State the keyword used to prevent inheritance of a class method.
    final. Final prevents method from being overridden in subclasses.
  38. What is the primary use of static import?
    Access static members without class name. Allows direct access to static members without qualification.

Study these flashcards