|
C++ INTERVIEW QUESTIONS - 2 : 1.What is the problem that is solved by namespace feature?
Multiple providers of libraries may use common global identifiers and this may result in a name collision when an application tries to link with two or more such libraries. Therefore, the namespace feature surrounds a library's external declarations with a unique namespace that eliminates the potential for those collisions.
namespace [identifier] { namespace-body }
A namespace declaration identifies and assigns a name to a declarative region. The identifier in a namespace declaration should be unique in the declarative region in which it is used. The identifier is the name of the namespace and it is used to reference its members.
2.What is the purpose of 'using' declaration?
The using declaration makes it possible to use a name from a namespace without the scope operator.
3.What is an Iterator class?
An Iterator class is one that is used to traverse through the objects maintained by a container class. There are five categories of iterators: input iterators, output iterators, forward iterators, bidirectional iterators, random access. An iterator is an entity that gives access to the contents of a container object without violating encapsulation constraints. Access to the contents is granted on a one-at-a-time basis in order. The order can be storage order (as in lists and queues) or some arbitrary order (as in array indices) or according to some ordering relation (as in an ordered binary tree). The iterator is a construct, which provides an interface that, when called, yields either the next element in the container, or some value denoting the fact that there are no more elements to examine. Iterators hide the details of access to and update of the elements of a container class. Something like a pointer.
4. What are the operators that cannot be overloaded?
The following are the operators that cannot be overloaded:
sizeof, ., .*, .->, ::, ?:
5. What is a container class? What are the types of container classes?
A container class is a class that holds objects in memory or external storage. A container class acts as a generic holder and it has a predefined behavior and a well-known interface. It is a supporting class and its purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; when the container is holding a group of objects that are all the same, the container is called a homogeneous container.
6.What is overloading?
Overloading is the practice of supplying more than one definition for a given function name in the same scope. In C++ language, the programmer can overload functions and operators.
Rules for overloading:- - Any two functions in a set of overloaded functions must have different argument lists. - Overloading functions with argument lists of the same types, based on return type alone, is an error.
7. What is Overriding?
When a subclass of the class that originally declared the method declares a method with the same name, return type (or a subclass of that return type), and same parameter list then it is called as overriding.
The characteristics of the method overriding are as follows:-
* Must have same method name. * Must have same data type. * Must have same argument list.
Overriding a method means replacing a method's functionality in derived class. To imply overriding functionality we need base and derived classes. In the derived class we define the same method signature as one defined in the base class.
8. What is "this" pointer?
A pointer that is accessible only within the member functions of a class, struct, or union type is known as the 'this' pointer. The 'this' pointer points to the object for which the member function is called. Static member functions can not have a this pointer.
9. How can you implement virtual functions in C++?
Virtual functions can be implemented in C++ using a table of function pointers, called the vtable. There is one entry in the table per virtual function in the class. This table is created by the constructor of the class. When a derived class is constructed, its base class is constructed first which creates the vtable. If the derived class overrides any of the base classes virtual functions, those entries in the vtable are overwritten by the derived class constructor. This is why you should never call virtual functions from a constructor: because the vtable entries for the object may not have been set up by the derived class constructor yet, so we may end up calling base class implementations of those virtual functions
10. What is name mangling in C++??
Name mangling is the process of encoding the parameter types with the function/method name into a unique name. The reverse process is called demangling.
11. What is Stack unwinding?
Stack unwinding 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 was caught.
12. State the difference between a pointer and a reference?
A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer on the other hand can be reassigned to point to different objects while a reference always refers to an object with which it was initialized.
13. How are prefix and postfix versions of operator++() differentiated?
The postfix version of operator++() has a dummy parameter of type int. The prefix version does not have dummy parameter.
14. What do you mean by a dangling pointer?
A dangling pointer comes into the picture when we 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.
15. What is the difference between const char *aPointer and char *const aPointer?
Const char *aPointer is a non constant pointer to constant data; while char *const aPointer is a constant pointer to non constant data.
16. How can one handle a constructor that fails?
One can handle a constructor that fails by throwing an exception. Constructors don't have a return type, so it is not possible to use return codes. The best way to handle and notify a constructor failure is to throw an exception.
17. How can one handle a destructor that fails?
One can handle a destructor that fails by writing a message to a log-file. But do not throw an exception. The C++ rule is that you must never throw an exception from a destructor that is being called during the "stack unwinding" process of another exception.
18. What is a Virtual Destructor?
A Virtual Destructor is one that allows us to destroy objects without knowing their type - the correct destructor for the object is invoked using the virtual function mechanism. The destructors can also be declared as pure virtual functions for abstract classes.
19. What do you mean by an inline function?
The inline function is a function, whose code is substituted for every instance of a function call. Still , substitution occurs only at the compiler's discretion. For example, the compiler does not inline a function if its address is taken or if it is too large to inline.
20. Is it possible for a program to crash without reaching the breakpoint which we set at the beginning of main()?
Yes it is possible for a program to crash without reaching the breakpoint which we set at the beginning of main(). C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. So , if this function crashes, then the crash will occur before main() is entered. 21. Name two cases where we should use initialization list as opposed to assignment in constructors.
The two cases where we should use initialization list as opposed to assignment in constructors are while using non-static const data members and reference data members. Both non-static const data members and reference data members cannot be assigned values; instead, we should use initialization list to initialize them.
22. Can we overload a function based on just whether a parameter is a value or a reference?
No we cannot overload a function based on just whether a parameter is a value or a reference. Passing by value and by reference looks identical to the caller.
23. What are the differences between a C++ struct and C++ class?
The default member and base class access specifiers are different for a C++ struct and C++ class.
The C++ struct has all the features of the class. The only difference is that a struct defaults to public member access and public base class inheritance, and a class defaults to the private access specifier and private base class inheritance.
24. What does extern "C" int func(int *, MyClass) accomplish?
It will turn off "name mangling" for func so that one can link to code compiled by a C compiler.
25. How can you access the static member of a class?
The static member of a class can be accessed in the following manner:- <ClassName>::<StaticMemberName>
26. What is meant by multiple inheritance or virtual inheritance? What are the advantages and disadvantages of multiple inheritance?
Multiple Inheritance is the process in which a child class can be derived from more than one parent class.
Advantage:- The advantage of multiple inheritance is that it allows a class to inherit the functionality of more than one base class thus allowing for modeling of complex relationships.
Disadvantage:- The disadvantage of multiple inheritance is that it can lead to a lot of confusion(ambiguity) when two base classes implement a method with the same name.
27. What are the various access privileges that are available in C++? What is the default access level in C++?
*The access privileges in C++ are private, public and protected. *The default access level assigned to members of a class is private. *Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it's sub-classes. Public members of a class can be accessed by anyone.
28. What is meant by a nested class? What is its use?
A class which enclosed within the scope of another class is called as a nested class.
Example:
// Nested class // class OuterClass { class NestedClass { // ... }; // ... }; Nested classes are useful for organizing code and controlling access and dependencies. Nested classes obey access rules just like other parts of a class do; so, in eample if NestedClass is public then any code can name it as OuterClass::NestedClass. Often nested classes contain private implementation details, and are therefore made private; in this example, if NestedClass is private, then only OuterClass's members and friends can use NestedClass.
When you instantiate as outer class, it won't instantiate inside class.
29. What is a local class? What is its purpose?
A 'local class' is a class that is defined within the scope of a function.(Any function, whether a member function or a free function).
Example: // Local class // int f() { class LocalClass { // ... }; // ... }; Like nested classes, local classes can be a useful tool for managing code dependencies.
30. Can a copy constructor accept an object of the same class as parameter, instead of reference of the object? No a copy constructor cannot accept an object of the same class as parameter, instead of a reference of the object. It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference.
|