Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Can we run a Java application without implementing the OOPs concept?

Answer»

No. Java applications are based on Object-oriented PROGRAMMING models or OOPs concept, and hence they cannot be implemented without it.

However, on the other HAND, C++ can be implemented without OOPs, as it ALSO supports the C-like structural programming MODEL.

2.

What is meant by Garbage Collection in OOPs world?

Answer»

Object-oriented programming revolves around entities like objects. Each object consumes memory and there can be multiple objects of a class. So if these objects and their memories are not handled PROPERLY, then it MIGHT lead to certain memory-related errors and the system might fail.

GARBAGE collection refers to this mechanism of handling the memory in the PROGRAM. Through garbage collection, the unwanted memory is freed up by REMOVING the objects that are no longer needed.

3.

What is meant by exception handling?

Answer»

No one wants its software to fail or crash. Exceptions are the major REASON for software failure. The exceptions can be handled in the program beforehand and prevent the execution from stopping. This is known as exception HANDLING.
So exception handling is the mechanism for identifying the undesirable STATES that the program can reach and specifying the desirable outcomes of such states.
Try-catch is the most COMMON METHOD used for handling exceptions in the program.

4.

What is an exception?

Answer»

An exception can be considered as a special event, which is RAISED during the execution of a program at runtime, that brings the execution to a halt. The reason for the exception is mainly DUE to a POSITION in the program, where the user wants to do something for which the program is not SPECIFIED, like UNDESIRABLE input.

5.

What are access specifiers and what is their significance?

Answer»

Access specifiers, as the name suggests, are a special type of KEYWORDS, which are used to control or SPECIFY the accessibility of entities like classes, methods, ETC. Some of the access specifiers or access modifiers include “PRIVATE”, “public”, etc. These access specifiers also PLAY a very vital role in achieving Encapsulation - one of the major features of OOPs.

6.

How is an abstract class different from an interface?

Answer»

Interface and ABSTRACT class both are special types of classes that contain only the methods declaration and not their implementation. But the interface is entirely DIFFERENT from an abstract class. The main DIFFERENCE between the two is that, when an interface is implemented, the SUBCLASS must define all its methods and provide its implementation. Whereas when an abstract class is inherited, the subclass does not NEED to provide the definition of its abstract method, until and unless the subclass is using it.

Also, an abstract class can contain abstract methods as well as non-abstract methods.

7.

How is data abstraction accomplished?

Answer»

DATA abstraction is ACCOMPLISHED with the help of abstract METHODS or abstract classes.

8.

What is the difference between overloading and overriding?

Answer»

Overloading is a compile-time polymorphism FEATURE in which an entity has MULTIPLE implementations with the same name. For example, METHOD overloading and OPERATOR overloading.

Whereas Overriding is a runtime polymorphism feature in which an entity has the same name, but its implementation changes during EXECUTION. For example, Method overriding.
Image

9.

What is meant by dynamic polymorphism?

Answer»

Dynamic Polymorphism or Runtime polymorphism refers to the type of Polymorphism in OOPS, by which the actual IMPLEMENTATION of the FUNCTION is decided during the runtime or EXECUTION. The dynamic or runtime polymorphism can be achieved with the help of METHOD overriding.

10.

What is meant by static polymorphism?

Answer»

Static POLYMORPHISM is commonly KNOWN as the Compile time polymorphism. Static polymorphism is the FEATURE by which an object is linked with the respective FUNCTION or operator based on the values during the compile time. Static or Compile time Polymorphism can be ACHIEVED through Method overloading or operator overloading.

11.

Define a superclass?

Answer»

Superclass is also a part of INHERITANCE. The superclass is an ENTITY, which ALLOWS subclasses or CHILD classes to INHERIT from itself.

12.

What is a subclass?

Answer»

The subclass is a part of Inheritance. The subclass is an entity, which inherits from another CLASS. It is also KNOWN as the CHILD class.

13.

What are the various types of inheritance?

Answer»

The VARIOUS TYPES of INHERITANCE INCLUDE:

  • SINGLE inheritance
  • Multiple inheritances
  • Multi-level inheritance
  • Hierarchical inheritance
  • Hybrid inheritance
14.

Are there any limitations of Inheritance?

Answer»

Yes, with more powers COMES more complications. Inheritance is a very powerful feature in OOPs, but it has some limitations too. Inheritance needs more time to process, as it needs to NAVIGATE through multiple classes for its implementation. Also, the classes involved in Inheritance - the base class and the child class, are very tightly coupled together. So if ONE needs to make some changes, they might need to do nested changes in both classes. Inheritance might be complex for implementation, as well. So if not correctly implemented, this might lead to unexpected errors or incorrect outputs.

15.

Explain Inheritance with an example?

Answer»

Inheritance is one of the major features of object-oriented programming, by which an entity inherits some characteristics and behaviors of some other entity and makes them their own. Inheritance helps to improve and facilitate code REUSE.

Let me explain to you with a common example. Let's take three different vehicles - a car, TRUCK, or bus. These three are entirely different from one another with their own specific characteristics and behavior. But. in all three, you will find some common elements, like steering WHEEL, accelerator, clutch, brakes, etc. Though these elements are USED in different vehicles, still they have their own features which are common among all vehicles. This is achieved with inheritance. The car, the truck, and the bus have all inherited the features like steering wheel, accelerator, clutch, brakes, etc, and used them as their own. Due to this, they did not have to create these components from scratch, thereby facilitating code reuse.

16.

