Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
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++?
- The default arguments are used in C++ functions to provide optional arguments.
- 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.
- We can create any argument in the function as a default argument.
- Default arguments sometimes reduce the creation of multiple functions using function overloading.
Options:
- A and B
- A and C
- A, B, and D
- A, B, C, and D
Correct Answer - 3
A, B, and D
Explanation:
Statements A, B, and D are correct about default arguments in C++.
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:
-
5
5 12
5 12 17
-
5 10 20
5 12 20
5 12 17
- Compile-time error
- Runtime error
Correct Answer - 2
5 10 20
5 12 20
5 12 17
Explanation:
The 2nd option is the correct output of the above code.
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:
- 5
- 5 10
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate the compile-time error because we can define only trailing argument as a default argument in the functions.
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:
- 5
- 5 100
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time error because we cannot define pointer as a default argument like this.
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:
- 5
- 5 5
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time error because we cannot define pointer as a default argument like this.
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:
- 5
- 5 10
- Compile-time error
- Runtime error
Correct Answer - 2
5 10
Explanation:
The code will print "5 10" on the console screen.
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:
- $$: 5 20
- ##: 5 20
- Compile-time error
- Runtime time
Correct Answer - 1
$$: 5 20
Explanation:
The above code will print "$$: 5 20" on the console screen.
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:
- $$: 5 20
- ##: 5 20
- Compile-time error
- Runtime error
Correct Answer - 2
##: 5 20
Explanation:
The above code will print "##: 5 20" on the console screen.
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:
- $$: A 20
- $$: 65 20
- ##: 65 20
- Compile-time error
Correct Answer - 4
Compile-time error
Explanation:
The above code will generate a syntax 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:
- 8 30
- 8
- Compile-time error
- Garbage value
Correct Answer - 1
8 30
Explanation:
The above code will print "8 30" on the console screen.