Home Exam answers CPA – Programming Essentials in C++ Module 4 Exam Answers

CPA – Programming Essentials in C++ Module 4 Exam Answers

0

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main()

{

    int *it[3];

    for(int i = 0; i < 3; i++) {

        it[i] = new int [i + 1];

        for(int j = 0; j < i + 1; j++)

            it[i][j] = 10 * i + j;

    }

    cout << it[2][2];

    for(int i = 0; i < 3; i++)

        delete [] it[i];

}

  • 11
  • Compilation fails
  • 33
  • 22

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main()

{

    short s = 1;

    int i = 2;

    float f = 4.4;

    double d = 6.6;

    cout << i/static_cast<float>(s) + int(f)/i + (long)d/s;

    return 0;

}

  • 8.0
  • Compilation fails
  • 10
  • 8.8

What is the output of the following snippet?

#include <iostream>

#include <vector>

using namespace std;

int main()

{

    vector<float *> ft = { new float[3], new float[3], new float[3] };

    float *p = nullptr;

    for(int i = 0; i < 3; i++) {

        p = ft[i];

        *p = p[1] = *(p + 2) = 10 * i;

    }

    cout << ft[1][1];

    delete [] ft[0];

    delete [] ft[1];

    delete [] ft[2];

}

  • 10
  • 20
  • Compilation fails
  • 30

What is the output of the following snippet?

#include <iostream>

using namespace std;

namespace S {

    int A = 1;

}

namespace S {

    int B = A + 2 ;

}

int main()

{

    S::A = S::A + 1;

    {

        using namespace S;

        ++B;

    }

    cout << S::B << S::A;

    return 0;

}

  • 22
  • 32
  • 42
  • Compilation fails

What is the output of the following snippet?

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string s = “ABC”;

    for(int i = 1; i < s.length(); i += 2)

        s[i – 1] = s[i] + ‘a’ – ‘A’;

    cout << s;

}

  • bBc
  • bBC
  • Compilation fails
  • aAcC

What is the output of the following snippet?

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string s1 = “aleph”;

    string s2 = “alpha”;

    string s;

    s1.swap(s2);

    s2.swap(s);

    s.swap(s2);

    cout << s2;

}

  • alpha
  • Compilation fails
  • aleph
  • It prints an empty string

What is the output of the following snippet?

#include <iostream>

using namespace std;

    int main()

{

    string s1 = “Ab”;

    string s2 = “Abc”;

    cout << s1.compare(s1);

}

  • Compilation fails
  • 1
  • -1
  • 0

What is the output of the following snippet?

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string s = “AB”;

    s.append(s).push_back(s[s.length() – 1]);

    cout << s;

}

  • ABABA
  • ABAB
  • ABABB
  • Compilation fails

What is the output of the following snippet?

#include <iostream>

#include <string>

using namespace std;

    int main()

{

    string s = “a”;

    cout << s  << “b” + “c”;

}

  • a
  • ab
  • Compilation fails
  • abc

What is the output of the following snippet?

#include <iostream>

#include <string>

using namespace std;

  int main()

{

    string s = “0123”;

    cout << s.substr(1,3).substr(2).substr();

}

  • It prints an empty string
  • 3
  • 123
  • Compilation fails

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;

}

  • 2
  • 2
  • 4
  • Compilation fails

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;

}

  • 2
  • 2
  • 4
  • Compilation fails

What is the output of the following snippet?

#include <iostream>

#include <string>

#include <vector>

using namespace std;

int main()

{

    vector<string> t = { “alpha”, “beta”, “gamma” };

    int *cnt = new int[3], *p = &cnt[0];

    for(int i = 0; i < t.size(); i++)

        *p++ = t[i].length();

    cout << cnt[0] << cnt[1] << cnt[2];

    delete [] cnt;

}

  • 012
  • 545
  • 111
  • Compilation fail

What is the output of the following snippet?

#include <iostream>

using namespace std;

   int main()

{

    short s = 1;

    int i = 2;

    float f = 4.4;

    double d = 8.8;

    cout << s/i + f/i + d/s;

}

  • 11
  • Compilation fails
  • 6.6
  • 4.4

What is the output of the following snippet?

#include <iostream>

using namespace std;

namespace S1 {

    int A = 1;

}

namespace S2  {

    int A = 2 ;

}

int main()

{

    { using namespace S1;

        S2::A = A + 1;

    }

    { using namespace S2;

        S1::A = A + 1;

    }

    cout << S1::A << S2::A;

}

  • Compilation fails
  • 32
  • 33
  • 23

What is the output of the following snippet?

include <iostream>

using namespace std;

    int main()

{

    int i = 2;

    float f = 5.8;

    f = static_cast<int>(f);

    i = static_cast<float>(i);

    cout << f/i;

}

  • 1
  • Compilation fails
  • 2.5
  • 3

What is the output of the following snippet?

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string s = “a”;

    cout << s + “b” + “c”;

}

  • abc
  • ab
  • Compilation fails
  • a

What is the output of the following snippet?

#include <iostream>

#include <string>

using namespace std;

int main() {

    string s1 = “ab”;

    string s2 = “Abc”;

    if(s1 > s2)

        cout << “yes”;

    else

        cout << “NO”;

}

  • Compilation fails
  • yes
  • It prints nothing
  • NO

What is the output of the following snippet?

#include <iostream>

namespace SpaceA {

    int A;

}

namespace SpaceB {

    int A;

}

using namespace SpaceA, SpaceB;

int main()

{

    SpaceA::A = SpaceB::A = 1;

    std::cout << A + 1;

}

  • Compilation fails
  • 1
  • 0
  • 2

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main() {

    string s1 = “1”;

    string s2 = “12”;

    cout << s1.compare(s2);

}

  • 1
  • 0
  • 1
  • Compilation fails

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main()

{

    int i = 2;

    float f = 4.4;

    cout << f % static_cast<float>(i);

}

  • 0
  • 2
  • 0.2
  • Compilation fails

LEAVE A REPLY

Please enter your comment!
Please enter your name here