C++ Operator Overloading Aptitude Questions and Answers

C++ Operator Overloading Aptitude: This section contains C++ Operator Overloading Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on March 01, 2021

1) There are the following statements that are given below, which of them are correct about operator overloading in C++?

  1. Operator overloading is used to redefine existing operators in C++.
  2. Operator overloading is a type of compile-time polymorphism.
  3. Operator overloading is a type of runtime polymorphism.
  4. We can overload all existing operators in C++.

Options:

  1. A and B
  2. A and C
  3. A, B, and D
  4. A, C, and D

2) Which of the following operators cannot be overloaded in C++?

  1. ?:
  2. << 
  3. sizeof()

Options:

  1. A and B
  2. A and D
  3. A, C, and D
  4. A, B, and C

3) Which of the following keyword is used to overload operators in C++?

  1. overload
  2. operator
  3. operate
  4. op

4) How can we overload an operator in C++?

  1. By defining a user-defined structure.
  2. By defining a user-defined function with the specified operator.
  3. By defining multiple functions with the same name.
  4. None of the above

5) Which of the following is the correct syntax for operator overloading in C++?

  1. <return_type> <class_name>:: operator <op>(argument lists){}
  2. <class_name>:: <return_type> operator <op>(argument lists) {}
  3. <return_type> operator <return_type> operator <op>(argument lists) <op>(argument lists){}
  4. None of the above

6) Can we overload the "new" operator in C++?

  1. Yes
  2. No

7) Can we use friend function to overload operators in C++?

  1. Yes
  2. No

8) The binary operator that overloaded through a member function takes only one explicit argument?

  1. Yes
  2. No

9) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
private:
    int number;

public:
    Sample()
    {
        number = 10;
    }
    void operator++()
    {
        number = number + 1;
    }
    void Display()
    {
        cout << number << endl;
    }
};

int main()
{
    Sample S;

    S++;
    S++;

    S.Display();

    return 0;
}

Options:

  1. 12
  2. 10
  3. Syntax error
  4. Garbage value

10) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
private:
    int number;

public:
    Sample()
    {
        number = 10;
    }
    void operator++()
    {
        number = number + 1;
    }
    void Display()
    {
        cout << number << endl;
    }
};

int main()
{
    Sample S;

    ++S;
    ++S;

    S.Display();

    return 0;
}

Options:

  1. 12
  2. 10
  3. Syntax error
  4. Garbage value

11) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
private:
    int number;

public:
    Sample()
    {
        number = 10;
    }
    void operator++(int)
    {
        number = number + 1;
    }
    void Display()
    {
        cout << number << endl;
    }
};

int main()
{
    Sample S;

    S++;
    S++;

    S.Display();

    return 0;
}

Options:

  1. 12
  2. 10
  3. Syntax error
  4. Garbage value

12) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
private:
    int number;

public:
    Sample()
    {
        number = 0;
    }
    Sample(int n)
    {
        number = n;
    }
    Sample operator+(Sample S)
    {
        Sample temp;

        temp.number = number + S.number;

        return temp;
    }
    void Display()
    {
        cout << number << endl;
    }
};

int main()
{
    Sample S1(10);
    Sample S2(20);
    Sample S3;

    S3 = S1 + S2;

    S2.Display();

    return 0;
}

Options:

  1. 10
  2. 20
  3. 30
  4. Syntax error

13) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
private:
    int number;

public:
    Sample()
    {
        number = 0;
    }
    Sample(int n)
    {
        number = n;
    }

    friend Sample operator+(Sample S1, Sample S2);

    void Display()
    {
        cout << number << endl;
    }
};

friend Sample operator+(Sample S1, Sample S2)
{
    Sample temp;

    temp.number = S1.number + S2.number;

    return temp;
}

int main()
{
    Sample S1(10);
    Sample S2(20);
    Sample S3;

    S3 = S1 + S2;

    S3.Display();

    return 0;
}

Options:

  1. 10
  2. 20
  3. 30
  4. Syntax error

14) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
private:
    int number;

public:
    Sample()
    {
        number = 0;
    }
    Sample(int n)
    {
        number = n;
    }

    friend Sample operator+(Sample S1, Sample S2);

    void Display()
    {
        cout << number << endl;
    }
};

Sample operator+(Sample S1, Sample S2)
{
    Sample temp;

    temp.number = S1.number + S2.number;

    return temp;
}

int main()
{
    Sample S1(10);
    Sample S2(20);
    Sample S3;

    S3 = S1 + S2;

    S3.Display();

    return 0;
}

Options:

  1. 10
  2. 20
  3. 30
  4. Synatx error

15) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
private:
    int number;

public:
    Sample()
    {
        number = 0;
    }
    Sample(int n)
    {
        number = n;
    }

    friend Sample operator++(Sample S, int);

    void Display()
    {
        cout << number << endl;
    }
};

Sample operator++(Sample S, int)
{
    S.number = S.number + 1;
    return S;
}

int main()
{
    Sample S1(10);

    S1++;

    S1.Display();

    return 0;
}

Options:

  1. 10
  2. 11
  3. Garbage value
  4. Syntax error

16) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
private:
    int number;

public:
    Sample()
    {
        number = 0;
    }
    Sample(int n)
    {
        number = n;
    }

    friend Sample operator++(Sample S);

    void Display()
    {
        cout << number << endl;
    }
};

Sample operator++(Sample S)
{
    S.number = S.number + 1;
    return S;
}

int main()
{
    Sample S1(10);

    ++S1;

    S1.Display();

    return 0;
}

Options:

  1. 10
  2. 11
  3. Garbage value
  4. Syntax error

17) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
private:
    float num1, num2;

public:
    Sample(float n1, float n2)
    {
        num1 = n1;
        num2 = n2;
    }

    operator float() const
    {
        return float(num1) / float(num2);
    }
};

int main()
{
    Sample S(11.0, 2.0);

    float num = S;

    cout << num << endl;

    return 0;
}

Options:

  1. 5.5
  2. Garbage value
  3. Syntax error
  4. Runtime error

18) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
private:
    int num1, num2;

public:
    Sample(int n1, int n2)
    {
        num1 = n1;
        num2 = n2;
    }

    operator int()
    {
        return int(num1) / int(num2);
    }
};

int main()
{
    Sample S(11.0, 2.0);

    int num = S;

    cout << num << endl;

    return 0;
}

Options:

  1. 5.5
  2. 5
  3. Garbage value
  4. Syntax error

19) Can we overload membership operator (.) in C++?

  1. Yes
  2. No

20) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
private:
    int number;

public:
    Sample()
    {
        number = 0;
    }
    Sample(int n)
    {
        number = n;
    }

    friend Sample operator ::(Sample S1, Sample S2);

    void Display()
    {
        cout << number << endl;
    }
};

Sample operator ::(Sample S1, Sample S2);
{
    Sample S;

    S.number = S1.number + S2.number;
    return S;
}

int main()
{
    Sample S1(10);
    Sample S2(20);
    Sample S3;

    S3 = S1::S2

             S1.Display();

    return 0;
}

Options:

  1. 30
  2. Garbage value
  3. Syntax error
  4. Runtime error





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.