New Year Sale 2026! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CPA-21-02 Exam - 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:

0/2000 characters
Erick
3 months ago
This is basic stuff, it’s definitely not 10 or ?1.
upvoted 0 times
...
Belen
4 months ago
Just to clarify, it allocates an array of size 2.
upvoted 0 times
...
Belen
4 months ago
Wait, are you sure? I thought it might be 0.
upvoted 0 times
...
Selma
4 months ago
Totally agree, it's definitely 1!
upvoted 0 times
...
Cheryl
4 months ago
It prints: 1
upvoted 0 times
...
Jeff
5 months ago
I’m confused about the output. I thought it might print something weird like ?1, but that doesn’t seem right.
upvoted 0 times
...
Ling
5 months ago
I practiced a similar question where the output was based on the last assigned value. I think this one will print 1.
upvoted 0 times
...
Mirta
5 months ago
I'm not entirely sure, but I remember something about how arrays work in C++. I feel like it should print 0 instead.
upvoted 0 times
...
Cherrie
5 months ago
I think the code initializes an array and assigns values, so it should print the value at index 1, which is 1.
upvoted 0 times
...
Serita
5 months ago
Ah, I see what's going on here. The key is that we're not initializing the memory before printing the second element. So the output could be unpredictable, like D. I'll make sure to double-check my work on this type of question.
upvoted 0 times
...
Joseph
5 months ago
Wait, I'm confused. Didn't we learn that if you don't initialize the dynamically allocated memory, it can contain random values? I'm not sure the answer is as straightforward as it seems.
upvoted 0 times
...
Terrilyn
5 months ago
Okay, let's see. We're dynamically allocating an array of 2 integers, and then printing the second element. I'm guessing the answer is B, since the array indices start at 0.
upvoted 0 times
...
Jess
5 months ago
Hmm, I'm a bit unsure about this one. I know we covered dynamic memory in class, but I'm having trouble remembering the details. Let me think this through step-by-step.
upvoted 0 times
...
Kati
5 months ago
This looks like a pretty straightforward dynamic memory allocation question. I'm pretty confident I can figure this one out.
upvoted 0 times
...
Billye
5 months ago
This seems straightforward. The question states that I should review the report after running the sensor with the Aggressive Detection slider configuration, so the "Prevention Policy Audit Trail" report is the one I should check.
upvoted 0 times
...
Kimi
5 months ago
Okay, I think I've got this. The vendor should be able to request a new user account and view/modify contact person information, but they can't maintain invoices or delete contacts they didn't create.
upvoted 0 times
...
Cassie
5 months ago
Ah, I remember learning about this in class. The three synchronized components are policies, objects, and logs. I'll select those.
upvoted 0 times
...
Maurine
6 months ago
Easy peasy, the question clearly states that 4 connectors are required for Cisco Spark to be fully enabled. I've got this one locked down.
upvoted 0 times
...
Georgene
10 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
9 months ago
User 3: It prints: 1
upvoted 0 times
...
Leigha
9 months ago
User 2: I believe it prints: 1
upvoted 0 times
...
Kristian
9 months ago
User 1: I think it prints: 1
upvoted 0 times
...
...
Grover
10 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
11 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
11 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
Tomas
9 months ago
Stanton: Exactly! Understanding how pointers and arrays work in C++ is crucial for handling dynamic memory allocation.
upvoted 0 times
...
Janessa
9 months 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
10 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
10 months ago
User 1: It prints: 1
upvoted 0 times
...
...
Serina
11 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
10 months ago
Yes, you're correct. It prints: 1
upvoted 0 times
...
Salome
10 months ago
I think it prints: 1
upvoted 0 times
...
...
Yolando
11 months ago
I'm not sure, but I think it prints: 1 as well
upvoted 0 times
...
Sharika
11 months ago
I agree with Adelina, because t[1] is assigned the value of 1
upvoted 0 times
...
Adelina
11 months ago
I think it prints: 1
upvoted 0 times
...

Save Cancel