Monday, December 1, 2008

Placement Question Basic C/ C++ programming language - Frequently Asked Questions

What is the difference between a copy constructor and an overloaded assignment operator?

Answer: 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?

Answer: 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?

Answer: 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?

Answer: The constructor never throws a error.

How the compilers arranges the various sections in the executable image?

Answer: The executable had following sections:-Data Section (uninitialized data variable section, initialized data variable section )Code SectionRemember that all static variables are allocated in the initialized variable section.


When is a template a better solution than a base class?

Answer: 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?

Answer: 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?

Answer: 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?

Answer: 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?

Answer: 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()?

Answer: 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?

Answer: 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;
?

Answer: The result is undefined. You should never do this. A reference must always refer to some object.

Why do C++ compilers need name mangling?

Answer: 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?

Answer: 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.

What is an object?

Answer: A region of storage with associated semantics. For example, int i; "i is an object of type int". In Object Oriented C++, "object" usually means "an instance of a class." Thus a class defines the behavior of possibly many objects.

No comments:

Post a Comment

Thanks to given comments.......

My Blog List