Sail E0 Webinar

MCQs

Total Questions : 72 | Page 7 of 8 pages
Question 61.

What will be output if you compile following c code ?


#include<stdio.h>
struct values
{
int i;
float f;
};
void main()
{
struct values var={555,67.05501};
printf("%2d %.2f",var.i,var.f);
}


  1.    Compile error
  2.    55 67.05
  3.    555 67.06
  4.    555 67.05
 Discuss Question
Answer: Option C. -> 555 67.06

The members of a structure variable can be assigned initial values in much the same manner as the elements of an array. The initial values must appear in order in which they will be assigned to their corresponding strucutre members, enclosed in braces and separated by commas.



Question 62.

What will be output if you compile following c code ?


#include<stdio.h>
struct my_struct
{
int i;
unsigned int j;
};
void main()
{
struct my_struct temp1={-32769,-1},temp2;
temp2=temp1;
printf("%d %u",temp2.i,temp2.j);
}


  1.    32767 -1
  2.    -32769 -1
  3.    -32769 65535
  4.    32767 65535
 Discuss Question
Answer: Option D. -> 32767 65535

An entire structure variable can be assigned to another structure variable, provided both variables have the same composition.


Question 63.

What will be output if you compile following c code ?


#include<stdio.h>
void main()
{
static struct my_struct
{
unsigned a:1;
int i;
unsigned b:4;
unsigned c:10;
}v={1,10000,15,555};
printf("%d %d %d %d",v.a,v.b,v.c,v.d);
printf("\nSize=%d bytes",sizeof v);
}


  1.    Compile Error
  2.    1 10000 15 555 size = 4 bytes
  3.    10000 1 15 555 size = 4 bytes
  4.    10000 1 15 555 size = 5 bytes
 Discuss Question
Answer: Option D. -> 10000 1 15 555 size = 5 bytes

Here the bit field variable 'a' will be in first byte of one word, the variable 'i' will be in the second word and the bit fields 'b' and 'c' will be in the third word. The variables 'a', 'b' and 'c' would not get packed into the same word. [NOTE: one word=2 bytes]



Question 64.

Which of the following declaration is NOT valid?


#include<stdio.h>
(i) struct A{
int a;
struct B {
int b;
struct B *next;
}tempB;
struct A *next;
}tempA;
(ii) struct B{
int b;
struct B *next;
};
struct A{
int a;
struct B tempB;
struct A *next;
};
(iii)struct B{
int b;
}tempB;
struct {
int a;
struct B *nextB;
};
(iv) struct B {
int b;
struct B {
int b;
struct B *nextB;
}tempB;
struct B *nextB;
}tempB;


  1.    iv only
  2.    iii only
  3.    All of these
  4.    None of these
 Discuss Question
Answer: Option D. -> None of these

Since all the above structure declarations are valid in C.


Question 65.

What will be output if you compile following c code ?


#include<stdio.h>
struct A
{
int i;
float f;
union B
{
char ch;
int j;
}temp;
}temp1;
void main()
{
struct A temp2[5];
printf("%d %d",sizeof(temp1),sizeof(temp2));
}


  1.    6 30
  2.    8 40
  3.    9 45
  4.    None of these
 Discuss Question
Answer: Option B. -> 8 40

Since int (2 bytes) + float (4 bytes) = (6 bytes) + Largest among union is int (2 bytes) is equal to (8 bytes). Also the   total number of bytes the array 'temp2' requires :
(8 bytes) * (5 bytes) = (40 bytes).



Question 66.

What will be output if you compile following c code ?


#include<stdio.h>
struct
{
int i;
float f;
}var;
void main()
{
var.i=5;
var.f=9.76723;
printf("%d %.2f",var.i,var.f);
}


  1.    Compile Error
  2.    5 9.76723
  3.    5 9.76
  4.    5 9.77
 Discuss Question
Answer: Option D. -> 5 9.77

Though both <struct type name> and <structure variables> are optional, one of the two
must appear. In the above program, <structure variable> i.e. var is used. (2 decimal places or)
2-digit precision of 9.76723 is 9.77


Question 67.

What will be output if you compile following c code ?


#include<stdio.h>
struct my_struct1
{
int arr[2][2];
};
typedef struct my_struct1 record;
struct my_struct2
{
record temp;
}list[2]={1,2,3,4,5,6,7,8};
void main()
{
int i,j,k;
for (i=1; i>=0; i--)
for (j=0; j=0; k--)
printf("%d",list[i].temp.arr[j][k]);
}




  1.    Compile Error
  2.    Run time Error
  3.    65872143
  4.    56781243
 Discuss Question
Answer: Option C. -> 65872143

This program illustrates the implementation of a nested structure i.e. structure inside another structure.


Question 68.

What will be output if you compile following c code ?


#include<stdio.h>
union A
{
char ch;
int i;
float f;
}tempA;
void main()
{
tempA.ch='A';
tempA.i=777;
tempA.f=12345.12345;
printf("%d",tempA.i);
}


  1.    compile error
  2.    12345
  3.    777
  4.    Erroneous Output
 Discuss Question
Answer: Option D. -> Erroneous Output

The above program produces erroneous output (which is machine dependent). In effect, a union creates a storage location that can be used by any one of its members at a time. When a different member is assigned a new value, the new value supercedes the previous member's value.
[NOTE : The compiler allocates a piece of storage that is large enough to hold the largest variable type in the union i.e. all members share the same address.]


Question 69.

What will be output if you compile following c code ?


#include<stdio.h>
typedef struct
{
int i;
float f;
}temp;
temp alter(temp *ptr,int x,float y)
{
temp tmp = *ptr;
printf("%d %.2f\n",tmp.i,tmp.f);
tmp.i=x;
tmp.f=y;
return tmp;
}
void main()
{
temp a={65535,777.777};
a = alter(&a,-1,666.666);
printf("%d %.2f\n",a.i,a.f);
}




  1.    Compile Error
  2.    65535 777.777 -1 666.666
  3.    65535 777.78 -1 666.67
  4.    -1 777.78 -1 666.67
 Discuss Question
Answer: Option D. -> -1 777.78 -1 666.67

This program illustrates the transfer of a structure to a function by passing the structure's address (a pointer) to the function. Also the altered structure is now returned directly to the calling portion of the program.



Question 70.

What will be output if you compile following c code ?


#include<stdio.h>
void main()
{
struct sample
{
unsigned a:1;
unsigned b:4;
}v={0,15};
unsigned *vptr=&v.b;
printf("%d %d",v.b,*vptr);
}


  1.    Compile Error
  2.    0 0
  3.    15 15
  4.    None of these
 Discuss Question
Answer: Option A. -> Compile Error

Since we cannot take the address of a bit field variable i.e. Use of pointer to access the bit fields is prohibited. Also we cannot use 'scanf' function to read values into a bit field as it requires the address of a bit field variable. Also array of bit-fields are not permitted and a function cannot return a bit field.


Latest Videos

Latest Test Papers