What is the advantage of using function pointers in C for implementing callback functions?

  • Allows dynamic callback selection
  • Enhances code modularity
  • Reduces code redundancy
  • Simplifies debugging
Function Pointers allow dynamic selection of callback functions in C, which is useful in scenarios where you need to change callbacks at runtime without modifying the main code. It reduces code redundancy and enhances code modularity, but it might make debugging more challenging due to indirection.

What is the effect of not specifying the size of the array when initializing it with a list of values?

  • Compilation error
  • The array is initialized with a default size of 10
  • The array size is automatically set based on the number of values
  • Undefined behavior
When initializing an array with a list of values in C without specifying the size, the array size is automatically set based on the number of values. It is a convenient feature in C known as array initialization without a size specification.

The function ________ is used to read formatted input from the standard input.

  • print
  • scanf
  • printf
  • cout
The "scanf" function is used in C and C++ to read formatted input from the standard input (usually the keyboard). It allows you to input and store values in variables while specifying the format in which data should be entered. Options like "print," "printf," and "cout" are used for output, not input.

String literals in C are stored in ________ memory segment.

  • Code
  • Data
  • Heap
  • Stack
In C, string literals are stored in the data memory segment. This segment contains constants, such as global and static variables.

You're developing a program to calculate the area of a circle. What data type would be most suitable to store the radius, considering it can be a fractional value?

  • char
  • double
  • float
  • int
To store the radius of a circle with fractional values, the most suitable data type in C is 'double' as it offers high precision for such calculations.

What is the purpose of using random access in file handling?

  • To access files in a random order.
  • To create files with random names.
  • To generate random numbers for file operations.
  • To perform direct read/write operations at any position within the file.
The purpose of using random access in file handling is to perform direct read and write operations at any position within the file. Random access allows you to jump to a specific location in a file and read or write data without having to sequentially process the entire file. It's especially useful for large files or databases.

In a scientific computation program, you need to represent a matrix of real numbers. What kind of array would be suitable for this purpose?

  • 1D Array
  • 2D Array
  • Binary Tree
  • Dynamic Array (ArrayList)
A 2D array is suitable for representing a matrix of real numbers as it offers a grid-like structure to store rows and columns of values.

In C, what is the result of the expression (5 & 3)?

  • 0
  • 1
  • 3
  • 5
In C, the '&' operator is the bitwise AND operator. When you perform (5 & 3), it bitwise ANDs the binary representation of 5 (0101) and 3 (0011). The result is 0001, which is 1 in decimal.

What is the behavior of malloc when the size requested is zero?

  • It goes into an infinite loop
  • It returns a null pointer (NULL)
  • It returns a pointer to a zero-sized memory block
  • It returns an error
When you request zero bytes of memory using malloc, it returns a null pointer (NULL) rather than allocating a zero-sized memory block. This is useful for special cases when you need a pointer that doesn't point to any memory.

How can double pointers be used in dynamic memory allocation in C?

  • To allocate multi-dimensional arrays
  • To create pointer arrays
  • To manage pointer arithmetic
  • To pass a pointer to a pointer
Double pointers are used in dynamic memory allocation when you need to pass a pointer to a pointer (to allocate and manage multi-dimensional arrays) or create arrays of pointers. They provide an extra level of indirection, which is beneficial for managing memory efficiently.