CPA – Programming Essentials in C++ Summary Test Answers
Question 1: What is the value of the i variable?
float x = 1.0 / 5.0;
int i=x;
- 1
- 0.20
- 5.0
- 0
Question 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;
}
- block
- brick
- It prints an empty string
- Compilation fails
Question 3: 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
- 0-1
- -1-2
Question 4: What is printed on the screeen 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;
}
- -9
- 9
- 0.9
- -0.9
Question 5: 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];
}
}
- 2
- Compilation fails
- 0
- 1
Question 6: 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];
}
- 0
- 2
- 1
- 3
Question 7: 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;
}
- 23
- 42
- 24
- complilation fails
Question 8: What is the value of the k variable?
int k = 2 % 3 + 5 % 3;
- 1
- 2
- 0
- 4
Question 9: Which of the following strings represent a legal integer literal?
(Select two answers.)
- -111222333
- x2FE
- b’11001111′
- 111’222’333
Question 10: 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);
}
- 222
- 2-22
- 01-2
- 2-48
Question 11: 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” ;
}
- 0
- true
- 1
- false
Question 12: 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;
}
- 2
- 3
- 1
- 4
Question 13: Which of the following strings represent a legal floating point literal?
(Select two answers.)
- 1E-1
- 2.2f
- 1.2e1.2
- 2.2d
Question 14: What is printed on the screeen 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;
}
- 4
- 2
- 0
- 1
Question 15: 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();
}
- 3
- 2
- 4
- 1
Question 16: 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 = 1 << c;
int e = d >> d;
cout << e;
}
- 4
- 1
- 2
- 0
Question 17: 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;
}
- 4
- 2
- 3
- 1
Question 18: Which of the following strings represent a legal variable name?
(Select two answers.)
- my Variable 1
- 1myVariable
- myVariable1
- my_variable_1
Question 19: 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;
}
- 4
- 3
- 2
- 1
Question 20: 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);
}
- 0
- 1
- 1.0
- compliation failed
Question 21: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = “123”;
s.append(s.substr(2)).push_back(s[s.length() – 2]);
cout << s;
}
- 12333
- Compilation fails
- 12312323
- 12233
Question 22: What is printed on the screen whent 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);
}
- x
- a
- ax
- xa
Question 23: 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;
}
- 4
- 5
- 2
- 3
Question 24: 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
- 1
- 4
- 2
Question 25: 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;
}
- Compilation fails
- The program causes runtime error
- 1
- 0
Question 26: 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.);
}
- 4
- 2
- 3
- 8
Question 27: What is printed on the screen when the following program 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;
}
- 0
- 1
- 2
- 4
Question 28: 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;
}
- Compilation fails
- 3
- 4
- 3.5
Question 29: 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);
}
- 8
- 0
- 4
- 2
Question 30: 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)));
}
- 1
- 2
- 0
- 4