πŸ“Š C++
Q. What will initialize the list of arguments in stdarg.h header file?
  • (A) va_start
  • (B) va_arg
  • (C) va_list
  • (D) All of above
πŸ’¬ Discuss
βœ… Correct Answer: (A) va_start

Explanation:

In the C standard library <stdarg.h>, used for handling variable argument lists, the roles of each macro/type are as follows:

va_list:
This is a type used to declare a variable that will hold the information needed to retrieve the additional arguments.

va_start(va_list, last_fixed_param):
This initializes the va_list variable so it can be used to retrieve the variable arguments. This is the correct answer to the question.

va_arg(va_list, type):
This retrieves the next argument in the list.

So only va_start is responsible for initializing the list of arguments.

βœ… Correct Answer: (A) va_start

πŸ“Š C++
Q. What is the output of this program?
Code:
#include 
    #include 
    using namespace std;
    void function(std::string message, ...);
    int main()
    {
        function("UdayMCQBuddy", 5, 7, 10, 8, 9);
        return 0;
    }
    void function(std::string message, ...)
    {
        va_list ptr;
        int number;
        va_start(ptr, message);
        number = va_arg(ptr, int);
        number = va_arg(ptr, int);
        cout << number;
    }
  • (A) UdayMCQBuddy
  • (B) 5
  • (C) 7
  • (D) 8
πŸ’¬ Discuss
βœ… Correct Answer: (C) 7
πŸ“Š C++
Q. Which header file should you include if you are to develop a function that can accept variable number of arguments?
  • (A) stdio.h
  • (B) varag.h
  • (C) stdarg.h
  • (D) stdlib.h
πŸ’¬ Discuss
βœ… Correct Answer: (C) stdarg.h
πŸ“Š C++
Q. What is the output of this program?
Code:
#include 
    #include 
    using namespace std;
    int funcion(char ch,...);
    int main()
    {
        int p, q;
        p = funcion('B', 2, 3, 4);
        q = funcion('3', 2.0, 2, '3', 2.0f, 10);
        cout << p<<" " << q;
        return 0;
    }
    int funcion(char ch,...)
    {
        return ch;
    }
  • (A) 51 66
  • (B) 66 51
  • (C) Compilation Error
  • (D) Runtime Error
πŸ’¬ Discuss
βœ… Correct Answer: (B) 66 51
πŸ“Š C++
Q. What is the output of this program?
Code:
#include 
    #include 
    using namespace std;
    void List(int, ...);
    int main()
    {
        List(2, 5, 10);
        List(3, 6, 9, 7);
        return 0;
    }
    void List(int num, ...)
    {
        va_list a;
        int k;
        va_start(a, num);
        while (num-->0) 
        {
            k = va_arg(a, int);
            cout << k;
        }
        va_end(a);
    }
  • (A) 2 5 10 3 6 9 7
  • (B) 2 3 5 6 7 9 10
  • (C) 10 9 7 6 5 3 2
  • (D) 5 10 6 9 7
πŸ’¬ Discuss
βœ… Correct Answer: (D) 5 10 6 9 7
πŸ“Š C++
Q. What is the output of this program?
Code:
#include 
    #include 
    using namespace std;
    int Addition(int number, ...)
    {
        int sum = 0;
        va_list args;
        va_start (args,number);
        for (int i = 0; i < number; i++) 
        {
            int number = va_arg (args,int);
            sum += number;
        }
        va_end (args);
        return sum;
    }
    int main (void)
    {
        int Result = Addition(1, 10, 9, -1, 13, 23, -2, 9, 17);
        cout << "The result is " << Result;
        return 0;
    }
  • (A) 1
  • (B) 5
  • (C) 10
  • (D) 20
πŸ’¬ Discuss
βœ… Correct Answer: (C) 10
πŸ“Š C++
Q. What is the maximum number of arguments or parameters that can be present in one function call?
  • (A) 16
  • (B) 256
  • (C) 255
  • (D) 64
πŸ’¬ Discuss
βœ… Correct Answer: (B) 256
πŸ“Š C++
Q. What is the output of this program?
Code:
#include 
    #include 
    using namespace std;
    float avg( int Count, ... )
    {
        va_list num;
        va_start(num, Count);
        int Sum = 0;
        for (int i = 0; i < Count; ++i )
            Sum += va_arg(num, int);
        va_end(num);
        return (Sum/Count);
    }
    int main()
    {
        float AVG = avg(5, 0, 1, 2, 3, 4);
        cout << "Average of first 5 whole numbers : " << AVG;
        return 0;
    }
  • (A) 2
  • (B) 3
  • (C) 4
  • (D) 5
πŸ’¬ Discuss
βœ… Correct Answer: (A) 2
πŸ“Š C++
Q. How can you access the arguments that are manipulated in the function?
  • (A) arg_list
  • (B) va_list
  • (C) both arg_list & va_list
  • (D) All of above
πŸ’¬ Discuss
βœ… Correct Answer: (B) va_list
πŸ“Š C++
Q. Which header file is used to pass unknown number of arguments to function?
  • (A) stdarg.h
  • (B) stdlib.h
  • (C) string.h
  • (D) All of above
πŸ’¬ Discuss
βœ… Correct Answer: (A) stdarg.h