What is the output of the following pseudocode ? int arr[] = {1, 2, 3, 4, 5}…

2026

What is the output of the following pseudocode ?

int arr[] = {1, 2, 3, 4, 5}
int *ptr = arr + 2
print(*ptr + *(ptr + 1))

  1. A.

    3

  2. B.

    5

  3. C.

    7

  4. D.

    9

Attempted by 228 students.

Show answer & explanation

Correct answer: C

  1. Array Initialization: An array arr is initialized with 5 elements:

    • arr = 1

    • arr = 2

    • arr = 3

    • arr = 4

    • arr = 5

  2. Pointer Arithmetic:

    In pointer arithmetic, arr refers to the memory address of the first element (arr).

    • arr + 2 moves the pointer forward by 2 elements, pointing to arr.

    • Therefore, int *ptr = arr + 2; sets ptr to point to the element 3.

  3. Dereferencing and Addition:

    • *ptr dereferences the pointer, which gives the value at arr, which is 3.

    • ptr + 1 points to the next consecutive element in memory, which is arr.

    • *(ptr + 1) dereferences that next position, giving the value at arr, which is 4.

  4. Final Print:

    • print(*ptr + *(ptr + 1)) becomes print(3 + 4), which outputs 7.

Explore the full course: Tpsc Assistant Technical Officer