OOPs Interview Questions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What are the limitations of inheritance?

- Increases the time and effort required to execute a program as it requires jumping back and forth between different classes - The parent class and the child class get tightly coupled - Any modifications to the program would require changes both in the parent as well as the child class - Needs careful implementation else would lead to incorrect results

Why use OOP?

- OOPs allows clarity in programming thereby allowing simplicity in solving complex problems - Code can be reused through inheritance thereby reducing redundancy - Data and code are bound together by encapsulation -OOPs allows data hiding, therefore, private data is kept confidential Problems can be divided into different parts making it simple to solve - The concept of polymorphism gives flexibility to the program by allowing the entities to have multiple forms

What are the limitations of OOPs?

- Usually not suitable for small problems Requires intensive testing - Takes more time to solve the problem - Requires proper planning - The programmer should think of solving a problem in terms of objects

What is a subclass?

A class that inherits from another class is called the subclass. For example, the class Car is a subclass or a derived of Vehicle class.

What is an interface?

A class that only declares method. The methods are not implemented in the interface. Another class must be created to supply the implementation.

What is a copy constructor?

A constructor that creates objects by copying variables from another object of the same class. The main aim is to create a new object from an existing one.

What is a finally block?

A finally block consists of code that is used to execute important code such as closing a connection, etc. This block executes when the try block exits. It also makes sure that finally block executes even in case some unexpected exception is encountered.

What is a destructor?

A method that is automatically invoked when an object is destroyed. The destructor also recovers the heap space that was allocated to the destroyed object, closes the files and database connections of the object, etc.

What is data abstraction?

Allows displaying only the important information and hiding the implementation details. For example, while riding a bike, you know that if you raise the accelerator, the speed will increase, but you don't know how it actually happens. This is data abstraction as the implementation details are hidden from the rider.

What is operator overloading?

Allows us to change how existing operators behave with types that both already exist.

What is an object?

An object is a real-world entity which is the basic unit of OOPs for example chair, cat, dog, etc. Different objects have different states or attributes, and behaviors.

What are pure virtual functions?

Are functions that are only declared in the base class. This means that they do not contain any definition in the base class and need to be redefined in the subclass.

What are virtual functions?

Are functions that are present in the parent class and are overridden by the subclass. These functions are used to achieve runtime polymorphism.

What are 'access specifiers'/ access modifiiers?

Are keywords that determine the accessibility of methods, classes, etc in OOPs. These access specifiers allow the implementation of encapsulation. The most common access specifiers are public, private, and protected

How to achieve data abstraction?

Can be achieved through: Abstract class Abstract method

What is method overloading?

Cccurs when two or more methods in the same class have the exact same name but 1.) The number of parameters are different for the methods. 2.) The parameter types are different (float vs int). 3.) Just changing the return type is not overloading

Differentiate between a class and a method.

Class - A class is basically a template that binds the code and data together into a single unit. Classes consist of methods, variables, etc Method - Callable set of instructions also called a procedure or function that are to be performed on the given data

What is the difference between a class and a structure?

Class is a user-defined blueprint from which objevts are created. It consists of methods or set of instructions that are to be performed on the objects. Structure is a user-defined collection of variables of different data types.

Differentiate between data abstraction and encapsulation

Data abstraction - Solves the problem at the design level - Allows showing important aspects while hiding implementation details Encapsulation - Solves the problem at the implementation level - binds code and data together into a single unit and hides it from the world and other class

What is encapsulation?

Encapsulation is the combining or binding of data and code into a single object. For example, a class. Encapsulation also allows data-hiding as the data specified in one class is hidden from other classes.

What is the difference between an error and an exception?

Error - Errors are problems that should not be encountered by applications Exception - Conditions that an application might try to catch

What is Garbage Collection(GC)?

GC is an implementation of automatic memory management. The GC frees up space occupied by objects that are no longer in existence.

What is hierarchical inheritance?

Hierarchical inheritance refers to inheritance where one base class has more than one subclasses. For example, the vehicle class can have 'car', 'bike', etc as its subclasses.

What is hybrid inheritance?

Hybrid inheritance is a combination of multiple and multi-level inheritance.

What is a final variable?

If you make any variable as final, you cannot change the value of final variable(It will be constant).

What are the main features of OOPs?

Inheritance Encapsulation Polymorphism Data Abstraction

What is inheritance?

Inheritance is a mechanism in which one object acquires (inherits) all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.

What is a class?

Is a blueprint that defines an objects variables, methods, and behaviors

