What is the output of the following code?string str = "Hello";cout << str.length();4560
Question
What is the output of the following code?string str = "Hello";cout << str.length();4560
Solution
The output of the code will be 5.
Here's the step by step explanation:
- A string variable
stris declared and initialized with the value "Hello". - The
length()function is called onstrwhich returns the number of characters in the string. - The
coutstatement prints the length of the stringstrto the console.
Since the string "Hello" has 5 characters, the output will be 5.
Similar Questions
What is the output of the following code?string str = "hello";str.insert(1, "123");cout << str;"h123ello""1hello""hello123""h123lo"
What will be the output of the following code?string str = "abcdef";cout << str.substr(2, 3);"abc""def""cde""bcd"
What will be the output of the following code?string str = "Hello World";str.replace(6, 5, "C++");cout << str;"Hello C++""Hello C++ World""C++ World""World C++"
What is the output of the following code?char str[] = "hello";printf("%d\n", sizeof(str));
What is the output after this code is compiled and its executable run?#include <iostream>#include <string>using namespace std;int main(){ string str ( "Let us do something crazy" ); cout << "size: " << str.size() << ", "; cout << "length: " << str.length() << ", "; cout << "capacity: " << str.capacity() << ", "; cout << "max size: " << str.max_size() << "\n"; return 0;}Select one:a.size: 2, length: 2, capacity: 2, max size: 25b.size: 5, length: 5, capacity: 5, max size: 4611686018427387897c.size: 25, length: 5, capacity: 25, max size: 25d.size: 25, length: 25, capacity: 25, max size: 4611686018427387897
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.