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

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

0

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main()

{

    float x = 0.9, y=-0.5;

    int i,j = 1;

    i = x + j + y;

    cout << i;

    return 0;

}

  • 2.4
  • Compilation error
  • 1
  • 2

What is the output of the following snippet?

#include <iostream>

#include <string>

#include <exception>

using namespace std;

class a

{

public: virtual string whose()

        { return “mine”; }

};

class b

{

public: virtual string whose()

        { return “yours”; }

};

int main () {

    a b;

    try  { throw b; }

    catch (a& e)  { cout << e.whose() << endl; }

    return 0;

}

  • yours
  • mineyours
  • mine
  • yoursmine

What is the output of this program if the string bar is supplied as input?

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string s1 = “foo”;

    string s2;

    getline(cin,s2);

    cout << s2.append(s1);

    return( 0 );

}

  • barfoo
  • bar
  • foo
  • foobar

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main()

{

        int a = -1, *p = &a;

        cout << ((p == NULL) ? 1.1 : 2.2);

        return 0;

}

  • None of these
  • Compilation error
  • 1.1
  • 2.2

What is the output of the following snippet?

#include <iostream>

using namespace std;

class foo {

        int p;

protected:

        int q;

public:

        int r;

};

class bar : public foo {

public:

        void assign() {

            p = q = r = 2;

        }

        void out() { cout << q << r; }

};

int main () {

        bar b;

        b.assign();

        b.out();

        return 0;

}

  • Compilation fails
  • 20
  • 22
  • 02

What is the output of the following snippet?

#include <iostream>

using namespace std;

#define A    0

#define B    A+1

#define C    1-B

int main() {

    cout << C;

    return 0;

}

  • 2
  • 0
  • 3
  • 1

Which of the following statements are correct about the following array?

char array[255];

  • The expression tab[2] designates the second element in the array.
  • The array can store 255 elements.
  • The expression tab[255] designates the last element in the array.
  • The array may be initialized at the time of declaration

Which code, inserted into the main function, generates the output za

#include <iostream>

using namespace std;

namespace SpaceOne {  char a = ‘a’; }

namespace SpaceTwo {  char a = ‘z’; }

int main () {

    // insert code here

    return 0;

}

  • None of these
  • cout << SpaceTwo::a << SpaceOne::a;
  • cout << SpaceOne::a << SpaceTwo::a;
  • cout << a << a;

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main()

{

    char str[] = “Hello\0World\0”;

    cout << str;

    return 0;

}

  • HelloWorld
  • Compilation fails
  • Hello
  • Hello\0World

What is the output of the following snippet?

#include <iostream>

using namespace std;

int var = -1;

int static Static(int i)

{

    static int y = 0;

    y += ++i;

    return y;

  }

  int main()

  {

    var++;

    Static(var++);

    cout << var << Static(var);

}

  • 13
  • 7
  • 9
  • 11

What is the output of the following snippet?

#include <cstdlib>

#include <iostream>

using namespace std;

char c;

char* inc(char par1, int par2)

{

        c = par1 + par2;

        return &c;

}

int main()

{

        int a = ‘a’, b = 3;

        char *f;

        f = inc(a,b);

        cout << *f;

        return 0;

}

  • c
  • d
  • c
  • a

What is the output of the following snippet?

#include <cstdlib>

#include <iostream>

using namespace std;

char c;

char* inc(char par1, int par2)

{

        c = par1 + par2;

        return &c;

}

int main()

{

        int a = ‘a’, b = 3;

        char *f;

        f = inc(a,b);

        cout << *f;

        return 0;

}

  • c
  • d
  • c
  • a

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main()

{

        char i = ‘1’;

        switch(i)

        {

            case ‘1’:

                cout<<“Hello “;

            case ‘2’:

                cout<<“world “; break;

            case ‘3’:

                cout<<“!”;

        }        return 0;

}

  • Hello
  • Hello world !
  • It prints nothing
  • Hello world

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main()

{

        int val = 0, *adr = &val;

        cout << *val;

        return 0;

}

  • 0
  • It prints address of val
  • 1
  • Compilation fails

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main (int argc, const char *argv[])

{

    int a = 1, b = 1, c = 1, i = 1;

    i = b < a < c;

    cout << i;

    return 0;

}

  • 1
  • Compilation fails
  • 4
  • 2

What is the output of the following snippet?

#include <iostream>

void fun(int *i)

{

        *i = *i >> *i – 1;

}

using namespace std;

int main()

{

        int i = 2;

        fun(&i);

        cout << i;

        return 0;

}

  • 2
  • 4
  • 0
  • 1

How can we pass arguments to functions?

  • By invocation
  • By telepathy
  • None of these
  • By default

What is the output of the following snippet?

#include <iostream>

#include <sstream>

#include <string>

using namespace std;

int main(void)

{

    string s;

    s = “abcd”;

    s.append(s);

    s.resize(s.size() / 2);

    cout << s;

    return 0;

}

  • Compilation fails
  • It prints an empty string
  • abcdabcd
  • abcd

