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 6 Discussion

What happens when you compile and run the following program?#include int fun (void) {static int i = 1;i += 2;return i;}int main (void) {int k, 1;k = fun ();1 = fun () ;printf ("%d", 1 - k);return 0;}Choose the right answer:
A) The program outputs 2
B) The program outputs 4
C) The program outputs 1
D) The program outputs 0
E) The program outputs 3

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

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

What happens when you compile and run the following program?

#include

int fun (void) {

static int i = 1;

i += 2;

return i;

}

int main (void) {

int k, 1;

k = fun ();

1 = fun () ;

printf ("%d", 1 - k);

return 0;

}

Choose the right answer:

Show Suggested Answer Hide Answer
Suggested Answer: A

The provided program has a few key points to consider:

1. fun is a function that uses a static variable i. This means i retains its value between function calls. It's initialized to 1 and then incremented by 2 each time fun is called.

2. The main function calls fun twice, assigning the results to k and l (though there's a typo in the variable name l, it should be l = fun();, not 1 = fun();).

Let's step through the code:

* First call to fun: i starts at 1, increments by 2, so i becomes 3. This value (3) is as-signed to k.

* Second call to fun: i is now 3, increments by 2 again, so i becomes 5. This value (5) is assigned to l.

Finally, the printf statement attempts to print l - k, which is 5 - 3, resulting in 2.

So, the correct answer is:

A . The program outputs 2.


Contribute your Thoughts:

0/2000 characters

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


Save Cancel