Unit 6 - Practice Quiz

CSE109

1 Which keyword is used to declare a structure in C?

A. struc
B. struct
C. structure
D. type

2 What is the correct syntax to declare a structure variable emp of type struct Employee?

A. Employee emp;
B. struct Employee emp;
C. struct emp Employee;
D. new struct Employee emp;

3 Which operator is used to access a member of a structure variable?

A. . (dot operator)
B. -> (arrow operator)
C. * (asterisk)
D. & (ampersand)

4 If ptr is a pointer to a structure, which operator is used to access its members?

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

5 Which of the following statements about structure declaration is TRUE?

A. It allocates memory for variables.
B. It defines a new data type but does not allocate memory.
C. It must contain only integers.
D. It cannot contain arrays.

6 What does the following code snippet signify?
c
struct Point {
int x;
int y;
};

A. Definition of a structure variable
B. Declaration of a structure type
C. Initialization of a structure
D. A syntax error due to the semicolon

7 Given the following structure:
c
struct Data {
int id;
char name[20];
} d1 = {1, "Test"};

How is d1 initialized?

A. Compile-time initialization
B. Run-time initialization
C. Dynamic initialization
D. Copy initialization

8 Can a structure contain another structure of the same type as a member variable?

A. Yes, directly.
B. No, never.
C. Yes, but only as a pointer.
D. Yes, but only as a static member.

9 What is the size of the following union assuming int is 4 bytes and char is 1 byte?
c
union Packet {
int a;
char b;
};

A. 1 byte
B. 4 bytes
C. 5 bytes
D. 8 bytes

10 How do you access the member age of the structure variable s using a pointer p pointing to s?

A. p.age
B. p->age
C. *p.age
D. s->age

11 In a nested structure, how are inner members accessed?

A. outer.inner_member
B. inner.outer_member
C. outer.inner.member
D. outer->inner->member

12 Which of the following creates an alias Student for struct student_record?

A. typedef struct student_record Student;
B. struct student_record Student;
C. alias struct student_record Student;
D. typedef Student struct student_record;

13 What happens if you assign one structure variable to another of the same type (e.g., struct1 = struct2)?

A. It causes a compilation error.
B. It copies the memory address.
C. It performs a member-wise copy.
D. It only copies the first member.

14 What is the primary difference between a structure and a union?

A. Structures cannot contain functions, unions can.
B. Structures allocate memory for all members; unions share memory for all members.
C. Structures are always smaller than unions.
D. Unions can only contain integers.

15 Consider the declaration: struct { int x; } s[5];. What is s?

A. A structure containing an array.
B. A pointer to a structure.
C. An array of 5 structures.
D. A function returning a structure.

16 Which of the following is correct to define an array inside a structure?

A. struct s { int arr[10]; };
B. struct s { int arr; };
C. struct s { array int arr; };
D. struct s { [10]int arr; };

17 Given: union Data { int i; float f; }; union Data d; d.i = 10; d.f = 2.5;. What is the value of d.i?

A. 10
B. 2.5
C. Undefined/Garbage
D.

18 Can you compare two structure variables directly using ==?

A. Yes, it compares all members.
B. No, C does not support operator overloading for structs.
C. Yes, if they are initialized.
D. No, unless they are pointers.

19 What is the memory alignment/padding in structures?

A. Adding extra bytes to ensure members sit on specific address boundaries.
B. Removing unused variables.
C. Adding comments to code.
D. Compressing the structure size.

20 Given struct Node { int data; struct Node *next; };, what is this structure typically used for?

A. Arrays
B. Linked Lists
C. Unions
D. Files

21 If struct A { int x; }; and struct B { int x; };, are struct A and struct B compatible types?

A. Yes, because they have the same members.
B. No, they are distinct types.
C. Yes, if casted.
D. No, unless typedef is used.

22 How do you access the element at index 2 of an integer array arr inside a structure variable s?

A. s.arr.2
B. s->arr[2]
C. s.arr[2]
D. s[2].arr

23 What is the output size of sizeof(struct empty) in C (GCC/Standard behavior for empty structs)?

A.
B. 1
C. Compilation Error (or undefined in pure C standard)
D. 4

24 Which syntax initializes a union?

A. union Data d = {10, 20.5};
B. union Data d = {10};
C. union Data d = 10;
D. union Data d(10);

25 Is it possible to have a pointer to a bit-field in a structure?

A. Yes
B. No
C. Only if it is an integer
D. Only in Unions

26 What implies (*ptr).member?

A. Accessing member of a pointer variable.
B. Dereferencing a pointer to a structure and accessing the member.
C. Multiplying ptr by member.
D. Syntax error.

27 Given: struct { int a; char b; } arr[2] = {{1, 'a'}, {2, 'b'}};. What is arr[1].a?

A. 1
B. 2
C. 'a'
D. 'b'

28 Which header file is required to use structures?

A. <stdio.h>
B. <struct.h>
C. <stdlib.h>
D. None

29 Can a union be nested inside a structure?

A. Yes
B. No
C. Only if the union is named
D. Only if the structure is named

30 What is the size of struct { char a; int b; } on a machine with 4-byte integers and 4-byte alignment?

A. 5 bytes
B. 8 bytes
C. 4 bytes
D. 12 bytes

31 What is an 'anonymous structure'?

A. A structure without members.
B. A structure without a variable.
C. A structure without a tag name.
D. A structure that is hidden.

32 How are members of a union stored in memory?

A. Sequentially
B. In a linked list
C. Overlapping at the same start address
D. Randomly

33 Consider struct Box { int l, w, h; }; struct Box *b;. Which expression calculates the memory size required for the pointer?

A. sizeof(Box)
B. sizeof(struct Box)
C. sizeof(b)
D. sizeof(*b)

34 Can a structure be passed to a function by value?

A. Yes
B. No
C. Only if it is small
D. Only if it contains no arrays

35 Why is passing a large structure by pointer preferred over passing by value?

A. It is syntactically required.
B. It avoids copying the entire structure data, saving memory and time.
C. It allows the structure to be immutable.
D. Pointers are safer.

36 What is the result of struct { int a; } s1, s2; s1.a = 5; s2 = s1; s2.a = 10; on s1.a?

A. 5
B. 10
C.
D. Undefined

37 How do you declare a pointer ptr to a function that returns a structure S?

A. struct S *ptr();
B. struct S (*ptr)();
C. struct S ptr*();
D. pointer struct S ptr();

38 In the declaration struct { int a : 4; } x;, what does : 4 indicate?

A. Initialization to 4
B. Array size 4
C. Bit-field of width 4 bits
D. Byte alignment of 4

39 Which macro is used to find the offset of a member within a structure?

A. sizeof
B. typeof
C. offsetof
D. alignof

40 If u is a union variable and p is a pointer to u, which is valid?

A. p.member
B. u->member
C. p->member
D. *u.member

41 Can you use the scanf function to read input directly into a structure member?

A. No
B. Yes, e.g., scanf("%d", &s.age);
C. Yes, e.g., scanf("%d", s.age);
D. Only for strings

42 What happens if a structure is partially initialized? E.g., struct { int a, b; } s = {1};

A. Compile error
B. b is uninitialized (garbage)
C. b is initialized to 0
D. Runtime error

43 Given struct Test { char c; int i; };, which order are members stored in memory?

A. In order of declaration (c then i)
B. Largest to smallest (i then c)
C. Smallest to largest (c then i)
D. Compiler dependent entirely

44 What is a 'flexible array member' in C?

A. An array inside a struct with size 0 or undefined [], declared as the last member.
B. An array that changes type.
C. An array declared in a union.
D. A pointer.

45 Can a structure contain a pointer to a union?

A. Yes
B. No
C. Only if they are defined in same scope
D. Only in C++

46 Consider struct S { int *p; }; struct S s1, s2; int x = 10; s1.p = &x; s2 = s1;. What is the relationship between s1.p and s2.p?

A. They point to different addresses.
B. They point to the same address.
C. s2.p is null.
D. Copying is invalid for pointers.

47 If you have an array of unions, what is the memory layout?

A. Contiguous blocks, each the size of the largest union member.
B. Contiguous blocks, each the size of the sum of union members.
C. Scattered memory.
D. Linked list.

48 How do you define a structure type Person and a variable p1 in a single statement?

A. struct Person { ... } p1;
B. struct Person p1 { ... };
C. define struct Person { ... } p1;
D. Person p1 struct { ... };

49 Which statement best describes sizeof(union U) where union U { double d; char c[10]; };?

A. sizeof(double)
B. sizeof(char) * 10
C. MAX(sizeof(double), 10 bytes) + potential padding
D. Sum of both

50 Is struct T var = {0}; a valid way to initialize all members to zero?

A. Yes, it is the universal zero initializer.
B. No, must list all members.
C. Only for integers.
D. No, it initializes only the first member to 0.