1. Which one of the following is a string class constructor?
Explanation:
Correct answer: b) string (const char * str);
Option b is the correct answer. The string class constructor that takes a C-style string (const char *) as an argument is used to create a string object from a null-terminated character array (C-style string). For example, you can use this constructor to create a string object from a string literal like "Hello" or a character array containing a sequence of characters followed by a null character ('\0').
2. Which one of the following initialization statements is illegal?
Explanation:
Correct answer: c) string city2= "New" + city;
The statement in option c is illegal. In C++, you cannot directly concatenate a string literal ("New") with a string object (city) using the '+' operator. To concatenate two strings, you need to use the +
operator with string objects or use the append()
member function. The correct way to initialize a string with a concatenated value is:
string city2 = "New" + city;
should be replaced with string city2 = "New" + city;
or string city2 = "New"; city2 += city;
.
3. Identify the error in the following statement
for (int i=1; i<=string1.length( );i++)
cout <<string1.at(i);
Explanation:
Correct answer: b) Initial value for i should be zero
The statement in option b is incorrect. In C++, string indices (positions) start from 0, not 1. So, when accessing individual characters of a string using string1.at(i)
, the initial value of i
should be zero, not one. The correct loop statement is:
for (int i = 0; i < string1.length(); i++)
4. What will be the output of the following code segment?
int main()
{
string s1("Hello");
string s2("123");
s1.insert(4, s2);
s1.replace(2, 2, s2);
cout << s1;
return 0;
}
Explanation:
Correct answer: c) Hell123
Let's analyze the code step by step:
s1.insert(4, s2);
inserts the strings2
at index 4 ofs1
. After this operation,s1
will become "Hell123o" (Hello + 123 + o).s1.replace(2, 2, s2);
replaces the substring starting at index 2 ofs1
withs2
. The substring "ll" is replaced with "123", sos1
will become "Hell123o".- Therefore, the final output of
s1
is "Hell123".
5. Which of the following functions is not supported by the string class?
Explanation:
Correct answer: d) count()
Option d is the correct answer. The count()
function is not supported by the C++ string class. The count()
function is used to count the occurrences of a specified element in a container, but it is not a member function of the string class. Instead, the C++ string class provides various other useful member functions like length()
, size()
, substr()
, etc.
6. Which of the following operators cannot be used for string objects?
Explanation:
Correct answer: b) &
Option b is the correct answer. The '&' operator (address-of operator) cannot be directly used with string objects. The '&' operator is used to obtain the memory address of a variable, and it is generally used with pointers. For example, if s
is a string object, you cannot write &s
to get its address. Instead, you can use the address()
function or obtain the address using the &
operator on the first character of the string (e.g., &s[0]
).
7. Which of the following are TRUE for the statement s1.compare(s2);
(i) returns 1, when s1 and s2 are equal
(ii) returns 0, when s1 and s2 are equal
(iii) returns –1, if s1 < s2
(iv) returns –1, if s1 > s2
(v) returns 1, if s1 < s2
(vi) returns 1, if s1 > s2
Explanation:
Correct answer: c) ii, iii and vi
The statement s1.compare(s2)
is used to compare two strings s1
and s2
. The comparison result is based on the lexicographical order of the strings.
- If
s1
ands2
are equal, the function returns 0 (option ii). - If
s1
is lexicographically less thans2
, the function returns a value less than 0 (option iii). - If
s1
is lexicographically greater thans2
, the function returns a value greater than 0 (option vi).
compare()
function.
8. To access individual characters, we must use the function
Explanation:
Correct answer: d) at()
Option d is the correct answer. The at()
function is used to access individual characters (elements) of a string. For example, to access the character at position 3 in a string s
, you can use s.at(3)
.
9. Which of the following is TRUE?
Explanation:
Correct answer: b) Function end() returns an iterator to the invoking objects
Option b is the correct answer. The end()
function in C++ string class returns an iterator (pointer-like object) pointing to the position just after the last character in the string. This iterator does not point to a valid character; it is used as an end marker to indicate the end of the string. It is essential to note that C++ strings are not null-terminated; they are managed using the length of the string, which is stored internally in the string object.
10. To replace the contents of string s1 with that of s2 and contents of s2 with that of s1, we can use the statement
Explanation:
Correct answer: b) swap(s1, s2);
Option b is the correct answer. To exchange the contents of two strings s1
and s2
, you can use the swap()
function. The swap()
function swaps the contents of the two strings, making s1
contain the contents of s2
and vice versa. The correct statement is: swap(s1, s2);
THE STUDENT FRIENDLY BOOK TO LEARN C++. Let us C++ by Famous author Yashavant Kanetkar Indian writer |
Topic wise C++ MCQs Index ≡
- Principles of Object-Oriented Programming
- Beginning with C++
- Tokens, Expressions, and Control Structures
- Functions in C++
- Classes and Objects
- Constructors and Destructors
- Operator Overloading
- Inheritance
- Pointers, Virtual Functions, and Polymorphism
- Managing Console I/O Operations
- Working with Files
- Templates
- Exception Handling
- Standard Template Library
- Manipulating Strings
- New Features in ANSI C++ Standards
- Object-Oriented System Development
Comments
Post a Comment