Unit 6 - Practice Quiz

CSE109 50 Questions
0 Correct 0 Wrong 50 Left
0/50

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

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

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

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

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

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

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 cannot contain arrays.
B. It must contain only integers.
C. It defines a new data type but does not allocate memory.
D. It allocates memory for variables.

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

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

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

How is d1 initialized?

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

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

A. Yes, but only as a pointer.
B. Yes, directly.
C. No, never.
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. 5 bytes
B. 8 bytes
C. 4 bytes
D. 1 byte

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. s->age
D. p.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 Student struct student_record;
B. typedef struct student_record Student;
C. struct student_record Student;
D. alias struct student_record Student;

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

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

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

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

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

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

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

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

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. Undefined/Garbage
B. 2.5
C. 10
D. 0

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

A. Yes, it compares all members.
B. Yes, if they are initialized.
C. No, C does not support operator overloading for structs.
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. Adding comments to code.
C. Removing unused variables.
D. Compressing the structure size.

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

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

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

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

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

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

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

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

24 Which syntax initializes a union?

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

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

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

26 What implies (*ptr).member?

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

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

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

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. Only if the union is named
C. Only if the structure is named
D. No

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

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

31 What is an 'anonymous structure'?

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

32 How are members of a union stored in memory?

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

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

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

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

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

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

A. It allows the structure to be immutable.
B. It is syntactically required.
C. It avoids copying the entire structure data, saving memory and time.
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. Undefined
C. 0
D. 10

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. pointer struct S ptr();
D. struct S *ptr();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

45 Can a structure contain a pointer to a union?

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

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 the same address.
B. Copying is invalid for pointers.
C. s2.p is null.
D. They point to different addresses.

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

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

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

A. define struct Person { ... } p1;
B. struct Person p1 { ... };
C. 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. Sum of both
D. MAX(sizeof(double), 10 bytes) + potential padding

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.