Select Category 
 

C++ Interview Questions Answers

C++ Interview Question - 1 : -

How many ways are there to initialize an int with a constant?

C++ Interview Answer - 1 : -

Two.
There are two formats for initializers in C++ as shown in the example that follows. The first format uses the traditional C notation. The second format uses constructor notation.
int foo = 123;
int bar (123);
 

C++ Interview Question - 2 : -

What problem does the namespace feature solve?

C++ Interview Answer - 2 : -

Multiple providers of libraries might use common global identifiers causing a name collision when an application tries to link with two or more such libraries. The namespace feature surrounds a library’s external declarations with a unique namespace that eliminates the potential for those collisions.
This solution assumes that two library vendors don’t use the same namespace identifier, of course.
 

C++ Interview Question - 3 : -

How can you tell what shell you are running on UNIX system?

C++ Interview Answer - 3 : -

You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.
 

C++ Interview Question - 4 : -

What is the need for a Virtual Destructor ?

C++ Interview Answer - 4 : -

Destructors are declared as virtual because if do not declare it as virtual the base class destructor will be called before the derived class destructor and that will lead to memory leak because derived class’s objects will not get freed.Destructors are declared virtual so as to bind objects to the methods at runtime so that appropriate destructor is called.
 

C++ Interview Question - 5 : -

What is an accessor?

C++ Interview Answer - 5 : -

An accessor is a class operation that does not modify the state of an object. The accessor functions need to be declared as const operations
 

C++ Interview Question - 6 : -

What’s the meaning of ARP in TCP/IP?

C++ Interview Answer - 6 : -

The "ARP" stands for Address Resolution Protocol. The ARP standard defines two basic message types: a request and a response. a request message contains an IP address and requests the corresponding hardware address; a replay contains both the IP address, sent in the request, and the hardware address.
 

C++ Interview Question - 7 : -

Does c++ support multilevel and multiple inheritance?

C++ Interview Answer - 7 : -

Yes.
 

C++ Interview Question - 8 : -

What problems might the following macro bring to the application?

C++ Interview Answer - 8 : -

#define sq(x) x*x
 

C++ Interview Question - 9 : -

What are the differences between a C++ struct and C++ class?

C++ Interview Answer - 9 : -

The default member and base-class access specifier are different.
 

C++ Interview Question - 10 : -

What is the use of ‘using’ declaration. ?

C++ Interview Answer - 10 : -

A using declaration makes it possible to use a name from a namespace without the scope operator.
 

C++ Interview Question - 11 : -

What is the difference between declaration and definition?

C++ Interview Answer - 11 : -

The declaration tells the compiler that at some later point we plan to present the definition of this declaration.
E.g.: void stars () //function declaration
The definition contains the actual implementation.
E.g.: void stars () // declarator
{
for(int j=10; j > =0; j--) //function body
cout << *;
cout << endl; }
 

C++ Interview Question - 12 : -

STL Containers - What are the types of STL containers?

C++ Interview Answer - 12 : -

There are 3 types of STL containers:

1. Adaptive containers like queue, stack
2. Associative containers like set, map
3. Sequence containers like vector, deque

 

C++ Interview Question - 13 : -

What can I safely assume about the initial values of variables which are not explicitly initialized?

C++ Interview Answer - 13 : -

It depends on complier which may assign any garbage value to a variable if it is not initialized.
 

C++ Interview Question - 14 : -

What is an adaptor class or Wrapper class?

C++ Interview Answer - 14 : -

A class that has no functionality of its own. Its member functions hide the use of a third party software component or an object with the non-compatible interface or a non-object-oriented implementation.
 

C++ Interview Question - 15 : -

What is virtual constructors/destructors?

C++ Interview Answer - 15 : -

1.
Virtual destructors:
If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object.
There is a simple solution to this problem declare a virtual base-class destructor.
This makes all derived-class destructors virtual even though they don’t have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called. Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error.

2.
Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object.
There is a simple solution to this problem – declare a virtual base-class destructor. This makes all derived-class destructors virtual even though they don’t have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called.

Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. Does c++ support multilevel and multiple inheritance?
Yes.