Advanced flow control (if, else, switch; loops) and data aggregates CPPE1 M2 Test
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int i = 5, j = 0; while(i > 0) { i--; j++; } cout << j; }
3
4
5
2
-
What is the output of the following snippet?
#include <iostream> #inlcude <vector> using namespace std; struct S { int t; }; struct SS { S t; }; int main() { vector<SS> t = { {1}, {2} }; cout << t[0].t.t << t[1].t.t; }
12
22
21
11
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { vector<vector<int>> g = { {2, 4}, {3, 6}, {5, 10}}; for(int i = 1; i >= 0; i--) for(int j = 0; j < 2; j++) g[i][j] += g[j][i]; cout << g[1][1]; }
9
12
6
15
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { for(float val = -10.0; val < 100.0; val = -val * 2) { if(val < 0 && -val >= 20) break; cout << "*"; } }
**
***
****
*****
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int i = 3, j = 0; do { i--; j++; }while(i >= 0); cout << j; }
5
4
2
3
-
What is the output of the following snippet?
double big = 1e15; double small = 1e-15; cout << fixed << big + small;
0.0
1000000000000000.0000000000000001
1000000000000000.000000
1.01e15
-
What is the output of the following snippet?
#include <iostream> #include <vector> using namespace std; struct Str { int in; char ch; }; int main() { vector<Str> t = { {1, 'a'}, {2, 'b'}}; for(int i = 0; i < 2; i++) t[i].in += (t[1 -i].ch - t[i].ch); cout << t[0].in << t[1].in; }
22
21
12
11
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int a = 1, b = 2; int c = a << b; int d = 1 << c; int e = d >> d; cout << e; }
1
0
4
2
-
What is the output of the following snippet?
#include <iostream> #include <vector> using namespace std; int main() { vector<int> t = { 8, 4, 3, 2, 1 }; int i; for(i = t[4]; i < t[3]; i++) t[i - 1] = -t[3]; cout << i; }
2
4
3
5
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { for(float val = -10.0; val < 20.0; val = -val * 2) cout << "*"; }
****
***
*
**
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { ibool yes = !false; bool no = !yes; if(!no) cout << "true"; else cout << "false"; }
false
true
yes
no
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int a = 2; switch(a << a) { case 8: a++; case 4: a++; case 2: break; case 1: a--; } cout << a; }
2
4
5
3
-
What is the output of the following snippet?
#include <iostream> #include <vector> using namespace std; int main() { vector<char> arr = { 'a', 'b', 'c' }; for(int i > 1; i < arr.size(); i++) { cout << "*"; if(arr[i] - arr[i - 1]) % 2) continue; cout << "*"; }
****
*
***
**
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { float val = 100.0; do { val = val / 5; cout << "*"; }while(val > 1.0); }
****
***
*****
**
-
What is the output of the following snippet?
#include <iostream> #include <vector> using namespace std; struct Stru { int i1, i2; char c1, c2; }; int main() { Stru a = { 1, 2, 'a', 'b' }; Stru b = { 5, 6, 'x', 'y' }; cout << static_cast<char>(b.c1 + a.i1); cout << static_cast<int>(a.c2 - a.c1); cout << static_cast<int>(b.c2 - b.c1); }
y11
b12
all
z22
-
What is the output of the following snippet?
#include <iostream> #include <vector> using namespace std; int main() { vector<bool> t(2); for(int i = 0; i < 2; i++) t(1 - i] = !(i % 2); cout << t[0] && t[1]; return 0; }
false
true
1
0
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int i = 0, j = i++, k = --i; if(i > 0) j++; else k++; else if(k > 0) k--; else k++; cout << i * j * k; }
2
-1
0
1
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int i = 1, j = i++, k = --i; if(i > 0) { j++; k++; } else { k++; i++; } if(k == 0) { i++; j++; } else { if(k > 0) k--; else k++; i++; } cout << i * j * k; }
8
4
0
2
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int a = 1, b = 2; int c = a | b; int d = c & a; int e = d ^ 0; cout << e << d << c; }
113
031
131
100
-
What is the output of the following snippet?
#include <iostream> #include <vector> using namespace std; int main() { vector<int> a = {2, 0, 1}; vector<int> b = {1, 2, 3}; for(int i = 0; i < 3; i++; b[a[i]] = b[2 - i]; cout << b[0] << b[2]; }
21
23
12
32
Subscribe
0 Comments
Newest