CPA – ProgrammingEssentials in C++ Chapter 1 Summary Test 1

Question 1: Which of the following strings represent a legal floating point literal?

(Select two answers.)

Answer:

1E-1

2.2f

Question 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;

}

Answer:

3.5

Question 3: 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;

}

Answer: (W)

0

Question 4:

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();

}

Answer:

4

Question 5:

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;

}

Answer:

Compilation fails

Question 6:

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;

}

Answer:

block

Question 7:

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);

}

Answer:

2

Question 8:

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;

}

Answer:

24

Question 9:

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;

}

Answer:

3

Question 10:

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;

}

Answer:

2

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" ;

}

Answer:

True

Question 12:

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;

}

Answer:

0-1

Question 13:

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);

}

Answer:

Compilation fails

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;

}

Answer:

2

Question 15:

Which of the following strings represent a legal integer literal?

(Select two answers.)

Answer:

-111222333

111'222'333

Question 16:

Which of the following strings represent a legal variable name?

(Select two answers.)

Answer:

myVariable1

my_variable_1

Question 17:

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;

}

Answer:

4

Question 18:

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];

}

Answer:

2

Question 19:

What is the value of the k variable?

int k = 2 % 3 + 5 % 3;

Answer:

4

Question 20:

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);

}

Answer:

2-22

Question 21:

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.);

}

Answer:

8

Question 22:

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;

}

Answer:

0

Question 23:

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;

}

Answer:

3

Question 24:

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);

}

Answer:

a

Question 25:

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)));

}

Answer:

1

Question 26:

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;

}

Answer:

-9

Question 27:

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;

}

Answer:

3

Question 28:

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;

}

Answer:

12333

Question 29:

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];

    }

}

Answer:

1

Question 30:

What is the value of the i variable?

float x = 1.0 / 5.0;

int i = x;

Answer:

0

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.