You are on page 1of 35

Structures and Union

QUESTION: Which of the following is a collection of different data types?

A. String
B. Array
C. Structure
D. Files

QUESTION: Which operator is used to connect structure name to its member name?
A. dot operator(.)
B. logical operator(&&)
C. pointer operator(&)
D. Arrow operator(->)

QUESTION: Which of the following comment about Union is false?


A. Union is a structure whose members share same memory area
B. The compiler will keep track of what type of information is currently stored
C. Only one of the members of union can be assigned a value at particular time
D. Size allocated for Union is the size of its member needing the maximum storage

QUESTION: Which of the following comment about the usage of structures in true?
A. Storage class can be assigned to individual member
B. The scope of the member name is confined to the particular structure, within which it is
defined
C. Individual members can be initialized within a structure type declaration
D. None of above

QUESTION: What is the output of following C code?

main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e ={"Tiger"} printf("%d%d
%f",e.age,e.sal);
}

A:Garbage Collection
B:Error
C:1 0.000000
D:0 0.000000

QUESTION: What is the similarity between a structure, union and enumeration?


A: All of them let you define new values
B: All of them let you define new data types
C: All of them let you define new pointers
D: All of them let you define new structures

QUESTION: What will be the output of the program ?

int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}

A: 3, 2, 515
B: 515, 2, 3
C: 3, 2, 5
D: 515, 515, 4
QUESTION: What will be the output of the program ?

int main()
{
union var
{
int a, b;
};
union var v;
v.a=10;
v.b=20;
printf("%d\n", v.a);
return 0;
}

A: 10
B: 20
C: 30
D: 0

QUESTION: What will be the output of the program ?

int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit={1, 2, 13};

printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);


return 0;
}

A: 1, 2, 13
B: 1, 4, 4
C:-1, 2, -3
D:-1, -2, -13
QUESTION: What will be the output of the program in 16 bit platform (Turbo C
under DOS) ?

int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit;
printf("%d\n", sizeof(bit));
return 0;
}

A: 1
B: 2
C: 4
D: 9

QUESTION: What will be the output of the program ?

int main()
{
enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
printf("%d, %d, %d, %d, %d, %d\n", MON, TUE, WED, THU, FRI, SAT);
return 0;
}

A: -1, 0, 1, 2, 3, 4
B: -1, 2, 6, 3, 4, 5
C: -1, 0, 6, 2, 3, 4
D: -1, 0, 6, 7, 8, 9

QUESTION: What will be the output of the program ?

int main()
{
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = absent;
stud3 = fail;
printf("%d %d %d\n", stud1, stud2, stud3);
return 0;
}

A: 0, 1, 2
B: 1, 2, 3
C: 0, 2, 1
D: 1, 3, 2

QUESTION: What will be the output of the program in Turbo C (under DOS)?
int main()

{
struct emp
{
char *n;
int age;
};
struct emp e1 = {"Dravid",
23}; struct emp e2 = e1;
strupr(e2.n);
printf("%s\n", e1.n);
return 0;
}
A: Error: Invalid structure assignment
B: DRAVID
C: Dravid
D: No output

QUESTION: What will be the output of the program in 16-bit platform (under DOS)?

int main()
{
struct node
{
int data;
struct node *link;
};
struct node *p, *q;
p = (struct node *) malloc(sizeof(struct
node)); q = (struct node *)
malloc(sizeof(struct node)); printf("%d,
%d\n", sizeof(p), sizeof(q));
return 0;
}

A: 2, 2
B: 8, 8
C: 5, 5
D: 4, 4

QUESTION: What will be the output of the program ?

int main()
{
struct byte
{
int one:1;
};
struct byte var = {1};
printf("%d\n", var.one);
return 0;
}

A: 1
B:-1
C: 0
D: Error

QUESTION: Which of the following statements correct about the below program?

int main()
{
struct emp
{
char name[25];
int age;
float sal;
};
struct emp e[2];
int i=0;
for(i=0; i<2; i++)
scanf("%s %d %f", e[i].name, &e[i].age, &e[i].sal);

for(i=0; i<2; i++)


scanf("%s %d %f", e[i].name, e[i].age, e[i].sal);
return 0;
}

A: Error: scanf() function cannot be used for structures elements.


B: The code runs successfully.
C: Error: Floating point formats not linked Abnormal program termination.
D: Error: structure variable must be initialized.

QUESTION: Which of the following statements correct about the below program?

int main()
{
union a
{
int i;
char ch[2];
};
union a u1 = {512};
union a u2 = {0, 2};
return 0;
}

1. u2 CANNOT be initialized as shown


2. u1 can be initialized as shown
3 To initialize char ch[] of u2 '.' operator should be used
4. The code causes an error 'Declaration syntax error'

A: 1, 2
B: 2,3
C: 1,2,3
D: 1,3,4

QUESTION: Which of the following statements correct about the below code?
maruti.engine.bolts=25;

A: Structure bolts is nested within structure engine.


B: Structure engine is nested within structure maruti
C: Structure maruti is nested within structure engine
D: Structure maruti is nested within structure bolts.

QUESTION: A union cannot be nested in a structure


A: True
B: False

QUESTION: Nested unions are allowed


A: True
B: False

QUESTION: A structure can be nested inside another structure


A: True
B: False

QUESTION: Which of the following statement is True?


A: User has to explicitly define the numeric value of enumerations
B: User has a control over the size of enumeration variables
C: Enumeration can have an effect local to the block, if desired
D: Enumerations have a global effect throughout the file

QUESTION: The '.' operator can be used access structure elements using a structure
variable
A: True
B: False

QUESTION: Union elements can be of different sizes


A: True
B: False

QUESTION: A structure can contain similar or dissimilar elements


A: True
B: False

QUESTION: size of union is size of the longest element in the union


A: Yes
B: No

QUESTION: The elements of union are always accessed using & operator
A: Yes
B: No

QUESTION: Is there easy way to print enumeration values symbolically?


A: Yes
B: No

QUESTION: By default structure variable will be of auto storage class


A: Yes
B: No
QUESTION: Is it necessary that the size of all elements in a union should be same
A: Yes
B:No

QUESTION: Can we have an array of bit fields?


A: Yes
B: No

QUESTION: Will the following declaration work?

typedef struct s
{
int a;
float b;
}s;

A: YeS
B: No

QUESTION: If a char is 1 byte wide, an integer is 2 bytes wide and a long integer is 4
bytes wide then will the following structure always occupy 7 bytes?

struct ex
{
char ch;
int i;
long int a;
};

A: Yes
B: No
QUESTION: Which of the following are themselves a collection of different data types?
A: String
B: Structure
C: Char
D: All of the mentioned

QUESTION: User-defined data type can be derived by .


A: struct
B: enum
C: typedef
D: All of the mentioned

QUESTION: Which operator connects the structure name to its member name
A: -
B: .
C: Both (b) and (c)
D: None of the above

QUESTION: Which of the following cannot be a structure member?


A: Another structure
B: Function
C: Array
D: None of the mentioned

QUESTION: Which of the following structure declaration will throw an error?


A: struct temp{}s;
main(){}
B: struct temp{};
struct temp s;
main(){}
C: struct temp s;
struct temp{};
main(){}
D: None of the mentioned

QUESTION: What is the output of this C code?


Void main()
{
struct Student
{
int no;
char name[20];
};
struct Student s;
s.no=8;
printf(“%d”,s.no);
}

A :Nothing
B: Compile time error
C: Junk
D: 8

QUESTION: Number of bytes in memory taken by the below structure is?

Struct test
{
int k;
char c;
};

A: Multiple of integer size


B: integer size+character size
C: Depends on the platform
D: Multiple of word size

QUESTION: Size of a union is determined by size of the


A: First member in the union
B: Last member in the union
C: Biggest member in the union
D: Sum of the sizes of all members

QUESTION: Comment on the following union declaration?

Union temp

{ int a;
float b;
char c;
};
union temp s={1,2.5,'A'}//REF LINE

A: A
B: B
C: C
D: Such declaration are illegal

QUESTION: What would be the size of the following union declaration?

union uTemp
{double a;
int b[10];
char c;
} u;

(Assuming size of double = 8, size of int = 4, size of char = 1)


A: 4
B: 8
C: 40
D: 80

QUESTION: Members of a union are accessed as .


A: union-name.member
B: union-pointer->member
C: Both a & b
D: None of the mentioned

QUESTION: Which of the following share a similarity in syntax?


1. Union, 2. Structure, 3. Arrays and 4. Pointers
A: 3 and 4
B: 1 and 2
C: 1 and 3
D: 1, 3 and 4

QUESTION: The correct syntax to access the member of the ith structure in the array
Of structures is?
Assuming:

struct temp
{
int b;
}s[50];

A: s.b.[i];
B: s.[i].b;
C: s.b[i];
D: s[i].b;

QUESTION: Which of the following are themselves a collection of different data types?
A: structures
B: string
C: char
D: All of the mentioned

QUESTION: Comment on the output of this C code?

#include <stdio.h>
struct temp
{
int a;
int b;
int c;
};
main()
{
struct temp p[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} }

A: No Compile time error, generates an array of structure of size 3


B: No Compile time error, generates an array of structure of size 9
C: Compile time error, illegal declaration of a multidimensional
array D: Compile time error, illegal assignment to members of
structure

QUESTION: What is the output of this C code?


(Assuming size of int be 4)

struct temp
{
int a;
int b;
int c;
} p[] = {0};
main()
{ printf("%d", sizeof(p));
}

A: 4
B: 12
C: 16
D: Can‟t be estimated due to ambigous initialization of array

QUESTION: What is the output of this C code?

struct student
{
};
void main()
{
struct student s[2];
printf("%d", sizeof(s));
}

A: 2
B: 4
C: 8
D: 0

QUESTION: What is the output of this C code?

struct
{
int k;
char c;
};
int main()
{
struct p;
p.k = 10;
printf("%d\n", p.k);
}

A: Compile time error


B: 10
C: Undefined behavior
D: Segmentation fault

QUESTION: What is the output of this C code?

#include <stdio.h>
struct
{
int k;
char c;
} p;
int p = 10;
int main()
{
p.k = 10;
printf("%d %d\n", p.k, p);
}

A: Compile time error


B: 10 10
C: Depends on the standard
D: Depends on the compiler

QUESTION: What is the output of this C code?

#include <stdio.h>
struct p
{
int k;
char c;
};
int p = 10;
int main()
{
struct p x;
x.k = 10;
printf("%d %d\n", x.k, p);
}

A: Compile time error


B: 10 10
C: Depends on the standard
D: Depends on the compiler

QUESTION: What is the output of this C code?

#include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int p = 10;
int main()
{
struct p x = {1, 97};
printf("%f %d\n", x.f, p);
}

A: Compile time error


B: 0.000000 10
C: Some garbage value 10
D: 0 10

QUESTION: What is the output of this C code?

#include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int main()
{
struct p x = {.c = 97, .f = 3, .k = 1};
printf("%f\n", x.f);
}

A: 3.000000
B: Compile time error
C: Undefined behavior
D: 1.000000

QUESTION: What is the output of this C code

#include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int main()
{
struct p x = {.c = 97, .k = 1, 3};
printf("%f \n", x.f);
}

A: 3.000000
B: 0.000000
C: Compile time error
D: Undefined behaviour

QUESTION: What is the output of this C code?

#include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int main()
{
struct p x = {.c = 97};
printf("%f\n", x.f);
}

A: 0.000000
B: Somegarbagevalue
C: Compile time error
D: None of the mentioned

QUESTION: What will happen when the structure is declared?


A: it will not allocate any memory
B: it will allocate the memory
C: it will be declared and initialized
D: none of the mentioned

QUESTION: The declaration of structure is also called as?


A: structure creator
B: structure signifier
C: structure specifier
D: none of the mentioned

QUESTION: About structure which of the following is true


1. Structure members are aligned in memory depending on their data type.
2. The size of a structure may not be equal to the sum of the size of its members.
A: Only option 1
B: Only option 2
C: Both option 1 and 2
D: Neither option 1 nor 2

QUESTION: What is the output of the following program?

struct x x
{
int a;
long b;
} s;
union y y
{
int a;
long b;
} u;
print sizeof( s ) and sizeof( u ) if sizeof( int ) = 4 and sizeof( long ) = 4

A:sizeof( s ) = 8, sizeof( u ) = 4
B:sizeof( s ) = 4, sizeof( u ) = 4
C:sizeof( s ) = 4, sizeof( u ) = 8
D:sizeof( s ) = 8, sizeof( u ) = 8

QUESTION: What is the output of the program?

#include <stdio.h>
main()
{
struct s1 { int i ;};
struct s2 { int i ;};
struct s1 st1;
struct s2 st2;
st1.i = 5;
st2 = st1;
printf(“ %d”, st2.i);
}

A:5
B:1004
C:Syntax error
D:None

QUESTION: For the following declaration

union x
{ char ch;
int I;
double j;
} u_var;

What is the value of sizeof( u_var)?


A:Same as sizeof( int )
B:Same as sizeof( double)
C:Same as sizeof( char )
D:None
QUESTION: The size of the following union, where an int occupies 4 bytes of
memory Is
union arc
{
char x;
int y;
char ax[8];
}aha;

A:16 byte
B:13 byte
C:8 byte
D:4 byte

QUESTION: Which of these are valid declaration?

i) union { int I; int j;};


ii) union u_tag { int I; int j;} u;
iii) union { int I; int j; FILE *K};
iv) union { int I; int j;} u;

A:All are correct


B:Option (i), (ii),and(iv)
C:Option (ii) and (iv)
D:Option (ii)only

QUESTION: Which of the following comments about union are true?


A:Union is a structure whose members share the same storage area
B:Size allocated for union is the size of its member needing the maximum storage
C:Only one of the members of union can be assigned a value at a particular time
D:All of these

QUESTION: The correct syntax to use typedef for struct is

A: typedef struct temp


{
int a;
}TEMP;
B: typedef struct
{
int a;
}TEMP;
C: struct temp
{
int a;
};
typedef struct temp TEMP;
D: All of the mentioned

QUESTION: Which of the following is FALSE about typedef?


A:typedef follow scope rules
B:typedef defined substitutes can be redefined again. (Eg: typedef char a; typedef int a;)
C:You cannot typedef a typedef with other term
D:All of the mentioned

QUESTION: typedef declaration:


A:Does not create a new type
B:It merely adds a new name for some existing type
C:Both a & b
D:None of the mentioned

QUESTION: What is the output of this C code?

typedef struct p
{
int x, y;
}k;
int main()
{
struct p p = {1, 2};
k k1 = p;
printf("%d\n", k1.x);
}

