Sunday 5 August 2012

Interview Questions in C++ Language


Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible ?
Ans: There is nothing like Virtual Constructor.
The Constructor can’t be virtual as the constructor is a code which is responsible for creating a instance of a class and it can’t be delegated to any other object by virtual keyword means.
What about Virtual Destructor?
Ans: Yes there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtime depending on the type of object pointer is pointing to , proper destructor will be called.
What is Pure Virtual Function? Why and when it is used ?
Ans: The abstract class whose pure virtual method has to be implemented by all the classes which derive on these. Otherwise it would result in a compilation error.
This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class.
What is problem with Runtime type identification?
Ans: The run time type identification comes at a cost of performance penalty. Compiler maintains the class.
How Virtual functions call up is maintained?

Ans: Through Look up tables added by the compile to every class image. This also leads to performance penalty.
Can inline functions have a recursion?
Ans: No.
Syntax wise It is allowed. But then the function is no longer Inline. As the compiler will never know how deep the recursion is at compilation time.
How do you link a C++ program to C functions?
Ans: By using the extern "C" linkage specification around the C function declarations. Programmers should know about mangled function names and type-safe linkages. Then they should explain how the extern "C" linkage specification statement turns that feature off during compilation so that the linker properly links function calls to C functions. Another acceptable answer is "I don't know. We never had to do that." Merely describing what a linker does indicates that the programmer does not understand the issue that underlies the question.
Explain the scope resolution operator?
Ans: It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.
How many ways are there to initialize an int with a constant?
Ans: Two:-
1. int foo = 123;
2. int bar(123);
What is your reaction to this line of code?
delete this;
Ans: It is not a good programming Practice.
A good programmer will insist that you should absolutely never use the statement if the class is to be used by other programmers and instantiated as static, extern, or automatic objects. That much should be obvious.
The code has two built-in pitfalls. First, if it executes in a member function for an extern, static, or automatic object, the program will probably crash as soon as the delete statement executes. There is no portable way for an object to tell that it was instantiated on the heap, so the class cannot assert that its object is properly instantiated. Second, when an object commits suicide this way, the using program might not know about its demise. As far as the instantiating program is concerned, the object remains in scope and continues to exist even though the object did itself in. Subsequent dereferencing of the pointer can and usually does lead to disaster. I think that the language rules should disallow the idiom, but that's another matter.
What is the difference between a copy constructor and an overloaded assignment operator?
Ans: A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.
When should you use multiple inheritance?
Ans: There are three acceptable answers:- "Never," "Rarely," and "When the problem domain cannot be accurately modeled any other way."
Consider an Asset class, Building class, Vehicle class, and CompanyCar class. All company cars are vehicles. Some company cars are assets because the organizations own them. Others might be leased. Not all assets are vehicles. Money accounts are assets. Real estate holdings are assets. Some real estate holdings are buildings. Not all buildings are assets. Ad infinitum. When you diagram these relationships, it becomes apparent that multiple inheritance is a likely and intuitive way to model this common problem domain. The applicant should understand, however, that multiple inheritance, like a chainsaw, is a useful tool that has its perils, needs respect, and is best avoided except when nothing else will do.
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

No comments:

Post a Comment

Write your openion about my blog spot..To get automatic facebook updates like my Pagehttps://www.facebook.com/shivashankar4u ..It takes only 1 min to write the comment and to like the page.. Thanks.