250+ TOP MCQs on Scope of a Variable and Answers

C programming quiz on “Scope of a Variable”. One shall practice these quizzes to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C quiz comes with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C programming quiz on “Scope of a Variable” along with answers, explanations and/or solutions:

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

  1.     #include 
  2.     int i;
  3.     int main()
  4.     {
  5.         extern int i;
  6.         if (i == 0)
  7.             printf("scope rulesn");
  8.     }

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
Answer: a
Clarification: None.

2. What will be the output of the following C code (without linking the source file in which ary1 is defined)?

  1.     #include 
  2.     int main()
  3.     {
  4.         extern ary1[];
  5.         printf("scope rulesn");
  6.     }

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
Answer: a
Clarification: None.

3. What will be the output of the following C code (after linking to source file having definition of ary1)?

  1.     #include 
  2.     int main()
  3.     {
  4.         extern ary1[];
  5.         printf("%dn", ary1[0]);
  6.     }

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
Answer: d
Clarification: 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
Answer: d
Clarification: 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
Answer: d
Clarification: None.

6. Comment on the output of the following C code.

  1.     #include 
  2.     int main()
  3.     {
  4.         int i;
  5.         for (i = 0;i < 5; i++)
  6.         int a = i;
  7.         printf("%d", a);
  8.     }

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
Answer: c
Clarification: None.

7. Which variable has the longest scope in the following C code?

  1.     #include 
  2.     int b;
  3.     int main()
  4.     {
  5.         int c;
  6.         return 0;
  7.     }
  8.     int a;

a) a
b) b
c) c
d) Both a and b
Answer: b
Clarification: None.

8. Comment on the following 2 C programs.

  1.     #include  //Program 1
  2.     int main()
  3.     {
  4.         int a;
  5.         int b;
  6.         int c;
  7.     }
  8.  
  9.    #include  //Program 2
  10.     int main()
  11.     {
  12.         int a;
  13.         {
  14.             int b;
  15.         }
  16.         {
  17.             int c;
  18.         }
  19.     }

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
Answer: c
Clarification: None.

  250+ TOP MCQs on Precedence and Order of Evaluation and Answers

Leave a Comment

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

Scroll to Top