How can you redirect error messages in a C program to a file instead of displaying them on the console?
- stderr_redirect()
- errorToFile()
- file_error()
- freopen()
The correct option is freopen(). This function can be used to redirect standard error (stderr) to a file. By using freopen("error.log", "w", stderr), error messages will be written to the specified file instead of the console.
In C, pointers can be used to pass ________ to a function.
- addresses
- constants
- data
- variables
In C, pointers can be used to pass addresses to a function. This allows functions to modify the original data.
Which preprocessor directive is used to include a header file in a C program?
- #define
- #ifdef
- #ifndef
- #include
The preprocessor directive #include is used to include a header file in a C program. It allows you to use functions and declarations from the included file in your program.
In a database application, you need to sort a list of names (strings) in alphabetical order. Which string handling function might be useful in this scenario?
- strchr
- strcmp
- strnlen
- strtok
To sort a list of names in alphabetical order, the strcmp function is useful. It compares two strings and returns a value indicating their relative order. strtok is used for tokenizing strings, strchr searches for a character in a string, and strnlen provides the length of a string up to a specified limit.
In a program managing a database of students, each student has attributes like name, age, and grades. How would pointers to structures be beneficial in this scenario?
- Enhance database security
- Improve data organization
- Minimize CPU usage
- Streamline user input
Pointers to structures can be beneficial in this scenario by improving data organization. By using pointers to structures, you can efficiently manage and access student information, such as name, age, and grades, making the database more organized and accessible. This optimization helps in effective data management.
What is a potential risk of using the gets() function for reading strings in C?
- It always returns NULL
- It can lead to buffer overflow
- It doesn't exist in C
- It has a higher time complexity
The gets() function is risky because it does not perform bounds checking and can lead to buffer overflows, potentially exposing the program to security vulnerabilities.
You're working on a program that continuously monitors sensor data and reacts when a certain threshold is reached. Which loop construct would be most suitable for this task?
- do-while loop
- for loop
- if-else statement
- while loop
The most suitable loop construct for continuously monitoring sensor data is a 'while loop.' A while loop checks a condition and executes its code block repeatedly as long as the condition is true. This allows continuous monitoring until the threshold is reached.
When a value is passed to a function by value, what is passed to the function?
- A copy of the value
- A pointer to the value
- A reference to the value
- The memory address of the value
When a value is passed to a function by value, a copy of the actual value is passed to the function. Changes made to the parameter inside the function do not affect the original value.
An array declared as int arr[10]; allocates memory for ________ integers.
- 10
- 15
- 20
- 5
When you declare an array like int arr[10], it allocates memory for 10 integers.
What is the primary purpose of structures in C programming?
- Allocate memory for arrays
- Create loops in C
- Define functions
- Group variables of different data types
In C programming, structures are used to group variables of different data types into a single unit. They allow you to create a composite data type, which is useful for organizing related information. This helps in better organization and management of data in your program.