OOPS through C via va questions
OOPS through C via va questions
It is conceptual It is real
An object is an instance of a class. It has its own state, behavior, and identity.
An object is a real-world entity that has attributes, behavior, and properties.
It is referred to as an instance of the class. It contains member functions,
variables that we have defined in the class. It occupies space in the memory.
Different objects have different states or attributes, and behaviors.
8) What are the differences between the constructor and the method in Java?
Constructor Method
It deals with data members and member functions It deals with data members only
Modification and extension of code are not easy We can easily modify and extend code
18) What do you understand by OOP?
OOP stands for object-oriented programming.
It is a programming paradigm that revolves around the object rather than
function and procedure.
In other words, it is an approach for developing applications that emphasize on
objects. An object is a real word entity that contains data and code.
19) What is Coupling in OOP and why it is helpful?
In programming, separation of concerns is known as coupling.
It means that an object cannot directly change or modify the state or behaviour
of other objects.
Objects that are independent of one another and do not directly modify the
state of other objects is called loosely coupled. Loose coupling makes the code
more flexible, changeable, and easier to work with.
20) What are the advantages of OOP?
It follows a bottom-up approach.
It models the real word well.
It allows us the reusability of code.
Decompose a complex problem into smaller chunks.
Programmer are able to reach their goals faster.
Minimizes the complexity.
21) What are the limitations of inheritance?
The main disadvantage of using inheritance is two classes get tightly coupled.
That means one cannot be used independently of the other.
If a method or aggregate is deleted in the Super Class, we have to refactor
using that method in SubClass.
Inherited functions work slower compared to normal functions.
Need careful implementation otherwise leads to improper solutions
22) What are the differences between Inheritance and Polymorphism?
Inheritance Polymorphism
Inheritance is one in which a derived class inherits Polymorphism is one that you can define
the already existing class’s features in different forms
23) What is constructor chaining?
In OOPs, constructor chaining is a sequence of invoking constructors upon
initializing an object. It is used when we want to invoke a number of
constructors, one after another by using only an instance.
24) What are the differences between copy constructor and assignment operator?
Copy Constructor Assignment Operator
The copy constructor is used when a new object is It is used when we want to assign an existing
created with some existing object. object to a new object
If no copy constructor is defined in the class, the If the assignment operator is not overloaded
compiler provides one then the bitwise copy will be made
25) What is the difference between Composition and Inheritance?
Inheritance means an object inheriting reusable properties of the base class.
Compositions mean that an object holds other objects.
In Inheritance, there is only one object in memory (derived object) whereas, in
Composition, the parent object holds references of all composed objects.
26) What is composition?
Composition is one of the vital concepts in OOP.
It describes a class that references one or more objects of other classes in
instance variables.
It allows us to model a has-a association between objects.
27) Why OOP is so popular?
OOPs, programming paradigm is considered as a better style of programming.
Not only that, the main pillar of OOPs – Data Abstraction, Encapsulation,
Inheritance, and Polymorphism, makes it easy for programmers to solve
complex scenarios.
28) What are the characteristics of an abstract class?
Instantiation of an abstract class is not allowed. It must be inherited.
An abstract class can have both abstract and non-abstract methods.
You must declare at least one abstract method in the abstract class.
It is always public.
An abstract class must have at least one abstract method.
29) What are the differences between error and exception?
Exception Error
Exception can be recovered by using the try-catch block An error cannot be recovered
30) What are the four main features of OOPs?
Inheritance
Encapsulation
Polymorphism
Data Abstraction
31) What is OOPS?
OOPS is abbreviated as Object Oriented Programming system in which programs are
considered as a collection of objects. Each object is nothing but an instance of a class.
32) What is Encapsulation?
Encapsulation is an attribute of an object, and it contains all data which is hidden. That
hidden data can be restricted to the members of that class.
Levels are Public, Protected, Private, Internal, and Protected Internal.
33) What is Polymorphism?
Polymorphism is nothing but assigning behavior or value in a subclass to something that was
already declared in the main class. Simply, polymorphism takes more than one form.
34) What is Inheritance?
Inheritance is a concept where one class shares the structure and behavior defined in another
class. If Inheritance applied to one class is called Single Inheritance, and if it depends on
multiple classes, then it is called multiple Inheritance.
35) Explain the term constructor
A constructor is a method used to initialize the state of an object, and it gets invoked at the
time of object creation. Rules for constructor are:
Constructor Name should be the same as a class name.
A constructor must have no return type.
36) Define Destructor?
A destructor is a method which is automatically called when the object is made of scope or
destroyed. Destructor name is also same as class name but with the tilde symbol before the
name.
37) 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.
38) What is a virtual function?
A virtual function is a member function of a class, and its functionality can be
overridden in its derived class. This function can be implemented by using a keyword
called virtual, and it can be given during function declaration.
A virtual function can be declared using a token(virtual) in C++. It can be achieved in
C/Python Language by using function pointers or pointers to function.
39) What is a friend function?
A friend function is a friend of a class that is allowed to access to Public, private, or
protected data in that same class. If the function is defined outside the class cannot
access such information.
A friend can be declared anywhere in the class declaration, and it cannot be affected
by access control keywords like private, public, or protected.
40) What is function overloading?
Function overloading is a regular function, but it is assigned with multiple parameters. It
allows the creation of several methods with the same name which differ from each other by
the type of input and output of the function.
Example
void add(int& a, int& b);
void add(double& a, double& b);
void add(struct bob& a, struct bob& b);
Types of Polymorphism
Ty
Example –
Virtual void function1() // Virtual, Not pure
Virtual void function2() = 0 //Pure virtual
Zero instances will be created for an abstract class. In other words, you cannot create an
instance of an Abstract Class.
66) Which keyword can be used for overloading?
Operator keyword is used for overloading.
67)Which OOPS concept is used as a reuse mechanism?
Inheritance is the OOPS concept that can be used as a reuse mechanism.
68) Which OOPS concept exposes only the necessary information to the calling
functions?
Encapsulation
69) What are some other programming paradigms other than OOPs?
Programming paradigms refers to the method of classification of programming languages
based on their features. There are mainly two types of Programming Paradigms:
Imperative Programming Paradigm
Declarative Programming Paradigm
Now, these paradigms can be further classified based:
Once the reference variable is assigned, The pointer variable is an independent variable
then it cannot be reassigned with different means that it can be reassigned to point to
address values. different objects.
A null value cannot be assigned to the A null value can be assigned to the reference
reference variable. variable.
The scope of a variable is defined as the extent of the program code within which the variable
remains active i.e. it can be declared, defined or worked with.
There are two types of scope in C++:
Local Scope: A variable is said to have a local scope or is local when it is declared inside a
code block. The variable remains active only inside the block and is not accessible outside the
code block.
Global Scope: A variable has a global scope when it is accessible throughout the program. A
global variable is declared on top of the program before all the function definitions.
#include <iostream.h>
int globalResult=0; //global variable
int main()
{
int localVar = 10; //local variable.
86) When there are a Global variable and Local variable with the same name, how will
you access the global variable?
When there are two variables with the same name but different scope, i.e. one is a local
variable and the other is a global variable, the compiler will give preference to a local
variable.
In order to access the global variable, we make use of a “scope resolution operator (::)”.
Using this operator, we can access the value of the global variable.
#include<iostream.h>
int x= 10;
int main()
{
int x= 2;
cout<<"Global Variable x = "<<::x;
cout<<"\nlocal Variable x= "<<x;
}
Output:
Global Variable x = 10
local Variable x= 2
int main()
{
map<int, int> test;
// inserting elements
test.insert(pair<int, int>(1, 2));
test.insert(pair<int, int>(2, 3));
111) What is the difference between an External Iterator and an Internal Iterator?
Describe an advantage of the External Iterator.
An internal iterator is implemented with member functions of the class that has items to step
through.
An external iterator is implemented as a separate class that can be bound to the object that has
items to step through. The basic advantage of an External iterator is that it’s easy to
implement as it is implemented as a separate class.
Secondly, as it’s a different class, many iterator objects can be active simultaneously.
112) What is vector in C++?
A sequence of containers to store elements, a vector is a template class of C++. Vectors are
used when managing ever-changing data elements. The syntax of creating vector.
vector (type) variable (number of elements)
113) Explain what is upcasting in C++?
Upcasting is the act of converting a sub class references or pointer into its super class
reference or pointer is called upcasting.
114) Explain what is pre-processor in C++?
Pre-processors are the directives, which give instruction to the compiler to pre-process the
information before actual compilation starts.
115) What is a virtual destructor?
A virtual destructor in C++ is used in the base class so that the derived class object
can also be destroyed. A virtual destructor is declared by using the ~ tilde operator
and then virtual keyword before the constructor.
Constructor cannot be virtual, but destructor can be virtual.
116) Can we call a virtual function from a constructor?
Yes, we can call a virtual function from a constructor. But the behavior is a little different in
this case. When a virtual function is called, the virtual call is resolved at runtime. It is always
the member function of the current class that gets called. That is the virtual machine doesn’t
work within the constructor.
For Example:
class base{
private:
int value;
public:
base(int x){
value=x;
}
virtual void fun(){
}
}
class derived{
private:
int a;
public:
derived(int x, int y):base(x){
base *b;
b=this;
b->fun(); //calls derived::fun()
}
void fun(){
cout<<"fun inside derived class"<<endl;
}
}
117) What are Default Parameters? How are they evaluated in the C++ function?
Default Parameter is a value that is assigned to each parameter while declaring a function.
This value is used if that parameter is left blank while calling to the function. To specify a
default value for a particular parameter, we simply assign a value to the parameter in the
function declaration.
If the value is not passed for this parameter during the function call, then the compiler uses
the default value provided. If a value is specified, then this default value is stepped on and the
passed value is used.
int multiply(int a, int b=2)
{
int r;
r = a * b;
return r;
}
int main()
{
cout<<multiply(6);
cout<<"\n";
cout<<multiply(2,3);
}