C Quiz – 111

Question 1

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;
}
Cross

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

Cross

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

Cross

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

Tick

Compile error
 



Question 1-Explanation: 

Key point in the above snippet is dereferencing of void pointer. It should be noted that dereferencing of void pointer isn’t allowed because void is an incomplete data type. The correct way to assign value of 5 would be first to typecast void pointer and then use it. So instead of *ptr, one should use *(int *)ptr. Correct answer is d.
 

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

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

int main()
{
 foo(), bar(), foo();
 return 0;
}
Cross
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.
Cross
Compile error because semicolon isn’t used while calling foo() and bar() in side main function.
Cross
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.
Tick
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-Explanation: 
Here, even though life of static variables span across function calls but their scope is respective to their function body only. That’s why staticVar of each function has separate copies whose life span across function calls. And d is correct.
Question 3
Pick the best statement for the below:
int arr[50] = {0,1,2,[47]=47,48,49};
Cross
This isn’t allowed in C and it’ll give compile error
Tick
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-Explanation: 
In C, initialization of array can be done for selected elements as well. By default, the initializer start from 0th element. Specific elements in array can be specified by []. It should be noted that the remaining elements (i.e. the ones not mentioned in array initialization) would be initialized to 0. For example, “int arr[10] = {100, [5]=100,[9]=100}” is also legal in C. This initializes arr[0], arr[5] and arr[9] to 100. All the remaining elements would be 0.
Question 4
Pick the best statement for the below program:
#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;
}

Cross
Definition of both arr1 and arr2 is incorrect because variable is used to specify the size of array. That’s why compile error.
Cross
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.
Tick
Initialization of arr1 is incorrect. arr1 can’t be initialized due to its size being specified as variable. That’s why compile error.
Cross
No compile error. The program would define and initializes both arrays to ZERO.


Question 4-Explanation: 
There’s no issue with definition of arr1 and arr2. In definition of these arrays, the mention of array size using variable is ok as per C standard but these types of arrays can’t be initialized at the time of definition. That’s why initialization of arr1 is incorrect. But initialization of arr2 is done correctly. Right answer is C.
Question 5
Pick the best statement for the below program:
#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;
}
Cross
No compile error and it’ll print “Initialized to ZERO”.
Cross
No compile error and it’ll print “Not initialized to ZERO”.
Tick
Compile error because size of arr has been defined using variable outside any function.
Cross
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-Explanation: 
An array whose size is specified as variable can’t be defined out any function. It can be defined only inside a function. So putting arr[size] outside main() would result in compile error. Answer is C.
Question 6
Consider the following C program:
#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 %sn", s.a, s.b);
    f1(s);
    printf ("%s %sn", s.a, s.b);
    f2(&s);
}
void f1(t s)
{
    s.a = "U";
    s.b = "V";
    printf ("%s %sn", s.a, s.b);
    return;
}
void f2(t *p)
{
    p -> a  = "V";
    p -> b = "W";
    printf("%s %sn", p -> a, p -> b);
    return;
}
What is the output generated by the program ?
Cross
A B U V V W V W
Tick
A B U V A B V W
Cross
A B U V U V V W
Cross
A B U V V W U V


Question 6-Explanation: 

The value of  structure s is passed by value in f1().   In f2(), address of s is passed.   Therefore any  changes made in f1() don't reflect in main() , but changes made in f2() reflect.

 printf ("%s %s\n", s.a, s.b);   //  print local static values of a and b;A B

f1(s); give call to printf ("%s %s\n", s.a, s.b); //print local values of a and b ;U V

printf ("%s %s\n", s.a, s.b);  //  print local static values of a and b;A B

f2(&s);-> printf("%s %s\n", p -> a, p -> b); // gives present contents at memory location i.e U V

Therefore answer is B See the for code solution at: http://code.geeksforgeeks.org/q36urV
There are 6 questions to complete.

  • Last Updated : 26 Sep, 2023

Share your thoughts in the comments
Similar Reads