{"subject": "C++ Programming", "topic": "Class and Object", "question": "What does the this pointer in C++ represent?", "options": ["Pointer to class name", "Pointer to base class", "Pointer to constructor"], "correct": 2, "explanation": "this is an implicit pointer that holds the address of the object that invoked the member function.", "source_url": "https://www.geeksforgeeks.org/quizzes/class-and-object-gq/"}
{"subject": "C++ Programming", "topic": "Class and Object", "question": "Assume that an integer and a pointer each takes 4 bytes. Also, assume that there is no alignment in objects. Predict the output following program. CPP #include&amp;lt;iostream&amp;gt; using namespace std; class Test { static int x; int *ptr; int y; }; int main() { Test t; cout &amp;lt;&amp;lt; sizeof(t) &amp;lt;&amp;lt; &amp;quot; &amp;quot;; cout &amp;lt;&amp;lt; sizeof(Test *); }", "options": ["12 4", "12 12", "8 8"], "correct": 3, "explanation": "For a compiler where pointers take 4 bytes, the statement \"sizeof(Test *)\" returns 4 (size of the pointer ptr). The statement \"sizeof(t)\" returns 8. Since static is not associated with each object of the class, we get (8 not 12).", "source_url": "https://www.geeksforgeeks.org/quizzes/class-and-object-gq/"}
{"subject": "C++ Programming", "topic": "Class and Object", "question": "What is the correct way to dynamically create an object in C++?", "options": ["ClassName obj = new ClassName();", "ClassName obj();", "ClassName = new object();"], "correct": 2, "explanation": "new returns a pointer. ClassName *obj = new ClassName(); dynamically allocates memory.", "source_url": "https://www.geeksforgeeks.org/quizzes/class-and-object-gq/"}
{"subject": "C++ Programming", "topic": "Class and Object", "question": "Which access specifier allows members to be accessed only within the class?", "options": ["public", "protected", "none of them"], "correct": 3, "explanation": "private restricts access strictly to the class itself.", "source_url": "https://www.geeksforgeeks.org/quizzes/class-and-object-gq/"}
{"subject": "C++ Programming", "topic": "Class and Object", "question": "What will be the size of an empty class in C++?", "options": ["0", "4", "compile time error"], "correct": 2, "explanation": "Even an empty class occupies 1 byte to ensure unique object addresses.", "source_url": "https://www.geeksforgeeks.org/quizzes/class-and-object-gq/"}
{"subject": "C++ Programming", "topic": "Class and Object", "question": "A member function can always access the data in __________ , (in C++).", "options": ["the object of which it is a member", "the public part of its class", "the private part of its class"], "correct": 1, "explanation": "A member function can access it's class member variables, irrespective of the access specifier in which the member variable is declared.So, a member function can always access the data in the class of which it is a member. So, option (A) is correct.", "source_url": "https://www.geeksforgeeks.org/quizzes/class-and-object-gq/"}
{"subject": "C++ Programming", "topic": "Class and Object", "question": "What will be the output? C++ class Test { int x; }; int main() { Test t; cout &amp;lt;&amp;lt; t.x; return 0; }", "options": ["0", "Garbage Value", "None of the Above"], "correct": 3, "explanation": "In C++, the default access is private. Since x is a private member of Test, it is compiler error to access it outside the class.", "source_url": "https://www.geeksforgeeks.org/quizzes/class-and-object-gq/"}
{"subject": "C++ Programming", "topic": "C++ const keyword", "question": "Predict the output of following program CPP #include &amp;lt;iostream&amp;gt; using namespace std; int main() { const char* p = &amp;quot;12345&amp;quot;; const char **q = &amp;amp;p; *q = &amp;quot;abcde&amp;quot;; const char *s = ++p; p = &amp;quot;XYZWVU&amp;quot;; cout &amp;lt;&amp;lt; *++s; return 0; }", "options": ["Compiler Error", "b", "Garbage Value"], "correct": 2, "explanation": "Output is &lsquo;c&rsquo; const char* p = &ldquo;12345&Prime; declares a pointer to a constant. So we can&rsquo;t assign something else to *p, but we can assign new value to p. const char **q = &amp;p; declares a pointer to a pointer. We can&rsquo;t assign something else to **q, but we can assign new values to q and *q. *q = &ldquo;abcde&rdquo;; changes p to point to &ldquo;abcde&rdquo; const char *s = ++p; assigns address of literal &rdquo;bcde&rdquo; to s. Again *s can&rsquo;t be assigned a new value, but s can be changed. The statement printf(&ldquo;%cn&rdquo;, *++s) changes s to &ldquo;cde&rdquo; and first character at s is printed.", "source_url": "https://www.geeksforgeeks.org/quizzes/const-keyword-gq/"}
{"subject": "C++ Programming", "topic": "C++ const keyword", "question": "In C++, const qualifier can be applied to 1) Member functions of a class 2) Function arguments 3) To a class data member which is declared as static 4) Reference variables", "options": ["Only 1, 2 and 3", "Only 1, 2 and 4", "Only 1, 3 and 4"], "correct": 3, "explanation": "When a function is declared as const, it cannot modify data members of its class. When we don't want to modify an argument and pass it as reference or pointer, we use const qualifier so that the argument is not accidentally modified in function. Class data members can be declared as both const and static for class wide constants. Reference variables can be const when they refer a const location.", "source_url": "https://www.geeksforgeeks.org/quizzes/const-keyword-gq/"}
{"subject": "C++ Programming", "topic": "C++ const keyword", "question": "C #include &amp;lt;stdio.h&amp;gt; int main() { const int x; x = 10; printf(&amp;quot;%d&amp;quot;, x); return 0; }", "options": ["10", "0", "Runtime Error"], "correct": 1, "explanation": "One cannot change the value of 'const' variable except at the time of initialization. Compiler does check this.", "source_url": "https://www.geeksforgeeks.org/quizzes/const-keyword-gq/"}
{"subject": "C++ Programming", "topic": "C++ Constructors", "question": "Output of following C++ code will be? C++ #include &amp;lt;iostream&amp;gt; using namespace std; class X { public: int x; }; int main() { X a = {10}; X b = a; cout &amp;lt;&amp;lt; a.x &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; b.x; return 0; }", "options": ["Compiler Error", "10 followed by Garbage Value", "10 0"], "correct": 3, "explanation": "The following may look like an error, but it works fine. X a = {10}; Like structures, class objects can be initialized. The line \"X b = a;\" calls copy constructor and is same as \"X b(a);\". Please note that, if we don't write our own copy constructor, then compiler creates a default copy constructor which assigns data members one object to other object.", "source_url": "https://www.geeksforgeeks.org/quizzes/constructors-gq/"}
{"subject": "C++ Programming", "topic": "C++ Constructors", "question": "Predict the output of the following program: C++ #include &amp;lt;iostream&amp;gt; using namespace std; class GfG { public: int val; GfG(): val(11) {} GfG(int x): val(x) {}; GfG(const GfG&amp;amp; other): GfG(other.val) {} }; int main() { GfG gfg1(22); GfG gfg2(gfg1); cout &amp;lt;&amp;lt; gfg2.val; return 0; }", "options": ["0", "Compiler Error", "11"], "correct": 2, "explanation": "The process of calling one constructor from other is called constructor delegation in C++. Its syntax is:constructor1: constructor2 { ... }In the above program, we called the parameterized constructor of GfG class from the copy constructor with the val variable of other object (gfg1 here). So the value of gfg1's val will be copied to gfg2's val, hence the output 22.", "source_url": "https://www.geeksforgeeks.org/quizzes/constructors-gq/"}
{"subject": "C++ Programming", "topic": "C++ Constructors", "question": "Predict the output of the following program: C++ #include &amp;lt;iostream&amp;gt; using namespace std; class GfG { public: int val; GfG(int x = 22) { this-&amp;gt;val = x; } }; int main() { GfG gfg; cout &amp;lt;&amp;lt; gfg.val; return 0; }", "options": ["0", "Runtime Error", "Compiler Error"], "correct": 2, "explanation": "Just like other functions, constructors in C++ can also have default values for their arguments. In case where all the arguments have default values and no argument is provided to the constructor while creating object, the compiler matches the constructor call with all the default values of the arguments, effectively working as non-parameterized constructor.So, in this case, 22 will be used to initialize val and it will be printed.", "source_url": "https://www.geeksforgeeks.org/quizzes/constructors-gq/"}
{"subject": "C++ Programming", "topic": "C++ Constructors", "question": "Implicit return type of a class constructor is:", "options": ["not of class type itself", "a destructor of class type", "a destructor not of class type"], "correct": 2, "explanation": "Truly, constructor doesn't have any return type not even void. However, as the definition of Constructor goes, it is used to initialize an object of the class. So implicitly, they return the current instance of the class whose constructor it is. Therefore, Implicit return type of a class constructor is class type itself. For more information Refer:C++ Classes and Objects Option (B) is correct.", "source_url": "https://www.geeksforgeeks.org/quizzes/constructors-gq/"}
{"subject": "C++ Programming", "topic": "C++ Destructors", "question": "Predict the output of following C++ progran CPP #include &amp;lt;iostream&amp;gt; using namespace std; int i; class A { public: ~A() { i=10; } }; int foo() { i=3; A ob; return i; } int main() { cout &amp;lt;&amp;lt; foo() &amp;lt;&amp;lt; endl; return 0; }", "options": ["0", "10", "None of the above"], "correct": 2, "explanation": "While returning from a function, destructor is the last method to be executed. The destructor for the object &ldquo;ob&rdquo; is called after the value of i is copied to the return value of the function. So, before destructor could change the value of i to 10, the current value of i gets copied &amp; hence the output is i = 3. See this for more details.", "source_url": "https://www.geeksforgeeks.org/quizzes/destructors-gq/"}
{"subject": "C++ Programming", "topic": "C++ Destructors", "question": "CPP #include &amp;lt;iostream&amp;gt; using namespace std; class A { int id; static int count; public: A() { count++; id = count; cout &amp;lt;&amp;lt; &amp;quot;constructor for id &amp;quot; &amp;lt;&amp;lt; id &amp;lt;&amp;lt; endl; } ~A() { cout &amp;lt;&amp;lt; &amp;quot;destructor for id &amp;quot; &amp;lt;&amp;lt; id &amp;lt;&amp;lt; endl; } }; int A::count = 0; int main() { A a[3]; return 0; }", "options": ["constructor for id 1 constructor for id 2 constructor for id 3 destructor for id 1 destructor for id 2 destructor for id 3", "Compiler Dependent.", "constructor for id 1 destructor for id 1"], "correct": 1, "explanation": "In the above program, id is a static variable and it is incremented with every object creation. Object a[0] is created first, but the object a[2] is destroyed first. Objects are always destroyed in reverse order of their creation. The reason for reverse order is, an object created later may use the previously created object. For example, consider the following code snippet. A a; B b(a); In the above code, the object &lsquo;b&rsquo; (which is created after &lsquo;a&rsquo;), may use some members of &lsquo;a&rsquo; internally. So destruction of &lsquo;a&rsquo; before &lsquo;b&rsquo; may create problems. Therefore, object &lsquo;b&rsquo; must be destroyed before &lsquo;a&rsquo;.", "source_url": "https://www.geeksforgeeks.org/quizzes/destructors-gq/"}
{"subject": "C++ Programming", "topic": "C++ Destructors", "question": "What will be the output? C++ #include &amp;lt;iostream&amp;gt; class Test { public: ~Test() { std::cout &amp;lt;&amp;lt; &amp;quot;Destructor called &amp;quot;; } }; int main() { Test t1; }", "options": ["No output", "Compilation error", "Runtime Error"], "correct": 2, "explanation": "The object t1 is a local variable, so its destructor is automatically called at the end of main().In C++, Objects are automatically destroyed once they go out of scope.", "source_url": "https://www.geeksforgeeks.org/quizzes/destructors-gq/"}
{"subject": "C++ Programming", "topic": "C++ Destructors", "question": "What will be the output? C++ #include &amp;lt;iostream&amp;gt; class Sample { public: ~Sample() { std::cout &amp;lt;&amp;lt; &amp;quot;Destructor executed &amp;quot;; } }; void createObject() { Sample s; } int main() { createObject(); std::cout &amp;lt;&amp;lt; &amp;quot;Main done &amp;quot;; }", "options": ["Main done", "Destructor executed", "Main doneDestructor executed"], "correct": 3, "explanation": "The object s is local to the function createObject(), so its destructor runs when the function ends and prints \"Destructor executed\". Then control returns to main(), which then prints \"Main done\".", "source_url": "https://www.geeksforgeeks.org/quizzes/destructors-gq/"}
{"subject": "C++ Programming", "topic": "C++ Exception Handling", "question": "Which of the following is used to open a file for reading in C++?", "options": ["ofstream file(\"data.txt\");", "fstream file(\"data.txt\", ios::out);", "file.open(\"data.txt\", ios::write);"], "correct": 2, "explanation": "ifstream is used to open files in read mode in C++.", "source_url": "https://www.geeksforgeeks.org/quizzes/exception-handling-gq/"}
{"subject": "C++ Programming", "topic": "C++ Exception Handling", "question": "What happens if an exception is thrown but no catch block matches it?", "options": ["Program skips the exception", "It is silently ignored", "&amp;nbsp;Control goes back to the main function"], "correct": 2, "explanation": "If no matching catch block is found, C++ calls std::terminate() and the program ends immediately.", "source_url": "https://www.geeksforgeeks.org/quizzes/exception-handling-gq/"}
{"subject": "C++ Programming", "topic": "C++ Exception Handling", "question": "Which standard exception class is thrown when memory allocation with new fails?", "options": ["std::runtime_error", "std::overflow_error", "std::invalid_argument"], "correct": 2, "explanation": "When new fails to allocate memory, it throws an exception of type std::bad_alloc from &amp;lt;new&amp;gt; header.", "source_url": "https://www.geeksforgeeks.org/quizzes/exception-handling-gq/"}
{"subject": "C++ Programming", "topic": "C++ Exception Handling", "question": "What is the output of the following code? C++ #include &amp;lt;iostream&amp;gt; using namespace std; int main() { try { throw 10; } catch (char e) { cout &amp;lt;&amp;lt; &amp;quot;Char Exception&amp;quot;; } catch (...) { cout &amp;lt;&amp;lt; &amp;quot;Default Handler&amp;quot;; } }", "options": ["Char Exception", "Compilation Error", "No Output"], "correct": 2, "explanation": "The thrown value 10 is an int, but there&rsquo;s no catch(int) block. The catch(...) block matches any type, so it is executed.", "source_url": "https://www.geeksforgeeks.org/quizzes/exception-handling-gq/"}
{"subject": "C++ Programming", "topic": "C++ Exception Handling", "question": "&amp;nbsp;Which keyword is used to rethrow the currently handled exception inside a catch block?", "options": ["retry", "catch", "throw e"], "correct": 2, "explanation": "A bare throw; inside a catch block rethrows the currently caught exception to be handled by another catch block higher up in the call stack.", "source_url": "https://www.geeksforgeeks.org/quizzes/exception-handling-gq/"}
{"subject": "C++ Programming", "topic": "C++ friend keyword", "question": "Predict the output of following program. CPP #include &amp;lt;iostream&amp;gt; using namespace std; class A { protected: int x; public: A() {x = 0;} friend void show(); }; class B: public A { public: B() : y (0) {} private: int y; }; void show() { A a; B b; cout &amp;lt;&amp;lt; &amp;quot;The default value of A::x = &amp;quot; &amp;lt;&amp;lt; a.x &amp;lt;&amp;lt; &amp;quot; &amp;quot;; cout &amp;lt;&amp;lt; &amp;quot;The default value of B::y = &amp;quot; &amp;lt;&amp;lt; b.y&amp;lt;&amp;lt;endl; }", "options": ["Compiler Error in show() because x is protected in class A", "The default value of A::x = 0 The default value of B::y = 0", "Compiler Dependent"], "correct": 2, "explanation": "Please note that show() is a friend of class A, so there should not be any compiler error in accessing any member of A in show(). Class B is inherited from A, the important point to note here is friendship is not inherited. So show() doesn't become a friend of B and therefore can't access private members of B.", "source_url": "https://www.geeksforgeeks.org/quizzes/friend-function-and-class-gq/"}
{"subject": "C++ Programming", "topic": "C++ friend keyword", "question": "Predict the output the of following program.&amp;nbsp; C #include &amp;lt;iostream&amp;gt; using namespace std; class B; class A { int a; public: A():a(0) { } void show(A&amp;amp; x, B&amp;amp; y); }; class B { private: int b; public: B():b(0) { } friend void A::show(A&amp;amp; x, B&amp;amp; y); }; void A::show(A&amp;amp; x, B&amp;amp; y) { x.a = 10; cout &amp;lt;&amp;lt; &amp;quot;A::a=&amp;quot; &amp;lt;&amp;lt; x.a &amp;lt;&amp;lt; &amp;quot; B::b=&amp;quot; &amp;lt;&amp;lt; y.b; } int main() { A a; B b; a.show(a,b); return 0; }", "options": ["Compiler Error", "A::a=0 B::b=0"], "correct": 2, "explanation": "This is simple program where a function of class A is declared as friend of class B. Since show() is friend, it can access private data members of B.", "source_url": "https://www.geeksforgeeks.org/quizzes/friend-function-and-class-gq/"}
{"subject": "C++ Programming", "topic": "C++ friend keyword", "question": "If a function is friend of a class, which one of the following is wrong?", "options": ["A function can only be declared a friend by a class itself.", "Friend functions are not members of a class, they are associated with it.", "It can have access to all members of the class, even private ones."], "correct": 3, "explanation": "Friend of the class can be member of some other class but Friend functions are not the members of a particular class. For more information on Friend function Refer:Friend class and function in C++ Option (C) is correct.", "source_url": "https://www.geeksforgeeks.org/quizzes/friend-function-and-class-gq/"}
{"subject": "C++ Programming", "topic": "C++ friend keyword", "question": "Which one of the following is correct, when a class grants friend status to another class?", "options": ["The member functions of the class generating friendship can access the members of the friend class.", "Class friendship is reciprocal to each other.", "There is no such concept."], "correct": 2, "explanation": "When a class grants friend status to another class then all member functions of the class granted friendship have unrestricted access to the members of the class granting the friendship. The member functions of the class generating friendship can access the members of the friend class.Incorrect statement. Class friendship is not reciprocal to each other. So, option (B) is correct.", "source_url": "https://www.geeksforgeeks.org/quizzes/friend-function-and-class-gq/"}
{"subject": "C++ Programming", "topic": "C++ Function Overloading and Default Arguments", "question": "What is the output of this C++ program? #include using namespace std;void square (int *x){*x = (*x)++ * (*x);}void square (int *x, int *y){*x = (*x) * --(*y);}int main ( ){int number = 30;square(&amp;amp;number, &amp;amp;number);cout &amp;lt;&amp;lt; number;return 0;}", "options": ["910", "920", "900"], "correct": 3, "explanation": "no_explanation_added", "source_url": "https://www.geeksforgeeks.org/quizzes/function-overloading-2-gq/"}
{"subject": "C++ Programming", "topic": "C++ Function Overloading and Default Arguments", "question": "Which of the following in Object Oriented Programming is supported by Function overloading and default arguments features of C++.", "options": ["Inheritance", "Encapsulation", "None of the above"], "correct": 2, "explanation": "Both of the features allow one function name to work for different parameter.", "source_url": "https://www.geeksforgeeks.org/quizzes/function-overloading-2-gq/"}
{"subject": "C++ Programming", "topic": "C++ Function Overloading and Default Arguments", "question": "Which of the following overloaded functions are NOT allowed in C++? 1) Function declarations that differ only in the return type int fun(int x, int y); void fun(int x, int y); 2) Functions that differ only by static keyword in return type int fun(int x, int y); static int fun(int x, int y); 3)Parameter declarations that differ only in a pointer * versus an array [] int fun(int *ptr, int n); int fun(int ptr[], int n); 4) Two parameter declarations that differ only in their default arguments int fun( int x, int y); int fun( int x, int y = 10);", "options": ["All except 2)", "All except 1)", "All except 2 and 4"], "correct": 1, "explanation": "See Function overloading in C++", "source_url": "https://www.geeksforgeeks.org/quizzes/function-overloading-2-gq/"}
{"subject": "C++ Programming", "topic": "C++ Function Overloading and Default Arguments", "question": "Output of following program? CPP #include &amp;lt;iostream&amp;gt; using namespace std; int fun(int=0, int = 0); int main() { cout &amp;lt;&amp;lt; fun(5); return 0; } int fun(int x, int y) { return (x+y); }", "options": ["Compiler Error", "0", "10"], "correct": 2, "explanation": "The statement \"int fun(int=0, int=0)\" is declaration of a function that takes two arguments with default values as 0 and 0. The last statement is definition of fun(). When we make a call fun(5), x gets the value 5 and y gets 0. So the returned value is 5.", "source_url": "https://www.geeksforgeeks.org/quizzes/function-overloading-2-gq/"}
{"subject": "C++ Programming", "topic": "C++ Function Overloading and Default Arguments", "question": "Predict the output of following C++ program? C include&amp;lt;iostream&amp;gt; using namespace std; class Test { protected: int x; public: Test (int i):x(i) { } void fun() const { cout &amp;lt;&amp;lt; &amp;quot;fun() const &amp;quot; &amp;lt;&amp;lt; endl; } void fun() { cout &amp;lt;&amp;lt; &amp;quot;fun() &amp;quot; &amp;lt;&amp;lt; endl; } }; int main() { Test t1 (10); const Test t2 (20); t1.fun(); t2.fun(); return 0; }", "options": ["Compiler Error", "fun() const fun() const", "fun() fun()"], "correct": 2, "explanation": "The two methods &lsquo;void fun() const&rsquo; and &lsquo;void fun()&rsquo; have same signature except that one is const and other is not. Also, if we take a closer look at the output, we observe that, &lsquo;const void fun()&rsquo; is called on const object and &lsquo;void fun()&rsquo; is called on non-const object. C++ allows member methods to be overloaded on the basis of const type. Overloading on the basis of const type can be useful when a function return reference or pointer. We can make one function const, that returns a const reference or const pointer, other non-const function, that returns non-const reference or pointer. See following for more details. Function overloading and const keyword", "source_url": "https://www.geeksforgeeks.org/quizzes/function-overloading-2-gq/"}
{"subject": "C++ Programming", "topic": "C++ Inheritance", "question": "CPP #include&amp;lt;iostream&amp;gt; using namespace std; class Base1 { public: Base1() { cout &amp;lt;&amp;lt; &amp;quot; Base1&amp;#39;s constructor called&amp;quot; &amp;lt;&amp;lt; endl; } }; class Base2 { public: Base2() { cout &amp;lt;&amp;lt; &amp;quot;Base2&amp;#39;s constructor called&amp;quot; &amp;lt;&amp;lt; endl; } }; class Derived: public Base1, public Base2 { public: Derived() { cout &amp;lt;&amp;lt; &amp;quot;Derived&amp;#39;s constructor called&amp;quot; &amp;lt;&amp;lt; endl; } }; int main() { Derived d; return 0; }", "options": ["Compiler Dependent", "Base2&prime;s constructor called Base1&prime;s constructor called Derived&rsquo;s constructor called", "Compiler Error"], "correct": 2, "explanation": "When a class inherits from multiple classes, constructors of base classes are called in the same order as they are specified in inheritance.", "source_url": "https://www.geeksforgeeks.org/quizzes/inheritance-gq/"}
{"subject": "C++ Programming", "topic": "C++ Inheritance", "question": "Output? CPP #include &amp;lt;iostream&amp;gt; using namespace std; class Base1 { public: ~Base1() { cout &amp;lt;&amp;lt; &amp;quot; Base1&amp;#39;s destructor&amp;quot; &amp;lt;&amp;lt; endl; } }; class Base2 { public: ~Base2() { cout &amp;lt;&amp;lt; &amp;quot; Base2&amp;#39;s destructor&amp;quot; &amp;lt;&amp;lt; endl; } }; class Derived: public Base1, public Base2 { public: ~Derived() { cout &amp;lt;&amp;lt; &amp;quot; Derived&amp;#39;s destructor&amp;quot; &amp;lt;&amp;lt; endl; } }; int main() { Derived d; return 0; }", "options": ["Base1 's destructor Base2 's destructor Derived 's destructor", "Derived 's destructor", "Compiler Dependent"], "correct": 2, "explanation": "Destructors are always called in reverse order of constructors.", "source_url": "https://www.geeksforgeeks.org/quizzes/inheritance-gq/"}
{"subject": "C++ Programming", "topic": "C++ Inheritance", "question": "Assume that an integer takes 4 bytes and there is no alignment in following classes, predict the output. C #include&amp;lt;iostream&amp;gt; using namespace std; class base { int arr[10]; }; class b1: public base { }; class b2: public base { }; class derived: public b1, public b2 {}; int main(void) { cout &amp;lt;&amp;lt; sizeof(derived); return 0; }", "options": ["40", "0", "4"], "correct": 2, "explanation": "Since b1 and b2 both inherit from class base, two copies of class base are there in class derived. This kind of inheritance without virtual causes wastage of space and ambiguities. virtual base classes are used to save space and avoid ambiguities in such cases. For example, following program prints 48. 8 extra bytes are for bookkeeping information stored by the compiler (See this for details) #include&amp;lt;iostream&amp;gt; using namespace std; class base { int arr[10]; }; class b1: virtual public base { }; class b2: virtual public base { }; class derived: public b1, public b2 {}; int main(void) { cout", "source_url": "https://www.geeksforgeeks.org/quizzes/inheritance-gq/"}
{"subject": "C++ Programming", "topic": "C++ Inheritance", "question": "C #include&amp;lt;iostream&amp;gt; using namespace std; class P { public: void print() { cout &amp;lt;&amp;lt;&amp;quot; Inside P&amp;quot;; } }; class Q : public P { public: void print() { cout &amp;lt;&amp;lt;&amp;quot; Inside Q&amp;quot;; } }; class R: public Q { }; int main(void) { R r; r.print(); return 0; }", "options": ["Inside P", "Compiler Error: Ambiguous call to print()"], "correct": 2, "explanation": "The print function is not present in class R. So it is looked up in the inheritance hierarchy. print() is present in both classes P and Q, which of them should be called? The idea is, if there is multilevel inheritance, then function is linearly searched up in the inheritance hierarchy until a matching function is found.", "source_url": "https://www.geeksforgeeks.org/quizzes/inheritance-gq/"}
{"subject": "C++ Programming", "topic": "C++ Inheritance", "question": "Output? C #include&amp;lt;iostream&amp;gt; using namespace std; class Base { private: int i, j; public: Base(int _i = 0, int _j = 0): i(_i), j(_j) { } }; class Derived: public Base { public: void show(){ cout&amp;lt;&amp;lt;&amp;quot; i = &amp;quot;&amp;lt;&amp;lt;i&amp;lt;&amp;lt;&amp;quot; j = &amp;quot;&amp;lt;&amp;lt;j; } }; int main(void) { Derived d; d.show(); return 0; }", "options": ["i = 0 j = 0", "Compiler Error: Could not call constructor of Base"], "correct": 2, "explanation": "no_explanation_added", "source_url": "https://www.geeksforgeeks.org/quizzes/inheritance-gq/"}
{"subject": "C++ Programming", "topic": "C++ Inheritance", "question": "C #include&amp;lt;iostream&amp;gt; using namespace std; class Base {}; class Derived: public Base {}; int main() { Base *bp = new Derived; Derived *dp = new Base; }", "options": ["No Compiler Error", "Compiler Error in line \"Base *bp = new Derived;\"", "Runtime Error"], "correct": 3, "explanation": "A Base class pointer/reference can point/refer to a derived class object, but the other way is not possible.", "source_url": "https://www.geeksforgeeks.org/quizzes/inheritance-gq/"}
{"subject": "C++ Programming", "topic": "C++ Inheritance", "question": "C #include&amp;lt;iostream&amp;gt; using namespace std; class Base { public: void show() { cout&amp;lt;&amp;lt;&amp;quot; In Base &amp;quot;; } }; class Derived: public Base { public: int x; void show() { cout&amp;lt;&amp;lt;&amp;quot;In Derived &amp;quot;; } Derived() { x = 10; } }; int main(void) { Base *bp, b; Derived d; bp = &amp;amp;d; bp-&amp;gt;show(); cout &amp;lt;&amp;lt; bp-&amp;gt;x; return 0; }", "options": ["Compiler Error in line \" bp-&gt;show()\"", "In Base 10", "In Derived 10"], "correct": 2, "explanation": "A base class pointer can point to a derived class object, but we can only access base class member or virtual functions using the base class pointer.", "source_url": "https://www.geeksforgeeks.org/quizzes/inheritance-gq/"}
{"subject": "C++ Programming", "topic": "C++ Inheritance", "question": "Output of following program?&amp;nbsp; CPP #include &amp;lt;iostream&amp;gt; #include&amp;lt;string&amp;gt; using namespace std; class Base { public: virtual string print() const { return &amp;quot;This is Base class&amp;quot;; } }; class Derived : public Base { public: virtual string print() const { return &amp;quot;This is Derived class&amp;quot;; } }; void describe(Base p) { cout &amp;lt;&amp;lt; p.print() &amp;lt;&amp;lt; endl; } int main() { Base b; Derived d; describe(b); describe(d); return 0; }", "options": ["This is Derived class This is Base class", "This is Base class This is Derived class", "Compiler Error"], "correct": 3, "explanation": "Note that an object of Derived is passed in describe(d), but print of Base is called. The describe function accepts a parameter of Base type. This is a typical example of object slicing, when we assign an object of derived class to an object of base type, the derived class object is sliced off and all the data members inherited from base class are copied. Object slicing should be avoided as there may be surprising results like above. As a side note, object slicing is not possible in Java. In Java, every non-primitive variable is actually a reference.", "source_url": "https://www.geeksforgeeks.org/quizzes/inheritance-gq/"}
{"subject": "C++ Programming", "topic": "C++ Misc", "question": "Which of the following is not correct for virtual function in C++ ?", "options": ["Must be declared in public section of class.", "Virtual function should be accessed using pointers.", "Virtual function is defined in base class."], "correct": 2, "explanation": "(B) Virtual functions cannot be declared as static. Static member functions in C++ do not participate in dynamic binding (polymorphism), which is a fundamental feature of virtual functions. Static functions are resolved at compile time based on the type of the pointer or reference, whereas virtual functions are resolved at runtime based on the actual type of the object they're invoked on.So, the correct choice is (B) Virtual function can be static.", "source_url": "https://www.geeksforgeeks.org/quizzes/misc-c-gq/"}
{"subject": "C++ Programming", "topic": "C++ Misc", "question": "Which of the following is not correct (in C++) ?Class templates and function templates are instantiated in the same wayClass templates differ from function templates in the way they are initiatedClass template is initiated by defining an object using the template argumentClass templates are generally used for storage classes", "options": ["(1)", "(2), (4)", "(4)"], "correct": 3, "explanation": "In C++ class template and function, template are similar in the way they are initiated. Class template are not used for storage class. Class template is not initiated by defining an object using the template.&amp;nbsp;So (2), (3), (4) are not correct in C++.&amp;nbsp;So, option (C) is correct.", "source_url": "https://www.geeksforgeeks.org/quizzes/misc-c-gq/"}
{"subject": "C++ Programming", "topic": "C++ Misc", "question": "Inline functions are useful when", "options": ["Function is large with many nested loops", "Function has many static variables", "None of the above"], "correct": 3, "explanation": "Inline functions are generally used in place of small macros. They are substitute to macros and better than macros. See following for details. http://en.wikipedia.org/wiki/Inline_function#Comparison_with_macros", "source_url": "https://www.geeksforgeeks.org/quizzes/misc-c-gq/"}
{"subject": "C++ Programming", "topic": "C++ Misc", "question": "C++ #include&amp;lt;iostream&amp;gt; using namespace std; int x = 1; void fun() { int x = 2; { int x = 3; cout &amp;lt;&amp;lt; ::x &amp;lt;&amp;lt; endl; } } int main() { fun(); return 0; }", "options": ["2", "3", "0"], "correct": 1, "explanation": "The value of ::x is 1. The scope resolution operator when used with a variable name, always refers to global variable.", "source_url": "https://www.geeksforgeeks.org/quizzes/misc-c-gq/"}
{"subject": "C++ Programming", "topic": "C++ Misc", "question": "How can we make a C++ class such that objects of it can only be created using new operator? If user tries to create an object directly, the program produces compiler error.", "options": ["Not possible", "By making constructor private", "By making both constructor and destructor private"], "correct": 2, "explanation": "See the following example. // Objects of test can only be created using new class Test { private: ~Test() {} friend void destructTest(Test* ); }; // Only this function can destruct objects of Test void destructTest(Test* ptr) { delete ptr; } int main() { // create an object Test *ptr = new Test; // destruct the object destructTest (ptr); return 0; } See http://www.geeksforgeeks.org/private-destructor/ for more details.", "source_url": "https://www.geeksforgeeks.org/quizzes/misc-c-gq/"}
{"subject": "C++ Programming", "topic": "C++ Misc", "question": "C #include&amp;lt;iostream&amp;gt; using namespace std; int x[100]; int main() { cout &amp;lt;&amp;lt; x[99] &amp;lt;&amp;lt; endl; } This question is contributed by Sudheendra Baliga", "options": ["Unpredictable", "Runtime error", "99"], "correct": 3, "explanation": "The correct answer is c. In C++ all the uninitialized global variables are initialized to 0.", "source_url": "https://www.geeksforgeeks.org/quizzes/misc-c-gq/"}
{"subject": "C++ Programming", "topic": "C++ Misc", "question": "Method over-riding can be prevented by using final as a modifier at ______.", "options": ["the start of the class.", "the start of derived class.", "the start of the method declaration in the derived class."], "correct": 2, "explanation": "Method over-riding can be prevented by using final as a modifier at the start of method declaration. For detailed information and program Refer:Overriding in Java Option (B) is correct.", "source_url": "https://www.geeksforgeeks.org/quizzes/misc-c-gq/"}
{"subject": "C++ Programming", "topic": "C++ new and delete", "question": "How to create a dynamic array of pointers (to integers) of size 10 using new in C++? Hint: We can create a non-dynamic array using int *arr[10]", "options": ["int *arr = new int *[10];", "int *arr = new int [10];", "Not Possible"], "correct": 2, "explanation": "Dynamic array of pointer having size 10 using new is created as, int **arr = new int *[10]; So, option (B) is correct.", "source_url": "https://www.geeksforgeeks.org/quizzes/new-and-delete-gq/"}
{"subject": "C++ Programming", "topic": "C++ new and delete", "question": "Predict the output? CPP #include &amp;lt;iostream&amp;gt; using namespace std; class Test { int x; Test() { x = 5;} }; int main() { Test *t = new Test; cout &amp;lt;&amp;lt; t-&amp;gt;x; }", "options": ["5", "Garbage Value", "0"], "correct": 1, "explanation": "There is compiler error: Test::Test() is private. new makes call to the constructor. In class Test, constructor is private (note that default access is private in C++).", "source_url": "https://www.geeksforgeeks.org/quizzes/new-and-delete-gq/"}
{"subject": "C++ Programming", "topic": "C++ Operator Overloading", "question": "How can we restrict dynamic allocation of objects of a class using new?", "options": ["By overloading new operator", "By making an empty private new operator.", "By overloading new operator and new[] operators"], "correct": 3, "explanation": "If we declare new and [] new operators, then the objects cannot be created anywhere (within the class and outside the class) See the following example. We can not allocate an object of type Test using new. #include &amp;lt;stdlib.h&amp;gt; #include &amp;lt;stdio.h&amp;gt; #include &amp;lt;iostream&amp;gt; using namespace std; class Test { private: void* operator new(size_t size) {} void* operator new[](size_t size) {} }; int main() { Test *obj = new Test; Test *arr = new Test[10]; return 0; }", "source_url": "https://www.geeksforgeeks.org/quizzes/operator-overloading-gq/"}
{"subject": "C++ Programming", "topic": "C++ Operator Overloading", "question": "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 ( = )", "options": ["Both 1 and 2", "Only 1", "None of the two"], "correct": 3, "explanation": "Assign operator is by default available in all user defined classes even if user has not implemented. The default assignement does shallow copy. But comparison operator \"==\" is not overloaded. #include&amp;lt;iostream&amp;gt; using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i =0) {real = r; imag = i;} }; int main() { Complex c1(10, 5), c2(2, 4); // For example, below code works fine c1 = c2; // But this code throws compiler error if (c1 == c2) cout", "source_url": "https://www.geeksforgeeks.org/quizzes/operator-overloading-gq/"}
{"subject": "C++ Programming", "topic": "C++ Operator Overloading", "question": "Which of the following operators should be preferred to overload as a global function rather than a member method?", "options": ["Postfix ++", "Comparison Operator", "Prefix++"], "correct": 3, "explanation": "cout is an object of ostream class which is a compiler defined class. When we do \"cout #include &amp;lt;iostream&amp;gt; using namespace std; class Complex { private: int real; int imag; public: Complex(int r = 0, int i =0) { real = r; imag = i; } friend ostream &amp; operator", "source_url": "https://www.geeksforgeeks.org/quizzes/operator-overloading-gq/"}
{"subject": "C++ Programming", "topic": "C++ Operator Overloading", "question": "Which of the following is an example of compile-time polymorphism?", "options": ["Virtual function", "Function Overriding", "Dynamic Casting"], "correct": 1, "explanation": "Function overloading is resolved by the compiler at compile time based on the number or type of arguments, which makes it a classic example of compile-time polymorphism.", "source_url": "https://www.geeksforgeeks.org/quizzes/operator-overloading-gq/"}
{"subject": "C++ Programming", "topic": "C++ Operator Overloading", "question": "Predict the output? CPP #include&amp;lt;stdlib.h&amp;gt; #include&amp;lt;stdio.h&amp;gt; #include&amp;lt;iostream&amp;gt; using namespace std; class Test { int x; public: void* operator new(size_t size); void operator delete(void*); Test(int i) { x = i; cout &amp;lt;&amp;lt; &amp;quot;Constructor called &amp;quot;; } ~Test() { cout &amp;lt;&amp;lt; &amp;quot;Destructor called &amp;quot;; } }; void* Test::operator new(size_t size) { void *storage = malloc(size); cout &amp;lt;&amp;lt; &amp;quot;new called &amp;quot;; return storage; } void Test::operator delete(void *p ) { cout&amp;lt;&amp;lt;&amp;quot;delete called &amp;quot;; free(p); } int main() { Test *m = new Test(5); delete m; return 0; }", "options": ["new called Constructor called delete called Destructor called", "Constructor called new called Destructor called delete called", "Constructor called new called delete called Destructor called"], "correct": 2, "explanation": "Consider the following statement Test *ptr = new Test; There are actually two things that happen in the above statement--memory allocation and object construction; the new keyword is responsible for both. One step in the process is to call operator new in order to allocate memory; the other step is to actually invoke the constructor. Operator new only allows us to change the memory allocation method, but does not do anything with the constructor calling method. Keyword new is responsible for calling the constructor, not operator new.", "source_url": "https://www.geeksforgeeks.org/quizzes/operator-overloading-gq/"}
{"subject": "C++ Programming", "topic": "C++ Operator Overloading", "question": "CPP #include&amp;lt;iostream&amp;gt; using namespace std; class Point { private: int x, y; public: Point() : x(0), y(0) { } Point&amp;amp; operator()(int dx, int dy); void show() {cout &amp;lt;&amp;lt; &amp;quot;x = &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;, y = &amp;quot; &amp;lt;&amp;lt; y; } }; Point&amp;amp; Point::operator()(int dx, int dy) { x = dx; y = dy; return *this; } int main() { Point pt; pt(3, 2); pt.show(); return 0; }", "options": ["Compiler Error", "x = 2, y = 3"], "correct": 1, "explanation": "This a simple example of function call operator overloading. The function call operator, when overloaded, does not modify how functions are called. Rather, it modifies how the operator is to be interpreted when applied to objects of a given type. If you overload a function call operator for a class its declaration will have the following form: return_type operator()(parameter_list)", "source_url": "https://www.geeksforgeeks.org/quizzes/operator-overloading-gq/"}
{"subject": "C++ Programming", "topic": "C++ Operator Overloading", "question": "Which of the following operator functions cannot be global, i.e., must be a member function.", "options": ["new", "delete", "All of the above"], "correct": 3, "explanation": "new and delete can be global, see following example. #include #include #include using namespace std; class Myclass { int x; public: friend void* operator new(size_t size); friend void operator delete(void*); Myclass(int i) { x = i; cout", "source_url": "https://www.geeksforgeeks.org/quizzes/operator-overloading-gq/"}
{"subject": "C++ Programming", "topic": "C++ Operator Overloading", "question": "Predict the output CPP #include&amp;lt;iostream&amp;gt; using namespace std; class A { int i; public: A(int ii = 0) : i(ii) {} void show() { cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; 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; }", "options": ["Compiler Error", "20 20", "10 10"], "correct": 2, "explanation": "Note that the class B has as conversion operator overloaded, so an object of B can be converted to that of A. Also, class A has a constructor which can be called with single integer argument, so an int can be converted to A.", "source_url": "https://www.geeksforgeeks.org/quizzes/operator-overloading-gq/"}
{"subject": "C++ Programming", "topic": "C++ References", "question": "Which of the following functions must use reference.", "options": ["Assignment operator function", "Destructor", "Parameterized constructor"], "correct": 2, "explanation": "A copy constructor is called when an object is passed by value. Copy constructor itself is a function. So if we pass argument by value in a copy constructor, a call to copy constructor would be made to call copy constructor which becomes a non-terminating chain of calls. Therefore compiler doesn&rsquo;t allow parameters to be pass by value. See http://geeksquiz.com/copy-constructor-in-cpp/ for details.", "source_url": "https://www.geeksforgeeks.org/quizzes/references-gq/"}
{"subject": "C++ Programming", "topic": "C++ References", "question": "What is the return value of f(p, p) if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value. int f(int &amp;x, int c) { c = c - 1; if (c == 0) return 1; x = x + 1; return f(x, c) * x; }", "options": ["3024", "55440", "161051"], "correct": 2, "explanation": "Since c is passed by value and x is passed by reference, all functions will have same copy of x, but different copies of c. f(5, 5) = f(x, 4)*x = f(x, 3)*x*x = f(x, 2)*x*x*x = f(x, 1)*x*x*x*x = 1*x*x*x*x = x^4 Since x is incremented in every function call, it becomes 9 after f(x, 2) call. So the value of expression x^4 becomes 9^4 which is 6561. CPP #include &amp;lt;stdio.h&amp;gt; int f(int &amp;amp;x, int c) { c = c - 1; if (c == 0) return 1; x = x + 1; return f(x, c) * x; } int main() { int p = 5; printf(&amp;quot;%d&amp;quot;, f(p, p)); }", "source_url": "https://www.geeksforgeeks.org/quizzes/references-gq/"}
{"subject": "C++ Programming", "topic": "C++ References", "question": "CPP #include&amp;lt;iostream&amp;gt; using namespace std; int &amp;amp;fun() { int x = 10; return x; } int main() { fun() = 30; cout &amp;lt;&amp;lt; fun(); return 0; }", "options": ["May cause compiler error", "Always works fine.", "0"], "correct": 1, "explanation": "Since we return reference to a local variable, the memory location becomes invalid after function call is over. Hence it may result in segmentation fault runtime error.", "source_url": "https://www.geeksforgeeks.org/quizzes/references-gq/"}
{"subject": "C++ Programming", "topic": "C++ References", "question": "Output of following C++ program? CPP #include&amp;lt;iostream&amp;gt; using namespace std; int main() { int x = 10; int&amp;amp; ref = x; ref = 20; cout &amp;lt;&amp;lt; &amp;quot;x = &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; endl ; x = 30; cout &amp;lt;&amp;lt; &amp;quot;ref = &amp;quot; &amp;lt;&amp;lt; ref &amp;lt;&amp;lt; endl; return 0; }", "options": ["x = 20 ref = 20", "x = 10 ref = 30", "x = 30 ref = 30"], "correct": 1, "explanation": "ref is an alias of x, so if we change either of them, we can see the change in other as well.", "source_url": "https://www.geeksforgeeks.org/quizzes/references-gq/"}
{"subject": "C++ Programming", "topic": "C++ Static Keyword", "question": "C #include &amp;lt;iostream&amp;gt; using namespace std; class Player { private: int id; static int next_id; public: int getID() { return id; } Player() { id = next_id++; } }; int Player::next_id = 1; int main() { Player p1; Player p2; Player p3; cout &amp;lt;&amp;lt; p1.getID() &amp;lt;&amp;lt; &amp;quot; &amp;quot;; cout &amp;lt;&amp;lt; p2.getID() &amp;lt;&amp;lt; &amp;quot; &amp;quot;; cout &amp;lt;&amp;lt; p3.getID(); return 0; }", "options": ["Compiler Error", "1 1 1", "3 3 3", "0 0 0"], "correct": 2, "explanation": "If a member variable is declared static, all objects of that class have access to a single instance of that variable. Static variables are sometimes called class variables, class fields, or class-wide fields because they don&rsquo;t belong to a specific object; they belong to the class. In the above code, static variable next_id is used to assign a unique id to all objects.", "source_url": "https://www.geeksforgeeks.org/quizzes/static-keyword-gq/"}
{"subject": "C++ Programming", "topic": "C++ Static Keyword", "question": "Predict the output of following C++ program. CPP #include &amp;lt;iostream&amp;gt; using namespace std; class A { private: int x; public: A(int _x) { x = _x; } int get() { return x; } }; class B { static A a; public: static int get() { return a.get(); } }; int main(void) { B b; cout &amp;lt;&amp;lt; b.get(); return 0; }", "options": ["0", "Linker Error: Cannot access static a", "Linker Error: multiple functions with same name get()"], "correct": 2, "explanation": "There is a compiler error because static member a is not defined in B. To fix the error, we need to explicitly define a. The following program works fine. #include &amp;lt;iostream &amp;gt; using namespace std; class A { private: int x; public: A(int _x) { x = _x; } int get() { return x; } }; class B { static A a; public: static int get() { return a.get(); } }; A B::a(0); int main(void) { B b; cout", "source_url": "https://www.geeksforgeeks.org/quizzes/static-keyword-gq/"}
{"subject": "C++ Programming", "topic": "C++ Static Keyword", "question": "Predict the output of following C++ program. C #include &amp;lt;iostream&amp;gt; using namespace std; class Test { static int x; public: Test() { x++; } static int getX() {return x;} }; int Test::x = 0; int main() { cout &amp;lt;&amp;lt; Test::getX() &amp;lt;&amp;lt; &amp;quot; &amp;quot;; Test t[5]; cout &amp;lt;&amp;lt; Test::getX(); }", "options": ["0 0", "5 5", "Compiler Error"], "correct": 3, "explanation": "Static functions can be called without any object. So the call \"Test::getX()\" is fine. Since x is initialized as 0, the first call to getX() returns 0. Note the statement x++ in constructor. When an array of 5 objects is created, the constructor is called 5 times. So x is incremented to 5 before the next call to getX().", "source_url": "https://www.geeksforgeeks.org/quizzes/static-keyword-gq/"}
{"subject": "C++ Programming", "topic": "C++ Templates", "question": "What will be the output? C #include &amp;lt;iostream&amp;gt; using namespace std; template &amp;lt;typename T&amp;gt; T add(T a, T b) { return a + b; } int main() { cout &amp;lt;&amp;lt; add(2, 3); }", "options": ["23", "Compiler Error", "6"], "correct": 1, "explanation": "The compiler deduces T as int. The function returns 2 + 3, which is 5.", "source_url": "https://www.geeksforgeeks.org/quizzes/templates-gq/"}
{"subject": "C++ Programming", "topic": "C++ Templates", "question": "C #include &amp;lt;iostream&amp;gt; using namespace std; template &amp;lt;typename T&amp;gt; T max(T x, T y) { return (x &amp;gt; y)? x : y; } int main() { cout &amp;lt;&amp;lt; max(3, 7) &amp;lt;&amp;lt; std::endl; cout &amp;lt;&amp;lt; max(3.0, 7.0) &amp;lt;&amp;lt; std::endl; cout &amp;lt;&amp;lt; max(3, 7.0) &amp;lt;&amp;lt; std::endl; return 0; }", "options": ["7 7.0 7.0", "Compiler Error in all cout statements as data type is not specified.", "None of the above"], "correct": 3, "explanation": "The first and second call to max function is a valid call as both the arguments passed are of same data type (i.e int and float respectively). But the third call to max function has arguments of different data type and hence it will generate Compiler Error in last cout statement as call to max is ambiguous. Hence option C is correct", "source_url": "https://www.geeksforgeeks.org/quizzes/templates-gq/"}
{"subject": "C++ Programming", "topic": "C++ Templates", "question": "What will be the output? CPP #include &amp;lt;iostream&amp;gt; using namespace std; template &amp;lt;typename T&amp;gt; T square(T x) { return x * x; } int main() { cout &amp;lt;&amp;lt; square(4); }", "options": ["4", "8", "Compiler Error"], "correct": 3, "explanation": "The compiler deduces T as int. The function returns 4 &times; 4, which is 16.", "source_url": "https://www.geeksforgeeks.org/quizzes/templates-gq/"}
{"subject": "C++ Programming", "topic": "C++ Templates", "question": "What is the output of the following code? CPP #include &amp;lt;iostream&amp;gt; using namespace std; template &amp;lt;typename T&amp;gt; void fun(const T&amp;amp; x) { static int count = 0; cout &amp;lt;&amp;lt; count &amp;lt;&amp;lt; &amp;quot; &amp;quot;; count++; } int main() { fun&amp;lt;int&amp;gt;(1); fun&amp;lt;int&amp;gt;(2); fun&amp;lt;double&amp;gt;(3.5); }", "options": ["0 0 0", "0 1 2", "Compiler Error"], "correct": 1, "explanation": "Each template instantiation has its own copy of the static variable. fun&amp;lt;int&amp;gt; maintains one counter (0 1), while fun&amp;lt;double&amp;gt; has a separate counter starting from 0.", "source_url": "https://www.geeksforgeeks.org/quizzes/templates-gq/"}
{"subject": "C++ Programming", "topic": "C++ Templates", "question": "Output of following program? Assume that the size of int is 4 bytes and size of double is 8 bytes, and there is no alignment done by the compiler. C #include&amp;lt;iostream&amp;gt; #include&amp;lt;stdlib.h&amp;gt; using namespace std; template&amp;lt;class T, class U, class V=double&amp;gt; class A { T x; U y; V z; static int count; }; int main() { A&amp;lt;int, int&amp;gt; a; A&amp;lt;double, double&amp;gt; b; cout &amp;lt;&amp;lt; sizeof(a) &amp;lt;&amp;lt; endl; cout &amp;lt;&amp;lt; sizeof(b) &amp;lt;&amp;lt; endl; return 0; }", "options": ["8 16", "20 28", "Compiler Error: template parameters cannot have default values."], "correct": 1, "explanation": "templates can also have default parameters. The rule is same all default values must be on the rightmost side. Since count is static, it is not counted in sizeof.", "source_url": "https://www.geeksforgeeks.org/quizzes/templates-gq/"}
{"subject": "C++ Programming", "topic": "C++ Templates", "question": "Output of following program. CPP #include &amp;lt;iostream&amp;gt; using namespace std; template &amp;lt;class T, int max&amp;gt; int arrMin(T arr[], int n) { int m = max; for (int i = 0; i &amp;lt; n; i++) if (arr[i] &amp;lt; m) m = arr[i]; return m; } int main() { int arr1[] = {10, 20, 15, 12}; int n1 = sizeof(arr1)/sizeof(arr1[0]); char arr2[] = {1, 2, 3}; int n2 = sizeof(arr2)/sizeof(arr2[0]); cout &amp;lt;&amp;lt; arrMin&amp;lt;int, 10000&amp;gt;(arr1, n1) &amp;lt;&amp;lt; endl; cout &amp;lt;&amp;lt; arrMin&amp;lt;char, 256&amp;gt;(arr2, n2); return 0; }", "options": ["Compiler error, template argument must be a data type.", "10000256", "11"], "correct": 2, "explanation": "The template function arrMin finds the minimum value in an array. The parameter max sets the initial comparison value. For arr1, the minimum is 10, and for arr2, the minimum is 1. The template allows using different data types and initial maximums in one function.", "source_url": "https://www.geeksforgeeks.org/quizzes/templates-gq/"}
{"subject": "C++ Programming", "topic": "C++ Templates", "question": "Output? CPP #include &amp;lt;iostream&amp;gt; using namespace std; template &amp;lt;class T&amp;gt; T max (T &amp;amp;a, T &amp;amp;b) { return (a &amp;gt; b)? a : b; } template &amp;lt;&amp;gt; int max &amp;lt;int&amp;gt; (int &amp;amp;a, int &amp;amp;b) { cout &amp;lt;&amp;lt; &amp;quot;Called &amp;quot;; return (a &amp;gt; b)? a : b; } int main () { int a = 10, b = 20; cout &amp;lt;&amp;lt; max &amp;lt;int&amp;gt; (a, b); }", "options": ["20", "Compiler Error"], "correct": 2, "explanation": "Above program is an example of template specialization. Sometime we want a different behaviour of a function/class template for a particular data type. For this, we can create a specialized version for that particular data type.", "source_url": "https://www.geeksforgeeks.org/quizzes/templates-gq/"}
{"subject": "C++ Programming", "topic": "C++ Templates", "question": "Output? CPP #include &amp;lt;iostream&amp;gt; using namespace std; template&amp;lt;int n&amp;gt; struct funStruct { static const int val = 2*funStruct&amp;lt;n-1&amp;gt;::val; }; template&amp;lt;&amp;gt; struct funStruct&amp;lt;0&amp;gt; { static const int val = 1 ; }; int main() { cout &amp;lt;&amp;lt; funStruct&amp;lt;10&amp;gt;::val &amp;lt;&amp;lt; endl; return 0; }", "options": ["Compiler Error", "2", "1"], "correct": 2, "explanation": "This is an example of template metaprogramming. The program mainly calculates 2^10.", "source_url": "https://www.geeksforgeeks.org/quizzes/templates-gq/"}
{"subject": "C++ Programming", "topic": "C++ this pointer", "question": "Which of the following is true about this pointer?", "options": ["It is passed as a hidden argument to all function calls", "It is passed as a hidden argument to all static functions", "None of the above"], "correct": 2, "explanation": "The &lsquo;this&rsquo; pointer is passed as a hidden argument to all non-static member function calls and is available as a local variable within the body of all non-static functions. &lsquo;this&rsquo; pointer is a constant pointer that holds the memory address of the current object. &lsquo;this&rsquo; pointer is not available in static member functions as static member functions can be called without any object (with class name). Source: &lsquo;this&rsquo; pointer in C++", "source_url": "https://www.geeksforgeeks.org/quizzes/this-pointer-gq/"}
{"subject": "C++ Programming", "topic": "C++ this pointer", "question": "Predict the output of following C++ program. C #include&amp;lt;iostream&amp;gt; using namespace std; class Test { private: int x; public: Test(int x = 0) { this-&amp;gt;x = x; } void change(Test *t) { this = t; } void print() { cout &amp;lt;&amp;lt; &amp;quot;x = &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; endl; } }; int main() { Test obj(5); Test *ptr = new Test (10); obj.change(ptr); obj.print(); return 0; }", "options": ["x = 5", "x = 10", "Runtime Error"], "correct": 3, "explanation": "this is a const pointer, so there is an error in line \"this = t;\"", "source_url": "https://www.geeksforgeeks.org/quizzes/this-pointer-gq/"}
{"subject": "C++ Programming", "topic": "C++ this pointer", "question": "Predict the output of following C++ program? CPP #include&amp;lt;iostream&amp;gt; using namespace std; class Test { private: int x; public: Test() {x = 0;} void destroy() { delete this; } void print() { cout &amp;lt;&amp;lt; &amp;quot;x = &amp;quot; &amp;lt;&amp;lt; x; } }; int main() { Test obj; obj.destroy(); obj.print(); return 0; }", "options": ["x = 0", "compiler error", "runtime error"], "correct": 2, "explanation": "delete operator works only for objects allocated using operator new (See this post). If the object is created using new, then we can do delete this, otherwise behavior is undefined.See &ldquo;delete this&rdquo; in C++ for more examples.", "source_url": "https://www.geeksforgeeks.org/quizzes/this-pointer-gq/"}
{"subject": "C++ Programming", "topic": "C++ Virtual Functions", "question": "Predict output of the following program C #include&amp;lt;iostream&amp;gt; using namespace std; class Base { public: virtual void show() { cout&amp;lt;&amp;lt;&amp;quot; In Base &amp;quot;; } }; class Derived: public Base { public: void show() { cout&amp;lt;&amp;lt;&amp;quot;In Derived &amp;quot;; } }; int main(void) { Base *bp = new Derived; bp-&amp;gt;show(); Base &amp;amp;br = *bp; br.show(); return 0; }", "options": ["In Base In Base", "In Base In Derived", "In Derived In Base"], "correct": 3, "explanation": "Since show() is virtual in base class, it is called according to the type of object being referred or pointed, rather than the type of pointer or reference.", "source_url": "https://www.geeksforgeeks.org/quizzes/virtual-functions-gq/"}
{"subject": "C++ Programming", "topic": "C++ Virtual Functions", "question": "In C++ inheritance, which constructor is called first when an object of a derived class is created?", "options": ["Derived class constructor", "Constructors are called randomly", "t depends on access specifier"], "correct": 2, "explanation": "C++ always calls base class constructors before derived class constructors.", "source_url": "https://www.geeksforgeeks.org/quizzes/virtual-functions-gq/"}
{"subject": "C++ Programming", "topic": "C++ Virtual Functions", "question": "C #include&amp;lt;iostream&amp;gt; using namespace std; class Base { public: virtual void show() = 0; }; int main(void) { Base b; Base *bp; return 0; }", "options": ["There are compiler errors in lines \"Base b;\" and \"Base bp;\"", "There is compiler error in line \"Base bp;\"", "No compiler Error"], "correct": 2, "explanation": "Since Base has a pure virtual function, it becomes an abstract class and an instance of it cannot be created. So there is an error in line \"Base b\". Note that there is no error in line \"Base *bp;\". We can have pointers or references of abstract classes.", "source_url": "https://www.geeksforgeeks.org/quizzes/virtual-functions-gq/"}
{"subject": "C++ Programming", "topic": "C++ Virtual Functions", "question": "Predict the output of following program. CPP #include&amp;lt;iostream&amp;gt; using namespace std; class Base { public: virtual void show() = 0; }; class Derived : public Base { }; int main(void) { Derived q; return 0; }", "options": ["Compiler Error: there cannot be an empty derived class", "No compiler Error"], "correct": 2, "explanation": "If we don't override the pure virtual function in derived class, then derived class also becomes abstract class.", "source_url": "https://www.geeksforgeeks.org/quizzes/virtual-functions-gq/"}
{"subject": "C++ Programming", "topic": "C++ Virtual Functions", "question": "C #include&amp;lt;iostream&amp;gt; using namespace std; class Base { public: Base() { cout&amp;lt;&amp;lt;&amp;quot;Constructor: Base&amp;quot;&amp;lt;&amp;lt;endl; } virtual ~Base() { cout&amp;lt;&amp;lt;&amp;quot;Destructor : Base&amp;quot;&amp;lt;&amp;lt;endl; } }; class Derived: public Base { public: Derived() { cout&amp;lt;&amp;lt;&amp;quot;Constructor: Derived&amp;quot;&amp;lt;&amp;lt;endl; } ~Derived() { cout&amp;lt;&amp;lt;&amp;quot;Destructor : Derived&amp;quot;&amp;lt;&amp;lt;endl; } }; int main() { Base *Var = new Derived(); delete Var; return 0; }", "options": ["Constructor: Base Constructor: Derived Destructor : Base", "Constructor: Base Constructor: Derived Destructor : Derived", "Constructor: Derived Destructor : Derived"], "correct": 1, "explanation": "Since the destructor is vitrual, the derived class destructor is called which in turn calls base class destructor.", "source_url": "https://www.geeksforgeeks.org/quizzes/virtual-functions-gq/"}
