Unit 6 - Practice Quiz
1 Which keyword is used in C to declare a user-defined data type that groups together variables of different data types?
class
struct
group
typedef
2 What is the main purpose of a structure in programming?
if-else
3
How do you correctly define a variable s1 of a structure type named Student?
define Student s1;
Student s1;
struct Student s1;
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; };
struct Point p1; p1 = (10, 20);
struct Point p1 = {10, 20};
struct Point p1 = (10, 20);
struct Point p1.x=10, p1.y=20;
5 Which operator is used to access a member of a structure variable?
* (Asterisk)
-> (Arrow operator)
. (Dot operator)
& (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?
b1->pages = 500;
b1[pages] = 500;
Book.pages = 500;
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?
& (Address-of operator)
* (Dereference operator)
. (Dot operator)
-> (Arrow operator)
8
How do you correctly declare a pointer p that can point to a structure of type Product?
Product *p;
pointer Product p;
struct Product *p;
struct *p Product;
9 What is a nested structure?
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?
emp.y
emp.Date.y
emp->dob->y
emp.dob.y
11 Which keyword is used to declare a union in C?
union
join
combine
struct
12
What is a key characteristic of a union regarding its members' memory?
13
What does a structure declaration, like struct Point { int x; int y; };, define?
14
If s_ptr is a pointer to a structure variable s1, which of the following is equivalent to s_ptr->member?
&s1.member
s1->member
*s_ptr.member
(*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?
students.name
students[0]->name
students[0].name
students.name[0]
16
Can you assign one structure variable to another of the same type using the assignment operator =?
17
What will the operator & return when applied to a structure variable, for example &my_struct?
18
Which of the following correctly defines a Person structure with a nested Address structure?
nest struct Address { char city[20]; } in Person;
struct Person { char name[50]; }; struct Address { char city[20]; Person p; };
struct Person { char name[50]; struct Address { char city[20]; } home; };
struct Person { char name[50]; Address home; };
19
What determines the size of a union in memory?
20
What is the result of not initializing a structure variable when it's defined (e.g., struct Point p;)?
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;
}
printf statement is missing the address-of operator &.
p is uninitialized and points to an invalid memory location.
p->x is incorrect and should be (*p).x.
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
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;
}
1 (or the value of the most significant byte of i).
258.
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;
}
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;
};
struct Book b = {year: 2023, title: "A Great Book"};
struct Book b = (.year = 2023, .title = "A Great Book");
struct Book b; b.year = 2023, b.title = "A Great Book";
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?
s.m is used for reading the member, while p->m is used for writing to it.
s.m is less efficient than p->m.
s.m is used when s is a structure object, while p->m is used when p is a pointer to a structure object.
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;
}
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;
Point_t p; instead of struct Point p;).
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;
}
30
Given a pointer p to a structure, which of the following expressions is syntactically equivalent to 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 };
y is not initialized.
x is 1, y is uninitialized, z is 5.
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;
};
sizeof(char[4]) + sizeof(int) + sizeof(double) (16 bytes).
sizeof(double) (8 bytes).
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;
};
struct Employee emp = {101, "New York", 10001};
struct Employee emp = {101, {"New York", 10001}};
struct Employee emp = (101, ("New York", 10001));
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;
}
Point objects that p1 and p2 point to.
x members of the Point objects.
p1 and p2, but not the Point objects they point to.
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];
36
What is the consequence of the following initialization?
c
struct Vector {
int x;
int y;
int z;
};
struct Vector v1 = {10, 20};
v1.x to 10 and v1.y and v1.z to 0.
v1.x to 10, v1.y to 20, and v1.z to an indeterminate garbage value.
v1.x to 10, v1.y to 20, and v1.z to 0.
37
What is the primary issue with the following structure declaration in standard C?
c
struct Node {
int data;
struct Node next;
};
typedef.
next is a reserved keyword.
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;
}
printf call.
**c.
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?
}
printf("%d", p.currentGame.score);
printf("%d", p.currentGame->score);
printf("%d", p->currentGame.score);
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?
++(s.x).
s itself.
x of the structure s, and the value of the expression is the incremented value of x.
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;
}
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;
};
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);
}
d.f remains exactly 10.0f.
d.f becomes 0.0f.
d.f becomes -10.0f.
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 };
c.timeout=100, c.mode is uninitialized, c.retries=5.
c.timeout=100, c.mode='A', c.retries=5.
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;
}
46
Which statement about Flexible Array Members (FAMs) in C99 is false?
c
struct Packet {
int length;
char contents[]; // Flexible Array Member
};
struct containing a FAM can be a member of another struct.
struct Packet p; on the stack.
struct with a FAM must have at least one other named member.
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;
}
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;
}
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;
}
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;
}
struct Point create_point(...) and returned the struct by value.
& 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?
}
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;
}
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;
}
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;
};
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;
};
char or unsigned char).
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;
}
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;
}
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.
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;
}
static structs cannot be initialized at declaration.
c.val will be initialized to 5 before main is executed.
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;
}
next pointer of the new node is not correctly linked.
Node cannot be passed to a function.
head pointer, not the original my_list pointer in main.
malloc may return NULL which is not handled.