31 | 07 | 2010
Main Menu
Affiliates
Login Form



Alexa
JoomlaWatch Stats 1.2.6 by Matej Koval
C++ Inetrview Questions-1
Written by Phoenix   
Tuesday, 05 August 2008 11:58

Rating 2.0/5 (3 votes)

C++  INTERVIEW QUESTIONS - 1 : 

 

 

1.What is encapsulation?

Encapsulation can be defined as the process of containing and hiding information about an object, such as internal data structures and code. Encapsulation helps in the isolation of the internal complexity of an object's operation from the rest of the application. For example, a client component asking for tax information from a income tax object need not know the data's source.




2.What is inheritance?

Inheritance can be defined as the process that allows one class to reuse the state and behavior of another class. The derived class will inherit the properties and method implementations of the base class and extends it by overriding methods and adding additional properties and methods.




3.What is meant by Polymorphism?

Polymorphism can be defined as the process that allows a client to treat different objects in the same way even if they were created from different classes and exhibit different behaviors.
One can use implementation inheritance to achieve polymorphism in languages such as Java  and C++ .
Base class object's pointer can invoke methods in derived class objects.
One can also achieve polymorphism in C++ by function overloading and operator overloading.




4.What do you mean by a constructor?

A constructor is one that creates an object and initializes it. It also creates vtable for virtual functions. It is different from the other methods in a class.


 

5.What do you mean by a destructor?

A destructor is one that deletes the extra resources allocated by the object.




6.What is default constructor?

A default constructor is a constructor with no arguments or all the arguments assigned to default values.




7.What is copy constructor?


A Copy Constructor is a constructor which initializes it's object member variables ( by shallow copying) with another object of the same class. If one does not implement a copy constructor in their class then compiler implements one for by itself.

Example:
Myclass Obj1(25); // calling Myclass constructor

Myclass Obj2(Obj1); // calling Myclass copy constructor
Myclass Obj2 = Obj1;// calling Myclass copy constructor




8.When are copy constructors called?

Copy constructors are called in following cases:
a) when a function returns an object of that class by value
b) when a object of that class is passed by value as an argument to a function
c) when we construct an object based on another object of the same class
d) When the compiler generates a temporary object




9.What is an assignment operator?

Default assignment operator handles assigning one object to another of the same class.I performs member to member copy which can also be called as shallow copy.




10.What are all the implicit member functions of the class? Or what are all the functions which compiler implements for us if we don't define one?

The following are the implicit member functions of the class:-

*default constructor
*copy constructor
*assignment operator
*default destructor
*address operator




11.What is a conversion constructor?

A constructor with a single argument makes that constructor as conversion constructor and it can be used for type conversion.

for example:

class MyClass
{
  public:
    MyClass( int i );
};

MyClass MyClassObject = 10 ; // assigning int 10 MyClass object




12.What is conversion operator?

Classes can have a public method for specific data type conversions.

for example:
class MyClass
{
  double value;
  public:
    MyClass(int i )
    operator double()
    {
  return value;
    }
};

MyClass MyClassObject;

double i  = MyClassObject; // assigning object to variable i of type double. now conversion  operator gets called to assign the value.




13.What is the difference between malloc()/free() and new/delete?

malloc() allocates memory for object in heap but doesn't invoke object's constructor to initiallize the object.

on the other hand,

new() allocates memory and also invokes constructor to initialize the object.

malloc() and free() do not support object semantics.
They do not construct and destruct objects.
string * ptr = (string *)(malloc (sizeof(string)))
Are not safe
Does not calculate the size of the objects that it construct
Returns a pointer to void

int *p = (int *) (malloc(sizeof(int)));
int *p = new int;
Are not extensible
new and delete can be overloaded in a class

"delete" first calls the object's termination routine (i.e. its destructor) and then releases the space the object occupied on the heap memory. If an array of objects was created using new, then delete must be told that it is dealing with an array by preceding the name with an empty []:-

Int_t *my_ints = new Int_t[10];


delete []my_ints;




14.What is the difference between "new" and "operator new" ?


"operator new" works like malloc.

while,
new() allocates memory and also invokes constructor to initialize the object.


15.What is difference between template and macro?

Macro:
In a macxro ,there is no way for the compiler to verify that the macro parameters are of compatible types. The macro is expanded without any special type checking.

Template:
On the other hand, a template uses generic types as its parameters. When an object of a specific type is defined for actual use, the template definition for that class is substituted with the required data type. Since a template is defined with a parameter that would be replaced by a specific data type at the time of actual use of the class or function, the templates are sometimes called 'parametrized classes' or 'functions'.




16.What are C++ storage classes?


The various C++ storage classes are as follows:-

*auto
*register
*static
*extern

auto: the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible outside that block

register: a type of auto variable. a suggestion to the compiler to use a CPU register for performance

static: a variable that is known only in the function that contains its definition but is never destroyed and retains its value between calls to that function. It exists from the time the program begins execution

extern: a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.




17.What are storage qualifiers in C++ ?

The various storage qualifiers in C++ are as follows:-

*const
*volatile
*mutable

Const keyword indicates that memory once initialized, should not be altered by a program.

Volatile keyword indicates that the value in the memory location can be altered even though nothing in the program code modifies the contents.

Example: If you have a pointer to hardware location that contains the time, where hardware changes the value of this pointer variable and not the program. The intent of this keyword to improve the optimization ability of the compiler.  

Mutable keyword indicates that particular member of a structure or class can be altered even if a particular structure variable, class, or class member function is constant.




18.What is reference ?


Reference can be defined as a name that acts as an alias, or alternative name, for a previously defined variable or an object.
Prepending a variable with "&" symbol makes it as reference.

for example:

int a;
int &b = a;
 



19.What is passing by reference?

Passing by reference is the method of passing arguments to a function which takes parameter of type reference.

for example:

void swap( int & x, int & y )
{
 int temp = x;
 x = y;
 y = temp;
}

int a=2, b=3;
swap( a, b );
Basically, inside the function there won't be any copy of the arguments "x" and "y" instead they refer to original variables a and b.
Therefore no new memory is needed to pass arguments and therefore passing by reference is more efficient.
 



20.Why do we use "const" reference arguments in function?


a) Using const protects us from programming errors that inadvertently change our data.
b) Using const also allows functions to process both const and non-const actual arguments, while a function without const in the prototype can only accept non constant arguments.
c) Using a const reference enables the function to generate and use a temporary variable appropriately.
 



21.What are the situations in which temporary variables are created by the C++ compiler?


If the function parameter is a "const reference", compiler generates temporary variable in following 2 ways.

a) The actual argument is of the correct type, but it isn't Lvalue

double Cube(const double & num)
{
  num = num * num * num;
  return num;

}

double temp = 2.0;
double value = cube(3.0 + temp); // argument is a expression and not a Lvalue;

b) The actual argument is of the wrong type, but of a type that can be converted to the correct type

long temp = 3L;
double value = cuberoot ( temp); // long to double conversion





22.What is a virtual function?

A virtual function is a mechanism for implementing polymorphism in c++.
When we use the same function name in the base class and the derived class, the function in the base class is declared as virtual using the keyword virtual preceding its normal declaration.
When a function is declared as virual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer rather than the type of the pointer.
When derived class overrides the base class method by redefining the same function, then if client wants to access redefined the method from derived class through a pointer from base class object, then you must define this function in base class as virtual function.

class base
{
   void Show()
{
cout << "i'm base" << endl;
}
};

class derived: public base
{
   void Show()
{
cout << "i'm derived" << endl;
}

};

base * base_object_ptr = new derived;

base_object_ptr->show() // calls base->show() i

VIRTUAL FUNCTIONS:-


class base
{
   virtual void Show()
{
cout << "i'm base" << endl;
}
};

class derived: public parent
{
   void Show()
{
cout << "i'm derived" << endl;
}

};

base * base_object_ptr = new derived;

base_object_ptr->show() // calls derived->show() 




23.What is a pure virtual function? or what is abstract class?

When we define only the function prototype in the base class without implementation and do the complete implementation in derived class. Then the base class is called abstract class , the function in it is called as the 'pure virtual function' , and the client will not be able to instantiate an object using this base class.

We can make a pure virtual function (abstract class) as follows:-

class MyClass
{
void fMyClass() = 0;
}

MyClass obj; // compilation error

 


24.What is Memory alignment?


The term alignment primarily means the tendency of an address pointer value to be a multiple of some power of two. So a pointer with two byte alignment has a zero in the least significant bit and a pointer with four byte alignment has a zero in both the two least significant bits. And so on. More alignment means a longer sequence of zero bits in the lowest bits of a pointer.






.

Comments
Search
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Tuesday, 05 August 2008 12:02 )