14.1 C
Delhi
Friday, December 27, 2024
Home > Interview TipsOOPS interview questions and answers

OOPS interview questions and answers

Whether you are a fresher or an experienced candidate, if you apply for any role that requires you to know programming languages, you can expect many questions on OOPS concepts from the interviewer without a shadow of a doubt. This article contains some important object oriented programming interview questions that will help you crack the interview. The article also contains several example programs to help you understand the OOPS concepts interview questions better. 

***PLEASE NOTE THAT public static void main(String[] args) is abbreviated as PSVM() in all the example programs.

  • OOPS interview questions and answers
  • Basic definitions 
  • OOPS interview questions on features, Core features of OOPS-questions related to inheritance, abstraction, encapsulation, and polymorphism
  • Questions based on inheritance
  • Questions based on polymorphism
  • Questions based on abstraction concept and encapsulation
  • OOPS interview questions based on interface concept
    • Questions based on wrapper classes
    • Questions based on keywords this, super and final 
    • Garbage collection
    • Ternary operator
    • Questions based on constructors
  • OOPS methods and functions interview questions
  • OOPS interview questions on exception handling
  • Applications, advantages, and disadvantages of object oriented programming system

Interview Tips

Try to explain the OOPS interview questions by giving real-life examples. Always try to explain the concept in simple terms. Relating the concepts to the real world impresses the interviewer.

The final tip, which is very important, is don’t panic and be honest if you don’t know the answer.

This article has the most asked object oriented programming interview questions.

OOPS interview questions and answers.

Basic definitions, OOPS interview questions:

1.What do you mean by OOPS?

Object oriented programming system abbreviated as OOPS is a programming paradigm formulated around Objects.

OOPS, or object-oriented programming system, is a kind of programming that revolves around objects. Unlike C language, which is based on functions, programming languages like Java and Python are widely based on OOPS. The core concepts in OOPS are classes and objects using which the programs are designed. 

2. What is a class?

Class is like a blueprint or a template that is required to create objects. It contains variables and methods. A class doesn’t occupy any memory and is a logical entity.

For example, Animal is a class, and Dog, Cat are objects. Here, methods could be Eat() and Run(). 

3. What is an object? 

An object is an instance of a class. An object is created from a class. An object has a state and behavior, which are variables(data members) and methods(functions) performed on the data members. An object is a physical entity.

This line of code in the program creates an object of Vehicle class in java.

Vehicle car= new Vehicle();

We call methods through objects in the main method.

4.What are access modifiers, and how many types are there?

Access modifiers describe the behavior of the class.

In java, the allowed access modifiers for the top-level classes are

  • public
  • default
  • abstract
  • final
  • strictfp

For inner level classes, in addition to the above five access modifiers, three more are allowed

  • private
  • protected
  • static

The Most restricted modifier is a private modifier, and the most accessible modifier is a public modifier.

AccessibilityProtectedpublicprivatedefault
If within the same classYesYesYesYes
From the derived class of the same packageYesYesNoYes
From the non child class of the same packageYesYesNoYes
From the child class of an outside packageYes, with child referenceYesNoNo
From non child class of an outside packageNoYesNoNo
  • OOPS interview questions on features of object oriented programming interview questions-questions related to inheritance, abstraction, encapsulation, and polymorphism.

5.What are the primary features of OOPS?

  1. inheritance
  2. abstraction
  3. encapsulation 
  4. polymorphism

Questions based on inheritance

6.What is inheritance?

This is a commonly asked OOPS interview question. The inheritance feature in OOPS lets you inherit the properties from other classes. This feature helps in reducing the size of the code and promotes the reusability of the code. In java, we use the keyword ‘extends’ to inherit the properties from the parent class.

For example, if there is a class Doctor and another class called Physician, if we use inheritance property on the Physician class from the parent class Doctor, then the Physician class acquires all the properties, i.e., methods and variables present in the parent class Doctor without having to rewrite those again in the Physician class.

Consider this example where class Guitar inherits class Instrument and class Flute also inherits class Instrument.

class Instrument

}

class Guitar extends Instrument//methods in Instrument class are now available in Guitar class since Guitar inherits the Instrument class.

}

class Flute extends Instrument

}

public class Main

}

7.What are different types of inheritance?

  1. Single inheritance 
  2. Hybrid inheritance
  3. Multilevel inheritance 
  4. Hierarchical inheritance
  5. Multiple inheritance

In java, multiple inheritance is not possible but can be accomplished through the concept of Interface.

Single inheritance                     Multilevel inheritance                            Hierarchical inheritance

 

Hybrid inheritance                      Multiple Inheritance

8.What is hybrid inheritance?

Hybrid inheritance combines multiple and multilevel inheritance.

 

9.Why is multiple inheritance not possible in java?

This is a frequently asked OOPS interview question. This can be explained through an example. Let’s say there is class A,

