Programming
April 14, 2024Question 6910 – UGC NET CS 2015 Dec – paper-3
April 14, 2024Programming
Question 19
|
Consider the following C program
int a, b, c = 0; void prtFun (void); int main () { static int a = 1; /* line 1 */ prtFun(); a += 1; prtFun(); printf ( "n %d %d " , a, b) ; } void prtFun (void) { static int a = 2; /* line 2 */ int b = 1; a += ++b; printf (" n %d %d " , a, b); }
What output will be generated by the given code segment if:
- Line 1 is replaced by auto int a = 1;
Line 2 is replaced by register int a = 2;
Question 19 Explanation:
Line 1 replaced by auto int a=1;
Line 2 replaced by register int a=2;
In main there will be no change if it is static or auto because of a+=1 the auto variable a is updated to 2 from 1.
In prtfun ( ), register makes a difference.
For first print statement a is updated to 4 & prints 4, 2.
But now it is not a static variable to retain the value of a to 4. So it becomes 2, when second function call takes place & prints 4, 2 again. There is no change in b, it acts like a local variable.
Hence,
4 2
4 2
2 0.
Line 2 replaced by register int a=2;
In main there will be no change if it is static or auto because of a+=1 the auto variable a is updated to 2 from 1.
In prtfun ( ), register makes a difference.
For first print statement a is updated to 4 & prints 4, 2.
But now it is not a static variable to retain the value of a to 4. So it becomes 2, when second function call takes place & prints 4, 2 again. There is no change in b, it acts like a local variable.
Hence,
4 2
4 2
2 0.
Correct Answer: D
Question 19 Explanation:
Line 1 replaced by auto int a=1;
Line 2 replaced by register int a=2;
In main there will be no change if it is static or auto because of a+=1 the auto variable a is updated to 2 from 1.
In prtfun ( ), register makes a difference.
For first print statement a is updated to 4 & prints 4, 2.
But now it is not a static variable to retain the value of a to 4. So it becomes 2, when second function call takes place & prints 4, 2 again. There is no change in b, it acts like a local variable.
Hence,
4 2
4 2
2 0.
Line 2 replaced by register int a=2;
In main there will be no change if it is static or auto because of a+=1 the auto variable a is updated to 2 from 1.
In prtfun ( ), register makes a difference.
For first print statement a is updated to 4 & prints 4, 2.
But now it is not a static variable to retain the value of a to 4. So it becomes 2, when second function call takes place & prints 4, 2 again. There is no change in b, it acts like a local variable.
Hence,
4 2
4 2
2 0.
Subscribe
Login
0 Comments