250+ TOP MCQs on String – 1 and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “String – 1”.

Provera works by suppressing the body's natural testosterone. The american heart association's (aha) get with the guidelines-for clinical practice guidelines (cpg) for chronic fatigue syndrome (cfs) is the result of 18 months of work Georgia http://raygungothicrocket.com/join-the-adventure by an international team of experts who reviewed the available medical literature and used their clinical knowledge to arrive at a comprehensive overview of the evidence and what we know about cfs and its treatment. The dose of the medication should be increased in the future if the person’s acne is not improving.

Abnormal bleeding (vomiting, spotting or bleeding in the vaginal canal or anus) It is http://byoungco.com/products/b425-stinghd buy stromectol 3 mg recommended to take the treatment for 6 months or 1 year. You have probably decided that the new place suits you much better than where you've been living for the past couple of years.

1. What is string objects in C++?
a) Stream of alphabets
b) A stream of well-defined characters
c) Stream of characters
d) A stream of characters terminated by
Answer: b
Clarification: String is defined as streams of characters, not necessarily terminated by . Also, a string can contain characters other than alphabets.

2. What is Character-Array?
a) array of alphabets
b) array of well-defined characters
c) array of characters
d) array of characters terminated by
Answer: c
Clarification: Character-Array is defined as an array of characters, not necessarily terminated by . Also, a character-array can contain characters other than alphabets.

3. Pick the incorrect statement about Character-Array.
a) Character-Array can be terminated by a null character(‘ ’)
b) Character-Array has a static size
c) Character-Array has a dynamic size
d) Character-Array has a threat of array-decay
Answer: c
Clarification: As Character-Array is an array, its size should be defined during its declaration hence the size of Character-Array is static. A Character-Array is not necessarily to be terminated by a null character. Also, it has a threat of array-decay.

4. Pick the correct statement about string objects in C++.
a) String objects must be terminated by a null character(‘ ’)
b) String objects have a static size
c) String objects have a dynamic size
d) String objects use extra memory than required.
Answer: c
Clarification: String objects are dynamic in nature i.e. their size varies as their value changes so they don’t use any extra memory and it is not necessary to terminate a string object by ‘ ’.

5. What will be the output of the following C++ code?

#include  
#include 
using namespace std; 
int main(int argc, char const *argv[])
{
	string str;
	cin>>str;
	cout<<str;
	return 0;
}

a) str
b) Input provided by the user
c) Error
d) Segmentation fault
Answer: b
Clarification: There is no error in the program and as we are asking the user to enter a string and printing that string to console. Therefore output will be the string provided by the user.

6. What will be the output of the following C++ code?

#include  
#include 
using namespace std; 
int main(int argc, char const *argv[])
{
	char str[] = "Hello World";
	cout<<str[0];
	return 0;
}

a) H
b) e
c) Error
d) o
Answer: a
Clarification: The program has no errors so and as str = “Hello World” and we are trying to print the first character of str. Hence “H” is the answer.

7. What will be the output of the following C++ code?

#include  
#include 
using namespace std; 
int main(int argc, char const *argv[])
{
	char str[10];
	cin>>str;
	cout<<str;
	return 0;
}

a) Compiler-time Error
b) Run-time Error
c) Input given by the user
d) Depends on the length of the string entered by the user
Answer: d
Clarification: As the character array size is 10 so if the string entered by the user is 10 then the program gives a run-time error because the string crosses the allocated memory space.
Output:

length  10
$ ./a.out 
C++Programming
*** stack smashing detected ***:  terminated
Aborted (core dumped)

8. What will be the output of the following C++ code if the string entered by the user is “Hello World”?

#include  
#include 
using namespace std; 
int main(int argc, char const *argv[])
{
	string str;
	cin>>str;
	cout<<str;
	return 0;
}

a) Hello World
b) Hello
c) World
d) Error
Answer: b
Clarification: As cin considers n or space as the terminating symbols for the input so when the user enters “Hello World” so only “Hello” will be stored into the str variable as cin stops scanning input after space.
Output:

$ ./a.out 
Hello World 
Hello

9. Which header file is used to include the string object functions in C++?
a) #include
b) #include
c) #include
d) #include
Answer: c
Clarification: #include header file is used as it contains all the string object functions.

10. What will be the output of the following C++ code?

#include  
#include 
using namespace std; 
int main(int argc, char const *argv[])
{
	char s1[6] = "Hello";
	char s2[6] = "World";
	char s3[12] = s1 + " " + s2;
	cout<<s3;
	return 0;
}

a) Hello World
b) Hello
c) World
d) Error
Answer: d
Clarification: There is no operation defined for the addition of character array in C++ hence the compiler throws an error as it does not understoods what to do about this expression.

11. What will be the output of the following C++ code?

#include  
#include 
using namespace std; 
int main(int argc, char const *argv[])
{
	string s1 = "Hello";
	string s2 = "World";
	string s3 = s1 + " " + s2;
	cout<<s3;
	return 0;
}

a) Hello World
b) Hello
c) World
d) Error
Answer: a
Clarification: The program runs perfectly as string class has defined the addition of two strings so when two strings are added then both the strings are concatenated. Hence the output is “Hello World”.

12. Which of the following is correct way of concatenating two string objects in C++?

way 1:
string s1 = "hello";
string s2 = "world";
string s3 = s1 + s2;
 
way 2:
string s1 = "hello";
string s2 = "world";
string s3 = s1.append(s2);
 
way 3:
string s1 = "hello";
string s2 = "world";
string s3 = strcat(s1,s2);

a) 1 and 2
b) 2 and 3
c) 1 and 3
d) 1, 2 and 3
Answer: a
Clarification: To concatenate two string objects we are provided with either direct addition or append() function in string class but strcat() is char* function hence they cannot be used to concatenate two string objects.

13. Which of the following is not a modifier function in string class?
a) operator+=()
b) operator[]()
c) push_back()
d) erase()
Answer: b
Clarification: [] operator is used to access one of the characters of the string objects whereas other functions are used to modify the string in some way.

14. Which function is used to get the length of a string object?
a) str.length()
b) str.size()
c) str.max_size()
d) both size() and length() function
Answer: d
Clarification: Both size() and length() are used to get the size of the string objects.

15. What is the identifier given to string class to declare string objects?
a) String
b) string
c) STRING
d) Any of the above can be used
Answer: b
Clarification: string identifier is used as the name of the class string.