Menu
CoddyTech

OOPs Basics Interview Questions

The basics placement rounds start with: what OOP is, classes and objects, access modifiers, two output questions.

10 questions
  1. What is OOPs (object-oriented programming)?

    Object-oriented programming (OOP) structures a program around objects: bundles of data (fields) and the methods that work on that data. A class describes the shape; each object is one instance with its own state.

    Java
    public class Main {
        public static void main(String[] args) {
            Counter a = new Counter("clicks");
            Counter b = new Counter("views");
            a.increment();
            a.increment();
            b.increment();
            System.out.println(a + ", " + b);
        }
    }
    
    class Counter {
        private final String name;
        private int value;
    
        Counter(String name) { this.name = name; }
    
        void increment() { value++; }
    
        @Override
        public String toString() { return name + "=" + value; }
    }

    One class, two objects counting separately: it prints clicks=2, views=1.

  2. What is the difference between object-oriented and procedural programming?

    Procedural code is functions that operate on data passed to them; object-oriented code keeps data and the functions that change it together in objects. The difference shows when you add a new kind of thing: procedural code edits every function that switches on the type, OOP adds one class.

  3. What is the difference between a class and an object?

    A class is a blueprint that declares fields and methods and holds no per-object data (only its static fields). An object is an instance created from it at runtime (with new in Java), with its own copy of the instance fields. Syntax in Java classes.

  4. What are the four pillars of OOPs?

    Encapsulation, abstraction, inheritance and polymorphism.

    1. Encapsulation: private fields changed only through methods that enforce the rules.
    2. Abstraction: expose what an object does, hide how.
    3. Inheritance: a class extends another in an is-a relation (class Dog extends Animal).
    4. Polymorphism: one call, behavior chosen by the object (shape.area()).
  5. What are the advantages and disadvantages of OOP?

    OOP makes large programs easier to change because each class owns its data and rules, but it adds ceremony small programs do not need. Gains: modularity, reuse, new types without editing callers, and testing with fakes. Costs: more indirection, fragile deep inheritance trees, and shared mutable objects that complicate concurrency.

  6. Is Java a pure object-oriented language?

    No. Java's eight primitive types (int, double, boolean and so on) are not objects, and static members belong to no object. Wrapper classes (Integer) and autoboxing bridge the gap. Smalltalk is pure OOP, and Python is close, since even integers and functions are objects:

    Python
    print((5).bit_length(), type(len).__name__, isinstance(len, object))

    It prints 3 builtin_function_or_method True.

  7. What is the difference between a method and a function?

    A method is a function that belongs to a class and runs in the context of an object (or of the class, if static); a function stands alone. In Java every function is a method. Python has both: len(items) is a function, items.append(3) a method that receives items as self.

  8. What are access modifiers in OOPs?

    Access modifiers decide which code may use a class member. Java has four: private (the class only), package-private with no keyword (the package), protected (the package plus subclasses elsewhere) and public (everywhere). Keep fields private. Python has only conventions (_name) and name mangling (__name). Details in Java access modifiers.

  9. What do count and id hold after three new Student() calls in Java?

    Java
    public class Main {
        public static void main(String[] args) {
            Student first = new Student();
            new Student();
            Student third = new Student();
            System.out.println(Student.count + " " + first.id + " " + third.id);
        }
    }
    
    class Student {
        static int count = 0;
        int id;
    
        Student() {
            count++;
            id = count;
        }
    }

    count is static, so the three objects share one copy and it ends at 3. id is an instance field, so each object keeps the value count had when it was built: 1 for the first, 3 for the third. A static method could read count but not id, because it has no this.

  10. How do == and equals() compare two strings in Java?

    Java
    public class Main {
        public static void main(String[] args) {
            String a = "hi";
            String b = "hi";
            String c = new String("hi");
            System.out.println((a == b) + " " + (a == c) + " " + a.equals(c));
        }
    }

    == compares references, equals() compares content. String literals are interned, so a and b are the same pooled object and a == b is true. new String("hi") always creates a new object, so a == c is false while a.equals(c) is true. Compare objects with equals(); use == for primitives, enums and deliberate identity checks.

Coddy programming languages illustration

Learn to code with Coddy

Get started