An array of ________ can be used to call different functions based on an index.
- Functions
- Objects
- Pointers
- Structures
In C, you can use an array of function pointers to call different functions based on the index. Function pointers point to functions, making it possible to create arrays of functions and select them dynamically.
The function ______ is used in C to print a string to the standard output.
- print()
- puts()
- display()
- writeString()
The correct option is b) puts(). This function is used to print a string to the standard output, followed by a newline character.
What is the impact on memory usage when using pointers to structures?
- It always increases memory usage
- It always reduces memory usage
- It depends on how pointers are used
- It has no impact on memory usage
The impact on memory usage when using pointers to structures depends on how the pointers are used. When used wisely, pointers can reduce memory usage because they allow for more efficient data manipulation without creating redundant copies. However, improper use can lead to memory leaks or increased memory usage. It's important to manage memory properly when working with pointers to structures.
In C programming, a function that calls itself and the recursive call is the last operation before it returns is known as ________ recursion.
- Direct
- Indirect
- Non-tail
- Tail
In C programming, a function that calls itself and the recursive call is the last operation before it returns is known as "Tail" recursion. Tail recursion is an efficient form of recursion as it allows the compiler to optimize the function calls and avoid unnecessary overhead.
In a software program managing a university's data, you need to store information about students and their enrolled courses. How can nested structures be used in this context?
- To create graphics for the university's website
- To manage student financial records
- To represent complex data relationships between students and their courses
- To send emails to students
Nested structures can be used to represent complex data relationships. In this context, they can be used to create a structure for students that contains another structure for their enrolled courses. This allows for efficient storage and retrieval of information about students and their courses.
You're developing a text editor and need to implement a search feature. Which function could be useful to check if a certain word exists in a text?
- strcat
- strlen
- strrev
- strstr
In this scenario, the strstr function is appropriate for searching for a specific word within a text. It returns a pointer to the first occurrence of the specified word in the text, allowing you to check if it exists. strrev reverses a string, strlen provides the length of a string, and strcat concatenates strings.
When a floating-point number is declared using the ________ keyword, it allows for more precision than a regular float.
- double
- float
- long double
- precision
When a floating-point number is declared using the 'long double' keyword in C, it allows for more precision than a regular 'float' or 'double'.
What is a potential risk when using the strcpy function in C?
- Buffer overflow
- Compilation error
- Memory leak
- Segmentation fault
The strcpy function in C copies a string to a destination buffer without checking the length, which can lead to buffer overflow if the source string is longer than the destination buffer. This can result in overwriting adjacent memory, causing potential security vulnerabilities or program crashes.
You're debugging a segmentation fault in a program and find that a pointer is being accessed after it has been freed. What is this type of pointer error called?
- Dangling pointer error
- Invalid pointer error
- Null pointer error
- Out-of-scope pointer error
This type of pointer error is called a "dangling pointer error." It occurs when a pointer is accessed after it has been freed or deallocated, leading to undefined behavior and segmentation faults.
What is the role of pointers in relation to structures in C?
- Pointers allow dynamic allocation of structures
- Pointers can only be used within structures
- Pointers can't be used with structures
- Structures cannot have pointers
Pointers in C can be used to dynamically allocate memory for structures, which is essential for creating flexible data structures and managing memory efficiently.