NEW
Font size
WorksheetsStructs, Unions, and Array Basics
Total questions: 30
Worksheet time: 15mins
struct test { int x; char y; }; Assuming sizeof(int)=4, sizeof(char)=1 and natural alignment, the value of sizeof(struct test) is
12
8
6
5
union U { int i; char c[4]; }; Assuming sizeof(int)=4, the size of union U is
1
8
4
5
struct A { char c; int i; double d; }; Which of the following is TRUE?
Members are rearranged by compiler for minimum size
Padding may be added between members
Padding is added only at the end
Size is always 13 bytes
Which statement is TRUE for a union?
Size of union is sum of sizes of its members
All members have separate memory
Only one member can be accessed at a time but memory is shared
Members are stored in different addresses
struct S { int *p; }; The size of the structure depends on
number of elements pointed by p
size of int
both int and pointer size
size of pointer
int a[5] = {1,2,3,4,5}; Which of the following expressions is equivalent to a[2]?
&a[2]
*(a + 2)
*(a + 1)
*a + 2
Given int a[5]; which statement is TRUE?
a can be assigned another address
a decays to pointer to first element in expressions
a is a pointer variable
a and &a have same type
int a[4] = {10,20,30,40}; int *p = a; What is the value of *(p + 3)?
40
30
20
10
int a[3][4]; Which is the type of a?
int *
array of 3 pointers to int
int **
array of 3 arrays of 4 int
For int a[3][4]; which is the correct type of a + 1?
pointer to pointer to int
int **
int *
pointer to array of 4 int
struct P { int x; }; struct P *ptr; Which is a correct way to access member x?
Both (*ptr).x and ptr->x
ptr.x
(*ptr).x
ptr->x
Which expression advances one row forward in int a[3][4] when using pointer arithmetic on a?
a[0] + 4
&a + 1
*a + 1
a + 1
If int b[2][3]; what is the value category and type of b[1]?
Lvalue of type array of 3 int
Rvalue of type int *
Rvalue of type pointer to array
Lvalue of type int
Consider int a[5]; Which is INVALID?
sizeof a
a = a + 1
*(a + 2)
&a[0] == a
struct Node { int data; struct Node *next; }; struct Node *p; Which pairs are equivalent for member access?
(*p).data and p->data
p->data and *p.data
p->next and (*p).next
p.data and (*p).data
int a[3][4]; Which expression gives the address of element in row 2, column 3 (0-based)?
*(a + 2) + 3
&a + 2 + 3
a + 2 + 3
&a[2][3]
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
Size of S is always equal to size of U
In U, a and b share the same memory location
In S, a and b share the same memory location
Both S and U occupy same memory
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]
Address of a[2]
2
1
3
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];
int p[10]
int *p[10]
int (*p)[10]
int **p
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
20 20
8 8
8 20
20 8
Given the union definition union V { float f; char c; };, what is the size of union V assuming sizeof(float)=4?
12
8
4
6
int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; What is the value of *(ptr + 4)?
3
4
5
1
struct Q { double d; char c; }; struct Q *q; Which of the following is a valid way to access member d?
q->d
(*q).d
Both q->d and (*q).d
q.d
int arr[6] = {10, 20, 30, 40, 50, 60}; int *ptr = arr; What is the value of *(ptr + 5)?
30
60
10
50
Which statement is TRUE for a structure?
Size of structure is always equal to sum of sizes of its members
Structure members cannot be of different data types
All members are stored in contiguous memory
Members can be accessed simultaneously
If int b[3][4]; what is the value category and type of b[0][1]?
Lvalue of type int
Rvalue of type pointer to int
Lvalue of type array of 4 int
Rvalue of type int
int arr[10][5]; What is the type of arr?
array of 10 arrays of 5 int
int **
array of 10 pointers to int
int *
struct B { float f; char c; }; Which of the following statements is TRUE?
Members can be accessed simultaneously
Size of struct B is always equal to sum of sizes of its members
Padding may be added for alignment
All members are stored in non-contiguous memory
For int b[4][3]; what is the correct type of b + 1?
int *
pointer to array of 3 int
pointer to pointer to int
int **
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?
4
8
6
5
