Social Icons

Friday, January 11, 2013

C++ INTERVIEW QUESTIONS FOR FRESHERS WITH ANSWERS PAPER 3



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.


What is virtual function?
When derived class overrides the base class method by redefining the same function, then if client wants to access redefined the method from derived class through a pointer from base class object, then you must define this function in base class as virtual function.
class parent
{
   void Show()
{
cout << "i'm parent" << endl;
}
};
class child: public parent
{
   void Show()
{
cout << "i'm child" << endl;
}
};
parent * parent_object_ptr = new child;
parent_object_ptr->show() // calls parent->show() i 
now we goto virtual world...
class parent
{
   virtual void Show()
{
cout << "i'm parent" << endl;
}
};
class child: public parent
{
   void Show()
{
cout << "i'm child" << endl;
}
};
parent * parent_object_ptr = new child;
parent_object_ptr->show() // calls child->show() 


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.


51. What do you mean by late binding?
Ans: Late binding refers to function calls that are not resolved until run time. Virtual functions are used to achieve late binding. When access is via a base pointer or reference, the virtual function actually called is determined by the type of object pointed to by the pointer.


52What is a friend function?
A friend function is used in object-oriented programming to allow access to private or protected data in a class from outside the class. Normally a function which is not a member of a class cannot access such information; neither can an external class. ...
friend funtion is used to access to protected and public data.
is denoted as:
this function defination is inside the class only.without using scoperesolution operator.
friend functionname(args)
{
-------//function defination
-------}

What is difference between template and macro??
There is no way for the compiler to verify that the macro parameters are of compatible types. The macro is expanded without any special type checking.
If macro parameter has a postincremented variable ( like c++ ), the increment is performed two times.
Because macros are expanded by the preprocessor, compiler error messages will refer to the expanded macro, rather than the macro definition itself. Also, the macro will show up in expanded form during debugging.
for example:
Macro:
#define min(i, j) (i < j ? i : j)
template:
template
T min (T i, T j)
{
return i < j ? i : j;
}

No comments:

Post a Comment