C++ Default Arguments Aptitude Questions and Answers

C++ Default Arguments Aptitude: This section contains C++ Default Arguments Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 19, 2021

1) There are the following statements that are given which of them are correct about default arguments in C++?

  1. The default arguments are used in C++ functions to provide optional arguments.
  2. If we did not pass the value to the function then default values of arguments used inside the function, if argument declared as a default argument.
  3. We can create any argument in the function as a default argument.
  4. Default arguments sometimes reduce the creation of multiple functions using function overloading.

Options:

  1. A and B
  2. A and C
  3. A, B, and D
  4. A, B, C, and D

2) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

void fun(int a, int b = 10, int c = 20)
{
    cout << a << " " << b << " " << c << endl;
}

int main()
{
    fun(5);
    fun(5, 12);
    fun(5, 12, 17);

    return 0;
}

Options:

  1. 5 
    5 12
    5 12 17
    
  2. 5 10 20
    5 12 20
    5 12 17
    
  3. Compile-time error
  4. Runtime error

3) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

void fun(int a = 10, int b)
{
    cout << a << " " << b << endl;
}

int main()
{
    fun(5);

    return 0;
}

Options:

  1. 5
  2. 5 10
  3. Compile-time error
  4. Runtime error

4) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

void fun(int a, int* b = 100)
{
    cout << a << " " << b << endl;
}

int main()
{
    fun(5);

    return 0;
}

Options:

  1. 5
  2. 5 100
  3. Compile-time error
  4. Runtime error

5) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

void fun(int a, int* b = &a)
{
    cout << a << " " << *b << endl;
}

int main()
{
    fun(5);

    return 0;
}

Options:

  1. 5
  2. 5 5
  3. Compile-time error
  4. Runtime error

6) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

int c = 10;
void fun(int a, int* b = &c)
{
    cout << a << " " << *b << endl;
}

int main()
{
    fun(5);

    return 0;
}

Options:

  1. 5
  2. 5 10
  3. Compile-time error
  4. Runtime error

7) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

void fun(int X, long Y)
{
    cout << "##: " << X << " " << Y << endl;
}

void fun(int a, int b = 10)
{
    cout << "$$: " << a << " " << b << endl;
}

int main()
{
    fun(5, 20);

    return 0;
}

Options:

  1. $$: 5 20
  2. ##: 5 20
  3. Compile-time error
  4. Runtime time

8) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

void fun(int X, long Y)
{
    cout << "##: " << X << " " << Y << endl;
}

void fun(int a, int b = 10)
{
    cout << "$$: " << a << " " << b << endl;
}

int main()
{
    fun(5, 20L);

    return 0;
}

Options:

  1. $$: 5 20
  2. ##: 5 20
  3. Compile-time error
  4. Runtime error

9) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

void fun(int X, int Y)
{
    cout << "##: " << X << " " << Y << endl;
}

void fun(int a = 8, int b = 10)
{
    cout << "$$: " << a << " " << b << endl;
}

int main()
{
    fun('A', 20);

    return 0;
}

Options:

  1. $$: A 20
  2. $$: 65 20
  3. ##: 65 20
  4. Compile-time error

10) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

int k = 30;

void fun(int a, int& b = k)
{
    cout << a << " " << b << endl;
}

int main()
{
    fun(8);

    return 0;
}

Options:

  1. 8 30
  2. 8
  3. Compile-time error
  4. Garbage value





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.