C++ MCQs with answers Page - 1

Here, you will find a collection of MCQ questions on C++. Go through these questions to enhance your preparation for upcoming examinations and interviews.

To check the correct answer, simply click the View Answer button provided for each question.

Have your own questions to contribute? Click the button below to share your MCQs with others!

+ Add Question

A

Admin • 799.53K Points
Coach

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

A

Admin • 799.53K Points
Coach

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

A

Admin • 799.53K Points
Coach

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

A

Admin • 799.53K Points
Coach

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

A

Admin • 799.53K Points
Coach

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

A

Admin • 799.53K Points
Coach

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

A

Admin • 799.53K Points
Coach

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

A

Admin • 799.53K Points
Coach

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

A

Admin • 799.53K Points
Coach

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

A

Admin • 799.53K Points
Coach

Q. What is the output of this program?

Code:
#include 
    using namespace std;
    int function(int p = 12, int q)
    {
        int s;
        s = p + q;
        return s;
    }
    int main()
    {
        cout << function(15);
        return 0;
    }
  • (A) 15
  • (B) 12
  • (C) Runtime Error
  • (D) Compilation Error

Add MCQ in this Category

If you want to share an MCQ question in this category, it's a great idea! It will be helpful for many other students using this website.

Share Your MCQ