MCQ

C++ MCQ Questions and Answers – Part 13

Multiple choice questions and answers (MCQs) on C++ to prepare for exams, tests, and certifications. These questions are taken from a real written exam and some parts are taken from an interview. So you will find questions on basic techniques such as Variables, Operators, Conditional Statement, Functions, and more. This quiz will easily prepare anyone to pass their online test.
 

1. Which of the following is the default return value of a function in C++?

A int

B char

C float

D void

A
C++ uses “int” as default return values for functions.

 

 
 

2. What is the output of the following C++ code?
#include <iostream>
using namespace std;
void f(int x, int y)
{
	x = 10;
	y = 5;
}
int main()
{
	int x = 5;
	f(x, x);
	cout << x;
	return 0;
}

A 5

B 10

C Compilation error.

D None of the above

A
In this program, we have called the function f by value so that the value is not changed. The output is therefore 5.
 

 

 

3. What happens to a function defined in a class without any complex operation (like loop, etc.)?

A It becomes a virtual function of a class

B This becomes a default function of a class.

C This becomes an inline function of a class.

D The program displays an error

C
Any function defined in a class with no complex operations, it is then created inline.

 

 

4. What is an inline function?

A function to increase the execution time of a program.

B A function called during compilation

C A function that is not checked for syntax errors

D A function whose semantic analysis is not verified

A
Inline function in C++ to increase the execution time of a program, is a powerful concept commonly used with classes. If a function is inline, the compiler places a copy of the function’s code at each point where the function is called at compile time.

 

 

5. How many minimum functions must be present in a C++ program to run it?

A 0

B 1

C 2

D 3

B
The execution of a C++ program starts from the main() function therefore it is necessary to have at least one main() function in a C++ program.

 

 
 

6. What is the output of the following C++ code?
#include<iostream>
using namespace std;
 
int fun(int a = 0, int b = 0, int c)
{  return (a + b + c); }
 
int main()
{
   cout << fun(2);
   return 0;
}

A 2

B 0

C Compilation error

D None of the above

C
Default arguments should always be declared on the right side of the parameter list, but the above function has a variable on the right side that has no value, which produces a syntax error.
 

 

 

7. What is the output of the following C++ code?
#include <iostream>
using namespace std;
 
int f(int=0, int = 0);
 
int main()
{
  cout << f(2);
  return 0;
}
int f(int a, int b) { return (a+b); }

A 2

B 0

C Compilation error

D None of the above

A
C++ allows to define such a function prototype in which you are not obliged to give the variable names, you specify only the default values. In the function definition, you can provide the variable names corresponding to each parameter.
 

 

 

8. Which keyword do you use if you do not want to get a return value?

A static

B const

C void

D volatile

C
If the function does not return a value, void type is used.

 

 
 

9. From which function does the execution of a C++ program start?

A Start() function

B new() function

C main() function

D All the answers are true

C
The execution of a C++ program starts from the “main()” function.

 

 

10. What is the value of x?
#include <iostream>
using namespace std;
void f(int &x)
{
	x = 3;
}
int main()
{
	int x = 2;
	f(x);
	cout << "The value of x is " << x;
	return 0;
}

A 2

B 3

C Compilation error

D None of the above

B
When the parameter is passed by reference, its original value will be modified.

 

 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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