C++ Essentials CPPE1: Part 1 Summary Test
-
What is the output of the following program if a digit
3
followed by Enter is entered through the keyboard?#include <iostream> using namespace std; int main() { int i = 2, j = i++, k = i++; cin >> i; cout << k - i << j - i; }
0-2
-10
-1-2
0-1
-
What is the final value of the
k
variable?#include <iostream> using namespace std; int main() { int i = 0, k = i; while(i==0){ if(k > 1) i = k; ++k; } cout << k; }
3
1
4
2
-
What is the output of the following snippet?
#include <iostream> using namespace std; float combine(float x1 = 0.0, int x2 = 1.0) { return x2 + x1; } int main() { cout << combine() + combine(1.) + combine(2.,3.); }
8
2
4
3
-
What is the output of the following snippet?
#include <iostream> #include <vector> using namespace std; int main() { vector<float *> ft = {new float[1], new float[1], new float[1]}; for(int i = 0; i < 3; i++) { float *p = ft[i]; *p = i; } cout << *ft[1]; for(int i = 0; i < 3; i++){ delete[] ft[i]; } }
0
- Compilation fails
1
2
-
What is the output of the following snippet?
#include <iostream> using namespace std; int op(int i, int j = 1) { return i * j; } int op(char a, char b) { return b - a; } int op(float x, float y) { return x / y; } int main() { cout << op(2) << op('c', 'a') << op(4.f, 2.f); }
01-2
2-22
222
2-48
-
What is the output of the following snippet?
#include <iostream> using namespace std; char do1(char *x) { return *x; } char *do2(char *y) { return y; } char *do3(char &z) { return &z; } int main() { char sign = '1'; cout << do1(do2(do3(sign))); }
0
2
4
1
-
What is printed on the screen when the following code is run?
#include <iostream> using namespace std; int main() { int k = 3; if(k > 0){ if(k != 3) k--; if(k == 3) k++; } if(k < 0){ k = 5; } cout << k; }
5
3
2
4
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int a = 2, b = a >> 1; int c = a >> b; int d = a << c; int e = d >> d; cout << e; }
4
1
0
2
-
What is printed on the screen when the following code is run?
#include <iostream> using namespace std; int main() { int a = 2, b = a >> 1; int c = b >> a; int d = 1 << c; int e = d << d; cout << e; }
2
4
0
1
-
Which of the following strings represent a legal integer literal?
(Select two answers.)
-111222333
b'11001111'
111'222'333
x12FE
-
What is printed on the screen when the following code is run?
#include <iostream> #include <string> using namespace std; string replicate(string s = "x", int r =1) { string t; while(r--) t += s; return t; } int main() { string pattern = "a"; cout << replicate(pattern); }
ax
a
x
xa
-
What is the output of the following snippet?
#include <iostream> #include <vector> using namespace std; int main() { vector<char> text(5); char *chr1 = text.data() + 2, *chr2 = chr1 + 2; cout << chr2 - text.data(); }
4
1
3
2
-
What is the output of the following snippet?
#include <iostream> #include <string> using namespace std; int main() { string s1 = "brick"; string s2 = "block"; string s; s1.swap(s2); s2.swap(s); s.swap(s2); cout << s1; }
brick
- It prints an empty string
block
- Compilation fails
-
What is printed on the screen when the following code is run?
#include <iostream> using namespace std; int main() { int a = 0x02, b = 001; int c = a ^ b; int d = c | a; int e = d & 0; cout << e; }
1
0
2
4
-
What is printed on the screen when the following code is run?
#include <iostream> using namespace std; int main() { int i = 1, k = i << 1; switch(k){ case 1: i += 1; break; case 2: i += 2; break; default: i += 3; } cout << i; }
2
1
3
4
-
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; }
1
- Compilation fails
0
- The program causes runtime error
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int i = 2; float f = 1; cout << (static_cast<float>(i) >> 1); }
1.0
- Compilation fails
0
1
-
Which of the following strings represents a legal variable name?
(Select two answers.)
1myVariable
my_variable_1
myVariable1
my Variable 1
-
What is printed on the screen when the following code is run?
#include <iostream> using namespace std; int main() { int i, k = 1; for(i = 0; i < 3; i += 2) k++; cout << k; }
3
1
4
2
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { short s = 1; int i = 2; float f = 4; cout << i/static_cast<float>(s) + i/2 + i/f; return 0; }
3
4
- Compilation fails
3.5
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { string s = "123"; s.append(s.substr(2)).push_back(s[s.length() - 2]); cout << s; }
12233
12333
12312323
- Compilation fails
-
What is printed on the screen when the following code is run?
#include <iostream> #include <vector> using namespace std; int main() { vector<double> arr = {1e-1, 1e0, 1e1}; double *ptr = arr.data() + 2; cout << arr[1] - *ptr; }
-0.9
9
-9
0.9
-
What is the output of the following snippet?
#include <iostream> using namespace std; double eval(double x) { return x / (.5 * x); } void use(double n) { int v = 1 / n; v = eval(v); cout << v; } int main() { use(1.f); }
0
4
8
2
-
What is the value of the
i
variable?float x = 1.0 / 5.0; int i = x;
5.0
0.20
0
1
-
What is printed on the screen when the following code is run?
#include <iostream> using namespace std; int main() { int i = 1, k = i & 0; do { k++; if(k > 1) i = k; }while(i < 2); cout << k; }
3
2
4
1
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { bool b1 = !true; bool b2 = !b1 && false; bool b3 = b2 || true; if(b3) cout << "true"; else cout << "false"; }
true
0
false
1
-
What is the value of the
k
variable?int k = 2 % 3 + 5 % 3;
0
1
4
2
-
Which of the following strings represent a legal floating point literal?
(Select two answers.)
2.2f
1E-1
1.2e1.2
2.2d
-
What is the output of the following snippet?
#include <iostream> using namespace std; namespace Universe { int Galaxy = 1; } namespace Universe{ int Planet = Galaxy + 2; } int main() { Universe::Galaxy *= 2; { using namespace Universe; Planet++; } cout << Universe::Galaxy << Universe::Planet; }
- Compilation fails
24
23
42
-
What is the output of the following snippet?
#include <iostream> #include <vector> using namespace std; void swap(float* x, float *y) { float z = *x; *x = *y; *y = z; } int main() { vector<float> t = {3., 2., 1.}; swap(&t[0], &t[2]); cout << t[1]; }
2
1
3
0
Subscribe
0 Comments
Newest