C Programming Questions and Answers on Scope of a Variable for Freshers

1. What is the output of this C code?

  1.     #include 
  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 as i value is not zero being automatic variable
Answer: a
2. What is the output of this C code (without linking the source file in which ary1 is defined)?

  1.     #include 
  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
Answer: a
3. What is the output of this C code after linking with source file having definition of ary1?

  1.     #include 
  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
Answer: d
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
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
6. Comment on the output of this 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
7. Which variable has the longest scope?

  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
8. Comment on the output of this 2 C code?

  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) They are both the same
b) Scope of C is till the end of program
c) All operation in Program 1 can also be performed in Program 2
d) None of the mentioned
Answer: c
9. The sequence of allocation and deletion of variables for the following code is.

  1.     #include 
  2.     int main()
  3.     {
  4.         int a;
  5.         {
  6.             int b;
  7.         }
  8.     }
a) a->b, a->b
b) a->b, b->a
c) b->a, a->b
d) b->a, b->a
Answer: b
10. Array sizes are optional during array declaration by using ______ keyword.

a) auto
b) static
c) extern
d) register
Answer: c
11. What is the output of this C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int x = 3;
  5.         {
  6.             x = 4;
  7.             printf("%d", x);
  8.         }
  9.     }
a) 4
b) 3
c) 0
d) Undefined
Answer: a
12. What is the output of this C code?

  1.     #include 
  2.     int x = 5;
  3.     void main()
  4.     {
  5.         int x = 3;
  6.         m();
  7.         printf("%d", x);
  8.     }
  9.     void m()
  10.     {
  11.         x = 8;
  12.         n();
  13.     }
  14.     void n()
  15.     {
  16.         printf("%d", x);
  17.     }
a) 8 3
b) 3 8
c) 8 5
d) 5 3
Answer: a
13. What is the output of this C code?

  1.     #include 
  2.     int x;
  3.     void main()
  4.     {
  5.         m();
  6.         printf("%d", x);
  7.     }
  8.     void m()
  9.     {
  10.         x = 4;
  11.     }
a) 0
b) 4
c) Compile time error
d) Undefined
Answer: b
14. What is the output of this C code?

  1.     #include 
  2.     static int x = 5;
  3.     void main()
  4.     {
  5.         int x = 9;
  6.         {
  7.             x = 4;
  8.         }
  9.         printf("%d", x);
  10.     }
a) 9
b) 5
c) 4
d) 0
Answer: c
15. What is the output of this C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         {
  5.             int x = 8;
  6.         }
  7.         printf("%d", x);
  8.     }
a) 8
b) 0
c) Undefined
d) Compile time error
Answer: d

Related

Data Structure Questions and Answers on Depth First Search for Freshers

1. Depth First Search is equivalent to which of the traversal in the Binary Trees? a) Pre-order Traversalb) Post-order Traversalc) Level-order Traversald) In-order Traversal Answer: a Explanat...

Data Structure Questions and Answers on Merge Sort for Freshers

1. QuickSort can be categorized into which of the following? a) Brute Force techniqueb) Divide and conquerc) Greedy algorithmd) Dynamic programming Answer: b Explanation: First you divide(part...

Data Structures Multiple Choice Questions and Answers on Uniform Binary Search

1. When is the uniform binary search an optimization over the usual binary search? a) A table lookup is generally faster than an addition and a shiftb) Many searches will be performed on the same a...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item