Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

C Structures and Unions

Total questions: 84

Worksheet time: 1hrs 24mins

Name
Class
Date
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

2.

Choose the correct output from the options given below: #include int main() { struct site { char name[] = "GeeksQuiz"; int no_of_pages = 200; }; struct site *ptr; printf("%d ", ptr->no_of_pages); printf("%s", ptr->name); getchar(); return 0; }

a)

200 GeeksQuiz

b)

200

c)

Runtime Error

d)

Compiler Error

3.

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.

d)

None of the above

4.

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

5.

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. The memory requirement for variable t, ignoring alignment considerations, is (GATE CS 2000)

a)

22 bytes

b)

14 bytes

c)

18 bytes

d)

10 bytes

6.

Consider the following C declaration struct { short s[5]; union { float y; long z; } u; } t; Assume that the objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment consideration, is

a)

22 bytes

b)

18 bytes

c)

14 bytes

d)

10 bytes

7.

Consider the following declaration : struct addr { char city[10]; char street[30]; int pin; }; struct { char name[30]; int gender; struct addr locate; } person, *kd = &person; Then (*(kd -> name + 2) can be used instead of

a)

person.name + 2

b)

kd -> (name + 2)

c)

*((*kd).name + 2)

d)

either (A) or (B), but not (C)

8.

Pick the best statement for the below program: #include 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".

9.

Pick the best statement for the below program: #include int main() { struct { int i; char c; } myVar = {.i = 100, .c = 'A'}; 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.

10.

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.

11.

Assume that size of an integer is 32 bit. What is the output of following program? #include struct st { int x; static int y; }; int main() { printf("%d", sizeof(struct st)); return 0; }

a)

4

b)

8

c)

Compiler Error

d)

Runtime Error

12.

In the following program snippet, 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

13.

Anyone of the following can be used to declare a node for a singly linked list and "NODEPTR nodePtr;" can be used to declare pointer to a node using any of the following /* First declaration */ typedef struct node { int data; struct node *nextPtr; } * NODEPTR; /* Second declaration */ struct node { int data; struct node * nextPtr; }; typedef struct node * NODEPTR;

a)

TRUE

b)

FALSE

14.

Anyone of the followings can be used to declare a node for a singly linked list. If we use the first declaration, "struct node * nodePtr;" would be used to declare pointer to a node. If we use the second declaration, "NODEPTR nodePtr;" can be used to declare pointer to a node. /* First declaration */ struct node { int data; struct node * nextPtr; }; /* Second declaration */ typedef struct node{ int data; NODEPTR nextPtr; } * NODEPTR;

a)

TRUE

b)

FALSE

15.

Predict the output of following C program #include 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

16.

Consider the following C program: 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; } What is the output?

a)

Segmentation Fault

b)

SeeksQuiz

c)

GeeksQuiz

d)

Compiler Error

17.

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; } Predict the output of the above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.

a)

Nothing is printed

b)

G

c)

Garbage character followed by 'G'

d)

Garbage character followed by 'G', followed by more garbage characters

e)

Compiler Error

18.

union test { int x; char arr[8]; int y; }; int main() { printf("%d", sizeof(union test)); return 0; } Predict the output of the above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.

a)

12

b)

16

c)

8

d)

Compiler Error

19.

Choose the correct output from the options given below: #include 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; }

a)

Compiler Error

b)

10

c)

Runtime Error

d)

Garbage Value

20.

struct node { int i; float j; }; struct node *s[10]; The above C declaration define 's' to be

a)

An array, each element of which is a pointer to a structure of type node

b)

A structure of 2 fields, each field being a pointer to an array of 10 elements

c)

A structure of 3 fields: an integer, a float, and an array of 10 elements

d)

An array, each element of which is a structure of type node

21.

Which of the following are themselves a collection of different data types?

a)

String

b)

Struct

c)

char

d)

all

22.

Which operator connects the structure name to its member name?

a)

-

b)

=

c)

.

d)

>>

23.

Which of the following cannot be a structure member?

a)

another structure

b)

array

c)

Function

d)

None

24.

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

25.

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 error

c)

8

d)

Junk

26.

Size of a union is determined by size of the.

a)

First member in the union

b)

Last member in the union

c)

C. Biggest member in the union

d)

Sum of the sizes of all members

27.

Assuming size of long int is 4 bytes, what will be the output for:


typedef long long int L = 24;

printf("Size of long long int is :");

printf("%d",sizeof(L));

a)

24

b)

4

c)

8

d)

Error

28.

What will be the output

a)

Name:

Subject:

Percentage: 86.50000

b)

Name:Raju

Subject:

Percentage: 86.50000

c)

Name: Raju

Subject: Maths

Percentage: 86.50000

d)

Error

29.

Memory allocated for structure members are always contigious

a)

True

b)

False

30.

What do you understand by:

void update(struct student *s);

a)

update is a function that takes an argument as pointer to structure variable and returns nothing

b)

update is a method that takes structure type variable as parameter

c)

Error, we cannot pass structure variable as pointer to a function

d)

update takes pointer argument but it cannot return void

31.

