Java – 4 Mark Answers 1. Explain the concept of Object-Oriented Programming (OOP) in Java.
Java
uses the OOP model, which organizes software into classes and objects. OOP principles include
Encapsulation (bundling data and methods), Inheritance (acquiring properties of another class),
Polymorphism (methods behaving differently based on context), and Abstraction (hiding unnecessary
details). These help create modular, reusable, and maintainable code. 2. What are constructors in
Java? Explain with an example. Constructors are special methods used to initialize objects. They have
the same name as the class and no return type. Constructors run automatically when an object is
created. Example: class Student { Student(String n){ name = n; } } When Student s = new
Student("Asha") executes, the constructor assigns the value. 3. Differentiate between primitive and
reference data types. Primitive types store actual values (e.g., int, double, boolean) and are fast and
memory■efficient. Reference types store memory addresses pointing to objects (e.g., String, arrays,
class objects). Primitives reside on the stack, while reference objects are stored on the heap.
Reference variables can be null, whereas primitives cannot. 4. Explain arrays in Java. An array is a
fixed■size data structure that stores multiple values of the same type in contiguous memory locations.
Arrays are reference types and are created using new. Example: int[] a = new int[5]; Accessing
elements is done using indices like a[0]. Arrays allow easy iteration using loops but cannot change size
once created. 5. Describe the purpose of control statements in Java. Control statements manage the
flow of execution in a program. Selection statements (if, if-else, switch) choose paths based on
conditions. Iteration statements (for, while, do-while) repeat code blocks. Jump statements (break,
continue) alter normal flow. These constructs allow decisions, repetition, and structured programming.
6. What is a class and an object in Java? A class is a blueprint defining properties (variables) and
behaviors (methods). An object is an instance of a class created using new. For example, class Car
defines features, and Car myCar = new Car() creates an object. Objects store data and interact through
methods, enabling modular and reusable design. 7. Explain getter and setter methods. Getter and
setter methods allow controlled access to private instance variables. A setter assigns a value (e.g.,
setName(String n)), while a getter retrieves it (getName()). They enforce encapsulation by preventing
direct modification of fields and allowing validation or restrictions during assignment. 8. Explain the
Java memory model. Java memory is divided into Stack, Heap, and Method Area. Stack stores local
variables and method calls. Heap stores objects created using new. The Method Area stores class
definitions, static variables, and metadata. The Garbage Collector automatically frees memory of
unused objects, helping prevent memory leaks.