C Programming Questions and Answers – Scope of a Variable – 1

«
»

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.

advertisement

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

  1.     #include <stdio.h>
  2.     int i;
  3.     int main()
  4.     {
  5.         extern int i;
  6.         if (i == 0)
  7.             printf("scope rules\n");
  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
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

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

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         extern ary1[];
  5.         printf("scope rules\n");
  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
View Answer

Answer: a
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         extern ary1[];
  5.         printf("%d\n", 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
View Answer

Answer: d
Explanation: None.
advertisement

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

Answer: d
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

Answer: d
Explanation: None.

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

  1.     #include <stdio.h>
  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
View Answer

Answer: c
Explanation: None.

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

  1.     #include <stdio.h>
  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
View Answer

Answer: b
Explanation: None.

8. Comment on the following 2 C programs.

  1.     #include <stdio.h> //Program 1
  2.     int main()
  3.     {
  4.         int a;
  5.         int b;
  6.         int c;
  7.     }
  8.  
  9.    #include <stdio.h> //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
View Answer

Answer: c
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.