CPA – Programming Essentials in C++ Module 2 Exam Answers
Question 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;
- 4
- 5
- 2
- 3
Question 2: 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;
}
- 3
- 2
- 5
- 4
Question 3: 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);
}
- z22
- b12
- y11
- a11
Question 4: 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 << “*”;
}
}
- **
- ****
- *****
- ***
Question 5: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
bool yes = !false;
bool no = !yes;
if(!no)
cout << “true”;
else
cout << “false” ;
}
- true
- no
- false
- yes
Question 6: 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);
}
- **
- *****
- ****
- ***
Question 7: 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
Question 8: 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;
}
- true
- 0
- 1
- false
Question 9: What is the output of the following snippet?
#include <iostream>
#include <vector>
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];
}
- 6
- 15
- 9
- 12
Question 10: 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++;
if(k == 0)
i++;
else if(k > 0)
k–;
else
k++;
cout << i * j * k;
}
- 1
- 2
- 0
- -1
Question 11: What is the output of the following snippet?
double big = 1e15;
double small = 1e-15;
cout << fixed << big+ small;
- 1000000000000000.0000000000000001
- 0.0
- 1.01e15
- 1000000000000000.000000
Question 12: 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 << “*”;
}
- **
- ***
- ****
- *
Question 13: 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
Question 14: 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;
}
- 131
- 113
- 100
- 031
Question 15: What is the output of the following snippet?
#include <iostream>
#include <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;
}
- 21
- 12
- 22
- 11
Question 16: 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 << “*”;
}
}
- ***
- **
- ****
- *
Question 17: 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
- 12
- 21
- 11
Question 18: 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;
}
- 2
- 3
- 4
- 5
Question 19: 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
- 3
- 4
Question 20: 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
- 2
- 3
- 4