How can you determine the size of an array in C?
- By counting the number of elements
- By using the 'size' function
- Using the 'length' keyword
- Using the 'sizeof' operator
You can determine the size of an array in C by using the 'sizeof' operator. It returns the size in bytes, so dividing it by the size of one element gives you the number of elements in the array.
When using pointers in a C program, what does the 'dereferencing' operation do?
- Accesses the value stored at a memory address
- Allocates memory for a new pointer
- Increases the pointer's size
- Retrieves the memory address of a variable
Dereferencing a pointer in C means accessing the value stored at the memory location pointed to by the pointer. It allows you to work with the actual data.
What happens if the fopen function fails to open the specified file?
- The program crashes
- It returns a NULL pointer
- It displays an error message on the console
- It opens a default file
The correct option is b. It returns a NULL pointer. When the fopen() function fails to open the specified file, it returns a NULL pointer, indicating that the file opening was unsuccessful. Programmers can then check for this NULL value to handle errors gracefully.
To access members of a structure using a pointer to that structure, the ________ operator is used.
- arrow
- colon
- dot
- hyphen
To access members of a structure using a pointer to that structure, the arrow (->) operator is used in C.
A ________ search works by repeatedly dividing the portion of the array that could contain the item until you've narrowed down the possible locations to just one.
- Binary
- Depth-First
- Hash
- Linear
A 'Binary' search works by repeatedly dividing the portion of the array that could contain the item until you've narrowed down the possible locations to just one. It's an efficient search algorithm for sorted arrays.
How does the 'ternary' operator (?:) work in C?
- It defines a function in C
- It performs bit-level operations
- It's used for binary arithmetic
- It's used to create a conditional expression
The 'ternary' operator in C, represented as '?:', is a shorthand way to write a simple conditional expression. It allows you to return one of two values based on a condition.
What is a key characteristic of a union in C?
- Allows multiple data types in a single memory location
- Automatically allocates memory on the stack
- Only works with integers
- Provides dynamic memory allocation
A union in C allows you to store different data types in a single memory location, and it's a crucial feature for creating composite data types.
In C, which function can be used to search for a substring within a string?
- find()
- locate()
- search()
- strstr()
The strstr() function in C is used to search for a substring within a string. It returns a pointer to the first occurrence of the substring or NULL if the substring is not found.
How does the 'switch' statement compare to a series of 'if-else' statements in terms of efficiency?
- The 'switch' statement is generally more efficient when comparing a single value to multiple options.
- The 'switch' statement is less efficient than 'if-else' statements.
- The 'switch' statement and 'if-else' statements have the same efficiency.
- Efficiency depends on the specific use case.
The 'switch' statement is typically more efficient than a series of 'if-else' statements when you need to compare a single value to multiple options. It allows for jump tables, which can lead to faster execution. However, the efficiency may vary depending on the compiler and specific scenarios.
In C++, if two overloaded functions have the same signature but differ in const qualification, it leads to ________.
- Ambiguity Error
- Compilation Error
- Linker Error
- Undefined Behavior
When two overloaded functions in C++ have the same signature but differ in const qualification, it leads to undefined behavior. The compiler cannot determine which function to call, resulting in ambiguous code. It's essential to avoid such situations for code clarity and reliability.