Independence Day Deal! Unlock 25% OFF Today – Limited-Time Offer - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPA-21-02 Topic 1 Question 18 Discussion

Actual exam question for C++ Institute's CPA-21-02 exam
Question #: 18
Topic #: 1
[All CPA-21-02 Questions]

What happens when you attempt to compile and run the following code?

#include

using namespace std;

int main()

{

int *t;

t = new int[2];

for (int i=0; i<2; i++) {

t[i] = i;

}

cout << t[1];

}

Show Suggested Answer Hide Answer
Suggested Answer: A, B

Contribute your Thoughts:

Georgene
2 months ago
This code is like a puzzle, but the solution is just a click away. I'm going to use my trusty debugger to figure this out.
upvoted 0 times
Helga
4 days ago
User 3: It prints: 1
upvoted 0 times
...
Leigha
5 days ago
User 2: I believe it prints: 1
upvoted 0 times
...
Kristian
15 days ago
User 1: I think it prints: 1
upvoted 0 times
...
...
Grover
2 months ago
Wait, did we forget to free the memory we allocated? I hope the examiner doesn't deduct points for that. Memory leaks are the bane of every programmer's existence.
upvoted 0 times
...
Francene
2 months ago
Haha, I bet the person who wrote this question is a real sadist. They're probably sitting back and laughing as they watch us struggle with this one.
upvoted 0 times
...
Remedios
2 months ago
Wow, this is a classic pointer question! I remember struggling with these in my exam. Good thing I brushed up on my dynamic memory allocation before taking this test.
upvoted 0 times
Janessa
9 days ago
User 3: I see, so the output is 1 because arrays are zero-indexed. The element at index 1 is the second element, which has a value of 1.
upvoted 0 times
...
Stanton
1 months ago
User 2: That's correct! The code allocates memory for an array of 2 integers and assigns values 0 and 1 to the elements. Then it prints the value at index 1, which is 1.
upvoted 0 times
...
Tamekia
1 months ago
User 1: It prints: 1
upvoted 0 times
...
...
Serina
2 months ago
The correct answer is B) It prints: 1. The code dynamically allocates an array of two integers and initializes them with 0 and 1 respectively. Then it prints the value at index 1, which is 1.
upvoted 0 times
Berry
1 months ago
Yes, you're correct. It prints: 1
upvoted 0 times
...
Salome
1 months ago
I think it prints: 1
upvoted 0 times
...
...
Yolando
2 months ago
I'm not sure, but I think it prints: 1 as well
upvoted 0 times
...
Sharika
2 months ago
I agree with Adelina, because t[1] is assigned the value of 1
upvoted 0 times
...
Adelina
3 months ago
I think it prints: 1
upvoted 0 times
...

Save Cancel
az-700  pass4success  az-104  200-301  200-201  cissp  350-401  350-201  350-501  350-601  350-801  350-901  az-720  az-305  pl-300  

Warning: Cannot modify header information - headers already sent by (output started at /pass.php:70) in /pass.php on line 77