OOP

1. Object-Oriented Programming (OOP) - Core Concepts

Q1: What is Object-Oriented Programming (OOP)?

  • Answer:
    Object-Oriented Programming (OOP) is a programming paradigm that structures code around objects that encapsulate state (data fields) and behavior (methods). The key benefits include modularity, reusability, and maintainability.


Q2: What are the four pillars of OOP?

  • Encapsulation: Restricting direct access to object data.

  • Abstraction: Hiding internal implementation and exposing only necessary details.

  • Inheritance: Allowing a class to inherit properties and methods from another class.

  • Polymorphism: Using the same method in different ways.


Q3: What is the difference between a class and an object?

  • Answer:

    • Class: A blueprint for creating objects.

    • Object: An instance of a class.

    java

    CopyEdit

    class Car { String model; } Car myCar = new Car(); // Object created


Q4: What is a constructor in Java?

  • Answer:
    A constructor is a special method that initializes an object when it is created.

    java

    CopyEdit

    class Car { String model; Car(String m) { model = m; } }


2. Encapsulation, Inheritance, and Polymorphism

Q5: What is encapsulation?

  • Answer:
    Encapsulation restricts direct access to object data and provides controlled access through getter and setter methods.

    java

    CopyEdit

    class Person { private String name; public void setName(String n) { name = n; } public String getName() { return name; } }


Q6: What is inheritance?

  • Answer:
    Inheritance allows a class (child class) to acquire properties of another class (parent class).

    java

    CopyEdit

    class Vehicle { int speed; } class Car extends Vehicle { int wheels = 4; }


Q7: What is polymorphism?

  • Answer:

    • Method Overloading: Multiple methods with the same name but different parameters.

    • Method Overriding: A subclass provides a specific implementation of a method from the parent class.


3. Abstract Classes & Interfaces

Q8: What is an abstract class?

  • Answer:
    An abstract class cannot be instantiated and may contain abstract methods.

    java

    CopyEdit

    abstract class Animal { abstract void sound(); }


Q9: What is an interface?

  • Answer:
    An interface defines a contract for classes to follow.

    java

    CopyEdit

    interface Animal { void sound(); }


4. Exception Handling

Q10: What is an exception?

  • Answer:
    An unexpected event that disrupts program execution.

    java

    CopyEdit

    try { int x = 5 / 0; // Division by zero } catch (ArithmeticException e) { System.out.println("Cannot divide by zero"); }


Q11: What are checked and unchecked exceptions?

  • Answer:

    • Checked exceptions (must be handled) – IOException.

    • Unchecked exceptions (runtime errors) – NullPointerException.


5. File Handling & I/O

Q12: How do you read a file in Java?

  • Answer:
    Using Scanner:

    java

    CopyEdit

    Scanner sc = new Scanner(new File("data.txt")); while (sc.hasNext()) { System.out.println(sc.nextLine()); }


6. Arrays (1D & 2D) & Collections

Q13: What is a 1D array in Java?

  • Answer:
    A 1D array stores a linear sequence of elements.

    java

    CopyEdit

    int[] numbers = {1, 2, 3, 4, 5};


Q14: How do you declare and initialize a 1D array?

  • Answer:

    java

    CopyEdit

    int[] arr = new int[5]; // Declaration and initialization int[] arr = {1, 2, 3, 4, 5}; // Direct initialization


Q15: How do you declare a 2D array in Java?

  • Answer:

    java

    CopyEdit

    int[][] matrix = new int[3][3]; // 3x3 matrix


Q16: What is a ragged array?

  • Answer:
    A ragged array (or jagged array) is a 2D array where rows have different column sizes.

    java

    CopyEdit

    int[][] ragged = { {1, 2, 3}, {4, 5}, {6, 7, 8, 9} };


7. Loops, Control Structures & Operators

Q17: What are the different types of loops in Java?

  • For loop, While loop, Do-while loop.


8. Java Memory Management, Multithreading, and Design Patterns

Q18: What is garbage collection in Java?

  • Answer:
    Java automatically reclaims unused objects.


Q19: What is multithreading?

  • Answer:
    Allows concurrent execution of tasks.

    java

    CopyEdit

    class MyThread extends Thread { public void run() { System.out.println("Thread running"); } }


Q20: What is the Singleton design pattern?

  • Answer:
    Ensures only one instance of a class.

    java

    CopyEdit

    class Singleton { private static Singleton instance; private Singleton() { } public static Singleton getInstance() { if (instance == null) instance = new Singleton(); return instance; } }