Why might you choose to use an enumeration instead of a series of #define statements for constants?
- #define statements are faster to evaluate
- Enumerations are more memory-efficient
- Enumerations cannot be used for constants
- Enumerations provide type checking
You might choose to use an enumeration instead of a series of #define statements for constants because enumerations provide type checking, making your code more robust and error-prone. #define statements are simple text replacements and do not offer type safety.
The ________ keyword in C suggests to the compiler to use a CPU register for the variable to optimize access time.
- const
- register
- static
- volatile
The 'register' keyword in C suggests to the compiler to use a CPU register for the variable to optimize access time, which can lead to faster variable access.
What is a variable declared within a block or a function known as?
- Dynamic variable
- Local variable
- Private variable
- Public variable
A variable declared within a block or a function is known as a local variable, and its scope is limited to that specific block or function.
You are designing a C program to handle a database of employees in a company. Each employee has attributes like name, ID, and salary. What would be an efficient way to manage this data?
- Array of Structures
- Linked List of Structures
- Queue
- Stack
An array of structures would be an efficient way to manage data about employees in a company. This data structure allows for easy access and manipulation of employee records, making it suitable for tasks like searching for specific employees or sorting employees by various attributes.
In the context of bit fields, if the declared width is larger than the width of the specified type, the behavior is considered ________.
- Compilation error
- Overflow
- Undefined
- Well-defined
If the declared width of a bit field is larger than the width of the specified type, the behavior is undefined in C and C++. This means that the result may vary depending on the compiler and platform.
The use of inline functions can potentially lead to faster execution time but may also increase the ________ of the binary.
- Complexity
- Efficiency
- Size
- Speed
Inline functions can optimize execution time by eliminating the function call overhead. However, they may increase binary complexity due to code expansion, making the binary larger.
When a pointer is incremented, it moves to the memory address of the next element based on the ________ of the data type it points to.
- Color
- Name
- Shape
- Size
Pointer incrementation in C is determined by the size (in bytes) of the data type it points to.
You're optimizing a program for memory usage. What considerations would you take into account when deciding between using character arrays and string literals?
- Character arrays consume less memory than string literals, making them a better choice for memory optimization.
- Character arrays provide better performance due to reduced memory overhead.
- String literals are less efficient in terms of memory usage compared to character arrays.
- String literals are more memory-efficient, as they share storage and avoid duplicating string data.
When optimizing a program for memory usage, you would consider using character arrays over string literals because character arrays generally consume less memory. String literals can lead to duplicate storage, whereas character arrays allow more control over memory usage.
Which function in C is used to flush the output buffer of a stream?
- clear()
- fflush()
- flush()
- flushout()
The correct function is fflush(). It is used to flush the output buffer of a stream, ensuring that any buffered data is written to the file or output device. This is particularly useful when you want to ensure that data is immediately written without waiting for the buffer to fill up.
How can you open a file for both reading and writing in C?
- fopen("file.txt", "rw")
- fopen("file.txt", "r+")
- fopen("file.txt", "w+")
- open("file.txt", "rw")
The correct option is b. fopen("file.txt", "r+"). This combination of mode flags in the fopen() function allows a file to be opened for both reading and writing, providing flexibility in file manipulation operations.