|
Object Oriented Interview Questions and Answers
Differentiate between the
message and method.
Message
* Objects communicate by sending messages to each other.
* A message is sent to invoke a method.
Method
* Provides response to a message.
* It is an implementation of an operation.
What is a dangling
pointer?
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. 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.
Differentiate between a
template class and class template.
Template class: A generic definition or a parameterized class not
instantiated until the client provides the needed information. It’s
jargon for plain templates.
Class template: A class template specifies
how individual classes can be constructed much like the way a class
specifies how individual objects can be constructed. It’s jargon for
plain classes.
What is a modifier?
A modifier, also called a modifying function is a member function that
changes the value of at least one data member. In other words, an operation
that modifies the state of an object. Modifiers are also known as
‘mutators’. Example: The function mod is a modifier in the following code
snippet:
class test
{
int x,y;
public:
test()
{
x=0; y=0;
}
void mod()
{
x=10;
y=15;
}
};
Page Numbers : 1
2
3
4
5
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
Structs Interview
Questions for more Structs Interview Questions with answers
Check
Job Interview Questions
for more Interview Questions with Answers
|