C-Variable Numbers and Arguments Questions and Answers

Practice Mode
Showing 10 of 28 questions
Q11
In variable argument functions, how does the function know how many arguments were passed?
  • A It automatically knows from the function signature
  • B Through a separate count parameter or sentinel value
  • C By checking the stack size
  • D The compiler provides this information
Answer: Option B
Explanation: The function must have a mechanism (like a count parameter or sentinel value) to determine the number of arguments.
Q12
Which standard library function commonly uses variable arguments?
  • A strlen
  • B malloc
  • C printf
  • D scanf
Answer: Option C
Explanation: printf is the most common example of a variable argument function in C standard library.
Q13
What happens if you call va_arg with the wrong type?
  • A The compiler gives an error
  • B It automatically converts the type
  • C It results in undefined behavior
  • D It returns NULL
Answer: Option C
Explanation: Using incorrect types with va_arg leads to undefined behavior and potential program crashes.
Q14
Can variable argument functions have no fixed parameters?
  • A Yes, they can have only variable arguments
  • B No, at least one fixed parameter is required
  • C Only in C++
  • D It depends on the compiler
Answer: Option B
Explanation: Variable argument functions must have at least one fixed parameter before the ellipsis.
Q15
What is the correct order of using va macros?
  • A va_start → va_arg → va_end
  • B va_arg → va_start → va_end
  • C va_end → va_start → va_arg
  • D va_start → va_end → va_arg
Answer: Option A
Explanation: The correct sequence is va_start → va_arg (multiple times) → va_end.
Q16
Which data type promotions occur in variable arguments?
  • A No promotions occur
  • B char→int, float→double
  • C int→long, double→long double
  • D All types are promoted to void*
Answer: Option B
Explanation: In variable arguments, char and short are promoted to int, float to double.
Q17
What is a common use case for variable arguments?
  • A Mathematical calculations
  • B Memory allocation
  • C Formatted output functions like printf
  • D File operations
Answer: Option C
Explanation: Variable arguments are commonly used for formatted I/O functions and functions that need flexibility in argument count.
Q18
Can you pass structures as variable arguments?
  • A No, structures are not allowed
  • B Yes, but only small structures
  • C Yes, but it is not portable and risky
  • D Only if they are passed by reference
Answer: Option C
Explanation: Structures can be passed but require careful handling due to type promotion rules and size considerations.
Q19
What is the type of the va_list in most implementations?
  • A Integer
  • B Character pointer
  • C Void pointer
  • D Implementation-defined type
Answer: Option D
Explanation: va_list is typically implemented as a pointer type that traverses the argument list.
Q20
How can you determine the type of each variable argument?
  • A Using typeof operator
  • B Through a format string or convention
  • C va_arg automatically detects types
  • D By checking the argument size
Answer: Option B
Explanation: The function must know the expected types, typically through format strings or explicit parameters.
Questions and Answers for Competitive Exams Various Entrance Test