Are class and structure the same? If not, what's the difference between a class and a structure?

Answer»

No, CLASS and structure are not the same. Though they appear to be SIMILAR, they have differences that make them apart. For example, the structure is SAVED in the stack memory, whereas the class is saved in the HEAP memory. Also, DATA Abstraction cannot be achieved with the help of structure, but with class, Abstraction is majorly used.

17.

What is a destructor?

Answer»

Contrary to constructors, which INITIALIZE OBJECTS and specify SPACE for them, Destructors are also special methods. But destructors free up the resources and MEMORY occupied by an object. Destructors are automatically called when an object is being destroyed. 

18.

What are the various types of constructors in C++?

Answer»

The most common classification of constructors includes:

Default constructor: The default constructor is the constructor which doesn’t take any ARGUMENT. It has no parameters.

class ABC{ int x; ABC() { x = 0; }}

Parameterized constructor: The constructors that take some arguments are known as parameterized constructors.

class ABC{ int x; ABC(int y) { x = y; }}

Copy constructor: A copy constructor is a member function that INITIALIZES an object USING another object of the same class.

class ABC{ int x; ABC(int y) { x = y; } // Copy constructor ABC(ABC abc) { x = abc.x; }}
19.

Is it always necessary to create objects from class?

Answer»

No. An OBJECT is necessary to be CREATED if the base CLASS has non-static methods. But if the class has static methods, then objects don’t NEED to be created. You can call the class method DIRECTLY in this case, using the class name.

20.

How much memory does a class occupy?

Answer»

Classes do not consume any memory. They are just a blueprint BASED on which OBJECTS are CREATED. Now when objects are created, they ACTUALLY initialize the class MEMBERS and methods and therefore consume memory.

21.

What is Abstraction?

Answer»

If you are a user, and you have a PROBLEM statement, you don't want to know how the components of the software work, or how it's made. You only want to know how the software SOLVES your problem. Abstraction is the method of hiding unnecessary details from the necessary ones. It is one of the main features of OOPs. 
For EXAMPLE, consider a car. You only need to know how to run a car, and not how the wires are connected inside it. This is OBTAINED USING Abstraction.

22.

What is meant by Inheritance?

Answer»

The term “inheritance” means “receiving some quality or behavior from a parent to an OFFSPRING.” In object-oriented programming, inheritance is the mechanism by which an object or CLASS (referred to as a child) is created using the definition of another object or class (referred to as a parent). Inheritance not only HELPS to keep the implementation simpler but also helps to facilitate CODE reuse.

23.

How does C++ support Polymorphism?

Answer»

C++ is an Object-oriented programming language and it supports Polymorphism as well:

  • COMPILE Time Polymorphism: C++ supports compile-time polymorphism with the help of features like TEMPLATES, function overloading, and default arguments.
  • Runtime Polymorphism: C++ supports Runtime polymorphism with the help of features like VIRTUAL FUNCTIONS. Virtual functions take the SHAPE of the functions based on the type of object in reference and are resolved at runtime.
24.

What is Compile time Polymorphism and how is it different from Runtime Polymorphism?

Answer»

COMPILE Time Polymorphism: Compile time polymorphism, also known as Static Polymorphism, refers to the TYPE of Polymorphism that happens at compile time. What it means is that the compiler DECIDES what shape or value has to be taken by the entity in the picture.

Example:

// In this program, we will see how multiple functions are created with the same name, // but the compiler decides which function to call easily at the compile time itself.class CompileTimePolymorphism{ // 1st method with name add public int add(int x, int y){ return x+y; } // 2nd method with name add public int add(int x, int y, int z){ return x+y+z; } // 3rd method with name add public int add(double x, int y){ return (int)x+y; } // 4th method with name add public int add(int x, double y){ return x+(int)y; }}class Test{ public static void main(String[] args){ CompileTimePolymorphism demo=new CompileTimePolymorphism(); // In the below statement, the Compiler looks at the ARGUMENT types and decides to call method 1 System.out.println(demo.add(2,3)); // Similarly, in the below statement, the compiler calls method 2 System.out.println(demo.add(2,3,4)); // Similarly, in the below statement, the compiler calls method 4 System.out.println(demo.add(2,3.4)); // Similarly, in the below statement, the compiler calls method 3 System.out.println(demo.add(2.5,3)); }}

In the above example, there are four versions of add methods. The FIRST method takes two parameters while the second one takes three. For the third and fourth methods, there is a change of order of parameters. The compiler looks at the method signature and decides which method to invoke for a particular method call at compile time.

Runtime Polymorphism: Runtime polymorphism, also known as Dynamic Polymorphism, refers to the type of Polymorphism that happens at the run time. What it means is it can't be decided by the compiler. Therefore what shape or value has to be taken depends upon the execution. Hence the name Runtime Polymorphism.

Example:

class AnyVehicle{ public void move(){ System.out.println(“Any vehicle should move!!”); }}class Bike extends AnyVehicle{ public void move(){ System.out.println(“Bike can move too!!”); }}class Test{ public static void main(String[] args){ AnyVehicle vehicle = new Bike(); // In the above statement, as you can see, the object vehicle is of type AnyVehicle // But the output of the below statement will be “Bike can move too!!”,  // because the actual implementation of object ‘vehicle’ is decided during runtime vehicle.move(); vehicle = new AnyVehicle(); // Now, the output of the below statement will be “Any vehicle should move!!”,  vehicle.move(); }}

As the method to call is determined at runtime, as shown in the above code, this is called runtime polymorphism.