cpp operator overloading Exercise

C++ exercise

1. Which is the correct example of a binary operator?

a. Dereferencing operator(*)
b. ++
c. —
d. +

Show Answer

The correct option is (d)
Explanation:
+(adding two operands) requires two operands whereas ++(increases value by 1), –(decreases value by 1) and *(dereferencing operator used for accessing value of pointers) requires only one operand.

2. Which one is the correct example of a unary operator?

a. ==
b. —
c. /
d. &

Show Answer

The correct option is (b)
Explanation:
&, == and / requires two operands whereas — requires only one operand, in general, it decreases the value of operand by 1

3. Which one is called ternary operator??

a. &&
b. |||
c. ===
d. ??

Show Answer

The correct option is (d)
Explanation:
?: is known ternary operator because it separates three expressions. exp1 ? exp2 : exp3.

4. Which of the following operators cannot be overloaded?

a. :: (Scope Resolution Operator)
b. .* (Pointer-to-member Operator )
c. .* (Pointer-to-member Operator )
d. All of the above

Show Answer

The correct option is (d)
Explanation:
All of the above operator cannot be overloaded.

5. While overloading binary operators using member function, it requires ___ argument?

a. 0
b. 1
c. 2
d. 3

Show Answer

The correct option is (a)
Explanation:
While overloading binary operators using member function, it requires 0 argument.

6. How many approaches are used for operator overloading?

a. 1.
b. 2.
c. 3
d. 4

Show Answer

The correct option is (c)
Explanation:
There are Three different approaches used for operator overloading:
i. Overloading unary operator.
ii. Overloading binary operator.
iii. Overloading binary operator using a friend function.

7. Which of the following operator cannot be overloaded?

a. –.
b. +.
c. %
d. ?:

Show Answer

The correct option is (d)
Explanation:
?:, :: and . cannot be overloaded +, -, % can be overloaded.

8. Which of the following operator can be used to overload when that function is declared as friend function?

a. ()
b. ->
c. |=
d. []

Show Answer

The correct option is (c)
Explanation:
When an operator overloaded function is declared as friend function then [], () and -> cannot be overloaded.

9. In the case of friend operator overloaded functions how many maximum object arguments a unary operator overloaded function can take?

a. 0
b. 1
c. 2
d. 3

Show Answer

The correct option is (b)
Explanation:
In the case of friend operator overloaded functions unary operator overloaded function should take maximum one object argument only.

10. What happens when objects s1 and s2 are added ?

string s1 = “c++”;
string s2 = “programming”;
string s3 = (s1+s2).substr(5);

a. Segmentation fault as two string cannot be added in C++.
b. The statements runs perfectly.
c. Error because s1+s2 will result into string and no string has substr() function.
d. Run-time error.

Show Answer

The correct option is (b)
Explanation:
string is class in C++, therefore when we do (s1+s2) a temporary object is created which stores the result of s1+s2 and then that object calls the function substr() and as that is an object of string class hence substr is a callable function for that temporary string object.

11. What will be the output of the following C++ code?

#include
#include
using namespace std;
class A
{
static int x;
public:
void show()
{
x++;
cout<<“x: “< }
};
int A::x = 5;
int main(int argc, char const *argv[])
{
A x;
return 0;
}

a. Segmentation fault.
b. No output
c. Error as a private member a is referenced outside the class.
d. Program compiles successfully but gives run-time error.

Show Answer

The correct option is (b)
Explanation:
As every static member must be initialized and we have initialized variable ‘x’ so no run time error. Also as variable ‘x’ is a static member and is referenced using the class for initialization therefore no compiler error.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise