Unit 6 - Practice Quiz

CSE101 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which keyword is used in C to declare a user-defined data type that groups together variables of different data types?

Declaration of a structure Easy
A. class
B. struct
C. group
D. typedef

2 What is the main purpose of a structure in programming?

Declaration of a structure Easy
A. To control the flow of a program like if-else
B. To group logically related data items of different types under a single name
C. To perform mathematical calculations
D. To store a list of items of the same data type

3 How do you correctly define a variable s1 of a structure type named Student?

Definition and initialization of structures Easy
A. define Student s1;
B. Student s1;
C. struct Student s1;
D. new Student s1;

4 Which of the following correctly initializes a Point structure variable p1 with x=10 and y=20? Assume struct Point { int x; int y; };

Definition and initialization of structures Easy
A. struct Point p1; p1 = (10, 20);
B. struct Point p1 = {10, 20};
C. struct Point p1 = (10, 20);
D. struct Point p1.x=10, p1.y=20;

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

Accessing structures Easy
A. * (Asterisk)
B. -> (Arrow operator)
C. . (Dot operator)
D. & (Ampersand)

6 Given struct Book { char title[50]; int pages; }; and struct Book b1;, how would you assign the value 500 to the pages member of b1?

Accessing structures Easy
A. b1->pages = 500;
B. b1[pages] = 500;
C. Book.pages = 500;
D. b1.pages = 500;

7 If ptr is a pointer to a structure, which operator is used to access a member of that structure through the pointer?

Structures and pointers Easy
A. & (Address-of operator)
B. * (Dereference operator)
C. . (Dot operator)
D. -> (Arrow operator)

8 How do you correctly declare a pointer p that can point to a structure of type Product?

Structures and pointers Easy
A. Product *p;
B. pointer Product p;
C. struct Product *p;
D. struct *p Product;

9 What is a nested structure?

Nested structures Easy
A. A structure that contains a pointer to itself
B. A structure defined as a member inside another structure
C. A pointer to a structure
D. An array of structures

10 Given struct Date {int d, m, y;}; and struct Employee {char name[20]; struct Date dob;};. If we have struct Employee emp;, how do we access the year of birth?

Nested structures Easy
A. emp.y
B. emp.Date.y
C. emp->dob->y
D. emp.dob.y

11 Which keyword is used to declare a union in C?

Declaration of a union Easy
A. union
B. join
C. combine
D. struct

12 What is a key characteristic of a union regarding its members' memory?

Declaration of a union Easy
A. All members share the same memory location.
B. Each member has its own dedicated memory location.
C. Members can only be accessed via pointers.
D. Only integer members are allowed.

13 What does a structure declaration, like struct Point { int x; int y; };, define?

Declaration of a structure Easy
A. A variable that holds data
B. A function
C. A pointer to memory
D. A new data type (a template)

14 If s_ptr is a pointer to a structure variable s1, which of the following is equivalent to s_ptr->member?

Structures and pointers Easy
A. &s1.member
B. s1->member
C. *s_ptr.member
D. (*s_ptr).member

15 If you have an array of structures, struct Student students[10];, how do you access the name of the first student?

Accessing structures Easy
A. students.name
B. students[0]->name
C. students[0].name
D. students.name[0]

16 Can you assign one structure variable to another of the same type using the assignment operator =?

Definition and initialization of structures Easy
A. Yes, this is a valid operation.
B. No, this operation is not allowed in C.
C. Yes, but only if they are global variables.
D. No, you must copy each member individually.

17 What will the operator & return when applied to a structure variable, for example &my_struct?

Structures and pointers Easy
A. The memory address of the structure
B. A syntax error
C. The value of the first member of the structure
D. The size of the structure in bytes

18 Which of the following correctly defines a Person structure with a nested Address structure?

Nested structures Easy
A. nest struct Address { char city[20]; } in Person;
B. struct Person { char name[50]; }; struct Address { char city[20]; Person p; };
C. struct Person { char name[50]; struct Address { char city[20]; } home; };
D. struct Person { char name[50]; Address home; };

19 What determines the size of a union in memory?

Declaration of a union Easy
A. It is always a fixed size, like 8 bytes
B. The sum of the sizes of all its members
C. The size of its smallest member
D. The size of its largest member

20 What is the result of not initializing a structure variable when it's defined (e.g., struct Point p;)?

Definition and initialization of structures Easy
A. The program will not compile.
B. Its members will contain garbage values.
C. Its members will be null.
D. Its members will be automatically initialized to zero.

21 Analyze the following C code snippet. What is the most likely issue?

c
#include <stdio.h>

struct Point {
int x;
int y;
};

int main() {
struct Point *p;
p->x = 10;
printf("%d\n", p->x);
return 0;
}

Structures and pointers Medium
A. The printf statement is missing the address-of operator &.
B. The pointer p is uninitialized and points to an invalid memory location.
C. The syntax p->x is incorrect and should be (*p).x.
D. A struct member cannot be modified via a pointer.

22 Given the following structure definitions, what is the correct way to access the year of a student object pointed to by studentPtr?

c
struct Date {
int day;
int month;
int year;
};

struct Student {
char name[50];
struct Date dob;
};

struct Student *studentPtr;
// Assume studentPtr is pointing to a valid Student object

Nested structures Medium
A. studentPtr->dob.year
B. studentPtr.dob->year
C. studentPtr.dob.year
D. (*studentPtr).dob->year

23 Consider the following union definition. If you store a value in i and then try to read from c, what will happen?

c
#include <stdio.h>

union Data {
int i; // 4 bytes
char c; // 1 byte
};

int main() {
union Data data;
data.i = 258; // Binary: 00000001 00000010
printf("%d", data.c);
return 0;
}

Declaration of a union Medium
A. The program will print 1 (or the value of the most significant byte of i).
B. The program will result in a compile-time error.
C. The program will print 258.
D. The program will print 2 (or the value of the least significant byte of i).

24 What is the output of the following C program?

c
#include <stdio.h>

struct Point { int x, y; };

void modify(struct Point p) {
p.x = 100;
p.y = 200;
}

int main() {
struct Point p1 = {10, 20};
modify(p1);
printf("%d %d", p1.x, p1.y);
return 0;
}

Accessing structures Medium
A. 100 200
B. 10 200
C. 10 20
D. 100 20

25 Which of the following correctly initializes a struct Book using designated initializers in C99?

c
struct Book {
char title[50];
char author[50];
int year;
};

Definition and initialization of structures Medium
A. struct Book b = {year: 2023, title: "A Great Book"};
B. struct Book b = (.year = 2023, .title = "A Great Book");
C. struct Book b; b.year = 2023, b.title = "A Great Book";
D. struct Book b = {.year = 2023, .title = "A Great Book"};

26 What is the primary difference in functionality between s.m and p->m when accessing a structure member m?

Structures and pointers Medium
A. s.m is used for reading the member, while p->m is used for writing to it.
B. s.m is less efficient than p->m.
C. s.m is used when s is a structure object, while p->m is used when p is a pointer to a structure object.
D. There is no functional difference; they can be used interchangeably.

27 What will be printed by the following code?

c
#include <stdio.h>

struct Dimensions {
float length;
float width;
};

struct Rectangle {
int id;
struct Dimensions size;
};

int main() {
struct Rectangle rect = {101, {10.5, 5.5}};
struct Rectangle *ptr = ▭
ptr->size.length = 12.0;
printf("%.1f", rect.size.length);
return 0;
}

Nested structures Medium
A. 12.0
B. 5.5
C. 10.5
D. The code will not compile.

28 Why would a programmer choose to use typedef when declaring a structure, as shown below?

c
typedef struct Point {
int x;
int y;
} Point_t;

Declaration of a structure Medium
A. It makes the structure members private and inaccessible from outside.
B. It allocates memory for the structure automatically.
C. It creates an alias for the structure type, allowing for cleaner variable declarations (e.g., Point_t p; instead of struct Point p;).
D. It is the only way to declare a structure in C.

29 What is the result of the following C code?

c
#include <stdio.h>

struct Value {
int v;
};

int main() {
struct Value a = {10};
struct Value b = a;
b.v = 20;
printf("%d", a.v);
return 0;
}

Accessing structures Medium
A. 20
B. A compile-time error will occur.
C. Undefined behavior.
D. 10

30 Given a pointer p to a structure, which of the following expressions is syntactically equivalent to p->member?

Structures and pointers Medium
A. &(*p).member
B. &p.member
C. *(p.member)
D. (*p).member

31 What are the values of s1.x, s1.y, and s1.z after this initialization?

c
struct Sample {
int x;
float y;
int z;
};

struct Sample s1 = { .z = 5, .x = 1 };

Definition and initialization of structures Medium
A. The code will fail to compile because y is not initialized.
B. x is 1, y is uninitialized, z is 5.
C. The code will fail to compile because the initializers are out of order.
D. x is 1, y is 0.0, z is 5.

32 Given the following union, what is the most likely result of sizeof(Packet) on a typical 64-bit system?

c
union Packet {
char header[4];
int payload;
double checksum;
};

Declaration of a union Medium
A. The sum of the sizes: sizeof(char[4]) + sizeof(int) + sizeof(double) (16 bytes).
B. The result is system-dependent and cannot be predicted.
C. The size of the largest member: sizeof(double) (8 bytes).
D. The size of the first member: sizeof(char[4]) (4 bytes).

33 Which initialization statement for the emp variable is correct based on the provided nested structure definitions?

c
struct Address {
char city[20];
int zipCode;
};

struct Employee {
int id;
struct Address addr;
};

Nested structures Medium
A. struct Employee emp = {101, "New York", 10001};
B. struct Employee emp = {101, {"New York", 10001}};
C. struct Employee emp = (101, ("New York", 10001));
D. struct Employee emp = {{101}, {"New York", 10001}};

34 What does the following function achieve?

c
struct Point { int x, y; };

void swapPoints(struct Point p1, struct Point p2) {
struct Point temp = p1;
p1 = p2;
p2 = temp;
}

Structures and pointers Medium
A. It swaps the contents of the Point objects that p1 and p2 point to.
B. It only swaps the x members of the Point objects.
C. It swaps the pointers p1 and p2, but not the Point objects they point to.
D. The code has a syntax error and will not compile.

35 Consider an array of structures. How would you access the price of the third element in the inventory array?

c
struct Product {
int id;
float price;
};

struct Product inventory[10];

Accessing structures Medium
A. inventory[2].price
B. inventory.price[2]
C. inventory[2]->price
D. inventory[3].price

36 What is the consequence of the following initialization?

c
struct Vector {
int x;
int y;
int z;
};

struct Vector v1 = {10, 20};

Definition and initialization of structures Medium
A. It initializes v1.x to 10 and v1.y and v1.z to 0.
B. It initializes v1.x to 10, v1.y to 20, and v1.z to an indeterminate garbage value.
C. It initializes v1.x to 10, v1.y to 20, and v1.z to 0.
D. It causes a compile-time error because not all members are initialized.

37 What is the primary issue with the following structure declaration in standard C?

c
struct Node {
int data;
struct Node next;
};

Declaration of a structure Medium
A. A structure cannot contain a member of its own type.
B. The structure is missing a typedef.
C. The member name next is a reserved keyword.
D. The member next should be declared as a pointer (struct Node *next;).

38 What is the output of this C program?

c
#include <stdio.h>
#include <stdlib.h>

struct Config {
int setting;
};

void setup(struct Config *c) {
c = (struct Config)malloc(sizeof(struct Config));
(
c)->setting = 99;
}

int main() {
struct Config *my_config = NULL;
setup(&my_config);
printf("%d\n", my_config->setting);
free(my_config);
return 0;
}

Structures and pointers Medium
A. A segmentation fault will occur at the printf call.
B. 0
C. The program will not compile due to the double pointer **c.
D. 99

39 Analyze the code snippet. Which printf statement will correctly print the score of the game played by the player?

c
struct Game {
int score;
};

struct Player {
struct Game *currentGame;
};

int main() {
struct Game g = {1500};
struct Player p;
p.currentGame = &g;
// Which line prints 1500?
}

Nested structures Medium
A. printf("%d", p.currentGame.score);
B. printf("%d", p.currentGame->score);
C. printf("%d", p->currentGame.score);
D. printf("%d", p->currentGame->score);

40 In C, the expression (++s.x) where s is a struct variable with an integer member x is valid. What does it do?

Accessing structures Medium
A. It is invalid due to operator precedence; it should be ++(s.x).
B. It increments the memory address of the structure s itself.
C. It increments the member x of the structure s, and the value of the expression is the incremented value of x.
D. It increments a temporary copy of s.x but does not modify the original structure.

41 Analyze the following C code snippet assuming sizeof(int) is 4 bytes. What is the printed output?

c
#include <stdio.h>

struct Point {
int x;
int y;
};

int main() {
struct Point p = {0x1A, 0x2B};
char cptr = (char)&p;
cptr += sizeof(int);
printf("%X", ((int)cptr));
return 0;
}

Structures and pointers Hard
A. Undefined Behavior
B. 2B
C. 1A
D. A memory address

42 Assuming a 64-bit system where pointers are 8 bytes, int is 4 bytes, and char is 1 byte, and standard member alignment is used, what is the output of sizeof(Outer)?

c
struct Inner {
int data1;
char data2;
};

struct Outer {
char a;
struct Inner i;
char* p;
};

Nested structures Hard
A. 21 bytes
B. 17 bytes
C. 32 bytes
D. 24 bytes

43 Consider the following code, which attempts to manipulate a floating-point number's bit representation. What is the most likely effect on d.f after the bitwise AND operation?

c
#include <stdio.h>

union Data {
float f;
unsigned int i;
};

int main() {
union Data d;
d.f = 10.0f; // A positive non-zero float
// Assume IEEE 754 representation
d.i = d.i & 0x7FFFFFFF;
printf("%f", d.f);
}

Declaration of a union Hard
A. The value of d.f remains exactly 10.0f.
B. The value of d.f becomes 0.0f.
C. The value of d.f becomes -10.0f.
D. The program results in a compilation error.

44 What is the status of the following C99 code snippet regarding compilation and the values of the struct members?

c
struct Config {
int timeout;
char mode;
int retries;
};

struct Config c = { .timeout = 100, 'A', .retries = 5 };

Definition and initialization of structures Hard
A. Compilation error: Mixing designated and non-designated initializers is not allowed this way.
B. Compiles successfully: c.timeout=100, c.mode is uninitialized, c.retries=5.
C. Compiles successfully: c.timeout=100, c.mode='A', c.retries=5.
D. Undefined behavior at runtime.

45 Analyze the const qualifiers in this C code. Which line, if uncommented, would cause a compilation error?

c
#include <stdlib.h>

struct Node {
int data;
};

void create_node(struct Node *p) {
p = malloc(sizeof(struct Node));
}

int main() {
const struct Node ptr1 = NULL;
struct Node
const ptr2 = NULL;

// create_node(&ptr1); // Line A
// create_node(&ptr2); // Line B
// ptr1->data = 10; // Line C
// ptr2 = NULL; // Line D

return 0;
}

Structures and pointers Hard
A. Line B
B. Line D
C. Line A
D. Line C

46 Which statement about Flexible Array Members (FAMs) in C99 is false?

c
struct Packet {
int length;
char contents[]; // Flexible Array Member
};

Declaration of a structure Hard
A. A struct containing a FAM can be a member of another struct.
B. You can create a variable struct Packet p; on the stack.
C. A struct with a FAM must have at least one other named member.
D. sizeof(struct Packet) returns the size of the structure excluding the FAM.

47 What will be the output of the following C code on a little-endian system?

c
#include <stdio.h>

union EndianCheck {
unsigned short val; // 2 bytes
unsigned char bytes[2];
};

int main() {
union EndianCheck ec;
ec.val = 256; // 0x0100 in hex
printf("%d %d", ec.bytes[0], ec.bytes[1]);
return 0;
}

Declaration of a union Hard
A. 1 0
B. 0 256
C. 256 0
D. 0 1

48 What is the output of this program that uses bit-fields? Assume int is 32 bits.

c
#include <stdio.h>

struct Flags {
unsigned int a : 1;
unsigned int b : 2;
unsigned int c : 3;
};

int main() {
struct Flags f = {0};
f.b = 5; // 5 is binary 101
printf("%u %u", f.a, f.b);
return 0;
}

Accessing structures Hard
A. 1 5
B. 0 1
C. 1 1
D. 0 5

49 What is the output of the following C code?

c
#include <stdio.h>

struct Inner { int val; };

struct Outer {
struct Inner i;
struct Inner p;
};

int main() {
struct Inner i1 = {10};
struct Inner i2 = {20};
struct Outer o = {i1, &i2};

o.p->val = o.i.val + 5;
o.i =
o.p;

printf("%d %d", o.i.val, i2.val);
return 0;
}

Nested structures Hard
A. 10 15
B. 15 15
C. 25 25
D. 15 20

50 What is the fundamental issue with the create_point function in the code below?

c
#include <stdio.h>

struct Point { int x, y; };

struct Point create_point(int x, int y) {
struct Point p;
p.x = x;
p.y = y;
return &p; // Warning/Error here
}

int main() {
struct Point
ptr = create_point(10, 20);
// The next line invokes undefined behavior
printf("%d", ptr->x);
return 0;
}

Structures and pointers Hard
A. A struct cannot be created inside a function that returns a pointer to it.
B. The function returns a pointer to a local variable, which leads to a dangling pointer.
C. The function should have been declared as struct Point create_point(...) and returned the struct by value.
D. The & operator cannot be used on a struct variable.

51 Given the following code, what is the value of p.c.y after the assignment?

c
struct Point {
int x;
int y;
};

struct Parent {
int id;
struct Point c;
};

struct Point get_point() {
struct Point temp = {100, 200};
return temp;
}

int main() {
struct Parent p = {1, {10, 20}};
p.c = get_point();
// What is p.c.y now?
}

Accessing structures Hard
A. 200
B. 20
C. 100
D. Undefined

52 What is the output of this C code that uses a compound literal?

c
#include <stdio.h>

struct Point { int x, y; };

void print_point(const struct Point *p) {
printf("%d,%d\n", p->x, p->y);
}

int main() {
print_point(&(struct Point){.y=30, .x=40});
return 0;
}

Definition and initialization of structures Hard
A. Compilation Error
B. 40,30
C. 30,40
D. 0,0

53 Consider a struct containing a function pointer. What is the output of the following program?

c
#include <stdio.h>

struct Operation {
int a, b;
int (op)(int, int);
};

int add(int x, int y) { return x + y; }
int mul(int x, int y) { return x
y; }

int main() {
struct Operation op1 = {10, 5, add};
struct Operation op2 = {4, 3, mul};
op1.op = op2.op;
op1.a = op2.a;
printf("%d", op1.op(op1.a, op1.b));
return 0;
}

Structures and pointers Hard
A. 50
B. 20
C. 15
D. 12

54 Why does the first declaration of struct Node cause a compilation error, while the second one is valid?

c
// Declaration 1 (Error)
struct Node {
int data;
struct Node next;
};

// Declaration 2 (Valid)
struct Node {
int data;
struct Node *next;
};

Declaration of a structure Hard
A. Structs cannot be self-referential in any way.
B. A struct cannot contain a member of its own type because it would have an infinite, undefined size.
C. Declaration 1 is a syntax error; the member name must come before the type.
D. The next member in Declaration 1 must be initialized to NULL.

55 In C11 and later, reading from a union member that is not the one last written to (the non-active member) is conditionally supported. Under which circumstance is it explicitly well-defined behavior to write to one union member and read from another?

c
union Data {
struct { int x, y; } s;
long long l;
float f;
};

Declaration of a union Hard
A. Only when reading character types (char or unsigned char).
B. Only when the types are of the same size.
C. When the member being read and the member last written to share a common initial sequence.
D. Never, it is always undefined behavior.

56 What is the result of the pointer arithmetic in the expression (arr + 1)->b?

c
#include <stdio.h>

struct S {
int a, b;
};

int main() {
struct S arr[2] = {{1, 2}, {3, 4}};
struct S *p = arr;
printf("%d", (arr + 1)->b);
return 0;
}

Structures and pointers Hard
A. 4
B. 2
C. Compilation Error
D. 3

57 Analyze the following code snippet which performs a struct assignment. What is the value of s2.data->val after the assignment?

c
#include <stdlib.h>

struct Data { int val; };

struct Container {
int id;
struct Data* data;
};

int main() {
struct Container s1;
s1.id = 1;
s1.data = malloc(sizeof(struct Data));
s1.data->val = 100;

struct Container s2 = s1; // Struct assignment
s2.id = 2;
s2.data->val = 200;

// What is the value of s1.data->val at this point?
return 0;
}

Nested structures Hard
A. 200
B. The code causes a segmentation fault.
C. A random garbage value
D. 100

58 Given the definitions below, which expression is equivalent to (*p).a[0]->c?

c
struct C {
int c;
};

struct B {
struct C a[2];
};

struct B b;
struct B
p = &b;
// Assume members are properly initialized.

Accessing structures Hard
A. *(p->a[0]).c
B. p->a[0]->c
C. p.a[0].c
D. (*p->a)->c

59 What is the main issue with the initialization of the static struct c in this C code snippet?

c
int get_val() {
return 5;
}

struct Config {
int val;
};

// Global scope
static struct Config c = { get_val() };

int main() {
return 0;
}

Definition and initialization of structures Hard
A. Compilation error: static structs cannot be initialized at declaration.
B. Compilation error: The initializer for an object with static storage duration must be a constant expression.
C. It is perfectly valid; c.val will be initialized to 5 before main is executed.
D. Runtime error: get_val() is called before main() in an undefined order.

60 The following function is intended to create and prepend a new node to a linked list, but it contains a logical flaw. What is it?

c
#include <stdlib.h>

typedef struct Node {
int data;
struct Node next;
} Node;

void prepend(Node
head, int data) {
Node new_node = malloc(sizeof(Node));
new_node->data = data;
new_node->next = head;
head = new_node; // The flawed line
}

int main() {
Node
my_list = NULL;
prepend(my_list, 10);
// my_list is still NULL here
return 0;
}

Structures and pointers Hard
A. The next pointer of the new node is not correctly linked.
B. A pointer to a Node cannot be passed to a function.
C. The function modifies a local copy of the head pointer, not the original my_list pointer in main.
D. malloc may return NULL which is not handled.