1

C Quiz - 112

Question 1
Pick the best statement for the below program:
#include "stdio.h"

int main()
{
 struct {int a[2];} arr[] = {{1},{2}};

 printf("%d %d %d %d",arr[0].a[0],arr[0].a[1],arr[1].a[0],arr[1].a[1]);

 return 0;
}
A
Compile error because arr has been defined using struct type incorrectly. First struct type should be defined using tag and then arr should be defined using that tag.
B
Compile error because apart from definition of arr, another issue is in the initialization of array of struct i.e. arr[].
C
Compile error because of initialization of array of struct i.e. arr[].
D
No compile error and it’ll print 1 2 0 0
E
No compile error and it’ll print 1 0 2 0
Structure & Union    C Quiz - 112    
Discuss it


Question 1 Explanation: 
Here, struct type definition and definition of arr using that struct type has been done in the same line. This is okay as per C standard. Even initialization is also correct. The point to note is that array size of arr[] would be 2 i.e. 2 elements of this array of this struct type. This is decided due to the way it was initialized above. Here, arr[0].a[0] would be 1 and arr[1].a[0] would be 2. The remaining elements of the array would be ZERO. correct answer is E.
Question 2
Pick the best statement for the below program snippet:
struct {int a[2];} arr[] = {1,2};
A
No compile error and it’ll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[1].a[0] would be 2.
B
No compile error and it’ll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[0].a[1] would be 2. The second element arr[1] would be ZERO i.e. arr[1].a[0] and arr[1].a[1] would be 0.
C
No compile error and it’ll create array arr of 1 element. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[0].a[1] would be 2.
Structure & Union    C Quiz - 112    
Discuss it


Question 2 Explanation: 
Since size of array arr isn’t given explicitly, it would be decided based on the initialization here. Without any curly braces, arr is initialized sequentially i.e. arr[0].a[0] would be 1 and arr[0].a[1] would be 2. There’s no further initialization so size of arr would be 1. Correct answer is C.
Question 3
Pick the best statement for the below program:
#include "stdio.h"

int main()
{
 struct {int a[2], b;} arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2};

 printf("%d %d %d and",arr[0].a[0],arr[0].a[1],arr[0].b);
 printf("%d %d %d\n",arr[1].a[0],arr[1].a[1],arr[1].b);

 return 0;
}
A
Compile error because struct type (containing two fields i.e. an array of int and an int) has been specified along with the definition of array arr[] of this struct type.
B
Compile error because of incorrect syntax for initialization of array arr[].
C
No compile error and two elements of arr[] would be defined and initialized. Output would be “1 0 1 and 2 0 2”.
D
No compile error and two elements of arr[] would be defined and initialized. Output would be “1 X 1 and 2 X 2” where X is some garbage random number.
Structure & Union    C Quiz - 112    
Discuss it


Question 3 Explanation: 
In C, designators can be used to provide explicit initialization. For an array, elements which aren’t initialized explicitly in program are set as ZERO. That’s why correct answer is C.
Question 4
Pick the best statement for the below program:
#include "stdio.h"

int main()
{
 struct {int i; char c;} myVar = {.c ='A',.i = 100};
 printf("%d %c",myVar.i, myVar.c);
 return 0;
}
A
Compile error because struct type (containing two fields of dissimilar type i.e. an int and a char) has been mentioned along with definition of myVar of that struct type.
B
Compile error because of incorrect syntax of initialization of myVar. Basically, member of operator (i.e. dot .) has been used without myVar.
C
Compile error for not only B but for incorrect order of fields in myVar i.e. field c has been initialized first and then field i has been initialized.
D
No compile error and it’ll print 100 A.
Structure & Union    C Quiz - 112    
Discuss it


Question 4 Explanation: 
As per C language, initialization of a variable of complete data type can be done at the time of definition itself. Also, struct fields/members can be initialized out of order using field name and using dot operator without myVar is ok as per C. Correct answer is D.
Question 5
Pick the best statement for the below program:
#include "stdio.h"

int main()
{
 union {int i1; int i2;} myVar = {.i2 =100};
 printf("%d %d",myVar.i1, myVar.i2);
 return 0;
}
A
Compile error due to incorrect syntax of initialization.
B
No compile error and it’ll print “0 100”.
C
No compile error and it’ll print “100 100”.
Structure & Union    C Quiz - 112    
Discuss it


Question 5 Explanation: 
Since fields/members of union share same memory, both i1 and i2 refer to same location. Also, since both i1 and i2 are of same type, initializing one would initialize the other as well implicitly. So answer is C.
Question 6
Consider the code fragment written in C below :
void f (int n)
{ 
  if (n <=1)  {
   printf ("%d", n);
  }
  else {
   f (n/2);
   printf ("%d", n%2);
  }
}
What does f(173) print?
A
010110101
B
010101101
C
10110101
D
10101101
Recursion    C Quiz - 112    Gate IT 2008    
Discuss it


Question 6 Explanation: 
(173)2 = 10101101
Question 7
Consider the code fragment written in C below :
void f (int n)
{
    if (n <= 1)  {
        printf ("%d", n);
    }
    else {
        f (n/2);
        printf ("%d", n%2);
    }
}
 
Which of the following implementations will produce the same output for f(173) as the above code? P1
void f (int n)
{
    if (n/2)  {
        f(n/2);
    }
    printf ("%d", n%2);
}
 
P2

void f (int n)
{
    if (n <=1)  {
        printf ("%d", n);
    }
    else {
        printf ("%d", n%2);
        f (n/2);
    }
}

A
Both P1 and P2
B
P2 only
C
P1 only
D
Neither P1 nor P2
Recursion    C Quiz - 112    Gate IT 2008    
Discuss it


Question 7 Explanation: 
  Here, basically the function f prints the binary representation of the number. function f1 also prints the binary representation of the number function f2 prints the binary representation but in reverse order. Output of f  is:- 10101101 Output of f1 is:- 10101101 Output of f2 is:- 10110101 So, the answer is option (C) which is P1 only. This solution is contributed  by Anil Saikrishna Devarasetty.
There are 7 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice