C++ MCQ Questions with 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
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.
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
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
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>
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
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
8. Which of the following is not a modifier function in the String class?
A operator[]()
B operator+=()
C erase()
D push_back()
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
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()