Curioustab
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
References Questions
C++ reference pre-decrement through an alias: what is printed? #include
int main() { int x = 80; int &y = x; x++; cout << x << " " << --y; return 0; }
C++ object slicing vs polymorphism: after assigning a Derived to a Base reference, what does Display print? #include
class CuriousTabBase { int x; public: CuriousTabBase(int xx = 0) { x = xx; } void Display() { cout << x; } }; class CuriousTabDerived : public CuriousTabBase { int y; public: CuriousTabDerived(int yy = 0) { y = yy; } void Display() { cout << y; } }; int main() { CuriousTabBase objBase(10); CuriousTabBase &objRef = objBase; CuriousTabDerived objDev(20); objRef = objDev; // slicing assignment copies base subobject objDev.Display(); return 0; }
C++ references passed twice (same object): what will the method print when both parameters alias the same int? #include
class CuriousTab { int x, y; public: void SetValue(int &xx, int &yy) { x = xx++; y = yy; cout << xx << " " << yy; } }; int main() { int x = 10; int &y = x; CuriousTab obj; obj.SetValue(x, y); return 0; }
C++ globals and references: follow constructor prints and later mutations—what is the final console output? #include
int x, y; class CuriousTabTest { public: CuriousTabTest(int xx = 0, int yy = 0) { x = xx; y = yy; Display(); } void Display() { cout << x << " " << y << " "; } }; int main() { CuriousTabTest obj(10, 20); // prints once in ctor int &rx = x; int &ry = y; ry = x; // y = 10 rx = y; // x = 10 cout << rx--; // prints 10 then decrements x to 9 return 0; }
C++ with enum and references: is this code valid, and if so what is printed? #include
enum xyz { a, b, c }; int main() { int x = a, y = b, z = c; int &p = x, &q = y, &r = z; p = ++x; q = ++y; r = ++c; // attempt to increment an enum constant cout << p << q << r; return 0; }
C++ reference to struct object: as both names alias the same
, how do prints change before and after mutation? #include
struct Tab { short n; }; int main() { Tab b; Tab &rb = b; b.n = 5; cout << b.n << " " << rb.n << " "; rb.n = 8; cout << b.n << " " << rb.n; return 0; }
In C++ (old-style headers shown), analyze operator overloading, reference binding, and assignment through a reference. What will the following program print? #include
class CuriousTab { int x, y; public: CuriousTab(int xx = 0, int yy = 0) { x = xx; y = yy; } void Display() { cout << x << " " << y; } CuriousTab operator +(CuriousTab z) { CuriousTab objTemp; objTemp.x = x + z.x; objTemp.y = y + z.y; return objTemp; } }; int main() { CuriousTab objCuriousTab1(90, 80); CuriousTab objCuriousTab2(10, 20); CuriousTab objSum; CuriousTab &objRef = objSum; objRef = objCuriousTab1 + objCuriousTab2; objRef.Display(); return 0; }
In C++ (references and post-increment), evaluate the following code and determine the final printed values of m and n. Explain how the references x and y affect the updates. #include
int main() { int m = 2, n = 6; int &x = m; int &y = n; m = x++; x = m++; n = y++; y = n++; cout << m << " " << n; return 0; }
In C++ with global ints i and j, a constructor writes to both and prints j. Then main uses references and mixed pre/post operations. What exactly is printed by this program (consider the constructor's output too)? #include
int i, j; class CuriousTab { public: CuriousTab(int x = 0, int y = 0) { i = x; j = x; Display(); } void Display() { cout << j << " "; } }; int main() { CuriousTab objCuriousTab(10, 20); int &s = i; int &z = j; i++; cout << s-- << " " << ++z; return 0; }
In C++ (passing references to a constructor), determine the program's output. The constructor takes two int& parameters and prints the stored values. #include
class CuriousTab { int x, y; public: CuriousTab(int &xx, int &yy) { x = xx; y = yy; Display(); } void Display() { cout << x << " " << y; } }; int main() { int x1 = 10; int &p = x1; int y1 = 20; int &q = y1; CuriousTab objCuriousTab(p, q); return 0; }
In modern C++ (iostream), evaluate references bound to the same int and an enum-to-int assignment. What does this program print and why? #include
enum curioustab { a = 1, b, c }; int main() { int x = c; // x = 3 int &y = x; int &z = x; y = b; // x becomes 2 std::cout << z--; // print then decrement return 0; }
In C++ (aliases to the same actual parameter), a member function takes two int& parameters and mutates the first. What output is printed when the same variable is passed twice? #include
class CuriousTab { int x, y; public: void SetValue(int &a, int &b) { a = 100; // mutates caller x = a; y = b; Display(); } void Display() { cout << x << " " << y; } }; int main() { int x = 10; CuriousTab objCuriousTab; objCuriousTab.SetValue(x, x); // both refs alias the same variable return 0; }
In C++ (references to arrays), identify the correctness of binding a reference to an array and the subsequent prints. What happens with the following code? #include
int main() { int arr[] = {1, 2, 3, 4, 5}; int &zarr = arr; // attempt to bind an int& to an int[5] for (int i = 0; i <= 4; i++) { arr[i] += arr[i]; } for (i = 0; i <= 4; i++) cout << zarr[i]; return 0; }
In C++ (post-increment on a referenced argument), both parameters alias the same variable. Predict the printed pair. #include
class CuriousTab { int x, y; public: void SetValue(int &xx, int &yy) { x = xx++; y = yy; Display(); } void Display() { cout << x << " " << y; } }; int main() { int x = 10; int &y = x; CuriousTab objCuriousTab; objCuriousTab.SetValue(x, y); return 0; }
In C++ (reference declaration syntax), spot the error and determine the outcome. What happens when compiling the following code? #include
int main() { int x = 80; int y& = x; // attempt to declare a reference x++; cout << x << " " << --y; return 0; }
In C++ (evaluation of function arguments with aliasing and a pre-increment), determine the printed triple. Consider potential aliasing effects between x and y (where y is a reference to x). #include
class CuriousTab { int a, b, c; public: void SetValue(int x, int y, int z) { a = x; b = y; c = z; } void Display() { cout << a << " " << b << " " << c; } }; int main() { CuriousTab objCuriousTab; int x = 2; int &y = x; y = 5; objCuriousTab.SetValue(x, ++y, x + y); objCuriousTab.Display(); return 0; }
In C++ (old-style headers, iostream.h), analyze pointer and reference increments and predict the output. #include
int main() { int x = 10, y = 20; int *ptr = &x; int &ref = y; *ptr++; // increments ptr (postfix), dereferenced value discarded; x unchanged ref++; // increments y cout << x << " " << y; return 0; }
In C++, a constructor taking int& parameters increments the arguments: what is printed? #include
class CuriousTabTest { public: CuriousTabTest(int &x, int &y) { x++; y++; } }; int main() { int a = 10, b = 20; CuriousTabTest objBT(a, b); cout << a << " " << b; return 0; }
C++ references and side effects inside a while loop: trace the output step by step. #include
int main() { int x = 0; int &y = x; y = 5; while (x <= 5) { cout << y++ << " "; x++; } cout << x; return 0; }
C++ reference aliasing and pre-decrement: what exact numbers are printed? #include
int main() { int x = 10; int &y = x; x = 25; y = 50; // x now 50 as well cout << x << " " << --y; // print x, then pre-decrement y (and thus x) return 0; }
1
2
3