Abstraction and Program Design - AP Computer Science A

  1. Which keyword is used to implement an interface in Java?
    The 'implements' keyword is used to implement an interface. Creates contractual obligation to provide method implementations.
  2. What is the significance of the 'public' keyword?
    'public' makes a class, method, or field accessible from any other class. Broadest access level, no restrictions on usage.
  3. Identify the keyword to declare a class as abstract.
    The 'abstract' keyword is used to declare an abstract class. Marks class as incomplete, requiring subclass implementation.
  4. Identify a benefit of using abstraction.
    Abstraction reduces complexity and increases efficiency. Simplifies development by hiding unnecessary implementation details.
  5. Identify the error: 'public void myMethod();'
    Methods must have a body unless declared abstract. Non-abstract methods require implementation in concrete classes.
  6. What is the significance of the 'private' keyword?
    'private' restricts access to the class itself. Most restrictive access, only within declaring class.
  7. What is an abstract class?
    An abstract class cannot be instantiated and may contain abstract methods. Serves as template, requires subclasses for instantiation.
  8. What is the significance of the 'protected' keyword?
    'protected' allows access within the same package and subclasses. Package access plus inheritance-based access for subclasses.
  9. What is a static method in Java?
    A static method belongs to the class rather than any object instance. Called on class itself, not requiring object instantiation.
  10. Identify the error: 'class B extends A, C {}'
    Java does not support multiple inheritance of classes. Classes can only extend one superclass in Java.
  11. What is abstraction in computer science?
    Abstraction is hiding complex details to simplify problem solving. Core concept of managing complexity by focusing on essential features.
  12. What is the result of 'System.out.println(7/2);'?
    The result is 3, due to integer division. Both operands are integers, so result truncates decimal portion.
  13. Which keyword is used to inherit a class in Java?
    The 'extends' keyword is used to inherit a class. Establishes is-a relationship between parent and child classes.
  14. What is the signature of a main method in Java?
    public static void main(String[] args). Entry point for Java applications, must match exact format.
  15. What is a superclass in Java?
    A superclass is the class from which a subclass inherits. Also called parent class, provides structure for child classes.
  16. What is polymorphism in OOP?
    Polymorphism allows for methods to do different things based on the object it is acting upon. Enables same method name to behave differently across classes.
  17. Identify the error: 'public void myMethod(int a, int a)'.
    Duplicate parameter name 'a'. Parameter names must be unique within method signature.
  18. What is the default access modifier in Java?
    The default access modifier is package-private. Accessible within package but not from outside packages.
  19. What is a constructor in Java?
    A constructor is a special method to initialize objects. Called automatically when creating new object instances.
  20. What is a concrete class?
    A concrete class is a class that has no abstract methods. Can be instantiated directly, no abstract methods present.
  21. Correct this snippet: 'public class Test { private Test() {} }'
    No error; this is a valid constructor. Private constructors are legal, often used in singleton patterns.
  22. Identify the error: 'interface I { void method(){} }'
    Interfaces cannot have method bodies. Only default and static methods can have implementations.
  23. What is the purpose of the 'final' keyword in Java?
    'final' is used to declare constants, prevent inheritance, and prevent method overriding. Prevents modification, inheritance, or method overriding.
  24. What is the purpose of the 'this' keyword in Java?
    'this' is used to refer to the current object instance. Distinguishes between instance variables and parameters with same names.
  25. What is the main advantage of using inheritance?
    Inheritance promotes code reuse. Avoids duplicating code across multiple related classes.
  26. Identify the error: 'class A { static int x; }'
    No error; the syntax is correct. Static variables are properly declared at class level.
  27. Correct this snippet: 'int x = new int();'.
    Use 'int x;' or 'Integer x = new Integer();'. Primitives cannot use constructor syntax, only reference types.
  28. What is a subclass in Java?
    A subclass is a class that inherits from another class, the superclass. Also called child class, extends functionality of parent class.
  29. What is the result of 'System.out.println(7.0/2);'?
    The result is 3.5, due to floating-point division. Double operand promotes calculation to floating-point arithmetic.
  30. What is method overriding?
    Method overriding is redefining a superclass method in a subclass with the same signature. Allows subclasses to provide specific implementations.
  31. Identify an example of polymorphism in Java.
    Method overriding is an example of polymorphism. Subclass methods can replace superclass method behavior.
  32. Identify a method to achieve abstraction in Java.
    Abstraction can be achieved using abstract classes and interfaces. Both provide contracts without revealing implementation details.
  33. What is method overloading?
    Method overloading is having multiple methods with the same name but different parameters. Provides flexible method calls with different parameter sets.
  34. What is a package in Java?
    A package is a namespace for organizing classes and interfaces. Groups related classes to prevent naming conflicts.
  35. Define encapsulation in object-oriented programming.
    Encapsulation is bundling data with methods that operate on it. Key principle protecting data integrity through controlled access.
  36. How does encapsulation improve code robustness?
    Encapsulation protects an object's internal state from unintended modification. Private fields prevent direct external manipulation of data.
  37. What does 'inheritance' mean in OOP?
    Inheritance allows a class to inherit fields and methods from another class. Enables code reuse by establishing parent-child class relationships.

Study these flashcards