CPA – Programming Essentials in C++ Chapter 1 Module 2

Question 1: What is the output of the following snippet?

double big = 1e15;

double small = 1e-15;

cout << fixed << big + small;

Answer:

1000000000000000.000000

 

Question 2:

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;

}

Answer:

0

 

Question 3:

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;

}

Answer:

5

 

Question 4:

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

        }

}

Answer:

**

 

Question 5:

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

}

Answer:

Y11


Question 6:

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

}

Answer:

12

 

Question 7:

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;

}


Answer:

21

 

Question 8:

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

    }

}

Answer:

**

 

Question 9:

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

}


Answer:

True

 

Question 10:

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;

}


Answer:

4

 

Question 11:

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;

}

Answer:

0

 

Question 12:

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;

}

Answer:

113

 

Question 13:

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;

}

Answer:

113


Question 14:

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;

}

Answer:

12

 

Question 15:

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

}

Answer:

***

 

Question 16:

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;

}

Answer:

4

 

Question 17:

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

}

Answer:

*

 

Question 18:

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;

}

Answer:

2


Question 19:

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;

}


Answer:

0

 

Question 20:

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;

}


Answer:

4

 

Question 21:

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

}

Answer:

23

Post a Comment

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