C Programming Questions and Answers – Static Variables – 2

«
»
Sanfoundry’s 1000+ MCQs on C helps anyone preparing for placement in Cisco and other companies. Anyone looking for Cisco placement papers should practice these 1000+ questions continuously for 2-3 months, thereby ensuring a top position in placements.

Here is a listing of C programming questions on “Static Variables” along with answers, explanations and/or solutions:

1. What will be the output of the following C code if these two files namely test.c and test1.c are linked and run?

advertisement
  1.       -------file test.c-------
  2.     #include <stdio.h>
  3.     #include ""test.h""
  4.     int main()
  5.     {
  6.         i = 10;
  7.         printf(""%d "", i);
  8.         foo();
  9.     }
  10.  
  11.     -----file test1.c------
  12.     #include <stdio.h>
  13.     #include ""test.h""
  14.     int foo()
  15.     {
  16.         printf(""%d\n"", i);
  17.     }
  18.  
  19.     -----file test.h-----
  20.     #include <stdio.h>
  21.     #include <stdlib.h>
  22.     static int i;

a) 10 0
b) 0 0
c) 10 10
d) Compilation Error
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

2. Functions have static qualifier for its declaration by default.
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer

Answer: b
Explanation: None.

3. Is initialisation mandatory for local static variables?
a) Yes
b) No
c) Depends on the compiler
d) Depends on the standard
View Answer

Answer: b
Explanation: None.
advertisement

4. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         foo();
  5.         foo();
  6.     }
  7.     void foo()
  8.     {
  9.         int i = 11;
  10.         printf("%d ", i);
  11.         static int j = 12;
  12.         j = j + 1;
  13.         printf("%d\n", j);
  14.     }

a) 11 12 11 12
b) 11 13 11 14
c) 11 12 11 13
d) Compile time error
View Answer

Answer: b
Explanation: None.
advertisement

5. Assignment statements assigning value to local static variables are executed only once.
a) True
b) False
c) Depends on the code
d) None of the mentioned
View Answer

Answer: b
Explanation: None.
advertisement

6. What is the format identifier for “static a = 20.5;”?
a) %s
b) %d
c) %f
d) Illegal declaration due to absence of data type
View Answer

Answer: b
Explanation: None.

7. Which of the following is true for the static variable?
a) It can be called from another function
b) It exists even after the function ends
c) It can be modified in another function by sending it as a parameter
d) All of the mentioned
View Answer

Answer: b
Explanation: None.

8. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     void func();
  3.     int main()
  4.     {
  5.         static int b = 20;
  6.         func();
  7.     }
  8.     void func()
  9.     {
  10.         static int b;
  11.         printf("%d", b);
  12.     }

a) Output will be 0
b) Output will be 20
c) Output will be a garbage value
d) Compile time error due to redeclaration of static variable
View Answer

Answer: a
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter