• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C Quiz - 111

Question 1

Pick the best statement for the following program snippet: C
#include \"stdio.h\"
void foo(void)
{
 static int staticVar;
 staticVar++;
 printf(\"foo: %d\\n\",staticVar);
}

void bar(void)
{
 static int staticVar;
 staticVar++;
 printf(\"bar: %d\\n\",staticVar);
}

int main()
{
 foo(), bar(), foo();
 return 0;
}
  • Compile error because same static variable name is used in both foo and bar. Since these static variables retain their values even after function is over, same name can’t be used in both the functions.
  • Compile error because semicolon isn’t used while calling foo() and bar() in side main function.
  • No compile error and only one copy of staticVar would be used across both the functions and that’s why final value of that single staticVar would be 3.
  • No compile error and separate copies of staticVar would be used in both the functions. That’s why staticVar in foo() would be 2 while staticVar in bar() would be 1.

Question 2

Pick the best statement for the below: C
int arr[50] = {0,1,2,[47]=47,48,49};
  • This isn’t allowed in C and it’ll give compile error
  • This is allowed in C as per standard. Basically, it’ll initialize arr[0], arr[1], arr[2], arr[47], arr[48] and arr[49] to 0,1,2,47,48 and 49 respectively. The remaining elements of the array would be initialized to 0.

Question 3

Pick the best statement for the below program: C
#include \"stdio.h\"
 
void fun(int n)
{
   int idx;
   int arr1[n] = {0};
   int arr2[n];
 
   for (idx=0; idx<n; idx++)
       arr2[idx] = 0;    
}
 
int main()
{
   fun(4);
   return 0;
}
  • Definition of both arr1 and arr2 is incorrect because variable is used to specify the size of array. That’s why compile error.
  • Apart from definition of arr1 arr2, initialization of arr1 is also incorrect. arr1 can’t be initialized due to its size being specified as variable. That’s why compile error.
  • Initialization of arr1 is incorrect. arr1 can’t be initialized due to its size being specified as variable. That’s why compile error.
  • No compile error. The program would define and initializes both arrays to ZERO.

Question 4

Pick the best statement for the below program: C
#include \"stdio.h\"

int size = 4;
int arr[size];

int main()
{
 if(arr[0])
  printf(\"Initialized to ZERO\");
 else
  printf(\"Not initialized to ZERO\");

 return 0;
}
  • No compile error and it’ll print “Initialized to ZERO”.
  • No compile error and it’ll print “Not initialized to ZERO”.
  • Compile error because size of arr has been defined using variable outside any function.
  • No compile error and it’ll print either “Initialized to ZERO” or “Not initialized to ZERO” depending on what value is present at arr[0] at a particular run of the program.

Question 5

Consider the following C program: C
#include <stdio.h>
typedef struct 
{
    char *a;
    char *b;
} t;
void f1(t s);
void f2(t *p);
main()
{
    static t s = {\"A\", \"B\"};
    printf (\"%s %s\\n\", s.a, s.b);
    f1(s);
    printf (\"%s %s\\n\", s.a, s.b);
    f2(&s);
}
void f1(t s)
{
    s.a = \"U\";
    s.b = \"V\";
    printf (\"%s %s\\n\", s.a, s.b);
    return;
}
void f2(t *p)
{
    p -> a  = \"V\";
    p -> b = \"W\";
    printf(\"%s %s\\n\", p -> a, p -> b);
    return;
}
What is the output generated by the program ?
  • A B U V V W V W
  • A B U V A B V W
  • A B U V U V V W
  • A B U V V W U V

Question 6

Pick the best statement for the following program snippet: 
 

C
#include <stdio.h>

int main()
{
 int var;  /*Suppose address of var is 2000 */

 void *ptr = &var;
 *ptr = 5;
 printf(\"var=%d and *ptr=%d\",var,*ptr);
             
 return 0;
}
  • It will print “var=5 and *ptr=2000”
     

  • It will print “var=5 and *ptr=5”
     

  • It will print “var=5 and *ptr=XYZ” where XYZ is some random address
     

  • Compile error
     

There are 6 questions to complete.

Last Updated :
Take a part in the ongoing discussion