top of page
  • Facebook
  • Twitter
  • Instagram

Top C-plus-plus Interview Questions

Mobiprep handbooks are free downloadable placement preparation resources that can help you ace any company placement process. We have curated a list of the 40 most important MCQ questions which are asked in most companies such as Infosys, TCS, Wipro, Accenture, etc. The placement handbooks also have detailed explanations for every question ensuring you revise all the questions with detailed answers and explanations.

Question

25

Explanation

Mention the restrictions that are placed on static member functions.

The restrictions that are placed on static member functions are:

i) They may only directly refer to other static members of the class.
ii) A static member function does not have a this pointer.
iii) There cannot be a static and a non-static version of the same function.
iv) A static member function may not be virtual.
v) They cannot be declared as const or volatile.

Question

26

Explanation

What are friend functions? What are the advantages of using friend functions? Write a
C++ program using a friend function.

Friend function: It is a function by which it is possible to grant a nonmember function access to the private members of a class by using it. A friend function has access to all private and protected members of the class for which it is a friend.
The advantages of using friend functions are:

a) Friend functions can be useful when we are overloading certain types of operators.
b) Friend functions make the creation of some types of I/O functions easier.
c) Friend functions may be desirable in cases as two or more classes may contain members that are interrelated relative to other parts of the program.

Program to find sum of two numbers using friend function:

#include<iostream>
using namespace std;
class A {
private:
int x, y;
public:
void set_xy ( int i, int j );
friend int add ( A ob);
add ( );
};
void A : : set_xy ( int i, int j)
{
x = i;
y =j;
}
int add ( A ob)
{
return ob.x + ob.y;
}
void main ( )
{
A ob1;
ob1.set_xy (10, 20);
cout << add ( ob1);
}

Question

27

Explanation

What are exceptions in C++ and how do you handle it ?

An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw.

> try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
> throw: A program throws an exception when a problem shows up. This is done using a throw keyword.
> catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

EXAMPLE PROGRAM:
main()
{ int r;
float a,c;
try{
cout<<”Enter radius in range (1-100) “;
cin>>r;
if(r<0)
throw 1001;
if(r>100)
throw 1002;
a=3.14*r*r;
c=2*3.14*r;
cout<<”Area is “<<a<<endl;
cout<<”Circumference is “<<c<<endl;
}
catch(int err)
{
if(err==1001)
cout<<”Radius cannot be –ve”;
if(err==1002)
cout<<”Radius must be in range 1-100”;
}
}

Question

28

Explanation

What is sope resolution operator in C++ and when do we use it?

In C++, scope resolution operator is ::. It is used for following purposes.

1. To access a global variable when there is a local variable with same name
2. To define a function outside a class.
3. To access a class’s static variables.
4. In case of multiple Inheritance

bottom of page