C++ Programming Questions and Answers – Operator Functions

This section on C++ questions and puzzles focuses on “Operator Functions”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions and puzzles on “Operator Functions” along with answers, explanations and/or solutions:

1. Pick the other name of operator function.
a) function overloading
b) operator overloading
c) member overloading
d) None of the mentioned
View Answer

Answer:b
Explanation:None.

2. Which of the following operators can’t be overloaded?
a) ::
b) +
c) -
d) []
View Answer

Answer:a
Explanation:None.

3. How to declare operator function?
a) operator operator sign
b) operator
c) operator sign
d) None of the mentioned
View Answer

Answer:a
Explanation:We have to declare the operator function by using operator, operator sign. Example “operator +” where operator is a keyword and + is the symbol need to be overloaded.

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample 
  4.     {
  5.         public:
  6.         int x, y;
  7.         sample() {};
  8.         sample(int, int);
  9.         sample operator + (sample);
  10.     };
  11.     sample::sample (int a, int b) 
  12.     {
  13.         x = a;
  14.         y = b;
  15.     }
  16.     sample sample::operator+ (sample param) 
  17.     {
  18.         sample temp;
  19.         temp.x = x + param.x;
  20.         temp.y = y + param.y;
  21.         return (temp);
  22.     }
  23.     int main () 
  24.     {
  25.         sample a (4,1);
  26.         sample b (3,2);
  27.         sample c;
  28.         c = a + b;
  29.         cout << c.x << "," << c.y;
  30.         return 0;
  31.     }

a) 5, 5
b) 7, 3
c) 3, 7
d) None of the mentioned
View Answer

Answer:b
Explanation:In this program, we are adding the first number of a with first number of b by using opertor function and also we are adding second number by this method also.
Output:
$ g++ oper.cpp
$ a.out
7, 3

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Box
  4.     {   
  5.         double length;
  6.         double breadth;
  7.         double height;
  8.         public:
  9.         double getVolume(void)
  10.         {  
  11.             return length * breadth * height;
  12.         }
  13.         void setLength( double len )
  14.         {   
  15.             length = len;
  16.         }
  17.         void setBreadth( double bre )
  18.         {   
  19.             breadth = bre;
  20.         }
  21.         void setHeight( double hei )
  22.         {   
  23.             height = hei;
  24.         }
  25.         Box operator+(const Box& b)
  26.         {  
  27.             Box box;
  28.             box.length = this->length + b.length;
  29.             box.breadth = this->breadth + b.breadth;
  30.             box.height = this->height + b.height;
  31.             return box;
  32.         } 
  33.     };
  34.     int main( )
  35.     {  
  36.         Box Box1;
  37.         Box Box2;
  38.         Box Box3;
  39.         double volume = 0.0;
  40.         Box1.setLength(6.0);
  41.         Box1.setBreadth(7.0);
  42.         Box1.setHeight(5.0);
  43.         Box2.setLength(12.0);
  44.         Box2.setBreadth(13.0);
  45.         Box2.setHeight(10.0);
  46.         volume = Box1.getVolume();
  47.         cout << "Volume of Box1 : " << volume <<endl;
  48.         volume = Box2.getVolume();
  49.         cout << "Volume of Box2 : " << volume <<endl;
  50.         Box3 = Box1 + Box2;
  51.         volume = Box3.getVolume();
  52.         cout << "Volume of Box3 : " << volume <<endl;
  53.         return 0;
  54.     }

a) Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
b) Volume of Box1 : 200
Volume of Box2 : 1560
Volume of Box3 : 5400
c) Volume of Box1 : 210
Volume of Box2 : 1550
Volume of Box3 : 5400
d) None of the mentioned
View Answer

Answer:a
Explanation:In this program, we finding the box3 area by adding box1 and box2.
Output:
$ g++ oper1.cpp
$ a.out
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Integer 
  4.     {
  5.         int i;
  6.         public:
  7.         Integer(int ii) : i(ii) {}
  8.         const Integer
  9.         operator+(const Integer& rv) const 
  10.         {
  11.             cout << "operator+" << endl;
  12.             return Integer(i + rv.i);
  13.         }
  14.         Integer&
  15.         operator+=(const Integer& rv) 
  16.         {
  17.             cout << "operator+=" << endl;
  18.             i += rv.i;
  19.             return *this;
  20.         }
  21.     };
  22.     int main() 
  23.     {
  24.         int i = 1, j = 2, k = 3;
  25.         k += i + j;
  26.         Integer ii(1), jj(2), kk(3);
  27.         kk += ii + jj;
  28.     }