What will be the size of the following structure? #include struct temp { int a[10]; char p; };

a)

5

b)

11

c)

41

d)

44

32.

What is the size of the bit field?

a)

1 to 18 bits

b)

1 to 16 bits

c)

1 to 8 bits

d)

1 to 24 bits

33.

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

34.

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

35.

Any C Program must contain

a)

Must contain at least one function.

b)

Need not contain any function.

c)

Needs input data.

d)

None of the above

36.

Use of functions

a)

Helps to avoid repeating a set of statements many times.

b)

Enhances the logical clarity of the program.

c)

Helps to avoid repeated programming across programs

d)

Makes the debugging task easier.

e)

All of the above

37.

What is function?

a)

Function is a block of statements that perform some specific task.

b)

Function is the fundamental modular unit. A function is usually designed to perform a specific task.

c)

Function is a block of code that performs a specific task. It has a name and it is reusable.

d)

All of the above

38.

What is the output:

a)

10

b)

9

c)

11

d)

12

39.

which is the formal parameter?

a)

10

b)

i

40.

which is the actual parameter?

a)

10

b)

i

41.

which are the actual arguments/parameters?

a)

n1 and n2

b)

sum and result

c)

a and b

42.

which are the formal arguments/parameters?

a)

n1 and n2

b)

sum and result

c)

a and b

43.

final value of n1 and n2?

a)

1 and 3

b)

2 and 4

44.
a)

struct.salary

b)

structjob.salary

c)

sJob.salary

45.

What will be the output?

#include<stdio.h>

struct Point {

int x, y; };

int main() {

struct Point p = {10, 20};

printf("%d %d", p.x, p.y);

return 0; }

a)

0 0

b)

10 0

c)

10 20

d)

Compilation error

46.

What is the size of the following structure (assuming 4-byte int)?

struct A {

int x;

char y; };

a)

5

b)

8

c)

6

d)

4

47.

Which of these is a correct structure declaration?

a)

struct { int a; } obj;

b)

struct obj { int a }

c)

struct obj = { int a; }

d)

structure obj { int a; }

48.

What will this program output?

#include<stdio.h>
struct Student {

char name[10];

int age; };

int main() {

struct Student s = {"Raj", 21};

struct Student t = s;

printf("%s %d", t.name, t.age);

return 0; }

a)

Raj 21

b)

Garbage values

c)

Compilation error

d)

Segmentation fault

49.

Can we compare structure variables using == operator directly?

a)

Yes

b)

No

c)

Only for int members

d)

Only if typedef is used

50.

Output of the following program?

#include<stdio.h>

struct Test {

int a; float b; };

int main() {

struct Test t1 = {10, 20.5};

struct Test t2 = t1;

t2.a = 15;

printf("%d %.1f", t1.a, t2.b);

return 0; }

a)

15 20.5

b)

10 20.5

c)

10 0.0

d)

Garbage

51.

What is the output?

#include<stdio.h>

struct A {

int a;

struct B { int b; } inner; };

int main() {

struct A x = {5, {10}};

printf("%d %d", x.a, x.inner.b);

return 0; }

a)

5 10

b)

10 5

c)

Compilation error

d)

Garbage

52.

What will this print?

#include<stdio.h>

struct A { int arr[3]; };

int main() {

struct A a = {{1, 2, 3}};

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

return 0; }

a)

1

b)

2

c)

3

d)

Garbage

53.

What is the output of this code?

#include<stdio.h>

struct A { int x; };

int main() {

struct A a = {10};

struct A b = {20};

if (a.x == b.x)

printf("Equal");

else

printf("Not Equal");

return 0; }

a)

Equal

b)

Not Equal

c)

Compilation error

d)

Garbage

54.

What will this output?

#include<stdio.h>

union Data {

int i;

float f; };

int main() {

union Data d;

d.i = 10;

d.f = 2.5;

printf("%d", d.i);

return 0; }

a)

10

b)

0

c)

Garbage value

d)

2

55.

What is the key difference between structure and union?

a)

Structures are faster than unions

b)

Structure members share memory

c)

Union uses more memory

d)

Union members share memory

56.

Size of this union?

union U {

int a;

char b[4];

};

a)

4

b)

8

c)

5

d)

Compiler dependent

57.

What is the output of this union code?

#include<stdio.h>

union U {

int x;

char y; };

int main() {

union U u;

u.x = 512;

printf("%d", u.y);

return 0; }

a)

0

b)

512

c)

2

d)

Garbage

58.

Choose the correct one about typedef and structures

a)

typedef is mandatory for structures

b)

typedef creates a new structure

c)

typedef gives a new name to existing type

d)

typedef is used only for pointers

59.

Output of this program?

#include<stdio.h>

struct S { int a; };

int main() {

struct S arr[2] = {{1}, {2}};

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

return 0; }

a)

1

b)

2

c)

Compilation error

d)

Garbage

60.

Which one is valid syntax for structure inside structure?

a)

struct A { struct B b; };

b)

struct A { B b; };

c)

struct A { B b; }; struct B { int x; };

d)

struct A { struct B; };

61.

What happens here?

#include<stdio.h>

