This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Scope of a Variable – 1”.
Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.
1. What will be the output of the following C code?
#include <stdio.h>
int i;
int main()
{
extern int i;
if (i == 0)
printf("scope rules\n");
}
a) scope rules
b) Compile time error due to multiple declaration
c) Compile time error due to not defining type in statement extern i
d) Nothing will be printed as value of i is not zero because i is an automatic variable
View Answer
Explanation: None.
2. What will be the output of the following C code (without linking the source file in which ary1 is defined)?
#include <stdio.h>
int main()
{
extern ary1[];
printf("scope rules\n");
}
a) scope rules
b) Linking error due to undefined reference
c) Compile time error because size of array is not provided
d) Compile time error because datatype of array is not provided
View Answer
Explanation: None.
3. What will be the output of the following C code (after linking to source file having definition of ary1)?
#include <stdio.h>
int main()
{
extern ary1[];
printf("%d\n", ary1[0]);
}
a) Value of ary1[0];
b) Compile time error due to multiple definition
c) Compile time error because size of array is not provided
d) Compile time error because datatype of array is not provided
View Answer
Explanation: None.
4. What is the scope of an external variable?
a) Whole source file in which it is defined
b) From the point of declaration to the end of the file in which it is defined
c) Any source file in a program
d) From the point of declaration to the end of the file being compiled
View Answer
Explanation: None.
5. What is the scope of a function?
a) Whole source file in which it is defined
b) From the point of declaration to the end of the file in which it is defined
c) Any source file in a program
d) From the point of declaration to the end of the file being compiled
View Answer
Explanation: None.
6. Comment on the output of the following C code.
#include <stdio.h>
int main()
{
int i;
for (i = 0;i < 5; i++)
int a = i;
printf("%d", a);
}
a) a is out of scope when printf is called
b) Redeclaration of a in same scope throws error
c) Syntax error in declaration of a
d) No errors, program will show the output 5
View Answer
Explanation: None.
7. Which variable has the longest scope in the following C code?
#include <stdio.h>
int b;
int main()
{
int c;
return 0;
}
int a;
a) a
b) b
c) c
d) Both a and b
View Answer
Explanation: None.
8. Comment on the following 2 C programs.
#include <stdio.h> //Program 1
int main()
{
int a;
int b;
int c;
}
#include <stdio.h> //Program 2
int main()
{
int a;
{
int b;
}
{
int c;
}
}
a) Both are same
b) Scope of c is till the end of the main function in Program 2
c) In Program 1, variables a, b and c can be used anywhere in the main function whereas in Program 2, variables b and c can be used only inside their respective blocks
d) None of the mentioned
View Answer
Explanation: None.
Sanfoundry Global Education & Learning Series – C Programming Language.
To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Computer Science MCQs
- Apply for C Internship
- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Check Computer Science Books