Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CLA-11-03 Exam - Topic 3 Question 3 Discussion

Posted by Beata on Sep 25, 2026 at 04:20 PM

-

What happens if you try to compile and run this program?

#include

int *f();

int main (int argc, char *argv[]) {

int *p;

p = f();

printf("%d",*p);

return 0;

}

int *f() {

static v = 1;

return &v;

}

Choose the right answer:

Suggested Answer: A

The program outputs 1 because the static variable v is initialized to 1 inside the f function, and it is visible to the main function. The f function returns the address of v, which is a pointer to an int. The main function dereferences the pointer and assigns it to p, which is another pointer to an int. Then, the main function prints the value of *p, which is the same as dereferencing p again. Therefore, the output of the program is:

f() = &v p = f() printf(''%d'',*p) = &v = 1

The other options are incorrect because they either do not match the output of the program or do not use the correct concept of static variables.


Get Premium CLA-11-03 Questions as Interactive Web-Based Practice Test or PDF Real Exam Format Detailed Explanations Instant Access Get Your Practice Test
4 comments · newest first
ME
Melissia 3 days ago
I’m confused about the output. Does the static variable get initialized every time? I thought it would just return the same value.
MO
Mozelle 8 days ago
I practiced a similar question where a function returned a pointer, and it worked fine. I guess it should compile without errors.
AN
Antione 13 days ago
I'm not entirely sure, but I remember something about static variables retaining their value. Could it be option A?
CA
Catalina 18 days ago
I think the function `f()` returns a pointer to a static variable, so it should output 1.
0/2000 characters

Save Cancel