You're working on an embedded system with limited memory. What feature of C would you use to efficiently pack multiple flags into a single byte?
- Bit fields
- Pointers
- Inline functions
- Function pointers
Option A, "Bit fields," is the correct choice. Bit fields allow you to efficiently pack multiple boolean flags into a single byte, which is essential in resource-constrained embedded systems, helping to save memory. Bit fields provide control over the size and alignment of data within a structure, allowing you to use memory more efficiently. Other options are not typically used for this purpose and may not offer the same level of memory optimization. Bit fields are an important concept for embedded systems programming.
The ________ function in C is used to search for the first occurrence of a character in a string.
- strchr()
- strnchr()
- strsearch()
- strstr()
In C, the strchr() function is used to search for the first occurrence of a character in a string. It returns a pointer to the first occurrence or a null pointer if the character is not found. This function is commonly used in C for string manipulation.
You are tasked with developing a program that must search for a specific item in a large dataset efficiently. What type of searching algorithm would be ideal for this task?
- Binary Search
- Bubble Sort
- Linear Search
- Quick Sort
Binary Search is the ideal choice for searching in a large dataset as it offers logarithmic time complexity, making it efficient for large datasets. Linear Search, Bubble Sort, and Quick Sort are not efficient for this task.
In C, the initial call to a recursive function is known as the ________.
- base case
- initial call
- recursive case
- termination case
The initial call to a recursive function is often referred to as the "initial call" because it's where the recursion begins.
Which function in C is used to flush the output buffer of a stream?
- fflush()
- fprintf()
- fputc()
- fputs()
The fflush() function in C is used to flush the output buffer of a stream, ensuring that any buffered data is written to the file or console.
How can you handle an error when attempting to allocate memory using malloc() in a C program?
- Check if the return value of malloc() is NULL and handle accordingly.
- Use try-catch blocks to handle memory allocation errors.
- Set a flag in case of an error and handle it in a separate error-handling function.
- Call exit() to terminate the program if malloc() fails.
The correct option is "Check if the return value of malloc() is NULL and handle accordingly." Malloc() returns a NULL pointer if it fails to allocate memory. It's crucial to check this return value to handle memory allocation errors gracefully.
How is the size of a union determined in C?
- By the number of members
- By the size of its largest member
- By the size of its smallest member
- By the sum of the sizes of its members
In C, the size of a union is determined by the size of its largest member. If you have a union with multiple members, the size will be at least as large as the largest member's size.
How does using an array of structures impact memory usage in a C program?
- Using an array of structures depends on the size of the array.
- Using an array of structures has no impact on memory usage.
- Using an array of structures increases memory usage.
- Using an array of structures reduces memory usage.
Using an array of structures in a C program increases memory usage because each element of the array holds a complete structure, and memory is allocated for each. This can be more memory-intensive compared to individual structures.
Imagine you are developing a C program to manage a library's book inventory. What data structure would be beneficial for storing information about each book?
- Array
- Binary Search Tree
- Hash Table
- Linked List
A hash table would be efficient for storing information about each book in the library's inventory. It allows for fast retrieval of book details based on a unique key (e.g., book ISBN or title). This data structure provides constant-time average lookup, making it suitable for efficient management of the library's book inventory.
In C, what is the relation between the addresses of consecutive elements of an array of type 'int'?
- The addresses are consecutive with a difference of 1
- The addresses are consecutive with a difference of 2
- The addresses are consecutive with a difference of 4
- The addresses are random and not related
In C, the addresses of consecutive elements of an array of type 'int' are typically consecutive with a difference of 4, as 'int' data type is usually 4 bytes in size. This allows for efficient traversal and indexing.