What is an abstract class?

Is a class designed with implementation gaps for subclasses to fill in and is deliberately incomplete Ia class that consists of abstract methods. These methods are basically declared but not defined. If these methods are to be used in some subclass, they need to be exclusively defined in the subclass

What is exception handling?

Is a mechanism to handle runtime errors. An exception handler allows errors to be thrown and caught and implements a centralized mechanism to resolve them.

What is a constructor?

Is a method that has the same name as the class and is used to initialize an object. It is invoked at the time of object creation.

What is an exception?

Is a problem that arises during the execution of a program. Exceptions are caught by handlers positioned along the thread's method invocation stack.

What is the use of 'finalize'?

Is an object method used to free up unmanaged resources and cleanup before Garbage Collection(GC). It performs memory management tasks.

What is a try/ catch block?

Is used to handle exceptions. The try block defines a set of statements that may lead to an error. The catch block basically catches the exception.

What is method overriding?

Method overriding is a feature that allows sub class to provide implementation of a method that is already defined in the main class. (redefine methods) This will overrides the implementation in the superclass by providing the same method name, same parameter and same return type.

What is the difference between multiple and multilevel inheritance?

Multiple Inheritance - when a class inherits more than one base class ex: child class inheriting from two base classes Mother and Father Multilevel Inheritance - when a class inherits from another class which itself is a subclass of some other base class ex: a class describing a sports car will inherit from a base class Car which inturn inherits from Vechicle class

Can you create an instance of an abstract class?

No. Instances of an abstract class cannot be created because it does not have a complete implementation. However, instances of subclass inheriting the abstract class can be created.

What is the difference between a class and an object?

Object - real - world entity which is an instance of a class - object acts like a variable of the class - is a physical entity - takes memory space when they are created - can be declared as and when required Class - is a template or blueprint with which object can be created - binds methods and data together into a singlet unit - is a logical entity - does not take memory space when created - declared just once

Differentiate between overloading and overriding.

Overloading - Two or more methods having the same name but different parameters or signature - Resolved during compile-time. Overriding - Child class redefining methods present in the base class with the same parameters/ signature - Resolved during runtime

What is the difference between public, private and protected access modifiers?

Public: - accessibile from own class - accessible from derived class - accessible from the world Protected: - accessibile from own class - accessible from derived class - NOT accessible from the world Private: - accessibile from own class - NOT accessible from derived class - NOT accessible from the world

What is polymorphism?

Refers to the ability to exist in multiple forms. Multiple definitions can be given to a single interface. For example, if you have a class named Vehicle, it can have a method named speed but you cannot define it because different vehicles have different speed. This method will be defined in the subclasses with different definitions for different vehicles.

What is dynamic polymorphism?

Runtime polymorphism or dynamic polymorphism (dynamic binding) is a type of polymorphism which is resolved during runtime. An example of runtime polymorphism is METHOD OVERRIDING.

What are the different types of inheritance?

Single inheritance Multiple inheritance Multilevel inheritance Hierarchical inheritance Hybrid inheritance

What is static polymorphism?

Static polymorphism (static binding) is a kind of polymorphism that occurs at compile time. An example of compile-time polymorphism is METHOD OVERLOADING.

What is a superclass?

The class from which a subclass is extended. A superclass or base class is a class that acts as a parent to some other class or classes. For example, the Vehicle class is a superclass of class Car.

Types of constructors

Types of constructors differ from language to language. However, all the possible constructors are: Default constructor Parameterized constructor Copy constructor Static constructor Private constructor

What is Object-Oriented Programming?

a programming language model organized around objects rather than "actions" (functions and procedures) and data rather than logic Individual objects are grouped into classes. OOPs implements real-world entities like inheritance, polymorphism, hiding, etc into programming. It also allows binding data and code together. - provides data hiding - can solve problems of any complexity - code can be reused thereby reducing redundancy

Can you call the base class method without creating an instance?

yes, you can call the base class method without instantiating it IF: - It is a static method - The base class is inherited by some other subclass


Set pelajaran terkait

Principles of Persuasion: Robert Cialdini

View Set

Chapter 6, Chapter 5: bioenergetics, Quiz 4, Beckers World of the Cell: chapter 3, Cell Biology Beckers World of the Cell Chapter 5

View Set

CSC440 Chapter 2: Software Processes (Software Engineering, Sommerville, 10th Edition)

View Set

Chapter Exam: Louisiana Laws and Rules

View Set

Increased Intracranial Pressure and Traumatic Brain Injury

View Set