Home Exam answers CPA – Programming Essentials in C++ Summary Test Answers

CPA – Programming Essentials in C++ Summary Test Answers

0

CPA – Programming Essentials in C++ Summary Test Answers

What is the value of the i variable?

float x = 1.0 / 5.0;

int i=x;

  • 1
  • 0.20
  • 5.0
  • 0

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

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

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

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

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

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

What is the value of the k variable?

int k = 2 % 3 + 5 % 3;

  • 1
  • 2
  • 0
  • 4

Which of the following strings represent a legal integer literal?

(Select two answers.)

  • -111222333
  • x2FE
  • b’11001111′
  • 111’222’333

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

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

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

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

(Select two answers.)

  • 1E-1
  • 2.2f
  • 1.2e1.2
  • 2.2d

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

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

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

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

Which of the following strings represent a legal variable name?

(Select two answers.)

  • my Variable 1
  • 1myVariable
  • 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 = 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

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

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

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

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

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

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

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

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

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

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

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here