MCQ

C++ MCQ Questions and Answers – Part 12

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. What is the output of the following C++ code?
#include <iostream>
using namespace std;
void carree(int *x)
{
	*x = (*x + 1) * (*x);
}
int main()
{
	int n = 10;
	carree(&n);
	cout << n;
	return 0;
}

A 100

B 110

C 101

D None of the above

B
We have incremented the x value, so it returns 110.

 

 

2. How many types of loops are there in C++?

A 1

B 2

C 3

D 4

C
There are three types of loops in C++: while, do while and for loop.

 

 
 

3. What is the output of the following C++ code?
#include <iostream>
using namespace std;
int sum(int x, int y);
int main()
{
	int i = 1, j = 2;
	cout << sum(i, j) << endl;
	return 0;
}
int sum(int x, int y )
{
	int s = x + y;
	x = 2;
	return x + y;
}

A 3

B 4

C Compilation error

D None of the above

B
The x value has been changed to 2, so the sum is 4.
 

 

 

4. Which loop is best used when the number of iterations is known in advance?

A do while

B while

C for

D all loops require that the number of iterations be known.

C
In “for” loop you are allowed to provide the start and end conditions, which allows you to set the number of loop iterations, whereas no other loop provides this.

 

 

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

A 2 1

B 2 3

C 2 5

D 3 5
 

 
C
The value of b will be modified in the function, because the function is passed by reference, this is called “Passing a parameter by reference in C++”.
 

 

 

6. What is the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
	int arr[] = {1, 2, 3, 4, 5};
	int *ptr = (arr + 1);
	cout << *arr + 2;
	return 0;
}

A 1

B 2

C 3

D 4

C
In this program, we add the value 2 to the value of the first element of the array (1+2). So it displays 3.
 

 

 

7. What is the output of the following C++ code?
#include <iostream>
using namespace std;
main()
{
	double x = 15.564702;
	float y = 5.99;
	int z ,w;
	z = (int) x;
	w = (int) y;
	cout << z <<' '<< w;
	return 0;
}

A 15 6

B 14 6

C 15 5

D None of the above

C
When converting a floating point (decimal) number to an integer using the conversion operator (cast), the decimal part of the floating point number is removed or truncated. So the program displays 15 and 5.
 

 

 

8. Which of the following is a correctly defined structure?

A struct {int x;}

B struct myStruct {int x;}

C struct myStruct int x;

D struct myStruct {int x;};
 

 
D
The syntax of a structure is :

struct [structure_name]
{
    type field1;
   [type field2;
   [...]]
};

 

 

9. Which of the following accesses the structure variable *a?

A a.var;

B a > var;

C a->var;

D a-var;

C
The operator (->) is used to access members of the structure pointer, while the dot (.) operator is used to access normal structure variables. Example :

struct date {  
	int day;
	int month;
	int year;
};

struct date x, y, *ptr; //ptr is a pointer to a structure

ptr-­>month   // access the 'month' field of the variable ptr(pointer)

x.month   // access the 'month' field of the variable x

 

 

10. What is the output of the following C++ code?
#include <iostream>
using namespace std;
void display()
void display()
{
	cout << "Hello World!";
}
int main()
{
	display();
	return 0;
}

A Hello World!

B Hello World!Hello World!

C Compilation error.

D None of the above

C
We must use the semicolon to declare the function in line 3. This is called a function declaration. A function declaration ends with a semicolon.

 

 
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 *