C++ Programming Questions and Answers – Unspecified Number of Arguments

C++ Programming Multiple Choice Questions focuses on “Unspecified Number of Arguments”. One shall practice these questions to improve their C++ programming skills needed for various interviews exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions comes with detailed explanation of the answers which helps in better understanding of C++ concepts.

C++ Programming Questions & Answers focuses on “Unspecified Number of Arguments” along with answers, explanations and/or solutions:

Q 1. Which header file is used to pass unknown number of arguments to function?
A. stdarg.h
B. string.h
C. stdlib.h
D. stdio.h

Show Answer Answer:-A. stdarg.h
Explanation Because the cstdarg defines this header file to process the unknown number of arguments.

Q 2. How can you access the arguments that are manipulated in the function?
A. arg_list
B. va_list
C. both va_list & arg_list
D. vg_list

Show Answer Answer:-B. va_list
Explanation va_list is provided by C++ to access manipulated arguments in function.

Q 3. What will be the output of the following C++ code?

  1. #include <iostream>
  2. #include <stdarg.h>
  3. using namespace std;
  4. float avg( int Count, … )
  5. {
  6. va_list Numbers;
  7. va_start(Numbers, Count);
  8. int Sum = 0;
  9. for (int i = 0; i < Count; ++i )
  10. Sum += va_arg(Numbers, int);
  11. va_end(Numbers);
  12. return (Sum/Count);
  13. }
  14. int main()
  15. {
  16. float Average = avg(10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  17. cout << “Average of first 10 whole numbers : ” << Average;
  18. return 0;
  19. }

A. 4
B. 5
C. 6
D. 7

Show Answer Answer:-A. 4
Explanation We are just calculating the average of these numbers using cstdarg. Output: $ g++ uka.cpp $ a.out Average of first 10 whole numbers 4

Q 4. What is the maximum number of arguments or parameters that can be present in one function call?
A. 64
B. 254
C. 255
D. 256

Show Answer Answer:-D. 256
Explanation C++ allows maximum number of 256 arguments in a function call.

Q 5. What will be the output of the following C++ code?

  1. #include <iostream>
  2. #include <stdarg.h>
  3. using namespace std;
  4. int add (int num, …)
  5. {
  6. int sum = 0;
  7. va_list args;
  8. va_start (args,num);
  9. for (int i = 0; i < num; i++)
  10. {
  11. int num = va_arg (args,int);
  12. sum += num;
  13. }
  14. va_end (args);
  15. return sum;
  16. }
  17. int main (void)
  18. {
  19. int total = add(8, 1, 2, -1, 4, 12, -2, 9, 7);
  20. cout << “The result is ” << total;
  21. return 0;
  22. }

A. 23
B. 32
C. 48
D. compile time error

Show Answer Answer:-B. 32
Explanation We are adding these numbers by using for statement and stdarg. Output: $ g++ uka.cpp $ a.out The result is 32

Q 6. What will be the output of the following C++ code?

  1. #include <iostream>
  2. #include <stdarg.h>
  3. using namespace std;
  4. void dumplist(int, …);
  5. int main()
  6. {
  7. dumplist(2, 4, 8);
  8. dumplist(3, 6, 9, 7);
  9. return 0;
  10. }
  11. void dumplist(int n, …)
  12. {
  13. va_list p;
  14. int i;
  15. va_start(p, n);
  16. while (n–>0)
  17. {
  18. i = va_arg(p, int);
  19. cout << i;
  20. }
  21. va_end(p);
  22. }

A. 2436
B. 48697
C. 1111111
D. compile time error

Show Answer Answer:-B. 48697
Explanation In this program, we are eradicating the first value by comparing using while operator. Output: $ g++ rka3.cpp $ a.out 48697

Q 7. What will be the output of the following C++ code?

  1. #include <iostream>
  2. #include <stdarg.h>
  3. using namespace std;
  4. int flue(char c,…);
  5. int main()
  6. {
  7. int x, y;
  8. x = flue(‘A’, 1, 2, 3);
  9. y = flue(‘1’, 1.0,1, ‘1’, 1.0f, 1l);
  10. cout << x << y;
  11. return 0;
  12. }
  13. int flue(char c,…)
  14. {
  15. return c;
  16. }

A. 4965
B. 6549
C. 6646
D. compile time error

Show Answer Answer:-B. 6549
Explanation In this program, we are returning the ascii value of the character and printing it. Output: $ g++ rka4.cpp $ a.out 6549

Q 8. Which of the header file should be added in the following C++ code to properly run the program?

  1. #include <iostream>
  2. using namespace std;
  3. int print_all (int num, …)
  4. {
  5. int sum = 0;
  6. va_list args;
  7. va_start (args,num);
  8. for (int i = 0; i < num; i++)
  9. {
  10. int num = va_arg (args,int);
  11. cout<<num<<” “;
  12. }
  13. va_end (args);
  14. return sum;
  15. }
  16. int main (void)
  17. {
  18. print_all(8, 1, 2, -1, 4, 12, -2, 9, 7);
  19. return 0;
  20. }

A. stdlib.h
B. stdarg.h
C. string.h
D. stdpar.h

Show Answer Answer:-B. stdarg.h
Explanation header provided to perform variable number of argument passing.

Q 9. What will be the output of the following C++ code?

  1. #include <iostream>
  2. #include <stdarg.h>
  3. using namespace std;
  4. void fun(std::string msg, …);
  5. int main()
  6. {
  7. fun(“IndiaBIX”, 1, 4, 7, 11, 0);
  8. return 0;
  9. }
  10. void fun(std::string msg, …)
  11. {
  12. va_list ptr;
  13. int num;
  14. va_start(ptr, msg);
  15. num = va_arg(ptr, int);
  16. num = va_arg(ptr, int);
  17. cout << num;
  18. }

A. 4
B. 5
C. 6
D. 8

Show Answer Answer:-A. 4
Explanation In this program, we are moving the pointer to the second value and printing it. Output: $ g++ uka6.cpp $ a.out 4

Q 10. What will initialize the list of arguments in stdarg.h header file?
A. va_list
B. va_start
C. va_arg
D. vg_arg

Show Answer Answer:-B. va_start
Explanation va_start initialises the the list of arguments in header file.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page