InterviewSolution
Saved Bookmarks
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Predict 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;}(A) Compiler Error(B)1020(C)2020(D)1010 |
| Answer» | |
| 2. |
Output 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;}(A) fun(int) called(B) fun(Test 2) called(C) Compiler Error: Ambiguous call to fun() |
| Answer» | |
| 3. |
Predict 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;}(A)new calledConstructor calleddelete calledDestructor called(B)new calledConstructor calledDestructor calleddelete called(C)Constructor callednew calledDestructor calleddelete called(D)Constructor callednew calleddelete calledDestructor called |
| Answer» None | |
| 4. |
#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;}(A) x = 3, y = 2(B) Compiler Error(C) x = 2, y = 3 |
| Answer» None | |
| 5. |
Which 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 )(E) All of the above |
| Answer» | |
| 6. |
Which of the following operator functions cannot be global, i.e., must be a member function.(A) new(B) delete(C) Conversion Operator(D) All of the above |
| Answer» None | |
| 7. |
Which of the following operators should be preferred to overload as a global function rather than a member method?(A) Postfix ++(B) Comparison Operator(C) Insertion Operator <<(D) Prefix++ |
| Answer» None | |
| 8. |
Which 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 ( = ) (A) Both 1 and 2(B) Only 1(C) Only 2(D) None of the two |
| Answer» None | |
| 9. |
How can we restrict dynamic allocation of objects of a class using new?(A) By overloading new operator(B) By making an empty private new operator.(C) By making an empty private new and new[] operators(D) By overloading new operator and new[] operators |
| Answer» None | |