MCQ

C++ MCQ Questions and Answers – Part 9

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. In which type are enums stored by the compiler?

A String

B Integer

C Float

D None of the above

B
In C++, enumerations are stored as integers by the compiler, starting by default with 0.

 

 
 

2. What is the output of the following C++ code?
#include <iostream>
using namespace std;
void add()
{
	static int s = 1;
	s++;
	cout << s;
}
int main()
{
	add();
	add();
	add();
	return 0;
}

A 123

B 111

C 234

D 122

C
A variable declared as static has a global scope.
 

 

 

3. What is the output of the following C++ code?
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
	char str[7] = "ABC";
	cout << str[3];
	cout << str;
	return 0;
}

A CAB

B ABC

C ABCD

D C

B
We simply display the values of the first 3 values.
 

 

 

4. To which type, enumerations can be assigned?

A Integer

B Float

C Enumeration

D All the answers are true

A, C
Since enumerations have integer values and integers can be assigned to enumerations, enumerations can be assigned to other enumerations.

 

 
 

5. Which of the following statements is not true about preprocessor directives?

A These are lines read and processed by the preprocessor

B They do not produce code by themselves

C These must be written on a single line

D They end with a semi-colon

D
No semi-colon is required for preprocessor instructions. Example :

#define SITE "www.stackhowto.com"
#define YEAR 2020

 

 

6. What is the output of the following C++ code?
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
	int arr[] = {100, 200, 300};
	cout << -2[arr];
	return 0;
}

A 300

B -300

C Compilation error

D Runtime error

D
This displays the negative value of the element concerned.
 

 

 

7. Which of the following statements is true about the following instruction?
const int b = 100;

A Declares a variable b with 100 as initial value.

B Declare an integer b with 100 as initial value.

C Declares a constant b whose value will be 100.

D Creates a variable of type integer with the identifier b and 100 as value.

C
Because the keyword “const” is used to declare only non-modifiable values.

 

 

8. Which variable is equal to the size of enum variable?

A string var;

B float var;

C int var;

D None of the above

C
The enum variable is converted to an integer and stored by the compiler. So both are equal in size.

 

 
 

9. What is the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
	int x = 10;
	if (x < 10) {
		for (i = 0; i < 10; i++)
			cout << i;
	}
	else {
		cout << i;
	}
	return 0;
}

A 12345678910

B 123456789

C 012345678

D Compilation error

D
We will have a compilation error because ‘i’ is an undeclared identifier.
 

 

 

10. The elements in a structure are also called _______

A objets

B members

C data

D None of the above

B
Variables declared in a structure are also called data members.

 

 
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 *