MCQ

C++ MCQ Questions and Answers – Part 14

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 max(int a, int b )
{
	return ( a > b ? a : b );
}
int main()
{
	int a = 3;
	int b = 8;
	cout << max(a, b);
	return 0;
}

A 8

B 3

C Compilation error

D None of the above

A
In this program, we return the maximum value using the ternary operator.

 

 
 

2. Choose the right statement about String objects in C++?

A String objects must be terminated by the null character (‘\0’)

B String objects have a static size

C String objects have a dynamic size

D String objects use more memory than necessary.

C
String objects are dynamic in nature, i.e. their size varies according to the value, so they do not exceed the necessary size and it is not necessary to end a String object with the null character (‘\0’).

 

 

3. If a parameter of a function is defined as constant, then ______

A It can be modified inside the function.

B It cannot be modified inside the function.

C It produces an error

D None of the above

B

 

 

4. What is the output of the following C++ code?
#include <iostream> 
#include <string>
using namespace std; 
int main(int argc, char const *argv[])
{
	char str[] = "Hello World";
	cout << str[0];
	return 0;
}

A Hello World

B H

C W

D e

B
We have str = "Hello World", in the above code we have str[0] display the first character of str which is ‘H.
 

 

 

5. Which header file is used to include String functions in C++??

A #include <string.cpp>

B #include <string.h>

C #include <string>

D #include <cstring>

C
The header file #include <string> is used because it contains all the functions of the string object.

 

 
 

6. What is the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std; 
int main(int argc, char const *argv[])
{
	char str1[20] = "Welcome to";
	char str2[20] = "StackHowTo";
	cout << str1 + " " + str2;
	return 0;
}

A Welcome to StackHowTo

B Welcome to

C StackHowTo

D Error

D
There is no defined operation to concatenate two String arrays in C++, so the compiler returns an error because it doesn’t understand what to do about this expression.
 

 

 

7. Which of the following is the correct way to concatenate two String objects in C++?
// Method 1:
string str1 = "Welcome to";
string str2 = "StackHowTo";
string str3 = str1 + str2;

// Method 2:
string str1 = "Welcome to";
string str2 = "StackHowTo";
string str3 = str1.append(str2);

// Method 3:
string str1 = "Welcome to";
string str2 = "StackHowTo";
string str3 = strcat(str1, str2);

A Method 2 et 3

B Method 1 et 2

C Method 1 et 3

D None of the above

B
To concatenate two String objects, we use either direct addition or the append() function in the string class, the strcat() function accepts char* and it cannot be used to concatenate two String objects.

 

 

8. Which of the following is not a modifier function in the String class?

A operator[]()

B operator+=()

C erase()

D push_back()

A
The [] operator is used to access one of the characters of the String object while the other functions allow to modify the String object in some way.

 

 
 

9. What is the output of the following C++ code?
#include <iostream>
using namespace std;
long factorial(long n)
{
	if (n > 1)
		return (n * factorial(n + 1));
	else
		return 1;
}
int main ()
{
	long n = 3;
	cout << n << "! = " << factorial( n );
	return 0;
}

A 6

B 3

C Compilation error

D Segmentation fault

D
The parameter n + 1 will exceed the size and it will result in “Segmentation fault”.
 

 
To calculate the factorial, we must replace the parameter n + 1, by n – 1.

 

 

10. Which function is used to get the length of a string in C++?

A str.length()

B str.size()

C str.max_size()

D The two functions size() and length()

D
“size()” and “length()” are used to get the size of a string in C++.

 

 
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 *