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.

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.

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.

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.

When defining a bit field, what does the number after the colon represent?

  • Bit order
  • Bit position
  • Bit size
  • Bit value
The number after the colon represents the bit size, indicating how many bits the bit field can occupy.

A two-dimensional array can be visualized as a ________.

  • Linked List
  • One-dimensional array
  • Table
  • Tree Structure
A two-dimensional array can be visualized as a table with rows and columns.

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.

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.

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.