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

-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:
A) The program outputs 1
B) Compilation fails
C) The program outputs 3
D) The program outputs 2
E) The program outputs 0

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

Actual exam question for C++ Institute's CLA-11-03 exam
Question #: 3
Topic #: 3
[All CLA-11-03 Questions]

-

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:

Show Suggested Answer Hide 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.


Contribute your Thoughts:

0/2000 characters

Currently there are no comments in this discussion, be the first to comment!


Save Cancel