What will be the output of the below code? #include <iostream> using namespace…

2025

What will be the output of the below code?

#include <iostream>
using namespace std;

int main()
{
    int arr[2] = {1, 2};

    cout << 0[arr] << ", " << 1[arr] << endl;

    return 0;
}

  1. A.

    1, 2

  2. B.

    Syntax error

  3. C.

    Run time error

  4. D.

    None of these

Attempted by 90 students.

Show answer & explanation

Correct answer: A

In C++, array indexing is symmetric: 0[arr] equals arr[0], and 1[arr] equals arr[1]. The code outputs the values at indices 0 and 1, which are 1 and 2 respectively.

This demonstrates that the array name acts as a pointer, and index notation works in either order.

Explore the full course: Tpsc Assistant Technical Officer