C Programming Questions and Answers on Static Variables for Freshers

1. What is the output of this 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
2. What is the output of this 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
3. What is the output of this 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
4. What is the output of this 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
5. What is the output of this 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
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
7. Which of the following cannot be static in C?

a) Variables
b) Functions
c) Structures
d) None of the mentioned
Answer: d
8. What is the output of code given below if these two files are linked and run?
in file test.c

  1.     #include 
  2.     #include "test.h"
  3.     int main()
  4.     {
  5.         i = 10;
  6.         printf("%d ", i);
  7.         foo();
  8.     }
  9.  
  10.     in file test1.c
  11.     #include 
  12.     #include "test.h"
  13.     int foo()
  14.     {
  15.         printf("%d\n", i);
  16.     }
  17.  
  18.     in file test.h
  19.     #include 
  20.     #include 
  21.     static int i;
a) 1 0 0
b) 0 0
c) 10 10
d) None of the mentioned
Answer: d
9. Functions have static qualifier for its declaration by default.

a) true
b) false
c) Depends on the compiler
d) Depends on the standard
Answer: b
10. Is initialisation mandatory for local static variables?

a) Yes
b) No
c) Depends on the compiler
d) Depends on the standard
Answer: b
11. What is the output of this C code?

  1.     #include 
  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
Answer: b
12. 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
Answer: b
13. 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
Answer: b
14. Which of the following is true for 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
Answer: b
15. Comment on the output of this C code?

  1.     #include 
  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
Answer: a

Related

C Programming Multiple Choice Questions and Answers on Mathematical Functions for Freshers

1. What is the output of this C code? #include #include int main() { int i = 90; printf("%f\n", sin(i)); return 0; } a) Compile...

Java Multiple Choice Questions & Answers on Applets Fundamentals for Freshers

1. Which of these functions is called to display the output of an applet? a) display()b) paint()c) displayApplet()d) PrintApplet() Answer: b Explanation: Whenever the applet requires to redraw it...

C Programming Multiple choice Questions and Answers on Storage Management (Dynamic Memory Allocation) for Freshers

1. The function ____ obtains block of memory dynamically. a) callocb) mallocc) Both calloc & mallocd) free Answer: c 2. void * malloc(size_t n) returns a) Pointer to n bytes of uninitialized...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item