MCQ

C++ MCQ Questions and Answers – Part 8

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;
int main()
{
   void x = 5, y = 5;
   int s;
   s = x + y;
   cout << s;
   return 0;
}

A 10

B Compilation error

C Runtime error

D None of the above

B
void won’t accept any value.
 

 

 

2. Identify the wrong option.

A Enumerations are constants.

B Enums are user-defined types.

C Enumerations are the same as macros.

D The enumeration values start at 0 by default.
 

 
C
Enumerations are used to create our own types while macros are textual substitutions.

Example of macros in C++ :

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

 
Example of enums in C++ :

enum season
{  
    spring = 0, 
    summer = 6, 
    autumn = 9,
    winter = 11
};

 

 

3. Select the right option.
extern int x;
int i;

A Line 1 and 2 declare x

B Line 1 declares the variable x and line 2 defines i

C Line 1 declares and defines x, line 2 declares i

D Line 1 declares x, line 2 declares and defines i

D
The keyword “extern” is not a definition and memory is not allocated to it before its initialization.

 

 

4. What is the output of the following C++ code?
#include <stdio.h>
int main()
{
	char x = '\012';
	 
	printf("%d", x);
	return 0;
}

A 10

B 12

C 0

D Compilation error

A
The value “\012” represented in octal, which is in decimal = 10.

 

 

5. Constants are also called ______

A Const

B Preprocessor

C Literals

D None of the above

C
Constants are literals.

 

 
 

6. What is the output of the following C++ code?
#include <iostream>
using namespace std;
int f = 100;
int main()
{
	int a;
	{
		int b;
		b = 10;
		a = 25;
		f = 35;
		cout << b << a << f;
	}
	a = 40;
	cout << a << f;
	return 0;
}

A 10253540100

B 1025354035

C 1040253535

D 1025352535

B
The local values of ‘a’ and ‘f’ in the block are more dominant than the global values.
 

 

 

7. How are the constants declared?

A With the keyword const

B With the pre-processor #define.

C With the keyword const and with the pre-processor #define.

D None of the above

C
The keyword “const” will declare constants with a specific type value and “#define” is used to declare user defined constants. Example:

const int a = 5;
#define PI 3.14159

 

 

8. What is the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
   int  const  c = 9;
   cout << ++c;
   return 0;
}

A 9

B 10

C Error

D None of the above

C
We cannot change a constant value.

 

 
 

9. What is the output of the following C++ code?
#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{
    int x = 5, y = 10, z = 15;
    int arr[3] = {&x, &y, &z};
    cout << *arr[*arr[1] - 7];
    return 0;
}

A 10

B 15

C 4

D Compilation error

D
The conversion is not valid in array. So, there will be an error. The following compilation error will be generated:
 

 
This is because &x, &y and &z represent int* while the defined array is of type int.

 

 

10. Can two functions declare (non-static) variables with the same name.

A No.

B Yes.

C Yes, but this is not a good practice.

D No, it gives a runtime error.

C
We can declare variables with the same name in two functions because their scope is limited by the function. Example:

void f1() {
   // declaration of local variable in f1
   int var;
}

void f2() {
   // declaration of local variable in f2
   int var;
}

 

 
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 *