wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Structs, Unions, and Array Basics

Total questions: 30

Worksheet time: 15mins

Name
Class
Date
1.

struct test { int x; char y; }; Assuming sizeof(int)=4, sizeof(char)=1 and natural alignment, the value of sizeof(struct test) is

a)

12

b)

8

c)

6

d)

5

2.

union U { int i; char c[4]; }; Assuming sizeof(int)=4, the size of union U is

a)

1

b)

8

c)

4

d)

5

3.

struct A { char c; int i; double d; }; Which of the following is TRUE?

a)

Members are rearranged by compiler for minimum size

b)

Padding may be added between members

c)

Padding is added only at the end

d)

Size is always 13 bytes

4.

Which statement is TRUE for a union?

a)

Size of union is sum of sizes of its members

b)

All members have separate memory

c)

Only one member can be accessed at a time but memory is shared

d)

Members are stored in different addresses

5.

struct S { int *p; }; The size of the structure depends on

a)

number of elements pointed by p

b)

size of int

c)

both int and pointer size

d)

size of pointer

6.

int a[5] = {1,2,3,4,5}; Which of the following expressions is equivalent to a[2]?

a)

&a[2]

b)

*(a + 2)

c)

*(a + 1)

d)

*a + 2

7.

Given int a[5]; which statement is TRUE?

a)

a can be assigned another address

b)

a decays to pointer to first element in expressions

c)

a is a pointer variable

d)

a and &a have same type

8.

int a[4] = {10,20,30,40}; int *p = a; What is the value of *(p + 3)?

a)

40

b)

30

c)

20

d)

10

9.

int a[3][4]; Which is the type of a?

a)

int *

b)

array of 3 pointers to int

c)

int **

d)

array of 3 arrays of 4 int

10.

For int a[3][4]; which is the correct type of a + 1?

a)

pointer to pointer to int

b)

int **

c)

int *

d)

pointer to array of 4 int

11.

struct P { int x; }; struct P *ptr; Which is a correct way to access member x?

a)

Both (*ptr).x and ptr->x

b)

ptr.x

c)

(*ptr).x

d)

ptr->x

12.

Which expression advances one row forward in int a[3][4] when using pointer arithmetic on a?

a)

a[0] + 4

b)

&a + 1

c)

*a + 1

d)

a + 1

13.

If int b[2][3]; what is the value category and type of b[1]?

a)

Lvalue of type array of 3 int

b)

Rvalue of type int *

c)

Rvalue of type pointer to array

d)

Lvalue of type int

14.

Consider int a[5]; Which is INVALID?

a)

sizeof a

b)

a = a + 1

c)

*(a + 2)

d)

&a[0] == a

15.

struct Node { int data; struct Node *next; }; struct Node *p; Which pairs are equivalent for member access?

a)

(*p).data and p->data

b)

p->data and *p.data

c)

p->next and (*p).next

d)

p.data and (*p).data

16.

int a[3][4]; Which expression gives the address of element in row 2, column 3 (0-based)?

a)

*(a + 2) + 3

b)

&a + 2 + 3

c)

a + 2 + 3

d)

&a[2][3]

17.

Which statement is TRUE? A. Both S and U occupy same memory B. Size of S is always equal to size of U C. In U, a and b share the same memory location D. In S, a and b share the same memory location

a)

Size of S is always equal to size of U

b)

In U, a and b share the same memory location

c)

In S, a and b share the same memory location

d)

Both S and U occupy same memory

18.

int a[5] = {1,2,3,4,5}; int (*p)[5] = &a; What is the value of (*p)[2]? A. 1 B. 2 C. 3 D. Address of a[2]

a)

Address of a[2]

b)

2

c)

1

d)

3

19.

Which of the following declarations represents “pointer to an array of 10 integers”? A. int *p[10]; B. int (*p)[10]; C. int **p; D. int p[10];

a)

int p[10]

b)

int *p[10]

c)

int (*p)[10]

d)

int **p

20.

int a[5]; printf("%d %d", sizeof(a), sizeof(&a)); Assuming sizeof(int)=4 and pointer size is 8 bytes, the output is A. 20 20 B. 20 8 C. 8 20 D. 8 8

a)

20 20

b)

8 8

c)

8 20

d)

20 8

21.

Given the union definition union V { float f; char c; };, what is the size of union V assuming sizeof(float)=4?

a)

12

b)

8

c)

4

d)

6

22.

int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; What is the value of *(ptr + 4)?

a)

3

b)

4

c)

5

d)

1

23.

struct Q { double d; char c; }; struct Q *q; Which of the following is a valid way to access member d?

a)

q->d

b)

(*q).d

c)

Both q->d and (*q).d

d)

q.d

24.

int arr[6] = {10, 20, 30, 40, 50, 60}; int *ptr = arr; What is the value of *(ptr + 5)?

a)

30

b)

60

c)

10

d)

50

25.

Which statement is TRUE for a structure?

a)

Size of structure is always equal to sum of sizes of its members

b)

Structure members cannot be of different data types

c)

All members are stored in contiguous memory

d)

Members can be accessed simultaneously

26.

If int b[3][4]; what is the value category and type of b[0][1]?

a)

Lvalue of type int

b)

Rvalue of type pointer to int

c)

Lvalue of type array of 4 int

d)

Rvalue of type int

27.

int arr[10][5]; What is the type of arr?

a)

array of 10 arrays of 5 int

b)

int **

c)

array of 10 pointers to int

d)

int *

28.

struct B { float f; char c; }; Which of the following statements is TRUE?

a)

Members can be accessed simultaneously

b)

Size of struct B is always equal to sum of sizes of its members

c)

Padding may be added for alignment

d)

All members are stored in non-contiguous memory

29.

For int b[4][3]; what is the correct type of b + 1?

a)

int *

b)

pointer to array of 3 int

c)

pointer to pointer to int

d)

int **

30.

Given the structure definition struct R { char c; int i; };, what is the size of struct R assuming sizeof(char)=1 and sizeof(int)=4?

a)

4

b)

8

c)

6

d)

5