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.
Which keyword is used in C to create an alias for a data type?
- enum
- struct
- typedef
- union
In C programming, the "typedef" keyword is used to create an alias or a new name for an existing data type. This makes the code more readable and can help in achieving better code organization.
The preprocessor directive ________ is used to include libraries in a C program.
- #define
- #ifdef
- #include
- #pragma
The correct answer is (b) #include. In C, the #include preprocessor directive is used to include libraries or header files. This allows you to use functions and definitions from those libraries in your program.
The ________ directive can be used in C to include a file only once in a program.
- #ifdef
- #ifndef
- #include
- #pragma
In C programming, the #include directive is used to include files in a program. When you want to include a file only once, you can use preprocessor directives like #ifndef and #define to create include guards.
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.
When is it appropriate to use the free function in C programming?
- After allocating memory using malloc
- At the beginning of your program
- When you are done using dynamically allocated memory
- Whenever you want
The free function in C is used to deallocate the dynamically allocated memory when you are done using it. It helps prevent memory leaks and frees up resources.
What is the significance of the return type in a function declaration in C?
- It defines the scope of variables used within the function
- It determines the function's execution order
- It indicates the data type of the value the function returns
- It specifies the number of arguments a function can take
The return type in a function declaration in C indicates the data type of the value that the function will return to the calling program. This information is vital for using the function correctly and processing its result.
In a two-dimensional array declared as int arr[5][10];, the expression arr[i][j] refers to the ________ element in memory.
- i-th column, j-th row
- i-th row, j-th column
- j-th column, i-th row
- j-th row, i-th column
In a two-dimensional array, the first index corresponds to rows, and the second index corresponds to columns. So, arr[i][j] refers to the element at the i-th row and j-th column.
What is the difference between stderr and stdout in the context of buffering?
- stderr is unbuffered, while stdout is line-buffered by default.
- stderr is line-buffered, while stdout is unbuffered.
- stderr is fully buffered, while stdout is line-buffered.
- stderr is fully buffered, while stdout is unbuffered.
The correct option is "stderr is unbuffered, while stdout is line-buffered by default." stderr is typically unbuffered, meaning that data is immediately written to the output, while stdout is line-buffered, meaning that it is buffered until a newline character is encountered.