Wednesday 19 September 2012

C++ Interview Questions-4


41. Name the operators that cannot be overloaded?
Ans: sizeof, ., .*, .->, ::, ?:

42. What is an adaptor class or Wrapper class?
Ans: A class that has no functionality of its own. Its member functions hide the use of a third party software component or an object with the non-compatible interface or a non-object-oriented implementation.

43. What is a Null object?
Ans: It is an object of some class whose purpose is to indicate that a real object of that class does not exist. One common use for a null object is a return value from a member function that is supposed to return an object with some specified properties but cannot find such an object.

44. What is class invariant?
Ans: A class invariant is a condition that defines all valid states for an object. It is a logical condition to ensure the correct working of a class. Class invariants must hold when an object is created, and they must be preserved under all operations of the class. In particular all class invariants are both preconditions and post-conditions for all operations or member functions of the class.

45. What is a dangling pointer?
Ans: A dangling pointer arises when you 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.
Example: The following code snippet shows this:
class Sample
{
public:
int *ptr;
Sample(int i)
{
ptr = new int(i);
}
~Sample()
{
delete ptr;
}
void PrintVal()
{
cout << “The value is ” << *ptr;
}
};
void SomeFunc(Sample x)
{
cout << “Say i am in someFunc ” << endl;
}
int main()
{
Sample s1= 10;
SomeFunc(s1);
s1.PrintVal();
}
In the above example when PrintVal() function is called it is called by the pointer that has been freed by the destructor in SomeFunc.

46. Differentiate between the message and method?
Ans:
Message:
Objects communicate by sending messages to each other. A message is sent to invoke a method.
Method
Provides response to a message and it is an implementation of an operation

47. How can we access protected and private members of a class?
Ans: In the case of members protected and private, these could not be accessed from outside the same class at which they are declared. This rule can be transgressed with the use of the friend keyword in a class, so we can allow an external function to gain access to the protected and private members of a class.

48. Can you handle exception in C++?
Ans: Yes we can handle exception in C++ using keyword: try, catch and throw. Program statements that we want to monitor for exceptions are contained in a try block. If an exception occurs within the try block, it is thrown (using throw).The exception is caught, using catch, and processed.

49. What is virtual function?
Ans: A virtual function is a member function that is declared within a base class and redefined by a derived class .To create a virtual function, the function declaration in the base class is preceded by the keyword virtual.

50. What do you mean by early binding?
Ans:Early binding refers to the events that occur at compile time. Early binding occurs when all information needed to call a function is known at compile time. Examples of early binding include normal function calls, overloaded function calls, and overloaded operators. The advantage of early binding is efficiency.

No comments:

Post a Comment