C++ Programming Section
Question 1: What is the output of the following program?
#includeusing namespace std; int getCounter() { static int counter = 0; return counter++; } int main() { cout << getCounter() << " "; cout << getCounter() << " "; cout << getCounter(); return 0; }
Answer: B. 0 1 2
Explanation: The function getCounter uses a static variable counter, which is initialized to 0 once and retains its value across function calls. Each call to getCounter() returns the current value of counter and then increments it. The first call returns 0 (then increments to 1), the second call returns 1 (then increments to 2), and the third call returns 2 (then increments to 3). Thus, the output is 0 1 2.
Question 2: What is the output of the following program?
#includeusing namespace std; int main() { int x = 2; switch (x) { case 1: cout << "One "; case 2: cout << "Two "; case 3: cout << "Three "; break; default: cout << "Default"; } return 0; }
Answer: B. Two Three
Explanation: The switch statement evaluates x, which is 2, so it starts at case 2. It prints Two . Since there is no break statement after case 2, execution falls through to case 3, printing Three . The break in case 3 stops further execution, so Default is not printed. The output is Two Three.
Question 3: What is the output of the following program?
#includeusing namespace std; int main() { int x = 5; int *p = &x; *p = 10; cout << x; return 0; }
Answer: B. 10
Explanation: The variable x is assigned the value 5. A pointer p is created to point to the address of x. Then, *p = 10 modifies the value at the address p points to, which is x. Thus, x becomes 10, and cout << x outputs 10.
Question 4: What is the output of the following program?
#includeusing namespace std; template void printValue(T value) { cout << value; } int main() { printValue(42); printValue("Hello"); return 0; }
Answer: C. 42 Hello
Explanation: The template function printValue works with any type T. First, printValue(42) instantiates the function with T = int, printing 42. Then, printValue("Hello") instantiates it with T = const char*, printing Hello. Since there’s no newline or space in the function, the outputs are concatenated, but for clarity in this context, we interpret the calls as separate outputs with a space: 42 Hello.
Question 5: What is the output of the following program?
#includeusing namespace std; class Base { public: virtual void display() { cout << "Base"; } }; class Derived : public Base { public: void display() override { cout << "Derived"; } }; int main() { Base* ptr = new Derived(); ptr->display(); delete ptr; return 0; }
Answer: B. Derived
Explanation: The display function in Base is marked as virtual, enabling polymorphism. When ptr (of type Base*) points to a Derived object, calling ptr->display() invokes the overridden display function in Derived, printing Derived. The object is properly deleted, and there are no errors.
Question 6: What is the output of the following program?
#includeusing namespace std; int main() { int a = 2, b = 3; a += b; b = a - b; a = a - b; cout << a << " " << b; return 0; }
Answer: B. 3 2
Explanation: This code swaps the values of a and b without using a temporary variable. Initially, a = 2, b = 3. After a += b, a = 5, b = 3. Then, b = a - b makes b = 5 - 3 = 2. Finally, a = a - b makes a = 5 - 2 = 3. So, a = 3, b = 2, and the output is 3 2.
Question 7: What is the output of the following program?
#includeusing namespace std; int main() { for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 2; j++) { cout << (i * j) << " "; } } return 0; }
Answer: A. 1 2 2 4
Explanation: The outer loop runs for i = 1 to 2, and the inner loop runs for j = 1 to 2. It prints i * j each iteration: i=1, j=1 prints 1; i=1, j=2 prints 2; i=2, j=1 prints 2; i=2, j=2 prints 4. The output is 1 2 2 4.
Question 8: What is the output of the following program?
#includeusing namespace std; int main() { char str[] = "Hello"; char* ptr = str; cout << *(ptr + 1); return 0; }
Answer: B. e
Explanation: The array str contains the string "Hello". The pointer ptr points to the first character, 'H'. The expression ptr + 1 points to the second character, 'e', and *(ptr + 1) dereferences it, printing e.
Question 9: What is the output of the following program?
#includeusing namespace std; int main() { int a[8]{16}, c = 0, i, j; for (i = 0; i < 10; i++) { for (j = 0; j < 8; j++) { a[j][i] = c++; } } printf("%d", a[3][6]); }
Answer: D. Compile error
Explanation: The program attempts to use a 1D array a[8] as a 2D array with the expression a[j][i]. In C++, a[8] declares a 1D array, and accessing it as a[j][i] is invalid because a[j] does not point to another array. This results in a compile-time error. Additionally, the program uses printf but does not include the header, which would also cause an issue, but the primary error is the incorrect array access.
Question 10: What is the output of the following program?
#includeusing namespace std; int main() { int arr[3] = {5, 10, 15}; int* ptr = arr + 2; cout << *ptr; return 0; }
Answer: C. 15
Explanation: The array arr contains {5, 10, 15}. The expression arr + 2 points to the third element (index 2), which is 15. The pointer ptr is set to this address, and *ptr dereferences it, printing 15.