When declaring a string literal in C, which character is automatically appended at the end?
- Exclamation mark '!'
- Null terminator ' '
- Question mark '?'
- Semicolon ';'
In C, a null terminator ' ' is automatically appended at the end of a string literal. This null character indicates the end of the string, allowing C functions to determine the string's length.
What would be the result of trying to access an array with a pointer that has not been initialized?
- It will access a random memory location, leading to undefined behavior.
- The pointer will point to the beginning of the array.
- The program will compile but throw a runtime error.
- The program will not compile.
Accessing an array with an uninitialized pointer in C leads to undefined behavior because the pointer doesn't point to a valid memory location. The result is unpredictable and can lead to crashes or incorrect data manipulation.
The ________ directive is used to define symbolic names or constants in a C program.
- #constant
- #define
- #enum
- #import
The correct answer is (b) #define. In C, the #define preprocessor directive is used to define symbolic names or constants. These names can simplify your code and make it more readable.
How does the execution stack change when a recursive function is called in C?
- A new thread is created
- Each function call adds a new stack frame
- The stack remains unchanged
- The stack shrinks
In C, when a recursive function is called, each function call adds a new stack frame to the execution stack. These stack frames store the local variables and return addresses for each function call. This process continues until the base case is reached.
In a graphics program, you need to define colors where each color can be represented as either a name or an RGB triplet. How would you efficiently represent this in C?
- Using a function
- Using a structure
- Using a union
- Using an enum
In C, a union would efficiently represent colors that can be either a name or an RGB triplet because it allows sharing the same memory location for different data types. Enums, structures, and functions are not suitable for this purpose.
To check for errors during file operations, you can use the ______ function.
- fileStatus()
- checkFileError()
- feof()
- ferror()
The correct option is d) ferror(). This function is used to check for errors during file operations and returns a non-zero value if an error is encountered.
What is the primary purpose of using typedef in C programming?
- Creating a new data type
- Declaring global variables
- Defining variables within a structure using bit fields
- Managing memory allocation
Typedef in C is used for creating aliases or alternative names for existing data types. It simplifies code, improves code readability, and enhances portability by allowing you to use more meaningful names for data types.
For efficient memory utilization in dynamic arrays, it is important to release the allocated memory using the function _________.
- free()
- malloc()
- realloc()
- calloc()
The correct option is 'free()'. It is essential to use the free() function to release dynamically allocated memory to avoid memory leaks.
Command line arguments are accessed in a C program using the parameters ________ and ________ in the main function.
- argc, argv
- input, output
- param1, param2
- source, target
The correct answer is (a) argc, argv. In C, the main function can take two parameters: argc, which represents the number of command-line arguments, and argv, which is an array of strings containing those arguments.
What is the main difference between function declaration and function definition in C?
- Declarations contain the function's code
- Declarations specify the function's name
- Definitions declare the function's return type
- Definitions provide the function's implementation
The main difference between function declaration and function definition in C is that declarations specify the function's name and data types, while definitions provide the actual implementation of the function.