IAE Academy

ISRO CS 2025 Q.34: solution and answer

ISRO 2025 · CSQ.34 · 1 mark · MCQ

Consider following C++ snippet, which allocates memory?

int *ptr;
ptr = new int [4];

Which of the following statements may make the program crash when executed subsequently?

  1. (A)

    ptr++;

  2. (B)

    ptr += 3;

  3. (C)

    ptr[4] = 0x16;

  4. (D)

    ptr = null;

See the question in the paper

Answer and solution

ANSWER

Option (c), ptr[4] = 0x16;, writes one element past the end of the 4-int block and may crash the program.

Official answer: CSource: ISRO's final answer key of 15 May 2026

  1. new int [4] allocates a block of 4 ints, so the valid indices are ptr[0] to ptr[3], and ptr points to the first element.

  2. (a) ptr++ moves the pointer to element 1, which is still inside the block. Only the pointer value changes and nothing is read or written, so nothing crashes.

  3. (b) ptr += 3 moves the pointer to element 3, the last valid element. It is still inside the block, and no memory is accessed, so nothing crashes.

  4. (c) ptr[4] is the same as *(ptr + 4), which is one element past the end of the block. Writing 0x16 there is an out-of-bounds write. It has undefined behaviour: it can corrupt the heap's bookkeeping data or touch unmapped memory, so the program may crash.

  5. (d) null is not a C++ keyword (the null pointer is written nullptr or NULL), so as printed this is a compile-time error, not a run-time crash. Even with a valid null pointer, the assignment would only lose the address of the block, which is a memory leak and does not crash by itself.

  6. So only (c) can make the program crash when it runs.

KEY IDEA

A block from new int [n] has valid indices 0 to n-1. Moving a pointer inside the block is harmless, but reading or writing at index n is an out-of-bounds access with undefined behaviour, and it may crash.

WATCH OUT FOR

Treating ptr[4] as the last element of a 4-element array (indices run 0 to 3), or thinking ptr = null; crashes the program. Assigning a null pointer only leaks the block, and null is not even valid C++.

From the ISRO CS 2025 paper. Every question of this paper · The full paper (PDF, ISRO) · All 2 ISRO Pointers & arrays questions, with notes