When a file is opened in 'w' mode using fopen, if the file already exists, its contents are ________.

  • Overwritten
  • Appended
  • Preserved
  • Erased
In 'w' mode, if the file already exists, its contents are overwritten, so option a) "Overwritten" is the correct answer.

In C, what happens if you attempt to open a file for reading using fopen() but the file does not exist?

  • fopen() returns NULL, indicating that the file does not exist.
  • fopen() raises an exception.
  • fopen() creates a new file with the specified name.
  • fopen() prompts the user to enter the correct file name.
The correct option is "fopen() returns NULL, indicating that the file does not exist." If the file does not exist or cannot be opened for some reason, fopen() returns a NULL pointer, indicating an error in opening the file.

What function is used to open a binary file for both reading and writing in C?

  • fopen()
  • open()
  • openfile()
  • readfile()
In C, the fopen() function is used to open a binary file for both reading and writing. It returns a file pointer, which is essential for performing file operations.

Pointers can lead to ________ if not managed correctly in a C program.

  • Compilation errors
  • Memory leaks
  • Segmentation faults
  • Stack overflows
Pointers in C can cause segmentation faults when not handled properly. A segmentation fault occurs when a program tries to access memory that it's not allowed to access.

Function pointers in C can be passed as arguments to functions, thereby providing a degree of ________.

  • Abstraction
  • Encapsulation
  • Flexibility
  • Polymorphism
In C, function pointers enable polymorphism by allowing you to call different functions dynamically. This provides flexibility in your code.

What term is used to describe the process of calling a function within itself?

  • Casting
  • Looping
  • Recursion
  • Structuring
The process of calling a function within itself is known as "recursion." It allows for solving problems that can be broken down into smaller, similar subproblems.

The strlen function returns the length of a string, excluding the ________.

  • null terminator
  • first character
  • whitespace characters
  • special characters
The correct answer is the null terminator. The strlen function in C/C++ returns the length of a string by counting characters until it encounters the null terminator (''). It excludes the null terminator from the count. The other options are not what strlen excludes.

The C function ________ is used to compare two strings lexicographically.

  • strcmp
  • strncmp
  • strcompare
  • strcasecmp
The correct answer is strcmp. This function compares two strings character by character and returns an integer value representing their lexicographical order. strncmp is used for comparing a specific number of characters, and the other options are not valid C functions.

You are debugging a C program and notice that the program crashes after running for a few minutes. Upon investigation, you find that a recursive function is being called multiple times. What could be the most likely cause of the crash?

  • Hardware Failure
  • Infinite Loop
  • Memory Leak
  • Stack Overflow
The most likely cause of the crash in this scenario is a stack overflow. When a recursive function is called multiple times without a proper base case or termination condition, it can fill up the call stack, causing a stack overflow and program crash.

What is the significance of using pointers in function arguments?

  • It allows passing arguments by reference
  • It enforces strong typing
  • It reduces code readability
  • It simplifies function declarations
Using pointers in function arguments allows passing arguments by reference, meaning the function can modify the original data, not just a copy. This is valuable when you want a function to modify variables from the caller's scope.