C Programming Questions and Answers on Static Variables for Freshers

https://www.computersprofessor.com/2017/12/c-programming-questions-and-answers-on_6.html
1. What is the output of this C code?
#include
void main()
{
m();
m();
}
void m()
{
static int x = 5;
x++;
printf("%d", x);
}
a) 6 7
b) 6 6
c) 5 5
d) 5 6
b) 6 6
c) 5 5
d) 5 6
Answer: a
2. What is the output of this C code?
#include
void main()
{
static int x;
printf("x is %d", x);
}
a) 0
b) 1
c) Junk value
d) Run time error
b) 1
c) Junk value
d) Run time error
Answer: a
3. What is the output of this C code?
#include
static int x;
void main()
{
int x;
printf("x is %d", x);
}
a) 0
b) Junkvalue
c) Run time error
d) Nothing
b) Junkvalue
c) Run time error
d) Nothing
Answer: b
4. What is the output of this C code?
#include
void main()
{
static double x;
int x;
printf("x is %d", x);
}
a) Nothing
b) 0
c) Compile time error
d) Junkvalue
b) 0
c) Compile time error
d) Junkvalue
Answer: c
5. What is the output of this C code?
#include
void main()
{
static int x;
if (x++ < 2)
main();
}
a) Infinite calls to main
b) Run time error
c) Varies
d) main is called twice
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
in file test.c
#include
#include "test.h"
int main()
{
i = 10;
printf("%d ", i);
foo();
}
in file test1.c
#include
#include "test.h"
int foo()
{
printf("%d\n", i);
}
in file test.h
#include
#include
static int i;
a) 1 0 0
b) 0 0
c) 10 10
d) None of the mentioned
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?
#include
int main()
{
foo();
foo();
}
void foo()
{
int i = 11;
printf("%d ", i);
static int j = 12;
j = j + 1;
printf("%d\n", j);
}
a) 11 12 11 12
b) 11 13 11 14
c) 11 12 11 13
d) Compile time error
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?
#include
void func();
int main()
{
static int b = 20;
func();
}
void func()
{
static int b;
printf("%d", b);
}
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
b) Output will be 20
c) Output will be a garbage value
d) Compile time error due to redeclaration of static variable
Answer: a