a) operator+
operator+=
b) operator+=
operator+
c) operator+
operator+
d) None of the mentioned
View Answer

Answer:a
Explanation:We are using two operator functions and executing them and result is printed according to the order.
Output:
$ g++ oper2.cpp
$ a.out
operator+
operator+=

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class myclass
  4.     {
  5.         public:
  6.         int i;
  7.         myclass *operator->()
  8.         {return this;}
  9.     };
  10.     int main()
  11.     {
  12.         myclass ob;
  13.         ob->i = 10; 
  14.         cout << ob.i << " " << ob->i;
  15.         return 0;
  16.     }

a) 10 10
b) 11 11
c) error
d) runtime error
View Answer

Answer:a
Explanation:In this program, -> operator is used to describe the member of the class and so we are getting this output.
Output:
$ g++ char4.cpp
$ a.out
10 10

8. Which of the following statements is NOT valid about operator overloading?
a) Only existing operators can be overloaded.
b) Overloaded operator must have at least one operand of its class type.
c) The overloaded operators follow the syntax rules of the original operator.
d) None of the mentioned
View Answer

Answer:d
Explanation:None.

9. Operator overloading is
a) making c++ operator works with objects
b) giving new meaning to existing operator
c) making new operator
d) both a & b
View Answer

Answer:d
Explanation:Operator overloading is the way adding operation to the existing operators.

10. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     ostream & operator<<(ostream & i, int n)
  4.     {
  5.         return i;
  6.     }
  7.     int main()
  8.     {
  9.         cout << 5 << endl;
  10.         cin.get();
  11.         return 0;
  12.     }

a) 5
b) 6
c) error
d) runtime error
View Answer

Answer:c
Explanation:In this program, there will arise an ambiguous overload for 5.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

Here’s the list of Best Reference Books in C++ Programming Language.

To practice all features of C++ programming language, here is complete set on 1000+ Multiple Choice Questions and Answers on C++.

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







1000 C++ MCQs | Quiz

Types
Booleans
Character Types
Integer Types
Floating Point Types
Sizes
Void
Enumerations
Declaration
Pointers
Arrays
Pointers into Arrays
Constants
References
Pointer to Void
Structures
Operators
Statements
Comments and Indentation
Function Declarations
Argument Passing
Value Return
Overloaded Function Names
Default Arguments
Unspecified Number of Arguments
Pointer to Function
Macros
Modularization and Interfaces
Namespaces
Exceptions
Linkage
Header Files Usage
Classes
User Defined Types
Objects
Operator Functions
Complex Number Type
Conversion Operators
Friends
Large Objects
Essential Operators
Subscripting
Function Call
Dereferencing
Increment and Decrement
String Class
Derived Classes
Abstract Classes
Design of Class Hierarchies
Class Hierarchies & Abstract Classes
Simple String Template
Function Templates
Template Arguments for Policy Usage
Specialization
Derivation and Templates
Error Handling
Grouping of Exceptions
Catching Exceptions
Resource Management
Exceptions That Are Not Errors
Exception Specifications
Uncaught Exceptions
Exceptions and Efficiency
Error Handling Alternatives
Standard Exceptions
Class Hierarchies Introduction
Multiple Inheritance
Access Control
Run Time Type Information
Pointers to Members
Free Store
Standard Library Design
Container Design
Vector
Sequences
Sequence Adapters
Associative Containers
Almost Containers
Defining a New Container
Standard Library Algorithms
Sequences and Containers
Function Objects
Nonmodifying Sequence Algorithms
Modifying Sequence Algorithms
Stored Sequences
Heaps
Min and Max
Permutations
C Style Algorithms
Iterators and Sequences
Checked Iterators
Allocators
String Characters
Basic String
C Standard Library
Output Stream
Input Stream
Formatting
File Streams and String Streams
Buffering
Locale
C Input Output
Numeric Limits
Standard Mathematical Functions
Vector Arithmetic
Generalized Numeric Algorithms
Random Numbers
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry