C Programming MCQ Questions and Answers on Data Types and Storage Classes 2

Image
C MCQ Questions and Answers

Learn C Data Types and Storage Classes with MCQ Questions and Answers. Find questions on types of Storage Classes like Life, Scope and the default value of variables. Easily attend exams after reading these Multiple Choice Questions.

Go through C Theory Notes on Data Types and Storage Classes before studying questions.



1) What is a C Storage Class.?
A) C Storage decides where to or which memory store the variable.
B) C Storage Class decides what is the default value of a variable.
C) C Storage Class decides what is the Scope and Life of a variable.
D) All the above.
Answer [=]
D
2) Every C Variable must have.?
A) Type
B) Storage Class
C) Both Type and Storage Class
D) Either Type or Storage Class
Answer [=]
C
Explanation:

Yes. A C Variable should have both Type and Storage Class.

Eg.

int a=5; //By default its Storage Class is auto.
auto int b=10;
3) Find a C Storage Class below.
A) static
B) auto
C) register & extern
D) All the above
Answer [=]
D
4) What is the default C Storage Class for a variable.?
A) static
B) auto
C) register
D) extern
Answer [=]
B
Explanation:
int a=5; // auto is by default
auto int b=10; // storage class auto is explicitly mentioned.
5) Choose a right answer.
A) auto variable is stored in 'Memory'. static variable is stored in 'Memory'. extern variable is stored in 'Memory'. register variable is stored in 'Memory'.
B) auto variable is stored in 'Memory'. static variable is stored in 'Memory'. extern variable is stored in 'Memory'. register variable is stored in 'Register'.
C) auto variable is stored in 'Register'. static variable is stored in 'Register'. extern variable is stored in 'Register'. register variable is stored in 'Memory'.
D) auto variable is stored in 'Register'. static variable is stored in 'Register'. extern variable is stored in 'Register'. register variable is stored in 'Register'.
Answer [=]
B
Explanation:

Only a register variable is stored in CPU Memory called Registers. Other variables are stored in RAM.

6) A register variable is stored in a Register. Where does a Register Present in a Computer.?
A) RAM ( Random Access Memory )
B) ROM ( Read Only Memory )
C) CPU (Central Processing Unit )
D) DMA ( Direct Memory Access )
Answer [=]
C
Explanation:

Yes. Registers are part of a CPU to store variable whose value changes frequently. Loop variables for example.

 

register int i=1;
while(i = 10)
{
	printf("i=%d\n", i); 
}
7) Variables of type auto, static and extern are all stored in .?
A) ROM
B) RAM
C) CPU
D) Compiler
Answer [=]
B


8) Find a right answer.
A) Default value of auto variable = Garbage Value Default value of static = Garbage Value Default value of extern = Garbage Value Default value of register = Garbage Value
B) Default value of auto variable = zero Default value of static = zero Default value of extern = zero Default value of register = zero
C) Default value of auto variable = Garbage Default value of static = zero Default value of extern = zero Default value of register = Garbage
D) Default value of auto variable = zero Default value of static = Garbage Default value of extern = Garbage Default value of register = zero
Answer [=]
C
9) Find a correct statement.
A) Scope of auto variable = local to block or function Scope of register variable = local to block or function Scope of static variable = local to block or function Scope of extern variable = global or available to all functions and blocks
B) Scope of auto variable = global or available to all functions and blocks Scope of register variable = global or available to all functions and blocks Scope of static variable = global or available to all functions and blocks Scope of extern variable = local to block or function
C) Scope of auto variable = global or available to all functions and blocks Scope of register variable =  local to block or function Scope of static variable = global or available to all functions and blocks Scope of extern variable = local to block or function
D) Scope of auto variable = local to block or function Scope of register variable = global or available to all functions and blocks Scope of static variable = local to block or function Scope of extern variable = global or available to all functions and blocks
Answer [=]
A
10) Choose a correct statement.
A) Life of an auto variable = persists between function calls Life of an register variable = with in the block or function Life of an static variable = persists between function calls Life of an extern variable = until program ends
B) Life of an auto variable = persists between function calls Life of an register variable = until program ends Life of an static variable = persists between function calls Life of an extern variable = with in the block or function
C) Life of an auto variable = until program ends Life of an register variable = until program ends Life of an static variable = until program ends Life of an extern variable = until program ends
D) Life of an auto variable = with in the block or function Life of an register variable = with in the block or function Life of an static variable = persists between function calls Life of an extern variable = until program ends
Answer [=]
D
11) Which among the following is a Local Variable.?
A) register
B) auto
C) static
D) extern
Answer [=]
B
12) Which among the following is a Global Variable.?
A) auto
B) register
C) static 
D) extern
Answer [=]
D
Explanation:

#include<stdio.h>

#include<myutils.c>

void myshow();
int a=10; // a is a global variable.
//Notice that there is no 'extern' keyword.
void main()
{
	printf("a=%d ", a);
	a = a + 1;
	myshow();
	show();

}

void show()
{
	printf("a=%d ", a);
}

//myutils.c file
//here we used extern to inform compiler
//about possible definition somewhere
extern int a;
void myshow()
{
	printf("a=%d ", a);
	a++;

}

//Output is 10 11 12

 

13) Choose a correct statement about static variable.
A) A static global variable can be accessed in other files.
B) A static global variable can be used only in a file in which it is declared.
C) A static global variable can not be declared without extern keyword.
D) Default value of a static variable is -1.
Answer [=]
B
Explanation:
#include<stdio.h>
static int a=15;
void myshow();
int main()
{
	printf("%d", a);
	myshow();
	return 0;
}


//myutils.c file
extern int a;
void myshow()
{
	printf("%d", a);
}


//Compiler error.
//You can not use static global varible outside of file
14)
register float a = 3.14f;
Choose right statement.
A) Variable a is stored in CPU registers for fast access.
B) Variable a is converted to int and then stored in a CPU register.
C) register Storage Class is ignored and treated as
auto float a = 3.14f;
D) You get a compiler error as you can not store non integer value in a CPU register.
Answer [=]
C
Explanation:

Yes. CPU register can not store anything more than 16 bits or 2 bytes. You will not any compiler errors. It will be treated just like an auto Storage Class variable.



15) What is the difference between Declaration and Definition.?
A) Declaration does allocate memory for a variable. Definition does allocate memory for a variable.
B) Declaration does allocate memory for a variable. Definition does not allocate memory for a variable.
C) Declaration does not allocate memory for a variable. Definition does allocate memory for a variable.
D) Declaration does not allocate memory for a variable. Definition does not allocate memory for a variable.
Answer [=]
C
Explanation:
int a; //Definition. Allocates memory.

extern int b; //Does not allocate memory.
16) Choose a right statement.
A) A non static global variable can not be used in included files.
B) A non static global variable can be used or referred to inside included files.
C) A non static global variable does not live till the end of program execution.
D) None of the above
Answer [=]
B
17) Choose a right statement.
A) Redeclaration of a variable is Ok.
B) Redefinition of a variable is not Ok.
C) Definition of a variable uses memory blocks.
D) All the above.
Answer [=]
D
18) Choose a correct statement.
A) Register variables are usually fast retrieving variables.
B) Static variables are usually maintain their values between function calls.
C) Auto variables release their memory after the block or function where they are declared.
D) All the above.
Answer [=]
D
19) Choose a right statement.
A) Variables of type auto are stored in Stack memory.
B) Variable of type Static are stored in Segmented Memory.
C) Variables of type register are stored in Micro Processor Memory.
D) All the above.
Answer [=]
D
20) Choose a right statement.
A) Variables of type auto are initialized fresh for each block or function call.
B) Variables of type static are initialized only first time the block or function is called.
C) Variables of type register are initialized each time the block or function is executed.
D) All the above.
Answer [=]
D