Which code, inserted into function main, generates the output abba? Choose all correct answers.

#include <iostream>

#include <string>

using namespace std;

string fun(string s)

{

        return s.substr(0,1)+s.substr(1,1)+s.substr(1,1)+s.substr(0,1);

}

int main()

{

        string *s = new string(“ab”);

        //insert code here

        return 0;

}

  • cout << fun(*s);
  • cout << fun(s);
  • cout << fun(“abba”);
  • cout << fun(“ab”);

Which code, inserted into the One class, generates the output 123?

#include <iostream>

using namespace std;

class One {

public:

    //insert code here

};

class Two : public One {

public:

        void foo(){ cout << 2; }

};

class Three : public Two {

public:

        void foo(){ cout << 3; }   

};

int main()

{

        One   o1;

        Two   o2;

        Three o3;

        One   *o = &o1;

        o->foo();

        o = &o2; o->foo();

        o = &o3; o->foo();

}

  • static void foo(){ cout << 1; }
  • None of these
  • virtual void foo(){ cout << 1; }
  • void foo(){ cout << 1; }

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main (int argc, const char * argv[])

{

        double dbl = -5.55;

        cout << (int)dbl;

}

  • 0
  • -5.5
  • -6
  • 5

What is the output of the following snippet?

#include <iostream>

using namespace std;

class one { 

public :

    void foo() { cout << 1; } 

};

class two {

public :

    void foo() { cout << 2; }  

};

int main() {

    two objects[2];

    two *object = objects;

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

        (object++)->foo();

    return 0;

}

  • 22
  • 11
  • 21
  • 12

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main ()

{

    int i = 0;

    cout << i;

    { int i = 1; cout << i; }

    { int i = 2; }

    cout << i;

    return 0;

}

  • 012
  • 120
  • None of these
  • 010

What is the output of the following snippet?

#include <iostream>

using namespace std;

int sub(int x, int y)

{

        x -= y;

        return x;

}

int main()

{

        int a = 0, b = 1, c, d;

        c = sub(a,b);

        d = sub(c,b);

        cout << c << d;

        return 0;

}

  • 12
  • -1-2
  • 1-2
  • -12

What is the output of the following snippet?

#include <iostream>

using namespace std;

void foo(int &parameter)

{

            parameter *= 2;

}

int main()

{

        int var = 2;

        foo(var);

        cout << var;

        return 0;

}

  • 8
  • 4
  • 2
  • 1

Which code, inserted into the main function, generate the output a 2?

#include <iostream>

using namespace std;

namespace Space

{  char a = ‘a’, b = ‘b’;  }

int a = 1, b = 2;

int main () {

    // insert code here

    cout << a << ” ” << b;

    return 0;

}

  • using namespace Space::a;
  • using Space::a;
  • None of these
  • using namespace Space;

What is the output of the following snippet?

#include <iostream>

using namespace std;

class Zero

{ public: void out(){ cout << 0;} };

class One : public Zero

{ public: void out(){ cout << 1;} };

class Two : public Zero

{ public: void out(){ cout << 2;} };

int main()

{

    Zero *obj;

    One obj1;

    obj = &obj1;

    obj->out();

    Two obj2;

    obj = &obj2;

    obj->out();

    return 0;

}

  • 00
  • 01
  • 12
  • 02

What is the output of the following snippet?

#include <iostream>

using namespace std;

class C1 {

friend class C2;

protected:  int y;

public:     int z;

private:    int x;

public:     C1() { x = y = z = 11; }

};

class C2 {

public: C1 a;

        C2 () { a.x = 22; };

        void foo() {

            cout << a.x << a.y << a.z;

        }

};

int main()

{

    C2 c;

    c.foo();

    return 0;

}

  • 221111
  • 112211
  • Compilation fails
  • 111122

Which code, inserted into the Class, generates the output abcd?

#include <iostream>

using namespace std;

class Class {

public:

    static char value;

    Class() { value++; };

    ~Class () { value++; };

    //insert code here

    void print() { cout << value;}

};

char Class::value = ‘a’;

int main () {

    Class a,*b;

    b = new Class();

    b->set(‘a’);

    b->print();

    delete b;

    a.print();

    a.set(‘c’);

    a.print();

    a.set();

    a.print();

    return 0;

}

  • void set(char c) { value = c; }
  • void set(char c = ‘d’) { value = c; }
  • void set(char c) { cout << c; }
  • void set() { value = ‘d’; }

Variable c in class B, will be:

class A {

    int a;

protected:

    int b;

public:

    int c;

};

class B : public A {

    float f;

public:

    void foo() {

        cout << f << c;

    }

};

  • protected
  • public
  • none of these
  • private

What is the output of the following snippet?

#include <iostream>

using namespace std;

class One

{

    public:

            void out(){ cout<<“One”;}

};

int main()

{

    One arr[2];

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

            arr[i].out();

}

  • Compilation error
  • One
  • Runtime error
  • OneOn

LEAVE A REPLY

Please enter your comment!
Please enter your name here