C++ Programming Questions and Answers – String – 1

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

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

Q 1.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<
            
A. H
B. e
C. o
D. Error
Show Answer Answer:-A. H
Explanation 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.

Q 2. 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<
            
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
Show Answer Answer:-D. Depends on the length of the string entered by the user
Explanation As the character array size is 10 so if the string entered by the user is <= 10 then there will be no error and the program runs perfectly otherwise if the length is > 10 then the program gives a run-time error because the string crosses the allocated memory space. Output: length < 10 $ ./a.out Hello Hello length > 10 $ ./a.out C++Programming *** stack smashing detected ***: terminated Aborted (core dumped)

Q 3.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<
            
A. Hello World
B. Hello
C. World
D. Error
Show Answer Answer:-B. Hello
Explanation 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

Q 4. Which header file is used to include the string object functions in C++?


            
A. #include
B. #include
C. #include
D. #include
Show Answer Answer:-B. #include
Explanation #include header file is used as it contains all the string object functions.

Q 5. 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<
            
A. Hello World
B. Hello
C. World
D. Error
Show Answer Answer:-D. Error
Explanation There is no operation defined for the addition of character array in C++ hence the compiler throws an error as it does not understood what to do about this expression.

Q 6. 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<
            
A. Hello World
B. World
C. Hello
D. Error
Show Answer Answer:-A. Hello World
Explanation 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”.

Q 7. 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
Show Answer Answer:-A. 1 and 2
Explanation 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.

Q 8.Which of the following is not a modifier function in string class?


            
A. push_back()
B. operator+=()
C. operator[]()
D. erase()
Show Answer Answer:-C. operator[]()
Explanation [] 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.

Q 9. 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
Show Answer Answer:-D. both size() and length() function
Explanation Both size() and length() are used to get the size of the string objects.

Q 10. 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
Show Answer Answer:-B. string
Explanation string identifier is used as the name of the class string.

Q 11. What is string objects in C++?


            
A. A stream of characters terminated by \0
B. Stream of alphabets
C. Stream of characters
D. A stream of well-defined characters
Show Answer Answer:-D. A stream of well-defined characters
Explanation String is defined as streams of characters, not necessarily terminated by \0. Also, a string can contain characters other than alphabets.

Q 12. What is Character-Array?


            
A. array of well-defined characters
B. array of characters
C. array of characters terminated by \0
D. array of alphabets
Show Answer Answer:-B. array of characters
Explanation Character-Array is defined as an array of characters, not necessarily terminated by \0. Also, a character-array can contain characters other than alphabets.

Q 13. Pick the incorrect statement about Character-Array.


            
A. Character-Array has a dynamic size
B. Character-Array has a threat of array-decay
C. Character-Array has a static size
D. Character-Array can be terminated by a null character(‘\0’)
Show Answer Answer:-A. Character-Array has a dynamic size
Explanation 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.

Q 14. Pick the correct statement about string objects in C++.


            
A. String objects must be terminated by a null character(‘\0’)
B. String objects have a dynamic size
C. String objects have a static size
D. String objects use extra memory than required.
Show Answer Answer:-B. String objects have a dynamic size
Explanation 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 ‘\0’.

Q 15.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<
            
A. Input provided by the user
B. str
C. Segmentation fault
D. Error
Show Answer Answer:-A. Input provided by the user
Explanation 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.

Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page