Abstraction and Program Design - AP Computer Science A
- 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.
- 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.
- 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.
- Identify a benefit of using abstraction.
- Abstraction reduces complexity and increases efficiency. Simplifies development by hiding unnecessary implementation details.
- Identify the error: 'public void myMethod();'
- Methods must have a body unless declared abstract. Non-abstract methods require implementation in concrete classes.
- What is the significance of the 'private' keyword?
- 'private' restricts access to the class itself. Most restrictive access, only within declaring class.
- What is an abstract class?
- An abstract class cannot be instantiated and may contain abstract methods. Serves as template, requires subclasses for instantiation.
- 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.
- 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.
- Identify the error: 'class B extends A, C {}'
- Java does not support multiple inheritance of classes. Classes can only extend one superclass in Java.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- Identify the error: 'public void myMethod(int a, int a)'.
- Duplicate parameter name 'a'. Parameter names must be unique within method signature.
- What is the default access modifier in Java?
- The default access modifier is package-private. Accessible within package but not from outside packages.
- What is a constructor in Java?
- A constructor is a special method to initialize objects. Called automatically when creating new object instances.
- What is a concrete class?
- A concrete class is a class that has no abstract methods. Can be instantiated directly, no abstract methods present.
- Correct this snippet: 'public class Test { private Test() {} }'
- No error; this is a valid constructor. Private constructors are legal, often used in singleton patterns.
- Identify the error: 'interface I { void method(){} }'
- Interfaces cannot have method bodies. Only default and static methods can have implementations.
- 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.
- 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.
- What is the main advantage of using inheritance?
- Inheritance promotes code reuse. Avoids duplicating code across multiple related classes.
- Identify the error: 'class A { static int x; }'
- No error; the syntax is correct. Static variables are properly declared at class level.
- Correct this snippet: 'int x = new int();'.
- Use 'int x;' or 'Integer x = new Integer();'. Primitives cannot use constructor syntax, only reference types.
- 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.
- 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.
- What is method overriding?
- Method overriding is redefining a superclass method in a subclass with the same signature. Allows subclasses to provide specific implementations.
- Identify an example of polymorphism in Java.
- Method overriding is an example of polymorphism. Subclass methods can replace superclass method behavior.
- Identify a method to achieve abstraction in Java.
- Abstraction can be achieved using abstract classes and interfaces. Both provide contracts without revealing implementation details.
- 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.
- What is a package in Java?
- A package is a namespace for organizing classes and interfaces. Groups related classes to prevent naming conflicts.
- Define encapsulation in object-oriented programming.
- Encapsulation is bundling data with methods that operate on it. Key principle protecting data integrity through controlled access.
- How does encapsulation improve code robustness?
- Encapsulation protects an object's internal state from unintended modification. Private fields prevent direct external manipulation of data.
- 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.