Java OOP Essentials: 58 flashcards
All 58 flashcards from this kit, free and with no sign-up.
1. What is a class in Java?
Show the answer
A class is a blueprint that defines fields (data) and methods (behavior) for objects.
2. What does the `new` keyword do?
Show the answer
It allocates memory for a new object and calls a constructor to initialize it.
3. What is the difference between a primitive variable and a reference variable?
Show the answer
A primitive variable stores the actual value directly, while a reference variable stores a memory address (reference) to an object.
4. What happens when you assign one reference variable to another?
Show the answer
Both variables point to the same object; the reference is copied, not the object itself.
5. How do you access fields and methods of an object?
Show the answer
Using the dot operator (`.`), e.g., `myCar.color` or `myCar.accelerate()`.
6. What is the default value of a reference variable that has not been assigned an object?
Show the answer
It is `null`.
7. What exception occurs when you call a method on a `null` reference?
Show the answer
`NullPointerException`.
8. What is object state?
Show the answer
Object state is represented by the values of its fields at a given time.
9. What is object behavior?
Show the answer
Object behavior is defined by its methods, which operate on the object's state.
10. What is a constructor?
Show the answer
A special method called when an object is created, used to initialize the object's fields.
11. If no constructor is defined in a class, what happens?
Show the answer
The compiler provides a default constructor with no parameters.
12. Do two objects of the same class share their instance fields?
Show the answer
No, each object has its own independent copy of instance fields.
13. What is the result of `Car myCar;` without `new`?
Show the answer
It declares a reference variable that is `null`; no object is created.
14. Why are classes considered reusable?
Show the answer
Because you can create many objects from the same class, each with its own state, and extend classes to create specialized versions.
15. What is a constructor in Java?
Show the answer
A special method that initializes a new object's state when it is created with `new`.
16. What happens if no constructor is defined in a class?
Show the answer
Java provides a default no-argument constructor that sets fields to default values (0, false, null).
17. When does the default constructor disappear?
Show the answer
As soon as you define any constructor (even a parameterized one), the default constructor is no longer provided.
18. What is a parameterized constructor?
Show the answer
A constructor that accepts arguments to initialize an object with specific values at creation time.
19. What is constructor overloading?
Show the answer
Having multiple constructors in the same class with different parameter lists.
20. What are the two main uses of the `this` keyword in constructors?
Show the answer
1) Disambiguate field names from parameters. 2) Call another constructor of the same class using `this(...)`.
21. What is the rule about `this(...)` in a constructor?
Show the answer
It must be the first statement in the constructor body.
22. What is the initialization order when creating an object?
Show the answer
Memory allocation → default field values → explicit field initializers → constructor body.
23. Do constructors have a return type?
Show the answer
No, constructors have no return type, and their name must exactly match the class name.
24. Can a constructor throw an exception?
Show the answer
Yes, a constructor can throw exceptions (e.g., `IllegalArgumentException`) to prevent invalid object creation.
25. What is the benefit of using `this(...)` to delegate constructors?
Show the answer
It reduces code duplication by centralizing initialization logic in one constructor.
26. What is the role of constructors in dependency injection frameworks?
Show the answer
Dependencies are passed via constructors, promoting immutability and testability.
27. What happens if you try to call `new ClassName()` after defining a parameterized constructor but no no-arg constructor?
Show the answer
You get a compile error because the default constructor is no longer provided.
28. Why must explicit field initializers run before the constructor body?
Show the answer
So that constructors can rely on fields already having their explicit initial values.
29. What is encapsulation in Java?
Show the answer
Encapsulation bundles data (fields) and methods that operate on that data into a single class, and restricts direct access to the data.
30. List the four access levels in Java from most restrictive to least restrictive.
Show the answer
private, package-private (default), protected, public.
31. What is the visibility of a `private` member?
Show the answer
It is accessible only within the same class.
32. What is the default access level when no modifier is specified?
Show the answer
Package-private (accessible within the same package).
33. What is the visibility of a `protected` member?
Show the answer
It is accessible within the same package and to subclasses in other packages.
34. What is the visibility of a `public` member?
Show the answer
It is accessible from anywhere in the program.
35. How do you enforce encapsulation for a field?
Show the answer
Make the field private and provide public getter/setter methods to control access.
36. What is a getter method?
Show the answer
A method that returns the value of a private field, allowing controlled read access.
37. What is a setter method?
Show the answer
A method that updates a private field, often after validating the input, allowing controlled write access.
38. What is the principle of least privilege in access control?
Show the answer
Use the most restrictive access modifier that still allows needed interactions.
39. Name two benefits of encapsulation.
Show the answer
Data hiding and maintainability (also flexibility and security).
40. What is the relationship between encapsulation and data hiding?
Show the answer
Data hiding is a part of encapsulation; encapsulation is broader, also including bundling data with methods and controlling access via an interface.
41. What is a common pattern that uses encapsulation in Java?
Show the answer
JavaBeans, which use private fields with public getters/setters.
42. What happens if you make a field public?
Show the answer
Any code can change it to invalid values, breaking encapsulation and creating tight coupling.
43. Why does encapsulation improve maintainability?
Show the answer
Because internal changes are isolated; you can change the implementation without breaking external code as long as the public interface stays stable.
44. What does the `static` keyword mean in Java?
Show the answer
It marks a member as belonging to the class itself, not to any instance. Static members are shared across all objects.
45. How many copies of a static field exist in memory?
Show the answer
Exactly one copy per class loader, regardless of how many objects are created.
46. Can a static method access an instance field directly?
Show the answer
No, because static methods have no `this` reference. They must use an object reference or make the field static.
47. When does a static block execute?
Show the answer
It runs exactly once when the class is first loaded, before any object is created or any static method is called.
48. What is the difference between `static` and `final`?
Show the answer
`static` means class-level (shared), `final` means unchangeable. Combining them (`static final`) creates a constant.
49. Why is the `main` method declared static?
Show the answer
Because the JVM calls it before any object of the class exists, so it must be accessible without an instance.
50. How are static methods called?
Show the answer
Using the class name, e.g., `Math.sqrt(16)`, without creating an object.
51. What happens when a subclass declares a static method with the same signature as a parent?
Show the answer
The parent method is hidden, not overridden. The method called depends on the reference type, not the object type.
52. Give an example of a static constant declaration.
Show the answer
`public static final int MAX_SIZE = 100;` — a fixed, unchangeable value shared by all instances.
53. What is a common use of static fields?
Show the answer
Tracking class-level state like instance counters, shared caches, or configuration values.
54. Can a static method use `this`?
Show the answer
No, `this` refers to the current object, which doesn't exist in a static context.
55. What is a static block used for?
Show the answer
Complex initialization of static fields, such as loading configuration files, that can't be done in one line.
56. Why are utility methods like `Math.sqrt()` static?
Show the answer
Because they don't depend on object state; they operate purely on their arguments and can be called without an instance.
57. How are static fields initialized?
Show the answer
When the class is loaded, in the order they appear in the source code.
58. What is the recommended way to access static members?
Show the answer
Using the class name, e.g., `ClassName.staticMember`, rather than through an object reference.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages