CPP Interview Question

  • By
  • October 11, 2020
  • C & C++
CPP Interview Question

In this blog, I  am discussing  Some of the interview  Questions in CPP.

Q1) Define  Storage Classes in CPP.

 Storage classes define the Scope of a variable,  and visibility of variable,  and Lifetime of a variable. In CPP  there  are  5 types  of the scope  of variable like

Auto, Extern, Static, register, mutable

Auto:  No need to define data type while declaring variables. The automatic deduction of data type is happening.  For example

auto  x=10;  which is converted as an integer.

auto y=20.00;   which is converted as a float.

auto name=”cpp” ;  which is converted as a string

auto  name =’A’;    which is converted as a char

Extern: In the extern variable becomes global you can define the variable above main using the extern keyword. Use this variable anywhere in the program.

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

Q2 ) Explain the friend function.

In Cpp, the data members of a class are private in nature. An only member function of that class can access private data members. In order to access private and protected data members of a class,  outside a class  Friend function is used. When we create a function with the friend keyword compiler will understand that is a friend of a  class. For Accessing of private and protected data only the friend function is declared inside a class using the friend keyword. 

Syntax  of friend function

class   class_name

{

friend  data_type function_name(args);

};

Features  of   a  friend  Function
  1. When we declare any function as a friend function that is not within a class scope.
  2. A friend function can not be called using objects.
  3. A friend function is called like a normal function.
  4. Friend functions can be either private or public.
  5. Friend function takes an object as a parameter to access the private/ protected data member.
 Q3) Explain  Call by value  and Call by reference  in CPP

In Call by value : Copy of value is passed to the function. The changes made inside a function does not reflect throughout the program.

In Call by value. Actual and formal parameters are in not the same.

In Call by reference: In Call by reference Address  of  variable  is passed  as  a parameter to the function.

The  changes  made  inside  a function  will  reflect  throughout the program.

In call by  reference Actual  and formal  arguments  are created in same memory locations.

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

Q4) What is  reference in CPP?

Ans) In CPP  reference variable is an alias name to the existing variable. The same variable you can express as  reference variable. Reference variable is mainly used as pass by reference. where the variable is works like an original copy of a  variable.

Example of reference variable

#include<iostream>

Using namespace std;

int main()

{

 

int  x=100; // variable  x is declared   and initialized with value 100; 

int &a = x;  // reference variable of x is a 

cout<<a<<endl;

cout<<x<<endl;  

Gives output  as  100 for both a,x.

Q5) What is a void pointer?

Void pointer is a  pointer that holds the address of any data type.  We can call a void pointer is a  general-purpose pointer.  Void pointer is not part of any data type.

Syntax of    void pointer

Void *ptr;

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

Q6)  what is  difference between reference and pointer in CPP?

Reference is an alias name to an existing variable. Reference is declared using & symbol. Reference is mainly used as  call by reference in CPP.

We cant  reassign the reference variable 

We cant  Assign  NULL   to reference.

A pointer is a  variable will stores the address of another variable. We can declare pointer with * Symbol. We can reassign the pointer variable.

The pointer is generally used to store the address of another variable.

We can assign NULL  to the pointer.

Pointers are having wide scope than the reference variable.

Q7)  What is a constructor in CPP?

Constructor is a special method which is called automatically when we create a object of a  class. Constructors are having the same name as a class name.

Constructors are mainly used to initialize the data members. If you are not initializing default values then the compiler will initialized the some random values to data members.

Constructors do not return any value. 

In CPP  there  are  3  types  of  constructors

Default constructor

Parameterized constructor

Copy constructor

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

Q8) Explain destructor in CPP.

As a  constructor will construct the object. Destructor will destroy the object. In a  class, you can have a number of overloaded constructors but the destructor will be single.

Destructor will be invoked automatically.

Q9)  Explain the new and delete keyword in CPP.

In  CPP    new,  delete keyword are used for dynamic memory allocation. The new is  dynamic memory allocation oerator. The memory is allocated at run time. A new keyword will allocate the memory in heap. 

Syntax of new keyword

Datatype variable = new  datatype(params_list);

For example

#include<iostream>

Int main()

{

int *ptr;

ptr = new int;

*ptr = 100;

cout<<*ptr<<endl;

delete ptr;

cout<<”after deleting”<<endl;

cout<<*ptr<<endl;

 

}

 

Delete  Keyword:  Delete keyword is used to de –  Allocate the  Dynamic memory. It is mainly used in the pointer. 

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

Syntax  is:

 

delete  pointer_name;

 

Q10) Explain this pointer in CPP.

In CPP  this keyword refers to current data members (instance)  of  a class. IT  is  used to pass the current object as  a parameter to the method.IT is also used to  refer  current class data members.

Q11) What is overloading in CPP?

Overloading one of  the way to implement the polymorphism. Overloading can be done on both methods(functions) and constructor also .

Overloading can be of  2 types.

Method overloading, constructor overloading

In method overloading:  Two  or  More  Methods having same name but with different types of parameters. In function or method overloading  the function is redefined by using different types of parameters.

Method overloading increases the  readability  of  method. Because of no  need to use a  different name for the same action.

Constructor overloading is also  same as  method overloading the same constructor name with different types of parameters.

Q12)  What is  Overriding?

In function  overriding derived class defines the method as it is in the base class. It is used to achieve the run time polymorphism.  In method overriding  derived class will provide the specific implementation to the method as defined in the base class.  

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

Q13)   Explain Copy Constructor.

Ans)  The copy constructor is an overloaded constructor. That will take an object as a parameter. The compiler defines the copy constructor for data members.  But when we pass  pointer as a parameters in that case  we can define user defined copy constructor.

Syntax  of  copy  constructor

Classname(const class_name &obj){}

Features  of  Copy Constructor

  1. Copy constructor is called when we pass  object as a parameter to the constructor of a  same class.
  2. When a function returns  the object of the same class type in that case also copy constructor is called.

In copy, constructor   copying  is  done in two  ways

  1. Deep  Copy: For pointers shallow copy is not done . In that case  we can go for a deep copy. Separate data is created for a deep copy. Use deep copy only when there is pointers.
  2. Shallow  Copy: Default constructor produces a shallow copy. For Data members.Shallow copy is the process of copying the object by sharing. For data members defined using built-in type not using pointers, shallow copy is done.
Q14) Explain  Enum in CPP?

In CPP  Enum is user-defined datatype that contains a fixed set of values. Or constants. 

Those constants are not changed. Suppose when we create  Grades Like A, B, C, D,E  are fixed one. 

Enum provides the type of safety of a  variable. Because  user has to select from a group only.

We can easily use  Enum in  switch…case also. 

Q15)  Explain static keyword in CPP.

In CPP  static keyword is a  modifier that belongs to  class, not  instance  (objects) . A variable which is sharable to all instance we can declare that variable as static. Static can be variable, static can be a method, even the operator also. Static provides an efficient memory to class.  

Q16) What  is  inheritance in CPP

Inheritance is  a  process by which the child class can get the properties of   parent class. 

Creating a   new class from an existing class  . is  also known as inheritance.

In inheritance provides code  reusability.

Syntax  :

class  base_class

{

};

class    derived_class   : <Access_specifier>  base_class

{

 // body of derived class.

};

In the above syntax, Access_specifier   defines the visibility mode of a   class. 

Types   of   inheritance

Single  inheritance

Multiple inheritances

Multilevel Inheritance

Hybrid Inheritance

Hierarchical  Inheritance.

For Free, Demo classes Call:8237077325
Registration Link: Click Here!

Q17) How to  Inherit private data members in CPP?

By default  access ability of a  data, member is private in nature. So private data member is available only inside a class. 

Public modifier defines anywhere access ability. But  According to the object-oriented principle  data hiding is important. 

So  CPP  provides another modifier is protected.  A  variable that is declared as a  protected is available to its child class also.  So the data member should be protected. 

 

Q18) What is   Ambiguity  Resolution in inheritance.

Ambiguity can occur in multiple inheritances when the same function name is used in more than one parent class.

To solve  Ambiguity we can use the scope resolution operator(::)

Q19) What is  Aggregation.

Aggregation is a process in which one class can have another class as a reference. IT is another way of code reuse ability. It provides a  HAS-A relationship.

In  next blog, I  will discuss the  Template, STL  components.

Author:
Bhagyashri Sangam | SevenMentor Pvt. Ltd.

Call the Trainer and Book your free demo class for Android Training now!!!

call icon

© Copyright 2020 | Sevenmentor Pvt Ltd.

 

Submit Comment

Your email address will not be published. Required fields are marked *

*
*