160k views
2 votes
The following 4 lines of C++ code, use strings.

string firstName;// Define a string object
char lastName[7];// Define a C-string
firstName = "Abraham";// Assign a value to the string object
lastName = "Lincoln";// Assign a value to the C-string. Which of the following statements is /are true?
A) The string object is defined incorrectly because no size is given for it.
B) The 2 strings definitions are correct, but the 2 assignment statements are wrong.
C) The string object is assigned a value correctly, but the C-string is not.
D) The C-string is assigned a value correctly, but the string object is not.
E) All 4 lines of codes are correct.

User Simon Vane
by
6.8k points

1 Answer

3 votes

Answer:

The answer to this question is option (C).C-String is not assigned value correctly.

Step-by-step explanation:

This is because the size with which c-string is initialized is 7 and we are providing a string of 7 characters to store So there is no space left to store the null character '\0' .We can only store a string of size 6 in the c string.There is no need to provide the size in the string object as it is handled by string class.

User Warpspace
by
6.4k points