Set - 2

Question 1 :

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

Answer :


Question 2 :

What are the advantages of inheritance?

Answer :

• It permits code reusability.
• Reusability saves time in program development.
• It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional.

Question 3 :

What is the difference between declaration and definition?

Answer :

The declaration tells the compiler that at some later point we plan to present the definition of this declaration.

void stars () //function declaration

The definition contains the actual implementation.

void stars () // declarator


Question 4 :

Does c++ support multilevel and multiple inheritance?

Answer :


Question 5 :

What is a template?

Answer :

Templates allow to create generic functions that admit any data type as parameters and return value without having to overload the function with all the possible data types. Until certain point they fulfill the functionality of a macro.

Its prototype is any of the two following ones:
template <class indetifier> function_declaration; template <typename indetifier> function_declaration;
The only difference between both prototypes is the use of keyword class or typename, its use is indistinct since both expressions have exactly the same meaning and behave exactly the same way.

Question 6 :

You have two pairs: new() and delete() and another pair : alloc() and free(). 
Explain differences between eg. new() and malloc() ?

Answer :

1.) "new and delete" are preprocessors while "malloc() and free()" are functions. [we dont use brackets will calling new or delete]. 
2.) no need of allocate the memory while using "new" but in "malloc()" we have to use "sizeof()". 
3.) "new" will initlize the new memory to 0 but "malloc()" gives random value in the new alloted memory location [better to use calloc()] 

new() allocates continous space for the object instace
malloc() allocates distributed space.
new() is castless, meaning that allocates memory for this specific type,
malloc(), calloc() allocate space for void * that is cated to the specific class type pointer. 

Question 7 :

What is the difference between an ARRAY and a LIST?

Answer :

Array is collection of homogeneous elements.
List is collection of heterogeneous elements. 

For Array memory allocated is static and continuous.
For List memory allocated is dynamic and Random. 

Array: User need not have to keep in track of next memory allocation.
List: User has to keep in Track of next location where memory is allocated. 

Array uses direct access of stored members, list uses sequencial access for members.

//With Array you have direct access to memory position 5
Object x = a[5]; // x takes directly a reference to 5th element of array
//With the list you have to cross all previous nodes in order to get the 5th node:
list mylist;
list::iterator it;

for( it = list.begin() ; it != list.end() ; it++ ) {
	if( i==5) {
		x = *it;


Question 8 :

Define a constructor - What it is and how it might be called (2 methods)?

Answer :

constructor is a member function of the class, with the name of the function being the same as the class name. It also specifies how the object should be initialized. 

Ways of calling constructor:
1) Implicitly: automatically by complier when an object is created.
2) Calling the constructors explicitly is possible, but it makes the code unverifiable.


class Point2D{
	int x; int y;
	public Point2D() : x(0) , y(0) {} //default (no argument) constructor

	Point2D MyPoint; // Implicit Constructor call. In order to allocate memory on stack, the default constructor is implicitly called.
	Point2D * pPoint = new Point2D(); // Explicit Constructor call. In order to allocate memory on HEAP we call the default constructor. 


Question 9 :

What is the difference between class and structure?

Answer :

Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public. 
Class: Class is a successor of Structure. By default all the members inside the class are private.

Question 10 :

What is RTTI?

Answer :

Runtime type identification (RTTI) lets you find the dynamic type of an object when you have only a pointer or a reference to the base type. RTTI is the official way in standard C++ to discover the type of an object and to convert the type of a pointer or reference (that is, dynamic typing). The need came from practical experience with C++. RTTI replaces many Interview Questions - Homegrown versions with a solid, consistent approach.

Question 11 :

What is encapsulation?

Answer :

Packaging an object's variables within its methods is called encapsulation.

Question 12 :

What is an object? 

Answer :

Object is a software bundle of variables and related methods. Objects have state and behavior.

Question 13 :

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

Answer :

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.

Question 14 :

What do you mean by inheritance?

Answer :

Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own.

Question 15 :

What is namespace?

Answer :

Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces.
The form to use namespaces is:
namespace identifier { namespace-body }
Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example:
namespace general { int a, b; } In this case, a and b are normal variables integrated within the general namespace. In order to access to these variables from outside the namespace we have to use the scope operator ::. For example, to access the previous variables we would have to put:
general::a general::b
The functionality of namespaces is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error.

Question 16 :

Describe run-time type identification?

Answer :

The ability to determine at run time the type of an object by using the typeid operator or the dynamic_cast operator.

Question 17 :

What problem does the namespace feature solve? 

Answer :

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.

Question 18 :

Are there any new intrinsic (built-in) data types? 

Answer :

Yes. The ANSI committee added the bool intrinsic type and its true and false value keywords.

Question 19 :

What is the difference between Mutex and Binary semaphore?

Answer :

semaphore is used to synchronize processes. where as mutex is used to provide synchronization between threads running in the same process.

Question 20 :

In C++, what is the difference between method overloading and method overriding? 

Answer :

Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.