WorksheetsExploring C Functions and Data Structures
Total questions: 20
Worksheet time: 7hrs 40mins
What is the correct syntax for declaring a function in C?
return_type function_name(parameter_name1, parameter_type1);
function_name return_type(parameter_type1, parameter_type2);
return_type function_name(parameter_type1 parameter_name1, parameter_type2 parameter_name2, ...);
function return_type(parameter_type1, parameter_name1);
Explain the purpose of a function prototype in C.
To automatically generate documentation for the function.
To define the function's implementation in advance.
To allow the function to be called without any parameters.
The purpose of a function prototype in C is to enable type checking and inform the compiler about the function's signature before its definition.
What is pointer arithmetic and how is it performed in C?
Pointer arithmetic can only be performed on integer data types.
Pointer arithmetic is a method to allocate memory dynamically in C.
Pointer arithmetic allows for efficient navigation through arrays and memory in C by adjusting pointer values based on the size of the data type.
Pointer arithmetic is only used for string manipulation in C.
How do you allocate memory dynamically in C using malloc?
malloc(size) = ptr;
void *ptr = allocate(size);
void *ptr = malloc(size);
ptr = malloc(size, flags);
What is the difference between calloc and malloc in C?
malloc is used for allocating memory for arrays only.
calloc initializes memory to zero, while malloc does not.
calloc can only allocate memory for structures.
calloc allocates memory faster than malloc.
Describe the basic operations of a stack data structure.
The basic operations of a stack are Push, Pop, Peek, and IsEmpty.
Enqueue, Dequeue, Front, and IsEmpty
Insert, Delete, Top, and IsEmpty
Add, Remove, View, and IsFull
How do you implement a stack using an array in C?
Define a struct with an array and an index, implement push, pop, and isEmpty functions.
Use a global variable to store the stack data.
Use a linked list to implement the stack.
Implement only the push function without pop.
What is a queue and how does it differ from a stack?
A queue is a LIFO data structure, while a stack is a FIFO data structure.
A queue allows random access, while a stack does not.
A queue is a data structure that only allows adding elements at the front.
A queue is a FIFO data structure, while a stack is a LIFO data structure.
List the types of queues and their applications.
Types of queues include Simple Queue, Circular Queue, Priority Queue, Double-Ended Queue (Deque), Blocking Queue, and Concurrent Queue, each with specific applications in task scheduling, resource management, algorithm implementation, and multithreading.
Static Queue
Stack Queue
Linear Queue
Explain the concept of a circular queue and its advantages.
Advantages of a circular queue include efficient space utilization, reduced time complexity for enqueue and dequeue operations, and the ability to reuse empty spaces created by dequeued elements.
Circular queues require more memory than linear queues.
Circular queues can only store fixed-size elements.
They do not allow for efficient space reuse after dequeuing.
What is the time complexity of enqueue and dequeue operations in a queue?
O(n) for both enqueue and dequeue operations.
O(1) for enqueue and O(n) for dequeue operations.
O(n) for enqueue and O(1) for dequeue operations.
O(1) for both enqueue and dequeue operations in a typical queue implementation.
How can you implement a queue using a linked list in C?
Use an array to store elements and implement push and pop functions.
Define a binary tree structure to manage queue operations.
Create a stack structure with top pointer and implement push and pop functions.
Define a linked list node structure, create a queue structure with front and rear pointers, implement enqueue and dequeue functions.
What are the potential issues with using pointers in C?
Using pointers guarantees faster execution
Pointers eliminate the need for variables in C
Pointers automatically manage memory allocation
Potential issues with using pointers in C include memory leaks, dangling pointers, pointer arithmetic errors, increased code complexity, and risk of segmentation faults.
How do you free dynamically allocated memory in C?
Use the 'delete' operator to free memory.
Call the 'dispose' function to release memory.
Set the pointer to NULL to free memory.
Use the 'free' function to release dynamically allocated memory.
What is the significance of the NULL pointer in C?
The NULL pointer is used to allocate memory dynamically.
The NULL pointer is a valid memory address that can be dereferenced.
The NULL pointer signifies that a pointer does not point to any valid memory location.
The NULL pointer indicates a pointer to the first element of an array.
Describe how to check if a stack is empty or full.
Check if the stack's memory allocation is zero.
Use top index comparison: empty if top == -1, full if top == max_size - 1.
Check if the stack contains any elements.
Use a counter to track the number of elements in the stack.
What are the common applications of stacks in programming?
Memory allocation management
Data sorting algorithms
Network communication protocols
Common applications of stacks include function call management, expression evaluation, backtracking algorithms, and undo mechanisms.
How can you reverse a string using a stack?
Replace each character with its ASCII value to reverse the string.
Reverse the string by sorting its characters alphabetically.
Use a queue to enqueue each character and then dequeue them.
Use a stack to push each character of the string, then pop them to form the reversed string.
What is the role of pointers in dynamic data structures?
Pointers facilitate the creation and manipulation of dynamic data structures by linking nodes and managing memory allocation.
Pointers eliminate the need for memory management in data structures.
Pointers are only relevant in low-level programming languages.
Pointers are used to create static data structures.
Explain how to traverse a circular queue.
Use a stack to access elements in the queue sequentially.
Use a loop to iterate from the front index to the rear index, applying modulo to wrap around the queue.
Directly access elements using their index without any iteration.
Traverse the queue by moving from rear to front without wrapping around.
