Haryana Engineering College , Engineering Institutes in Haryana, Engineering Colleges in Haryana, Haryana Engineering Colleges , Engineering Colleges in Haryana, Haryana Engineering Colleges, Engineering Institutes in Haryana, Haryana Engineering Institutes, List of Engineering Colleges in Haryana, Haryana Engineering Colleges List, List of Engineering Institutes in Haryana, Haryana Engineering Institutes List , Haryana Engineering College Exams in India , Ranking of haryana engineering colleges , Engineering Colleges in Haryana Institutes in India

Monday, August 18, 2008

C++ Interview Question




What is object?

An object is an instantiation of a class.


What is class?


A class is a combination of data and member functions.which hold the behavoiur of an objects.
It is declared with keyword Class.
As like this

Class name
{
Access specifier
Members
} objest name


Disadvantages of copy constructor?


When new variable is created then copy constructor is called.when we are not creating any copy constructor then C++ uses the default copy constructor and makes shallow copy means copies each field.

What are the operators we can not overload ?

. ,:? ,::. .* these are the oprator we cannot overload.


What is difference between constructor and copy constructor?
  • In copy constructor it accept reference of own class but in constructor its not.
  • the major difference between the copy constructor and the simple constructor is that in copy constructor we are first creating the object instance on which it is called and then we are initializing the values in its member variables.but in the constructor , we only assign the values to the member variables of the object instance which is already being created.

What do you means by friend function?


A friend function is a non member function of a class.with the help of friend function all access permissions are given to function. Friend function allows the functions of outside class to access the private data members.

what is the difference between procedure oriented language and object oriented language?

Procedure oriented language is based on function not on objects. Means no concept of class and encapsulation. But in object oriented language there is concept of class and objects and object oriented language is also used the concept of inheritance.

when is memory allocated to class?


When we create a class and define the data and member functions then no memory is allocated. Memory is allocated when we instantiate an objects of the class .

What is the Difference between realloc() and free()?


Realloc means reallocation of memory. With the help of realloc function we can reallocate the memory or we can say we can decrease or increase the memory size.

realloc(ptest,sizeof(test));

where ptest is the structure. After realloc ptest contains new address and memory allocated will double the size of pervious memory.

Free function is used to free the memory which is previously allocated.

What is function overloading?


Function overloading means same name with different parameter . when an overloaded function is called then compiler select the proper function finding out the number and type of argument .

What is operator overloading?


Operator overloading means we can overload exiting operator . we can redefined operator so that we can work on user defined class.

What do you mean by inline function?


When we add the inline keyword to function then code of the function is inserted instead of jump to function .
Inline specifier is just suggestion to the compiler then its depend on the compiler to ignore or performed.

What are the condition when the compiler is ignored the function to convert to inline ?

  • When the function contain static variable
  • When function is recursive .
  • When it contain loop and goto statement .
  • Inline function is used for small function only.

What is RTTI?

  • RTTI : Run Time Type Identification.
  • RTTI is used to find out what type of objects it is at run time .
  • For this we used operators dynamic_cast and typeid.
  • RTTI is used to cast derived class object into base class

What is pure virtual function?


A pure virtual functions doesn't have a body. You cannot create an instance of a class with a pure virtual function.And you must overloaded that function.

Class A
{
Public:
Virtual void func()=0;
}


Here is the example in which function func() is pure virtual. We can make pure virtual function with function equal zero.

What is virtual destructors?


When the object is destroyed explicitly by applying delete operator for the base class then base class destructor is called. But there is one problem is created when derived class inherit the base class then how it is referenced to solve the problem of this we use virtual destructor in which first derived class destructor is called after that base class .

What are the advantages of inheritance?

  • Code reusability
  • Save time during program development .
  • It is use for developing high quality software.
  • Reduce time during development.
  • Increase understandability .

What is the difference between Function and Member function?


Function define outside the scope of class but member function is define with in scope of class and member function we can access with the help of scope resolution operator.

What is the difference between Object and Instance?


Object is obtained when it has a life, means it has occupied some memory. Instance is the copy of the Reference that points to object at a point of time.



Difference between macro and inline()?

  • Inline follows strict parameter type checking, macros do not.
  • Macros are always expanded by preprocessor, whereas compiler may or may not replace the inline definitions.
  • Compiler can check in case of inline but not in case of macro.

What do you means by static ?


Static means that when it is initialized, it remains in memory until the end of program

Can destructor be private?


Yes destructors can be private.


What is this pointer?


The this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.


What is template?


Template are generic data type which can operate on different data type of information. Use templates in situations that result in duplication of the same code for multiple types.

What is "strstream”?


Class that reads and writes to an array in memory .

What is namespace?

Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.

The format of namespaces is:

namespace identifier

{

entities

}

Can you allocate the memory using malloc() in C and deallocate the same memory using free() in c++?


No, we should not do that. if we do that we can have un-defined or un-predictable results. Which can corrupt other memory too. Instead, we should make a simple rule not to do that.

What are the things contains in .obj file ? ( compiled result of .cpp file )


C++ .obj file holds code and data suitable for linking with other object files to create an executable or a shared object file.

what is the use of volatile keyword? Give me one example?


