Mahindra Placement Papers 2025 – C++ Programming Section- Set-1

C++ Programming Quiz: Test Your Skills

C++ Programming Quiz: Test Your Skills

Question 1: What is the output of the following program?

#include 
using 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]);
}
        

A. 31

B. 41

C. 51

D. Compile error

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 2: What is the output of the following program?

#include 
using namespace std;

int main() {
    int x = 5;
    int *p = &x;
    *p = 10;
    cout << x;
    return 0;
}
        

A. 5

B. 10

C. 0

D. Compile error

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 3: What is the output of the following program?

#include 
using namespace std;

int main() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= i; j++) {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}
        

A. *
  **
  ***

B. ***
  **
  *

C. * * *
  * *
  *

D. Compile error

Answer: A. *
  **
  ***

Explanation: The outer loop runs from i = 1 to i = 3. For each i, the inner loop runs from j = 1 to j = i, printing a * each time. After the inner loop, cout << endl adds a newline. So, for i = 1, it prints 1 star; for i = 2, it prints 2 stars; for i = 3, it prints 3 stars.

Question 4: What is the output of the following program?

#include 
using namespace std;

class main {
public:
    main() {
        cout << "ctor is called\n";
    }
    ~main() {
        cout << "dtor is called\n";
    }
};

int main() {
    main m; // LINE 11
}
        

A. ctor is called
dtor is called

B. dtor is called

C. ctor is called

D. Compile error

Answer: A. ctor is called
dtor is called

Explanation: The program defines a class named main (though using main as a class name is generally poor practice, it’s syntactically valid here). In the main() function, an object m of type main is created at LINE 11. This triggers the constructor, which outputs ctor is called\n. When m goes out of scope at the end of main(), the destructor is called, outputting dtor is called\n. Thus, the full output is:
ctor is called
dtor is called

Question 5: What is the output of the following program?

#include 
using namespace std;

void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 3, y = 7;
    swap(x, y);
    cout << x << " " << y;
    return 0;
}
        

A. 3 7

B. 7 3

C. 7 7

D. Compile error

Answer: B. 7 3

Explanation: The swap function takes two integers by reference, meaning it can modify the original variables. Inside swap, a and b are references to x and y. Initially, x = 3 and y = 7. The swap function swaps their values, so x becomes 7 and y becomes 3. The output is 7 3.

Question 6: What is the output of the following program?

#include 
using namespace std;

int main() {
    int arr[3] = {1, 2, 3};
    int *ptr = arr;
    cout << *(ptr + 1);
    return 0;
}
        

A. 1

B. 2

C. 3

D. Compile error

Answer: B. 2

Explanation: The array arr contains {1, 2, 3}. The pointer ptr is set to the base address of arr. The expression ptr + 1 points to the second element of the array (index 1), and *(ptr + 1) dereferences it, giving the value 2.

Question 7: What is the output of the following program?

#include 
class ClassA {
public:
    ClassA() {
        cout << "ClassA";
    }
};

class ClassB : public ClassA {
public:
    ClassB() {
        cout << "ClassB";
    }
};

void main() {
    ClassB objB;
}
        

A. ClassA

B. ClassAClassB

C. ClassB

D. None

Answer: B. ClassAClassB

Explanation: The program defines two classes: ClassA and ClassB, where ClassB inherits from ClassA publicly. In main() (though void main() and are outdated, they are assumed to work here for the question's context), an object objB of type ClassB is created. Since ClassB inherits from ClassA, the constructor of ClassA is called first, printing ClassA, followed by the constructor of ClassB, printing ClassB. Thus, the output is ClassAClassB.

Question 8: What is the output of the following program?

#include 
using namespace std;

class Test {
public:
    int x;
    Test(int val) : x(val) {}
    void display() {
        cout << x;
    }
};

int main() {
    Test t(42);
    t.display();
    return 0;
}
        

A. 0

B. 42

C. 1

D. Compile error

Answer: B. 42

Explanation: The class Test has a constructor that initializes the member variable x with the value passed to it. An object t is created with the value 42, so t.x is 42. The display method prints x, which outputs 42.

Question 9: What is the output of the following program?

#include 
using namespace std;

int main() {
    int x = 10;
    if (x > 5) {
        int x = 20;
        cout << x;
    }
    cout << " " << x;
    return 0;
}
        

A. 10 10

B. 20 10

C. 20 20

D. Compile error

Answer: B. 20 10

Explanation: The variable x is initially 10. Inside the if block, a new variable x is declared with the value 20, which shadows the outer x. The cout << x inside the block prints the inner x, which is 20. After the block, the outer x (still 10) is printed. Thus, the output is 20 10.

Question 10: What is the output of the following program?

#include 
using namespace std;

int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    cout << factorial(4);
    return 0;
}
        

A. 12

B. 24

C. 4

D. Compile error

Answer: B. 24

Explanation: The factorial function calculates the factorial of a number recursively. For n = 4, the computation is:
factorial(4) = 4 * factorial(3)
factorial(3) = 3 * factorial(2)
factorial(2) = 2 * factorial(1)
factorial(1) = 1
So, factorial(4) = 4 * 3 * 2 * 1 = 24. The output is 24.