A:Compile time error


B:1
C:0
D:Depends on the standard
QUESTION: What will be the output of following program ?

#include <stdio.h>
struct sample
{
int a=0;
char b='A';
float c=10.5;
};
int main()
{
struct sample s; printf("%d,%c,
%f",s.a,s.b,s.c); return 0;
}

A: Error
B: 0,A,10.5
C: 0,A,10.500000
D: No Error , No Output

QUESTION: What will be the output of following program ?

#include <stdio.h>
struct sample
{
int a;
}sample;

int main()
{
sample.a=100;
printf("%d",sample.a);
return 0;
}

A:0
B:100
C:ERROR
D:Warning

QUESTION: What will be the output of following program ? (On 16 bit compiler)

#include <stdio.h>
int main()
{
union values
{
int intVal;
char chrVal[2];i
};

union values val;


val.chrVal[0]='A'; val.chrVal[1]='B';

printf("\n%c,%c,%d",val.chrVal[0],val.chrVal[1],val.intVal);
return 0;
}

A: A,B,0
B: A,B,16961
C: B,B,66
D: A,A,65

QUESTION: What will be the output of following program ? (On 16 bit compiler)

#include <stdio.h>
int main()
{
union values
{
unsigned char a;
unsigned char b;
unsigned int c;
};
union values val;
val.a=1;
val.b=2;
val.c=300;

printf("%d,%d,%d",val.a,val.b,val.c);
return 0;
}

A: 44,44,300
B: 1,2,300
C: 2,2,300
D: 256,256,300

QUESTION: What will be the output of following program ? (On 16 bit compiler)

#include <stdio.h>
int main()
{
typedef struct
tag{ char str[10];
int a;
}har;

har h1,h2={"IHelp",10};
h1=h2;
h1.str[1]='h'; printf("%s,
%d",h1.str,h1.a); return 0;
}

A:ERROR
B:IHelp,10
C:IHelp,0
D:Ihelp,10
QUESTION: What will be the output of following program ?

#include <stdio.h>
int main()
{
struct std
{
char name[30];
int age;
};
struct std s1={"Mike",26};
struct std s2=s1;

printf("Name: %s, Age: %d\n",s2.name,s2.age);


}

A:Name: Mike, Age: 26


B:Name: Garbage, Age: Garbage
C:Error
D:Nothing

QUESTION: What will be the output of following program ?

#include <stdio.h>
int main()
{
union test
{
int i;
int j;
};
union test var=10; printf("%d,
%d\n",var.i,var.j);
}

A:10,10
B:10,0
C:0,10
D:Error

QUESTION: Which of the following accesses a variable in structure b?

A:b->var
B:b.var
C:b-var
D:b>var

QUESTION: Which of the following is a properly defined struct?


A:struct {int a;}
B:struct a_struct {int a;}
C:struct a_struct int a;
D:struct a_struct {int a;};

QUESTION: Which properly declares a variable of struct foo?


A:struct foo;
B:struct foo var;
C:foo;
D:int foo;

QUESTION: A short integer occupies 2 bytes an, ordinary integer 4 bytes and a long
integer occupies 8 bytes of memory
If a structure is defined as

struct TAB{
short a;
int b;
long c;
}TABLE[10];

the the total memory requirement for TABLE is


A:14
B:140
C:40
D:32

QUESTION: The declaration

union id{
char color[12];
int size;}
shirt,Jeans;
denotes shirt and Jeans are variable of type id and

A:Each can have a value of color and size


B:Each can represent either a 12-character color or an integer size at a time
C:Shirt and Jeans are same as struct variables
D:Variable shirt and Jeans cannot be used simultaneously in a statement

QUESTION: Most appropriate sentence to describe unions is


A:Union are like structures
B:Union contain members of different data types which share the same storage area in memory
C:Union are less frequently used in program
D:Union are used for set operations

QUESTION: Union differs from structure in the following way


A:All members are used at a time
B:Only one member can be used at a time
C:Union cannot have more members
D:Union initialized all members as structure

QUESTION: Identify the wrong syntax


A:typedef struct { member declaration; } NAME; NAME V1, V2;
B:typedef struct tag{ member declaration; } NAME; NAME V1, V2;
C:typedef struct { member declaration; } NAME; NAME V1, V2;
D:typedef struct tag { member declaration; } NAME; NAME V1, V2;
QUESTION: What is the output of above program. Assume that the size of an integer is
4 bytes and size of character is 1 byte

union test
{
int x;
char arr[4];
int y;
};

int main()
{
union test t;
t.x = 0;
t.arr[1] = 'G';
printf("%s", t.arr);
return 0;
}

A:Nothing is printed
B:G
C:Garbage character followed by „G‟
D:Garbage character followed by „G‟, followed by more garbage

characters

QUESTION:

#include<stdio.h>
struct st
{
int x;
struct st next;
};

int main()
{
struct st temp;
temp.x = 10;
temp.next = temp;
printf("%d", temp.next.x);
return 0;
}

What is the output of a program?


A:Compiler Error
B:10
C:Runtime Error
D:Garbage Value

QUESTION: Consider the following C declaration

struct {
short s[5];
union {
float y;
long z;
}u;
} t;

Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes,
respectively. How much memory requirement for variable t?
A:22 bytes
B:14 bytes
C:18 bytes
D:10 bytes

QUESTION:

#include<stdio.h>
struct st
{
int x;
static int y;
};
int main()
{
printf("%d", sizeof(struct st));
return 0;
}

A:4
B:8
C:Compile Error
D:Runtime Error

QUESTION: Which of the following operators can be applied on structure variables?


A:Equality comparison ( == )
B:Assignment ( = )
C:Both of the above
D:None of the above

QUESTION:

union test
{
int x;
char arr[8];
int y;
};

int main()
{
printf("%d", sizeof(union test));
return 0;
}

What is the output of above program. Assume that the size of an integer is 4 bytes and
size of character is 1 byte
A:12
B:16
C:8
D:Compile time error

QUESTION:

# include <iostream>
# include <string.h>
using namespace std;

struct Test
{
char str[20];
};

int main()
{
struct Test st1, st2;
strcpy(st1.str, "GeeksQuiz");
st2 = st1;
st1.str[0] = 'S';
cout << st2.str;
return 0;
}

A:Segmentation Fault
B:SeeksQuiz
C:GeeksQuiz
D:Compiler Error

QUESTION: What is the output of following C program


#include<stdio.h>
struct Point
{
int x, y, z;
};

int main()
{
struct Point p1 = {.y = 0, .z = 1, .x = 2};
printf("%d %d %d", p1.x, p1.y, p1.z);
return 0;
}

A:Compiler Error
B:2 0 1
C:0 1 2
D:2 1 0

QUESTION: In the following program , both s1 and s2 would be variables of structure


type defined as below and there won't be any compilation issue.
typedef struct Student
{
int rollno;
int total;
} Student;

Student s1;
struct Student s2;

A:True
B:False

QUESTION: 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:No compile error and it‟ll print 1 0 2 0
D:No compile error and it‟ll print 1 2 0 0

QUESTION: 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.

QUESTION: 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”.
D:None of the above

QUESTION: Presence of code like “s.t.b = 10” indicate


A:Syntax Error
B:structure
C:double data type
D:An ordinary variable name

You might also like