Overloading In C++ Mcq Exercise 5 - C++ Programming Tutorial
C++ Course / MCQ & Exercises / Overloading In C++ Mcq Exercise 5

Overloading In C++ Mcq Exercise 5

BLUF: Mastering Overloading In C++ Mcq Exercise 5 is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Overloading In C++ Mcq Exercise 5

C++ is renowned for its efficiency. Learn how Overloading In C++ Mcq Exercise 5 enables low-level control and high-performance computing in the tutorial below.

What is the expected result when running the code snippet below?

Example

#include<iostream>

using namespace std;

class Complex {

public:

    int real, imag;

    Complex(int r, int i): real(r), imag(i) {}

    Complex operator+(const Complex &c) {

        return Complex(real + c.real, imag + c.imag);

    }

};

int main()

{

    Complex c1(3, 4), c2(1, 2);

    Complex c3 = c1 + c2;

cout<<c3.real<<"+"<<c3.imag<<"i"<<endl;

    return 0;

}
  • 4 + 6i
  • 3 + 2i
  • 4 + 4i
  • 4 + 3i

Explanation:

The accurate solution is choice (a). The addition operator is redefined to combine the actual and imaginary components of two Complex objects. This operation yields a fresh Complex object containing a real value of 4 and an imaginary value of 6.

  1. What is the output of the code snippet below?
Example

#include<iostream>

using namespace std;

void func(int a,int b=0)

{

cout<<"int,int:"<<a<<","<<b<<endl;

}

void func(double a,double b=1.1)

{

cout<<"double,double:"<<a<<","<<b<<endl;

}

int main()

{

    func(2.5);

    return 0;

}
  • double, double: 2.5, 1.1
  • int, int: 2, 0
  • Compilation error
  • Runtime error

Explanation:

The accurate solution is choice (a). The func(double, double) overload is invoked with a sole argument 2.5. Subsequently, the default value 1.1 is employed for the second parameter.

  1. Which operator is not allowed to be overloaded in C++?

Explanation:

The accurate choice is alternative (c). Logical operators && and || are not capable of being overloaded. Conversely, the = operator and function call operator are overloadable.

  1. What is the expected output of the provided code snippet?
Example

#include <iostream>

using namespace std;

void print(int a) {

    cout << "Integer: " << a << endl;

}

void print(double a) {

    cout << "Double: " << a << endl;

}

int main() {

    print(10);

    print(10.5);

    return 0;

}
  • Integer: 10 \n Double: 10.5
  • Integer: 10 \n Integer: 10
  • Double: 10 \n Double: 10.5
  • Double: 10 \n Integer: 10.5

Explanation:

The accurate choice is option(a). The print method is overloaded for integer and decimal data types. If print(10) is executed, it triggers the integer variant, while executing print(10.5) invokes the double variant of the function.

  1. What output can be expected from the code snippet below?
Example

#include<iostream>

using namespace std;

class Integer {

    int value;

public:

    Integer(int v): value(v) {}

    Integer operator++(int) {

        Integer temp = *this;

        ++value;

        return temp;

    }

    void print() {

        cout << value << endl;

    }

};

int main()

{

    Integer i(5);

    i++;

    i.print();

    return 0;

}
  • Compilation error
  • Undefined behavior

Explanation:

The correct answer is option (a). The operator++(int) is the postfix increment operator. It returns the value before incrementing. After i++, i is incremented to 6 and then printed.

  1. Which of the following will be the right way for overloading << operator for user-defined class?
  • As a member function of the class.
  • As a friend function of the class.
  • As a static member function of the class.
  • Both b) and c).

Explanation:

The appropriate choice is alternative (b). The << operator utilized for output streaming necessitates access to the ostream object, which is not a part of the class. Therefore, it must be overloaded as a friend function.

  1. What is the expected result of the given code snippet?
Example

#include <iostream>

using namespace std;

class Integer {

    int value;

public:

    Integer(int v): value(v) {}

    friend ostream& operator<<(ostream &out, const Integer &i);

};

ostream& operator<<(ostream &out, const Integer &i) {

    out << i.value;

    return out;

}

int main()

{

    Integer i(10);

    cout << i << endl;

    return 0;

}
  • Compilation error
  • Runtime error
  • Undefined behavior

Explanation:

The correct answer is option (a). The << operator is correctly overloaded as a friend function. It prints the value of the Integer object.

  1. Which of the following is not a valid reason for feature overloading?
  • To perform comparable operations on one-of-a-kind sorts of data.
  • To avoid the use of the identical name for one-of-a-kind capabilities.
  • To enhance code clarity and maintainability.
  • To enhance overall performance through the usage of different algorithms for distinct data types.

Explanation:

The accurate choice is alternative (b). Function overloading is commonly employed to perform similar tasks on various data types, improving code readability and sustainability by not duplicating the same function call for distinct functionalities.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience