Variable Declaration And Scope

8 Questions | Total Attempts: 108

SettingsSettingsSettings
Please wait...
Variable Declaration And Scope

Questions and Answers
  • 1. 
    Consider the following two C linesint var1; extern int var2;Which of the following statements is correct
    • A. 

      Both statements only declare variables, don't define them

    • B. 

      First statement declares and defines var1, but second statement only declares var2

    • C. 

      Both statements declare define variables var1 and var2

  • 2. 
    Predict the output#include <stdio.h> int var = 20; int main() {     int var = var;     printf("%d ", var);     return 0; } 
    • A. 

      Garbage Value

    • B. 

      20

    • C. 

      Compiler Error

    • D. 

      Option 4

  • 3. 
    #include <stdio.h> extern int var; int main() {     var = 10;     printf("%d ", var);     return 0; }
    • A. 

      Compiler Error: var is not defined

    • B. 

      20

    • C. 

      0

  • 4. 
    #include <stdio.h> extern int var = 0; int main() {     var = 10;     printf("%d ", var);     return 0; }
    • A. 

      10

    • B. 

      Compiler Error: var is not defined

    • C. 

      0

  • 5. 
    Output?int main() {   {       int var = 10;   }   {       printf("%d", var);    }   return 0; }
    • A. 

      10

    • B. 

      Compiler Error

    • C. 

      Garbage value

  • 6. 
    Output?#include <stdio.h> int main() {   int x = 1, y = 2, z = 3;   printf(" x = %d, y = %d, z = %d \n", x, y, z);   {        int x = 10;        float y = 20;        printf(" x = %d, y = %f, z = %d \n", x, y, z);        {              int z = 100;              printf(" x = %d, y = %f, z = %d \n", x, y, z);        }   }   return 0; }
    • A. 

      X = 1, y = 2, z = 3 x = 10, y = 20.000000, z = 3 x = 1, y = 2, z = 100

    • B. 

      Compiler Error

    • C. 

      X = 1, y = 2, z = 3 x = 10, y = 20.000000, z = 3 x = 10, y = 20.000000, z = 100

    • D. 

      X = 1, y = 2, z = 3 x = 1, y = 2, z = 3 x = 1, y = 2, z = 3

  • 7. 
    Int main() {   int x = 032;   printf("%d", x);   return 0; }
    • A. 

      32

    • B. 

      0

    • C. 

      26

    • D. 

      50

  • 8. 
    Consider the following C program, which variable has the longest scope?int a; int main() {    int b;    // ..   // ..} int c; 
    • A. 

      A

    • B. 

      B

    • C. 

      C

    • D. 

      All have same scope

Back to Top Back to top