struct A { int x; };

int main() {

struct A a = {10};

struct A b = a;

b.x = 20;

printf("%d %d", a.x, b.x);

return 0; }

a)

20 20

b)

10 20

c)

10 10

d)

Compilation error

62.

Which of these is true about arrays of structures?

a)

Structure must be pointer to use array

b)

Only int members allowed

c)

You can create arrays of structures

d)

Arrays inside structures not allowed

63.

What is the output?

#include<stdio.h>

struct Book {

char name[10];

int price; };

int main() {

struct Book b[] = {{"C", 200}, {"Java", 300}};

printf("%s %d", b[1].name, b[1].price);

return 0; }

a)

Java 300

b)

C 200

c)

Java 200

d)

Compilation error

64.

In structure initialization, uninitialized members are:

a)

Assigned garbage

b)

Automatically initialized to 0

c)

Not allowed

d)

Assigned -1

65.

What happens if you pass a structure by value to a function?

a)

Address is passed

b)

Error

c)

Copy is made

d)

It’s converted to union

66.

Output of this program?

#include<stdio.h>

struct S { int a; };

void fun(struct S s) {

s.a = 50; }

int main() {

struct S s = {10};

fun(s);

printf("%d", s.a);

return 0; }

a)

50

b)

10

c)

0

d)

Garbage

67.

What is true about nested structures?

a)

Structures can't be nested

b)

A structure can’t contain itself directly

c)

Nested structures can't be initialized

d)

Members of inner structures can't be accessed

68.

What is the output? #include union Test { int i; char c; }; int main() { union Test t; t.c = 'A'; printf("%d", t.i); return 0; }

a)

65

b)

Garbage

c)

0

d)

Compilation error

69.

Which gives more memory flexibility: structure or union?

a)

Structure

b)

Union

c)

Both same

d)

None

70.

Which among the following is never possible in C when members in a structure are same as that in a union?    

//Let P be a structure    

//Let Q be a union

a)

a.sizeof(P) is greater than sizeof(Q)

b)

b. sizeof(P) is equal to sizeof(Q)

c)

c. sizeof(P) is less than to sizeof(Q)

d)

d. None of the above

71.

Which of the following are themselves a collection of different data types?

a)

String

b)

Structure

c)

Integer

d)

None of these

72.

Which of the following cannot be a structure member?

a)

Another structure

b)

Array

c)

Function

d)

None of these

73.

What is the output of this C code?

#include <stdio.h>

struct student

{

int no; char name[20];

};

void main()

{ student s;

s.no = 8;

printf("hello");

}

a)

Hello

b)

Nothing

c)

Compilation error

74.

Which of the following share a similarity in syntax?    

  1. Union, 2. Structure, 3. Arrays and 4. Pointers

a)

1 & 2

b)

3&4

c)

1&3

d)

2&3

75.

For what minimum value of x in a 32-bit Linux OS would make the size of s equal to 8 bytes?struct temp

{

int a : 13;

int b : 8;

int c : x;

}s;

a)

4

b)

8

c)

12

d)

16

76.

Which operator connects the structure name to its member name?

a)

-

b)

.

c)

=>

d)

@

77.

What will be the output of the following C code?

#include <stdio.h>

struct student

{

char *name;

};

struct student s;

struct student fun(void)

{

s.name = "newton";

printf("%s\n", s.name);

s.name = "alan";

return s;

}

void main()

{

struct student m = fun();

printf("%s\n", m.name);

m.name = "turing";

printf("%s\n", s.name);

}

a)

Alan alan newton

b)

Newton alan alan

c)

Alan newton alan

d)

Compile time error

78.

What will be the output of the below coding #include <stdio.h>

struct temp

{

int a;

} s;

void func(struct temp s)

{

s.a = 10;

printf("%d\t", s.a);

}

main()

{

func(s);

printf("%d\t", s.a);

}

a)

10,garbage value

b)

Garbage value,10

c)

0,10

d)

10,0

79.

Size of the following union (assume size of int=2, size of float=4 and size of char=1);

union ABC

{

int a;

float b;

char c;

};

a)

2

b)

4

c)

1

d)

8

80.

What is wrong with the following code?

struct Person{ char name; struct Person *Mother, *Father; }Anita;

a)

The ; should appear after the } and Anita be defined later

b)

name should be defined as an array

c)

There is no error in the code

d)

struct Person Mother, Father; must be defined as struct Person Mother, Father;

81.

What will be the output of the following code?

struct { int a;

double d;

float cp;

}s;

void main()

{

printf("%d\t%d\t%d\t%d", sizeof(s.a), sizeof(s.d), sizeof(s.cp), sizeof(s));

}

a)

4,8,4,24

b)

4,4,8,8

c)

4,8,24,8

d)

4,4,4,28

82.

What is important difference between structure & union?

a)

There is no difference

b)

Union takes less memory then structure

c)

Union woks faster then structure

d)

Structure takes less memory then union

83.

Is it necessary that all elements of structure should be different

a)

Yes

b)

No

84.

Which of the following accesses a variable in structure b?

a)

b->var

b)

b.var

c)

b*var

d)

b-var