with a method show(), class B extends class A, and Class C also extends class A. Let’s suppose there is a class D that extends both class B and class C. Now, if I create an object of class D and call the method show(), there is an ambiguity over which show() method to call. Should the Java compiler call the show() method from class B or class A? In order to avoid this confusion, multiple inheritance is not supported in Java. 

This is also called the diamond problem. Consider this example for a better understanding. This code gives an error. Java doesn’t support multiple inheritance.

class A

}

class B extends A

}

class C extends A

}

//java doesn’t support multiple inheritance due to ambiguity issue

class D extends B, C

}

public class Main

 

}

10. What are the drawbacks of inheritance?

1.Inheritance requires jumping between the classes.

2.It should be properly planned; otherwise, we might get errors.

3.The parent class and the child class get tightly coupled when a subclass extends the parent class. When any changes are made in the parent class, child classes are also affected.

Questions based on Polymorphism

11.What is polymorphism?

This is definitely asked as a part of OOPS interview questions.

As the name indicates, polymorphism can be translated into multiple forms. It is a concept where we create methods in different forms. If we want to understand this concept through a real-life example, Water is the best example. Water takes different forms – solid, liquid, and gaseous states.

In java, polymorphism is of two types

1.Compile time polymorphism or method overloading, which is handled by the compiler.

2.Runtime polymorphism or method overriding, which is handled by the JVM.

12.What is method overloading?

Method overloading is a concept wherein multiple methods have the same name. To execute overloading, certain rules are to be followed:

1.The methods should have the same name.

2.The methods should be within the same class.

3.One of the three things must be true. Methods should have:

  • Each overloaded method should have a different number of arguments or
  • The type of arguments must be different or
  • The sequence of arguments must be different.

Example in java:

display()

display(int a)

display(int a, String b)

display(String a, int b)

display(int a, String b, double c) These are overloaded methods with the same method name.

class A

public void display(int a)

public void display(int a, String b)

public void display(String a, int b)

}

public class Main

}

13.Is it possible to implement method overloading by changing the return type of the method in Java?

This is an important OOPS interview question. No, it is not possible to achieve overloading by changing the return type in Java since this leads to ambiguity.

14.What is method overriding?

Through method overriding, we can redefine the methods in subclasses. To achieve a method overriding, three things must be true.

  • The methods should be in different classes.
  • The methods should have:
  1. the same name
  2. the same arguments
  3. the same sequence of arguments
  • The class must have an IS-A relationship, i.e., follow inheritance property. Method overriding is only possible in sub classes that inherit the properties of the parent class.

Consider the following simple java code. In the code below, Animal is a parent class, and Dog is a subclass. Here the classes follow inheritance, and the methods have the same name and arguments. Hence overriding is achieved.

 

class Animal //parent class

}

class Dog extends Animal //sub class (inheritance)

}

public class Main

}

15.Can we overload the main method in java?

Yes. It is possible to overload the main method in java as the main method has a string array as arguments. There should be an original main() method where the method calls are made.

public class Main

   

//Overloaded main method 2   

public static void main(int args)   

   

 

//Original main method

 PSVM()

}

16.Is overriding of overloaded methods possible?

For this OOPS interview question, you could say; Yes. Overriding of overloaded methods is possible in java. Here the adding() method is overloaded in class A. Overriding of adding with three parameters is done in class B. This is possible in Java.

 

class A

  public void adding(int a, int b, int c)//overloading of adding() method

  

}

class B extends A

}

public class Main

}

17.What is hierarchical inheritance?

In this type of inheritance, one parent class is inherited by several subclasses. Consider this code snippet(*This is not the complete code). Here the parent class is Animal, two classes namely Dog and Cat, both extend Animal.

class Animal

class Dog extends Animal

class Cat extends Animal

18.What is a superclass?

super class is a class from which other classes can be derived. If a class inherits properties from a class, this class is called a superclass.

Consider this line of code.

class Animal

class Dog extends Animal //here Animal is the super class

 

19.What is a subclass?

The class which is derived from another class is a subclass. Here Dog is a subclass of the Animal class.

class Animal

class Dog extends Animal //here Dog is the subclass 

20.What are abstract classes and methods?

Abstract classes contain abstract methods which are only declared in the parent class. An abstract method does not provide the definition or the implementation. The implementation is provided in the sub classes. 

An abstract class can contain zero or any number of abstract methods, but if there is at least one abstract method in a class, the class must be declared abstract. It is mandatory to provide the implementation of an abstract method in the sub class. Object of an abstract class cannot be created, which also implies instantiation of an abstract class is not possible. In short, abstraction allows us to provide only the necessary and relevant details to the user, and the internal implementation is hidden. Abstraction has several advantages like:

  1. This increases security since only necessary details are made visible.
  2. Code reusability is possible.
  3. The code becomes less complex.

More Resources : Android Developer Job Description | Full-Stack Developer Interview Questions for freshers | Front End Developer Resume samples | Data Entry Operator Exam Questions and Answers

 

- Advertisement -spot_img

More articles

spot_img

Latest article

Build resume using templates

Explore urgently hiring jobs

Personalized jobs for you