Question
1. [10 marks] Suppose that s1 and s2 are given as follows: string s1(Hello C++ programming ); string s2(CSE 202); Assume that each expression is
1. [10 marks] Suppose that s1 and s2 are given as follows: string s1("Hello C++ programming "); string s2("CSE 202"); Assume that each expression is independent. What are the result of the following statements?
a) s1.append(s2); cout << s1;
b) cout << s1.at(0);
c) cout << s2.length();
d) cout << s1.substr(6);
e) cout << s1.substr(6, 3);
f) cout << s1.find("C++");
g) cout << (s1 > s2);
h) s1.replace(6, 3, "Java"); cout << s1;
i) s1.assign("Hello World"); cout << s1;
j) s1 = s2; ; cout << s1;
2. [5 marks] Show the printout of the following code:
int x = 100; int y = 200;
int* p_x = &x;
(*p_x)++;
cout << x << endl;
cout << *p_x << endl;
p_x = &y;
(*p_x)++;
cout << y << endl;
cout << *p_x << endl;
3. [5 marks] Show the printout of the following code:
char c[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
for (int i = 0; i < 6; i++)
{
cout << *(c + i) << endl;
}
4. [5 marks] Show the printout of the following code:
int a[] = {11, 12, 13, 14, 15};
int* p = a;
for (int i = 0; i < 5; i++)
{
cout << *(a + i) << ",";
cout << *(p + i) << ",";
cout << p[i] << endl;
}
5. [5 marks] Show the printout of the following code:
string* p_s = new string("abcdefg");
string s("ABCDE");
cout << (*p_s) << endl;
cout << p_s->substr(3,3) << endl;
cout << p_s->length() << endl;
delete p_s; p_s = & s;
cout << p_s->substr(3) << endl;
cout << p_s->length() << endl;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started