Top 50 Interview Questions & Answers on OOP Concepts
- Posted by: BlogWriter
- Category: Interview_Questions
#1 What is OOP?
Object-oriented programming is a programming paradigm based on the concept of “objects”, which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.
#2 Write basic concepts of OOP?
Following are the concepts of OOPS and are as follows:
#3 What is a class?
In object-oriented programming , a class is a template definition of the method s and variable s in a particular kind of object . Thus, an object is a specific instance of a class; it contains real values instead of variables. The class is one of the defining ideas of object-oriented programming.
#4 What is an object?
object refers to a particular instance of a class, where the object can be a combination of variables, functions, and data structures.
#5 What is Encapsulation?
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of bundling data and methods that work on that data within one unit, e.g., a class in Java. This concept is also often used to hide the internal representation, or state, of an object from the outside.
#6 What is Polymorphism?
In object-oriented programming, polymorphism refers to a programming language’s ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes.
#7 What is Inheritance?
In object-oriented programming, inheritance enables new objects to take on the properties of existing objects. A class that is used as the basis for inheritance is called a superclass or base class. A class that inherits from a superclass is called a subclass or derived class.
#8 What are manipulators?
Manipulators are used to change formatting parameters on streams and to insert or extract certain special characters.
#9 Define a constructor?
- A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type.
- A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values.
#10 Define Destructor?
A destructor is a special method called automatically during the destruction of an object. Actions executed in the destructor include the following: Recovering the heap space allocated during the lifetime of an object. Closing file or database connections. Releasing network resources.
#11 What is an Inline function?
An inline function is a technique used by the compilers and instructs to insert complete body of the function wherever that function is used in the program source code.
#12 What is a virtual function?
In object-oriented programming, in languages such as C++, and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method for which dynamic dispatch is facilitated. This concept is an important part of the (runtime) polymorphism portion of object-oriented programming (OOP).
#13 What is a friend function?
In object-oriented programming, a friend function, that is a “friend” of a given class, is a function that is given the same access as methods to private and protected data. A friend function is declared by the class that is granting access, so friend functions are part of the class interface, like methods.
#14 What is function overloading?
Function overloading (also method overloading) is a programming concept that allows programmers to define two or more functions with the same name and in the same scope. Each function has a unique signature (or header), which is derived from: function/procedure name. number of arguments. arguments’ type.
#15 What is operator overloading?
In programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by a programming language, a programmer, or both.
#16 What is an abstract class?
An abstract class is a class which cannot be instantiated. Creation of an object is not possible with an abstract class, but it can be inherited. An abstract class can contain only Abstract method. Java allows only abstract method in abstract class while for other languages allow non-abstract method as well.
#17 What is a ternary operator?
The ternary operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. If it helps you can think of the operator as shortened way of writing an if-else statement.
#18 What is the use of finalize method?
The java.lang.Object.finalize() is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.
#19 What are different types of arguments?
A parameter is a variable used during the declaration of the function or subroutine and arguments are passed to the an, and it should match with the parameter defined. There are two types of Arguments.
Call by Value – Value passed will get modified only inside the function, and it returns the same value whatever it is passed it into the function.
Call by Reference – Value passed will get modified in both inside and outside the functions and it returns the same or different value.
#20 What is the super keyword?
It is used inside a sub-class method definition to call a method defined in the super class. Private methods of the super-class cannot be called. Only public and protected methods can be called by the super keyword. It is also used by class constructors to invoke constructors of its parent class.
#21 What is method overriding?
Method overriding is a feature that allows a subclass to provide the implementation of a method that overrides in the main class. This will overrides the implementation in the superclass by providing the same method name, same parameter and same return type.
#22 What is an interface?
An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class). For example, say we have a car class and a scooter class and a truck class. Each of these three classes should have a start_engine() action.
#23 What is exception handling?
An exception is an event that occurs during the execution of a program. Exceptions can be of any type – Runtime exception, Error exceptions. Those exceptions are adequately handled through exception handling mechanism like try, catch and throw keywords.
#24 What are tokens?
A token is the smallest element of a C++ program that is meaningful to the compiler. The C++ parser recognizes these kinds of tokens: identifiers, keywords, literals, operators, punctuators, and other separators. … Tokens are usually separated by white space.
#25 Difference between overloading and overriding?
Overloading means creating methods with same name but different parameters. Overriding means re-defining body of a method of superclass in a subclass to change behavior of a method. Polymorphism is a wide concept which includes overriding and overloading and much more in it’s scope.
#26 Difference between class and an object?
Many programmers still get confused by the difference between class and object. In object-oriented terminology, a Class is a template for Objects and every Object must belong to a Class. The terms “Class” and “Object” are related to one another and each term holds its own distinct meaning.
#27 What is an abstraction?
Abstraction is a good feature of OOPS, and it shows only the necessary details to the client of an object. Means, it shows only required details for an object, not the inner constructors, of an object. Example – When you want to switch On television, it not necessary to show all the functions of TV. Whatever is required to switch on TV will be showed by using abstract class.
#28 What are access modifiers?
Access modifiers determine the scope of the method or variables that can be accessed from other various objects or classes. There are 5 types of access modifiers, and they are as follows:
#29 What are sealed modifiers?
similarly function can’t be used by the other class. Sealed types cannot be inherited & are concrete. Sealed modifiers can also be applied to instance methods, properties, events & indexes. It can’t be applied to static members.Sealed members are allowed in sealed and non-sealed classes.
#30 How can we call the base method without creating an instance?
Yes, it is possible to call the base method without creating an instance. And that method should be “Static method”.
Doing inheritance from that class.-Use Base Keyword from a derived class.
#31 What is the difference between new and override?
The new modifier instructs the compiler to use the new implementation instead of the base class function. Whereas, Override modifier helps to override the base class function.
#32 What are the various types of constructors?
There are three various types of constructors, and they are as follows:
– Default Constructor – With no parameters.
– Parametric Constructor – With Parameters. Create a new instance of a class and also passing arguments simultaneously.
– Copy Constructor – Which creates a new object as a copy of an existing object.
#33 What is early and late binding?
Early Binding and Late binding. The compiler performs a process called binding when an object is assigned to an object variable. The early binding (static binding) refers to compile time binding and late binding (dynamic binding) refers to runtime binding.Early binding refers to the assignment of values to variables during design time whereas late binding refers to the assignment of values to variables during run time.
#34 What is ‘this’ pointer?
The this pointer is a pointer accessible only within the nonstatic member functions of a class, struct , or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.
#35 What is the difference between structure and a class?
Objects are usually deallocated when instance is no longer referenced by other code. One main difference between them is that in a structure all members have by default public access specifier, but in a class all members have by default private access specifier.
#36 What is the default access modifier in a class?
The default access modifier of a class is Private by default.
The internal access specifier hides its member variables and methods from other classes and objects, that is resides in other namespace. The variable or classes that are declared with internal can be access by any member within application. It is the default access specifiers for a class in C# programming.
#37 What is a pure virtual function?
A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed “abstract” and they cannot be instantiated directly.
#38 What are all the operators that cannot be overloaded?
Following are the operators that cannot be overloaded -.
Scope Resolution (:: )
Member Selection (.)
Member selection through a pointer to function (.*)
#39 What is dynamic or run time polymorphism?
Dynamic or Run time polymorphism is also known as method overriding in which call to an overridden function is resolved during run time, not at the compile time. It means having two or more methods with the same name, same signature but with different implementation.
#40 Do we require a parameter for constructors?
No, we do not require a parameter for constructors.
#41 What is a copy constructor?
The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to – Initialize one object from another of the same type. Copy an object to pass it as an argument to a function.
#42 What does the keyword virtual represented in the method definition?
It means, we can override the method.
#43 Whether static method can use nonstatic members?
#44 What is a base class, sub class, and super class?
The base class is the most generalized class, and it is said to be a root class.
A Sub class is a class that inherits from one or more base classes.
The superclass is the parent class from which another class inherits.
#45 What is static and dynamic binding?
Binding is nothing but the association of a name with the class. Static binding is a binding in which name can be associated with the class during compilation time, and it is also called as early Binding.
Dynamic binding is a binding in which name can be associated with the class during execution time, and it is also called as Late Binding.
#46 How many instances can be created for an abstract class?
Zero instances will be created for an abstract class.
#47 Which keyword can be used for overloading?
Operator keyword is used for overloading.
#48 What is the default access specifier in a class definition?
Private access specifier is used in a class definition.
#49 Which OOPS concept is used as reuse mechanism?
Inheritance is the OOPS concept that can be used as reuse mechanism.
#50 Which OOPS concept exposes only necessary information to the calling functions?