200k views
2 votes
What will the following code output?

int *numbers = new int[5];

for (int i = 0; i <= 4; i++)
*(numbers + i) = i;

cout << numbers[2] << endl;

A) Five memory addresses
B) 0
C) 3
D) 2
E) 1

User Mitul Shah
by
9.4k points

1 Answer

7 votes

Final answer:

The code will output the number 2.

Step-by-step explanation:

The code will output the number 2. When the code runs, it creates a dynamic array of integers and assigns it to the variable 'numbers'. The for loop initializes 'i' to 0 and iterates through the array, assigning the value of 'i' to each element. So, *(numbers + 2) will be equal to 2. Finally, when 'numbers[2]' is printed to the console, the output will be 2.

User Chim
by
8.6k points