Sunday, 5 August 2012

c++ Interview Q and Ans

What is a virtual destructor?
Ans: The simple answer is that a virtual destructor is one that is declared with the virtual attribute.
The behavior of a virtual destructor is what is important. If you destroy an object through a pointer or reference to a base class, and the base-class destructor is not virtual, the derived-class destructors are not executed, and the destruction might not be comple
Can a constructor throw a exception? How to handle the error when the constructor fails?
Ans: The constructor never throws a error.
What are the debugging methods you use when came across a problem?
Ans: Debugging with tools like :
GDB, DBG, Forte, Visual Studio.
Analyzing the Core dump.
Using tusc to trace the last system call before crash.
Putting Debug statements in the program source code.
How the compilers arranges the various sections in the executable image?

Ans: The executable had following sections:-
Data Section (uninitialized data variable section, initialized data variable section )
Code Section
Remember that all static variables are allocated in the initialized variable section.
Explain the ISA and HASA class relationships. How would you implement each in a class design?
Ans: A specialized class "is" a specialization of another class and, therefore, has the ISA relationship with the other class.
This relationship is best implemented by embedding an object of the Salary class in the Employee class.
When is a template a better solution than a base class?
Ans: When you are designing a generic class to contain or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus, the generality) to the designer of the container or manager class.
What are the differences between a C++ struct and C++ class?
Ans: The default member and base-class access specifies are different.
This is one of the commonly misunderstood aspects of C++. Believe it or not, many programmers think that a C++ struct is just like a C struct, while a C++ class has inheritance, access specifies, member functions, overloaded operators, and so on. Actually, the C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base-class inheritance, and a class defaults to the private access specified and private base-class inheritance.
How do you know that your class needs a virtual destructor?
Ans: If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object.
What is the difference between new/delete and malloc/free?
Ans: Malloc/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory.
What happens when a function throws an exception that was not specified by an exception specification for this function?
Ans: Unexpected() is called, which, by default, will eventually trigger abort().
Can you think of a situation where your program would crash without reaching the breakpoint, which you set at the beginning of main()?
Ans: C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered.
What issue do auto_ptr objects address?
Ans: If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown.
Is there any problem with the following:
char *a=NULL; char& p = *a;?
Ans: The result is undefined. You should never do this. A reference must always refer to some object.
Why do C++ compilers need name mangling?
Ans: Name mangling is the rule according to which C++ changes function's name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name.
Is there anything you can do in C++ that you cannot do in C?
Ans: No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C.

le an� + s 0� �^� irtual destructor is one that is declared with the virtual attribute.
The behavior of a virtual

No comments:

Post a Comment

Write your openion about my blog spot..To get automatic facebook updates like my Page ..It takes only 1 min to write the comment and to like the page.. Thanks.