Quiz
Operator Overloading
Este quiz tem tempo limite, por isso abre no player de sempre.
Perguntas do quiz (11)
1How can we restrict dynamic allocation of objects of a class using new?
- ABy overloading new operator
- BBy making an empty private new operator.
- CBy making an empty private new and new[] operators
- DBy overloading new operator and new[] operators
2Which of the following operators cannot be overloaded
- A(Member Access or Dot operator)
- B?: (Ternary or Conditional Operator )
- C:: (Scope Resolution Operator)
- D* (Pointer-to-member Operator )
- EAll of the above
3Which of the following operators are overloaded by default by the compiler in every user defined classes even if user has not written?
1) Comparison Operator ( == )
2) Assignment Operator ( = )- ABoth 1 and 2
- Bonly 1
- Conly 2
- Dnone of the two
4Which of the following operators should be preferred to overload as a global function rather than a member method
- APrefix++
- BComparison Operator
- CPostfix ++
- DInsertion Operator <<
5How does C++ compiler differs between overloaded postfix and prefix operators?
- AC++ doesn't allow both operators to be overlaoded in a class
- BA postfix ++ has a dummy parameter
- CA prefix ++ has a dummy parameter
- DBy making prefix ++ as a global function and postfix as a member function.
6Predict the output
#include<iostream>
using namespace std;
class A
{
int i;
public:
A(int ii = 0) : i(ii) {}
void show() { cout << i << endl; }
};
class B
{
int x;
public:
B(int xx) : x(xx) {}
operator A() const { return A(x); }
};
void g(A a)
{
a.show();
}
int main()
{
B b(10);
g(b);
g(20);
return 0;
}- ACompiler Error
- B10
20 - C10
10 - D20
20
7Output of following program?
#include <iostream>
using namespace std;
class Test2
{
int y;
};
class Test
{
int x;
Test2 t2;
public:
operator Test2 () { return t2; }
operator int () { return x; }
};
void fun ( int x) { cout << "fun(int) called"; }
void fun ( Test2 t ) { cout << "fun(Test 2) called"; }
int main()
{
Test t;
fun(t);
return 0;
}- Afun(int) called
- Bfun(Test 2) called
- CCompiler Error: Ambiguous call to fun()
8Predict the output?
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;
class Test {
int x;
public:
void* operator new(size_t size);
void operator delete(void*);
Test(int i) {
x = i;
cout << "Constructor called n";
}
~Test() { cout << "Destructor called n"; }
};
void* Test::operator new(size_t size)
{
void *storage = malloc(size);
cout << "new called n";
return storage;
}
void Test::operator delete(void *p )
{
cout<<"delete called n";
free(p);
}
int main()
{
Test *m = new Test(5);
delete m;
return 0;
}- Anew called
Constructor called
delete called
Destructor called - Bnew called
Constructor called
Destructor called
delete called - CConstructor called
new called
Destructor called
delete called - DConstructor called
new called
delete called
Destructor called
- Anew called
9#include<iostream>
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;
}- Ax = 3, y = 2
- BCompiler Error
- Cx = 2, y = 3
10Which of the following operator functions cannot be global, i.e., must be a member function
- Anew
- Bdelete
- Cconversion operator
- Dall the above
11Which of the following operator(s) cannot be overloaded?
- A. (Member Access or Dot operator)
- B?: (Ternary or Conditional Operator )
- C:: (Scope Resolution Operator)
- Dall of the above
Informações
- Descrição
- C++ Quiz sobre Operator Overloading, criado por PathaPadha Support em 28-10-2020.
- Autor
- PathaPadha Support
- Idioma
- English
- Visualizações
- 91
- Etiquetas
- Avaliação
- Ainda sem avaliações Avalie este quiz