The volatile type qualifier is applied to a definition of a variable that may be altered from outside the program (i.e., the variable is not completely under the control of the program). Thus, the compiler cannot perform optimizations (such as speeding program execution or reducing memory consumption, for example) that depend on "knowing a variable's behavior is influenced only by program activities the compiler can observe."

(notes:)

1) volatile indicate the object is modified by something not directly under the compiler's control (i.e., the hardware itself)

2) one use of volatile qualifier is to provide access to memory locations used by asynchronous processes such as interrupt handlers.

3) Another example might be the global variables that keeps track of the total number of timer interrrupts.

What is function overriding?


The function overloading is the technique in which you can make the function with more than one name but only if you will change the paramaters or the number of the paramaters.

Can we take "main function" as of type float,char etc?


Its possible only with int.If a function is declared as void that it may return anything by default.

what is the difference betwen wait() and delay()?


Wait() and delay() works same but works on different platforms. Wait(2) will wait processing fro 2 second on Linux/Unix while delay(2000) with wait for 2 second but on DOS or Windows.

so wait(2) on linux == delay(2000) on DOS

Delay() is under while one can directly use wait in his/her program.

What is the difference between macro and inline()?


1. Inline follows strict parameter type checking, macros do not.

2. Macros are always expanded by preprocessor, whereas compiler may or may not replace the inline definitions.

What is operator overloading?what r the advantages of operator overloading?


programming which gives an extra ability to an operator to act on a User-defined operand(Objects).Uses of Operator Overloading:Extensability: An operator will act differently depending on the operands provided.Operator is not limited to work only with primitive Data type.

In c++ have a default constructor ?


Yes C++ does have a default constructor provided by the compiler.In this case all the members of the class are initialized to null values.These values act as the default values .For eg: MyClass me;In the above case since the object is not initialized to any value so the default constructor will be called which will initialize the class with the default values.

What are the types of STL containers?
deque

hash_map

hash_multimap

hash_multiset

hash_set

list map

multimap

multiset

set

vector

Difference between a "assignment operator" and a "copy constructor" ?


Copy constructor is called every time a copy of an object is made. When you pass an object by value, either into a function or as a function return value, a temporary copy of that object is made. Assignment operator is called whenever you assign to an object.

Assignment operator must check to see if the right-hand side of the assignment operator is the object itself. It executes only the two sides are not equal.

What is the Basic nature of "cin" and "cout" and what concept or principle we are using on those two?


Basically "cin and cout" are INSTANCES of istream and ostream classes respectively.And the concept which is used on cin and cout is operator overloading. Extraction and Insertion operators are overloaded for input and ouput operations.

What is conversion constructor?
constructor with a single argument makes that constructor as conversion ctor and it can be used for type conversion.


for example:


class Boo

{

public: Boo( int i );

};
Boo BooObject = 10 ; // assigning int 10 Boo object

What is conversion operator?


class can have a public method for specific data type conversions.
for example:

class Boo{

double value;

public: Boo(int i )

operator double()

{ return value;

}

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

What is difference between template and macro?


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.
If macro parameter has a postincremented variable ( like c++ ), the increment is performed two times.
Because macros are expanded by the preprocessor, compiler error messages will refer to the expanded macro, rather than the macro definition itself. Also, the macro will show up in expanded form during debugging.
for example:
Macro:
#define min(i, j) (i <>template:

template

T min (T i, T j)

{

return i <>

}

What are storage qualifiers in C++ ?


They are..
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 programcode modifies the contents. for 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.

What is reference ?
reference is a name that acts as an alias, or alternative name, for a previously defined variable or an object.
prepending variable with "&" symbol makes it as reference.
for example:

int a;

int &b = a;

When do use "const" reference arguments in function?

a) Using const protects you against programming errors that inadvertently alter data.

b) Using const allows function 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 allows the function to generate and use a temporary variable appropriately.

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.

What is the use of 'using' declaration?


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

What is a dangling pointer?


A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.

What do you mean by Stack unwinding?

It is a process during exception handling when the destructor is called for all local objects in the stack between the place where the exception was thrown and where it is caught.

What is Dynamic memory allocation?

The process of allocating Memory Reallocation in C at run time is known as dynamic memory allocation(Memory Reallocation in C ). Although c does not inherently have this facility there are four library routines which allow this function. Many languages permit a programmer to specify an array size at run time. Such languages have the ability to calculate and assign during executions, the memory space required by the variables in the program. But c inherently does not have this facility but supports with memory management functions, which can be used to allocate and free memory during the program execution. The following functions are used in c for purpose of memory management.

malloc : Allocates Memory Reallocation in C requests size of bytes and returns a pointer to the Ist byte of allocated space

calloc : Allocates space for an array of elements initializes them to zero and returns a pointer to the memory

free : Frees previously allocated space

realloc : Modifies the size of previously allocated space.

Memory Reallocation in C process : According to the conceptual view the program instructions and global and static variable in a permanent storage area and local area variables are stored in stacks. The memory space that is located between these two regions in available for dynamic allocation during the execution of the program. The free memory region is called the heap. The size of heap keeps changing when program is executed due to creation and death of variables that are local for functions and blocks. Therefore it is possible to encounter memory overflow during dynamic allocation process. In such situations, the memory allocation functions mentioned above will return a null pointer.


0 comments:

About This Blog

Lorem Ipsum

Back to TOP