How does the 'extern' keyword affect the scope and lifetime of a variable in C?
- It doesn't affect the scope or lifetime of a variable.
- It extends the scope and lifetime of a variable.
- It extends the scope but reduces the lifetime of a variable.
- It reduces the scope but extends the lifetime of a variable.
In C, the 'extern' keyword extends the scope of a variable but reduces its lifetime. It allows a variable declared outside a function to be used within that function, but the variable's lifetime remains outside the function.
How are strings typically terminated in C?
- With a comma
- With a newline character
- With a null character ( ' ' )
- With a space character
Strings in C are typically terminated with a null character (' ') to indicate the end of the string.
What is the default starting value of an enumeration in C?
- 0
- 1
- The value of the first enumeration constant
- Unspecified
In C, if no initial value is provided for the first enumeration constant, it starts with 0. Subsequent constants increment by 1.
In C, what is the difference in memory allocation between character arrays and string literals?
- Character arrays are automatically null-terminated, while string literals are not.
- Character arrays are stored in read-write memory, while string literals are stored in read-only memory.
- Character arrays cannot be used as function arguments, while string literals can.
- String literals are stored in read-write memory, while character arrays are stored in read-only memory.
Character arrays in C are typically stored in read-write memory, allowing you to modify their contents. In contrast, string literals are stored in read-only memory, making them immutable. This difference in memory allocation is essential for understanding how to work with strings in C.
How does function overloading in C++ enhance code readability and maintainability?
- It allows functions to have the same name and return type.
- It allows multiple functions with the same name but different parameter types.
- It is not related to code maintainability.
- It reduces code readability by introducing ambiguity.
Function overloading in C++ allows you to define multiple functions with the same name but different parameter types, making the code more readable and expressive. It enables you to use intuitive function names for various versions of a task.
When dealing with large datasets, what is the advantage of using 'long long int' over 'int'?
- Better precision
- Higher range
- No advantage
- Smaller memory usage
When dealing with large datasets, using 'long long int' provides a higher range of values that can be represented, which is useful for large data. It comes at the cost of increased memory usage.
When using pointers to structures, the ________ operator is used to access the members of the structure.
- Arrow (->)
- Asterisk (*)
- Comma (,)
- Period (.)
Pointers to structures are used with the arrow operator (->) to access structure members. The arrow operator is used to dereference the pointer and access the member directly.
How does C++ resolve calls to overloaded functions?
- Overloaded function resolution is based on the function name
- Overloaded function resolution is based on the number and types of parameters
- Overloaded function resolution is based on the return type
- Overloaded function resolution is random
In C++, when there are multiple overloaded functions with the same name, the compiler resolves the calls based on the number and types of parameters provided in the function call. This process is known as function overloading, and it allows C++ to choose the most appropriate function to call based on the arguments provided.
When working with binary files in C, what is the significance of the 'b' character in the mode string?
- It denotes the file is binary, and data is stored as a series of 0s and 1s.
- It indicates the file is a backup copy.
- It represents the file is a text file.
- It specifies the file is in compressed format.
In C, the 'b' character in the mode string indicates that the file is opened in binary mode. In this mode, data is read or written in its raw binary form, without any character encoding or translation. This is essential when working with binary files like images or executable files.
What function is used in C to allocate a block of memory for an array of elements, initialize them to zero, and then return a pointer to the memory?
- alloc
- calloc
- malloc
- realloc
The calloc function in C is used to allocate a block of memory for an array of elements, initializes them to zero, and returns a pointer to the memory. It is especially useful for dynamic memory allocation.