Accessing data: arrays of pointers, conversions, strings, namespaces, and exceptions CPPE1 M4 Test
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { short s = 1; int i = 2; float f = 4.4; double d = 6.6; cout << i/static_cast<float>(s) + int(f)/i + (long)d/s; return 0; }
8.8
- Compilation fails
10
8.0
-
What is the output of the following snippet?
#include <iostream> #include <string> using namespace std; int main() { int i = 2; string s = "2"; cout << s + i; }
2
4
2
- Compilation fails
-
What is the output of the following snippet?
#include <iostream> #inlcude <string> using namespace std; int main() { string s1 = "aleph"; string s2 = "alpha"; string s; s1.swap(s2); s2.swqp(s); s.swap(s2); cout << s2; }
- Compilation fails
alpha
aleph
- It prints an empty string
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int i = 2; float f = 4.4; cout << f % static_cast<float>(i); }
0
2
- Compilation fails
0.2
-
What is the output of the following snippet?
#include <iostream> #include <string> using namespace std; int main() { string s = "ABC"; for(int i = 1; i < s.length(); i += 2) s[i - 1] = s[i] + 'a' - 'A'; cout << s; }
bBC
- Compilation fails
bBc
aAcC
-
What is the output of the following snippet?
#include <iostream> #include <string> using namespace std; int main() { string s = "a"; cout << s + "b" + "c"; }
abc
- Compilation fails
ab
a
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int *it[3]; for(int i = 0; i < 3; i++) { it[i] = new int [i + 1]; for(int j = 0; j < i + 1; j++) it[i][j] = 10 * i + j; } cout << it[2][2]; for(int i = 0; i < 3; i++) delete [] it[i]; }
33
11
- Compilation fails
22
-
What is the output of the following snippet?
#include <iostream> namespace SpaceA { int A; } namespace SpaceB { int A; } using namespace SpaceA, SpaceB; int main() { SpaceA::A = SpaceB::A = 1; std:cout << A + 1; }
- Compilation fails
0
1
2
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int i = 2; float f = 5.8; f = static_cast<int>(f); i = static_cast<float>(i); cout << f/i; }
2.5
- Compilation fails
1
3
-
What is the output of the following snippet?
#include <iostream> #include <string> using namespace std; int main() { string s = "AB"; s.append(s).push_back(s[s.length() - 1]); cout << s; }
ABAB
ABABA
ABABB
- Compilation fails
-
What is the output of the following snippet?
#include <iostream> #include <string> using namespace std; int main() { string s1 = "ab"; string s2 = "ABC"; if(s1 >s2) cout << "yes"; else cout << "NO"; }
yes
NO
- Compilation fails
- It prints nothing
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { string s1 = "1"; string s2 = "2"; cout << s1.compare(s2); }
1
- Compilation fails
0
-1
-
What is the output of the following snippet?
#include <iostream> #include <string> using namespace std; int main() { string s ="a"; cout << s << "b" + "c"; }
ab
abc
- Compilation fails
a
-
What is the output of the following snippet?
#include <iostream> #include <string> using namespace std; int main() { string s = "0123"; cout << s.substr(1,3).substr(2).substr(); }
3
123
- It prints an empty string
- Compilation fails
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { string s1 = "Ab"; string s2 = "Abc"; cout << s1.compare(s1); }
- Compilation fails
0
1
-1
-
What is the output of the following snippet?
#include <iostream> #include <vector> using namespace std; int main() { vector<float *> ft = { new float[3], new float[3], new float[3] }; float *p = nullptr; for(int i = 0; i < 3; i++) { p = ft[i]; *p = p[1] = *(p + 2) = 10 * i; } cout << ft[1][1]; delete [] ft[0]; delete [] ft[1]; delete [] ft[2]; }
- Compilation fails
20
30
10
-
What is the output of the following snippet?
#include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> t = { "alpha", "beta", "gamma" }; int *cnt = new int[3], *p = &cnt[0]; for(int i = 0; i < t.size(); i++) *p++ = t[i].length(); cout << cnt[0] << cnt[1] << cnt[2]; delete [] cnt; }
012
111
545
- Compilation fails
-
What is the output of the following snippet?
#include <iostream> using namespace std; namespace S1{ int A = 1; } namespace S2{ int A = 2; } int main() { { using namespace S1; S2::A = A + 1; } { using namespace S2; S1::A = A + 1; } cout << S1::A << S2::A; }
32
33
23
- Compilation fails
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { short s = 1; int i = 2; float f = 4.4; double d = 8.8; cout << s/i + f/i + d/s; return 0; }
11
- Compilation fails
6.6
4.4
-
What is the output of the following snippet?
#include <iostream> using namespace std; namespace S { int A = 1; } namespace S { int B = A + 2; } int main() { S::A = S::A + 1; { using namespace S; ++B; } cout << S::B << S::A; return 0; }
- Compilation fails
32
42
22
Subscribe
0 Comments
Newest