250+ TOP MCQs on Static Variables and Answers

C questions and puzzles on “Static Variables” along with answers, explanations and/or solutions:

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

  1.     #include 
  2.     void main()
  3.     {
  4.         m();
  5.         m();
  6.     }
  7.     void m()
  8.     {
  9.         static int x = 5;
  10.         x++;
  11.         printf("%d", x);
  12.     }

a) 6 7
b) 6 6
c) 5 5
d) 5 6

Answer: a
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         static int x;
  5.         printf("x is %d", x);
  6.     }

a) 0
b) 1
c) Junk value
d) Run time error

Answer: a
Clarification: None.

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

  1.     #include 
  2.     static int x;
  3.     void main()
  4.     {
  5.         int x;
  6.         printf("x is %d", x);
  7.     }

a) 0
b) Junkvalue
c) Run time error
d) Nothing

Answer: b
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         static double x;
  5.         int x;
  6.         printf("x is %d", x);
  7.     }

a) Nothing
b) 0
c) Compile time error
d) Junkvalue

Answer: c
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         static int x;
  5.         if (x++ < 2)
  6.         main();
  7.     }

a) Infinite calls to main
b) Run time error
c) Varies
d) main is called twice

Answer: d
Clarification: None.

6. Which of following is not accepted in C?
a) static a = 10; //static as
b) static int func (int); //parameter as static
c) static static int a; //a static variable prefixed with static
d) all of the mentioned

Answer: c
Clarification: None.

7. Which of the following cannot be static in C?
a) Variables
b) Functions
c) Structures
d) None of the mentioned

Answer: d
Clarification: None.

Leave a Reply

Your email address will not be published. Required fields are marked *