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.

The memory consumed by an array declared as float arr[10][20]; is ________.

  • 2000 bytes
  • 400 bytes
  • 80 bytes
  • 800 bytes
The memory consumed by a two-dimensional array is calculated by multiplying the number of rows by the number of columns and then multiplying it by the size of the data type. In this case, it's 10 (rows) * 20 (columns) * 4 bytes (float) = 800 bytes.

In C, subtracting two pointers that point to elements of the same array yields a result of type ________.

  • double
  • int
  • ptrdiff_t
  • size_t
In C, subtracting two pointers that point to elements of the same array yields a result of type ptrdiff_t. This type represents the signed integral difference between the two pointers. It's important to use ptrdiff_t when performing pointer subtraction to ensure portability and accurate results, especially in situations involving pointer arithmetic.

What is the purpose of the 'switch' statement in C?

  • To perform iterative loops
  • To handle exceptions
  • To provide multiple choices
  • To enhance code readability
The 'switch' statement in C is used to provide multiple choices based on the value of an expression. It allows for a concise way to write code when there are multiple possible execution paths. Each 'case' represents a different possible value of the expression, improving code readability and maintainability, especially when dealing with menu-driven programs or handling different options.

The ________ loop guarantees at least one execution of the loop body, even if the condition is false from the start.

  • do-while
  • for
  • switch
  • while
The 'do-while' loop in programming ensures that the loop body is executed at least once, even if the condition is false initially. It's used when you want to run the loop body before checking the condition.

You are tasked with creating a system to manage a library's book inventory. How can structures be utilized in this scenario?

  • To define the layout of the library building
  • To manage software licenses
  • To organize and store information about each book
  • To represent information about library patrons
In this scenario, structures can be used to organize and store information about each book in the library. Each structure can hold data like the book's title, author, publication date, and available copies, making it an efficient way to manage the library's inventory.

When reading a text file in C, which function can be used to read data from the file?

  • fgets
  • fputs
  • fread
  • fwrite
When reading a text file in C, the fgets function is commonly used to read data from the file. It reads one line of text at a time, making it suitable for text file processing.

You're writing a C program that needs to read from a file and display the content on the screen. How would you handle the situation if the file does not exist?

  • Create an empty file
  • Ignore and continue
  • Print an error message and exit
  • Prompt user for a new file name
Printing an error message and exiting the program is a common approach when a required file is not found. It informs the user about the issue and halts the program execution to prevent further errors.

In what scenario would using an array of structures be more beneficial than using multiple arrays?

  • When data access patterns are unpredictable
  • When dealing with unrelated data
  • When each element has multiple attributes
  • When memory efficiency is not a concern
Using an array of structures is advantageous when dealing with related data that share the same attributes or fields, making it more organized and efficient compared to multiple arrays.

In C++, what happens when two overloaded functions have the same number and types of parameters but differ in return type?

  • It's a compilation error
  • It's undefined behavior
  • The function with the least specific return type is called
  • The function with the most specific return type is called
When two overloaded functions in C++ have the same number and types of parameters but differ in return type, it results in a compilation error. C++ uses the function's signature, including the return type, to differentiate between overloaded functions. If two functions have the same parameters and differ only in return type, it becomes ambiguous, and the compiler cannot determine which function to call.

What happens when realloc is called with a size parameter of zero?

  • It frees the previously allocated memory.
  • It leads to a memory leak.
  • It reallocates the memory with a size of zero.
  • It returns a null pointer.
When realloc is called with a size parameter of zero, it frees the previously allocated memory and returns a pointer to the freed memory, effectively deallocating it.

What is the function of the #define preprocessor directive in a C program?

  • Controls the execution flow of the program
  • Declares a variable within the program
  • Defines a constant value for use in the program
  • Includes a header file in the program
The #define directive is used to define constants. It allows you to specify a constant value that is used throughout the program, making it easier to maintain and modify the code.