Friday, 11 December 2015

CPP Interview Questions and Answers

CPP Interview Questions and Answers

1. Explain about Copy Constructor.
It is a constructore which initializes it’s object member variable with another object of the same class. If you don’t implement a copy constructor in your class, the compiler automatically does it.
2. When do you call copy constructors?
Copy constructors are called in these situations:
a)when compiler generates a temporary object
b)when a function returns an object of that class by value
c)when the object of that class is passed by value as an argument to a function
d)when you construct an object based on another object of the same class
3. Name the implicit member functions of a class.
a) default ctor      b) copy ctor     c) assignment operator      d) default destructor  e) address operator
4. Explain storage qualifiers in C++ programming.
a) const: This variable means that if the memory is initialised once, it should not be altered by a program.
b) volatile: This variable means that the value in the memory location can be altered even though nothing in the program code modifies the contents.
c) mutable: This variable means that a particular member of a structure or class can be altered even if a particular structure variable, class, or class member function is constant.
5. Explain about dangling pointer in c++.
When the address of an object is used after its lifetime is over, dangling pointer comes into existence. Some examples of such situations are: Returning the addresses of the automatic variables from a function or using the address of the memory block after it is freed.
6. In what situations do you have to use initialization list rather than assignment in constructors.
When you want to use non-static const data members and reference data members you should use initialization list to initialize them.
7. When does a class need a virtual destructor?
If your class has at least one virtual function, you should have a virtual destructor. This allows you to delete a dynamic object through a baller to a base class object. In absence of this, the wrong destructor will be invoked during deletion of the dynamic object.
8. What is the type of “this” pointer? When does it get created?
It is a constant pointer type. It gets created when a non-static member function of a class is called.
9. How would you differentiate between a pre and post increment operators while overloading?
Mentioning the keyword int as the second parameter in the post increment form of the operator++() helps distinguish between the two forms.
10. What is a pdb file?
A program database  file contains debugging and project state information that allows incremental linking of a Debug configuration of the program. This file is created when you compile a C/C++ program with /ZI or /Zi or a Visual Basic/C#/JScript .NET program with /debug.

C++ Interview Questions and Answers

11. You run a shell on UNIX system. How would you tell which shell are you running?
To check this you can simply do the Echo $RANDOM. The results will be:
a) Undefined variable if you are from the C-Shell
b) A return prompt if you are from the Bourne shell
c) A 5 digit random number if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.
12.What are Stacks? Give an example where is is useful.
A Stack is a linear structure in which insertions and deletions are always made at one end i.e the top – this is termed as last in, first out (LIFO). Stacks are useful when we need to check some syntex errors like missing parentheses.
13. Differentiate between an external iterator and an internal iterator? What is the advantage of an external iterator.
An external iterator is implemented as a separate class that can be “attach” to the object that has items to step through while an internal iterator is implemented with member functions of the class that has items to step through. With an external iterator many different iterators can be active simultaneously on the same object – this is its basic advantage.
14. Do you think the following code is correct? If not, what is the problem?
T *p = 0;
delete p;        No, the code has a problem. The program will crash in an attempt to delete a null pointer.
15. In a function declaration, what does extern mean?
The extern here tells the compiler about the existence of a variable or a function, even though the compiler hasn’t yet seen it in the file currently being compiled. This variable or function may be defined in another file or further down in the current file.
16. You want to link a C++ program to C functions. How would you do it?
This can be done by using the extern “C” linkage specification around the C function declarations.
17. Exlpain about STL(stranded template library).
STL stands for Standard Template Library. It is a library of container templates approved by the ANSI committee for inclusion in the standard C++ specification.
18. What are the different types of STL containers in C++?
Following are the 3 types of STL containers:
1. Adaptive containers – for e.g. queue, stack
2. Associative containers – for e.g. set, map
3. Sequence containers – for e.g. vector, deque
19. Explain Stack unwinding in C++
Stack unwinding is a process during exception handling when the destructor is called for all local objects between the place where the exception was thrown and where it is caught.
20. How would you find out if a linked-list is a cycle or not?
We can find out if the linked-list is not a cycle by using two pointers. One of them goes 2 nodes every time while the second one goes at 1 node each time. If there is a cycle, the one that goes 2 nodes each time will meet the one that goes slower. If this happens, you can say that the linked-list is a cycle else not.

1 comment: