CPP String Quiz - 20 MCQ Questions and Answers

Welcome to our blog post, "C++ String Quiz - MCQ Questions and Answers," a perfect resource for anyone looking to test and expand their knowledge of string handling in C++. Strings are a vital aspect of C++ programming, and mastering their manipulation is key to developing robust applications. 

In this quiz, you'll find a series of multiple-choice questions that range from basic string operations to more intricate functions and concepts. Whether you're a student, a programming enthusiast, or a seasoned developer, these questions will challenge your understanding and provide valuable insights into the nuances of string manipulation in C++. So, gear up to dive into the world of C++ strings and assess your skills with our carefully curated questions and comprehensive answers!

1. What is a string in C++?

a) A collection of characters
b) A single character
c) A data type for storing integers
d) A built-in array type

Answer:

a) A collection of characters

Explanation:

In C++, a string is a collection of characters, typically represented by an array of chars or by using the string class in the C++ Standard Library.

2. How do you declare a string using the string class in C++?

a) string str = "Hello";
b) char str[] = "Hello";
c) string str("Hello");
d) Both a) and c)

Answer:

d) Both a) and c)

Explanation:

A string can be declared using the string class in C++ either by direct initialization (string str = "Hello";) or by using the constructor (string str("Hello");).

3. Which header file is required to use the string class in C++?

a) #include <string>
b) #include <strings>
c) #include <cstring>
d) #include <text>

Answer:

a) #include <string>

Explanation:

The <string> header file must be included to use the string class in C++.

4. How do you concatenate two strings in C++?

a) Using the + operator
b) Using the append() method
c) Both a) and b)
d) Using the concat() method

Answer:

c) Both a) and b)

Explanation:

Strings in C++ can be concatenated using the + operator or the append() method of the string class.

5. How can you find the length of a string in C++?

a) Using the length() function
b) Using the size() function
c) Both a) and b)
d) By manually counting the characters

Answer:

c) Both a) and b)

Explanation:

The length of a string in C++ can be found using either the length() or size() functions of the string class. Both provide the same functionality.

6. What is the substring function in C++ used for?

a) To replace a part of the string
b) To find a pattern in the string
c) To extract a part of the string
d) To add a string at a specific position

Answer:

c) To extract a part of the string

Explanation:

The substring function (substr) in C++ is used to extract a part of the string, starting from a specified position for a specified length.

7. How do you convert a number to a string in C++?

a) Using the to_string() function
b) Using the stoi() function
c) By adding the number to an empty string
d) Using the sprintf() function

Answer:

a) Using the to_string() function

Explanation:

The to_string() function in C++ is used to convert numbers to strings. It can convert various numerical data types to their string representation.

8. What does the string::npos represent in C++?

a) The beginning of a string
b) The end of a string
c) A successful search in a string
d) An unsuccessful search in a string

Answer:

d) An unsuccessful search in a string

Explanation:

string::npos is a constant used to represent an unsuccessful search in a string, typically used with string functions like find.

9. How do you compare two strings in C++?

a) Using the == operator
b) Using the compare() function
c) Both a) and b)
d) Using the equals() function

Answer:

c) Both a) and b)

Explanation:

Two strings in C++ can be compared for equality using the == operator or the compare() function of the string class.

10. What is the result of the following C++ code?

   string str = "Hello";
   char ch = str[1];
a) 'H'
b) 'e'
c) 'l'
d) Error

Answer:

b) 'e'

Explanation:

The string index in C++ starts from 0. Therefore, str[1] returns 'e', the second character in the string "Hello".

11. How do you insert a character in a string at a specific position in C++?

a) Using the insert() method
b) Using the push_back() method
c) By using the + operator
d) Using the append() method

Answer:

a) Using the insert() method

Explanation:

The insert() method of the string class in C++ is used to insert characters or strings at a specified position in the string.

12. What is the correct way to clear a string in C++?

a) str.clear()
b) str.empty()
c) str.erase()
d) str = ""

Answer:

a) str.clear()

Explanation:

The clear() method of the string class in C++ is used to clear the contents of a string, making it an empty string.

13. How do you find a character or substring in a string in C++?

a) Using the find() method
b) Using the search() method
c) By iterating through the string
d) Using the locate() method

Answer:

a) Using the find() method

Explanation:

The find() method of the string class in C++ is used to find the position of a character or a substring within the string.

14. What is the output of the following C++ code?

string str = "Hello World";
   cout << str.substr(6, 5);
a) Hello
b) World
c) Hello World
d) llo W

Answer:

b) World

Explanation:

The substr() function returns a substring starting from the 6th index (0-based) of length 5, which is "World".

15. How do you check if a string is empty in C++?

a) str.isEmpty()
b) str.empty()
c) str == ""
d) Both b) and c)

Answer:

d) Both b) and c)

Explanation:

To check if a string is empty in C++, you can use the empty() method of the string class or compare the string with an empty string literal.

16. Which function is used to replace a part of a string in C++?

a) replace()
b) change()
c) modify()
d) alter()

Answer:

a) replace()

Explanation:

The replace() method of the string class in C++ is used to replace a part of the string with another string or character.

17. How do you convert a string to a number in C++?

a) Using the stoi() function for integers, stof() for floats
b) Using the atoi() and atof() functions
c) By adding the string to a numeric variable
d) Using the parse() function

Answer:

a) Using the stoi() function for integers, stof() for floats

Explanation:

Functions like stoi() and stof() are used in C++ to convert strings to integers and floating-point numbers, respectively.

18. What is string concatenation in C++?

a) Splitting a string into two parts
b) Joining two or more strings into one
c) Changing characters in a string
d) Reversing a string

Answer:

b) Joining two or more strings into one

Explanation:

String concatenation in C++ is the process of joining two or more strings end-to-end to create a new string.

19. What is the correct way to iterate over the characters of a string in C++?

a) Using a range-based for loop
b) Using the traditional for loop with the size() function
c) Both a) and b)
d) Using the foreach loop

Answer:

c) Both a) and b)

Explanation:

Characters of a string in C++ can be iterated using a range-based for loop or a traditional for loop using the size() or length() function to get the number of characters.

20. How do you reverse a string in C++?

a) Using the reverse() method
b) Using the std::reverse function from <algorithm>
c) By swapping characters manually
d) Both b) and c)

Answer:

d) Both b) and c)

Explanation:

To reverse a string in C++, you can use the std::reverse function from the <algorithm> library or manually swap characters from the beginning to the end of the string.

Comments