Site icon T4Tutorials.com

std::string class and its functions — C++ MCQs

Q#1: Which header file is required to use std::string in C++?
(A) <cstring>
(B) <string>
(C) <iostream>
(D) <cstdlib>
Answer: (B) <string>


Q#2: How do you declare a string object in C++?
(A) string str;
(B) std::string str;
(C) char str[];
(D) Both (A) and (B)
Answer: (B) std::string str;


Q#3: Which function returns the length of a std::string?
(A) length()
(B) size()
(C) Both length() and size()
(D) strlen()
Answer: (C) Both length() and size()


Q#4: Which operator is used to concatenate two std::string objects?
(A) +
(B) +=
(C) Both + and +=
(D) &
Answer: (C) Both + and +=


Q#5: Which function returns a substring from a std::string?
(A) substr(pos, len)
(B) substring(pos, len)
(C) substr(pos)
(D) Both (A) and (C)
Answer: (D) Both (A) and (C)


Q#6: How do you compare two std::string objects?
(A) Using == operator
(B) Using compare() function
(C) Both (A) and (B)
(D) strcmp()
Answer: (C) Both (A) and (B)


Q#7: Which function returns the position of the first occurrence of a character or substring?
(A) find()
(B) rfind()
(C) indexOf()
(D) search()
Answer: (A) find()


Q#8: Which function removes all characters from a std::string?
(A) clear()
(B) erase()
(C) reset()
(D) remove()
Answer: (A) clear()


Q#9: How do you insert a string into another string at a specific position?
(A) insert(pos, str)
(B) append(str)
(C) push_back(str)
(D) add(str)
Answer: (A) insert(pos, str)


Q#10: Which function converts a string to C-style string (char array)?
(A) c_str()
(B) data()
(C) Both c_str() and data()
(D) to_char()
Answer: (C) Both c_str() and data()

Exit mobile version