C++ Programming Challenge: Test Your Knowledge
Question 1: What is the output of the following program?
#includeusing namespace std; int main() { int x = 5, y = 10; if (x > y) { cout << "x is greater"; } else { cout << "y is greater"; } return 0; }
Answer: B. y is greater
Explanation: The program compares x
(5) and y
(10). Since x > y
evaluates to false (5 is not greater than 10), the else
block executes, printing y is greater
.
Question 2: What is the output of the following program?
#includeusing namespace std; class A { int id; public: A(int i) { id = i; } void print() { cout << id << endl; } }; int main() { A a[2]; a[0].print(); a[1].print(); return 0; }
Answer: D. Compilation error
Explanation: The class A
has a constructor that requires an integer parameter (A(int i)
). However, in main()
, the array A a[2]
is declared without providing any arguments to initialize the objects. Since there is no default constructor in class A
, the compiler cannot create the array elements, leading to a compilation error.
Question 3: What is the output of the following program?
#includeusing namespace std; int main() { int arr[3] = {10, 20, 30}; for (int i = 0; i < 3; i++) { cout << arr[i] << " "; } return 0; }
Answer: A. 10 20 30
Explanation: The array arr
is initialized with {10, 20, 30}
. The for
loop iterates over the array from index 0 to 2, printing each element followed by a space. Thus, the output is 10 20 30
.
Question 4: What is the output of the following program?
#includeusing namespace std; void increment(int &x) { x++; } int main() { int a = 5; increment(a); cout << a; return 0; }
Answer: B. 6
Explanation: The function increment
takes an integer by reference, so it modifies the original variable. Initially, a
is 5. The increment(a)
call increases a
to 6. Then, cout << a
prints 6.
Question 5: Assume that an integer takes 4 bytes and there is no alignment in the following classes, predict the output.
#includeusing 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 << sizeof(derived); return 0; }
Answer: B. 80
Explanation: The question states that an integer takes 4 bytes and there is no alignment. The class base
has an array arr[10]
of integers, so its size is 10 * 4 = 40
bytes. Class b1
inherits from base
, so sizeof(b1) = 40
bytes. Similarly, sizeof(b2) = 40
bytes. Class derived
uses multiple inheritance from b1
and b2
, meaning it includes two separate instances of base
(one from b1
and one from b2
). Therefore, sizeof(derived) = sizeof(b1) + sizeof(b2) = 40 + 40 = 80
bytes. The output is 80.
Question 6: What is the output of the following program?
#includeusing namespace std; int main() { int x = 3; int *p = &x; cout << *p * 2; return 0; }
Answer: B. 6
Explanation: The variable x
is 3, and p
is a pointer to x
. The expression *p
dereferences p
, giving the value of x
, which is 3. Then, *p * 2
computes 3 * 2 = 6
, which is printed.
Question 7: What is the output of the following program?
#includeusing namespace std; class Test { public: int value; Test() { value = 100; } }; int main() { Test t; cout << t.value; return 0; }
Answer: B. 100
Explanation: The class Test
has a default constructor that sets value
to 100. In main()
, an object t
of type Test
is created, invoking the constructor, so t.value
is 100. The output is 100.
Question 8: What is the output of the following code fragment?
int i = 0; while(i != 10) { cout << i << endl; i = i + 1; }
Answer: B. 0,1,2,3,4,5,6,7,8,9
Explanation: The while
loop starts with i = 0
and continues until i != 10
is false, i.e., when i
becomes 10. Inside the loop, it prints i
and increments i
by 1. So, it prints 0, then 1, then 2, and so on up to 9. When i
becomes 10, the condition i != 10
is false, and the loop exits without printing 10. Thus, the output is 0,1,2,3,4,5,6,7,8,9, each on a new line.
Question 9: What is the output of the following program?
#includeusing namespace std; int main() { int x = 7; do { cout << x << " "; x--; } while (x > 4); return 0; }
Answer: B. 7 6 5
Explanation: The do-while
loop executes at least once. Initially, x = 7
. It prints x
(7) and decrements x
to 6. The condition x > 4
is true (6 > 4), so it prints 6, decrements to 5, and the condition is still true (5 > 4). It prints 5, decrements to 4, and now the condition is false (4 > 4 is false), so the loop stops. The output is 7 6 5
.
Question 10: 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
.