Campus/Interview Preparation with C ++ language
Starting with Important topic of C ++ Language :-
source-www.mysirg.com
2.
An operator function is created using _____________ keyword.
(a) iterator
(b) allocator
(c) constructor
(d) operator
(a) Zero
(b) One
(c) Two
(d) Three
4.
1. Static member functions
2. Non- static member functions
3. Friend Functions
(a) Only 2
(b) Only 1, 3
(c) Only 2 , 3
(d) All 1 , 2, 3
5.
(a) Zero
(b) One
(c) Two
(d) None of these.
6.
(a) . (Member Access or Dot operator)
(b) ?: (Ternary or Conditional Operator
(c):: (Scope Resolution Operator)
(d) All of the above
7.
(a) Postfix ++
(b) Comparison Operator
(c) Insertion Operator <<
(d) Prefix++
8.
using namespace std;
9.
(a) C++ doesn’t allow both operators to be overloaded in a class
(b) A postfix ++ has a dummy parameter
(c) A prefix ++ has a dummy parameter
(d) By making prefix ++ as a global function and postfix as a member function.
10.
(a) By overloading new operator
(b) By making an empty private new operator.
(c) By making an empty private new and new[] operators
(d) By overloading new operator and new[] operators
If this article is helpful to you then please like share .... Thank you....
- Operator Overloading
- Virtual function
- Inheritance
- STL: Standard Template Library
![]() |
campus/interview preparation |
Objective type questions or multiple choice questions (MCQ) are often asked in various exams, including job recruitment tests. It also helps in understanding language constructs, syntax and fundamental rules.click the link given in above list to go to that topic.
Operator Overloading
1.
Using friend operator function, following perfect set of operators may not be overloaded.
(a) = , ( ) , [ ] , ->
(b) <<, = = , [ ] , >>
(c) ?, = , ( ) , ++
(d) None of these
Using friend operator function, following perfect set of operators may not be overloaded.
(a) = , ( ) , [ ] , ->
(b) <<, = = , [ ] , >>
(c) ?, = , ( ) , ++
(d) None of these
Answer
(a) You cannot overload = , ( ) , [ ] , ->
operator using friend function. Most of the operator you can overload such as
unary, binary operator etc
2.
An operator function is created using _____________ keyword.
(a) iterator
(b) allocator
(c) constructor
(d) operator
Answer
(d) An operator function is created using
operator keyword.
General form of a member operator function is as follows.
ret-type class-name::operator operator Sign (arg-list)
{
// operations
}
General form of a member operator function is as follows.
ret-type class-name::operator operator Sign (arg-list)
{
// operations
}
3.
While
overloading binary operators using member function, it requires ___ arguments.
(a) Zero
(b) One
(c) Two
(d) Three
Answer
(b) If overloaded as a member function, binary
operators require one argument. The argument
contains value of the object, which is to the right of the operator.
contains value of the object, which is to the right of the operator.
4.
In case of
operator overloading, operator function must be ______ .
1. Static member functions
2. Non- static member functions
3. Friend Functions
(a) Only 2
(b) Only 1, 3
(c) Only 2 , 3
(d) All 1 , 2, 3
Answer
(c) No other operators are overloaded
5.
When
overloading unary operators using Friend function, it requires_____ argument/s.
(a) Zero
(b) One
(c) Two
(d) None of these.
Answer
(b) The friend functions are more useful in
operator overloading. They offer better flexibility, which is not provided by
the member function of the class. The difference between member function and
friend function is that the member function takes argument ex-plicitly.On the
contrary, the friend function needs the parameters to be explicitly passed.
6.
Which of the
following operators cannot be overloaded.
(a) . (Member Access or Dot operator)
(b) ?: (Ternary or Conditional Operator
(c):: (Scope Resolution Operator)
(d) All of the above
Answer
(d) There is no need for explanation
7.
Which of the
following operators should be preferred to overload as a global function rather
than a member method?
(a) Postfix ++
(b) Comparison Operator
(c) Insertion Operator <<
(d) Prefix++
Answer
(c) cout is an object of ostream class which is
a compiler defined class.When we do “cout << obj” where obj is an object
of our class, the compiler first looks for an operator function in ostream,
then it looks for a global function. One way to overload insertion operator is
to modify ostream class which may not be a good idea. So we make a global
method.
8.
Find the
output of the Following:
using namespace std;
class Point
{
private:
int x, y;
public:
Point() : x(0), y(0) { }
Point& operator()(int dx, int dy);
void show() {cout << “x = ” << x << “, y = ” << y; }
};
Point& Point::operator()(int dx, int dy)
{
x = dx;
y = dy;
return *this;
}
int main()
{
Point pt;
pt(3, 2);
pt.show();
return 0;
}
private:
int x, y;
public:
Point() : x(0), y(0) { }
Point& operator()(int dx, int dy);
void show() {cout << “x = ” << x << “, y = ” << y; }
};
Point& Point::operator()(int dx, int dy)
{
x = dx;
y = dy;
return *this;
}
int main()
{
Point pt;
pt(3, 2);
pt.show();
return 0;
}
(a) x = 3, y
= 2
(b) Compiler Error
(c) x = 2, y = 3
(d) None of these.
(b) Compiler Error
(c) x = 2, y = 3
(d) None of these.
Answer
(a) Note that the class B has as conversion
operator overloaded, so an object of B can be converted to that of (A) Also,
class A has a constructor which can be called with single integer argument, so
an int can be converted to (A)
9.
How C++
compiler does differ between overloaded postfix and prefix operators?
(a) C++ doesn’t allow both operators to be overloaded in a class
(b) A postfix ++ has a dummy parameter
(c) A prefix ++ has a dummy parameter
(d) By making prefix ++ as a global function and postfix as a member function.
Answer
(b) The pre- and post-increment are two distinct
operators, and require separate overloads. C++ doesn’t allow overloading solely
on return type, so having different return types as in your example wouldn’t be
sufficient to disambiguate the two methods. The dummy argument is the mechanism
that the designer of C++ chose for the disambiguation.
10.
How can we
restrict dynamic allocation of objects of a class using new?
(a) By overloading new operator
(b) By making an empty private new operator.
(c) By making an empty private new and new[] operators
(d) By overloading new operator and new[] operators
Answer
(c) If we declare new and [] new operators, then
the objects cannot be created anywhere (within the class and outside the class)
If this article is helpful to you then please like share .... Thank you....
Comments
Post a Comment