CPA : C++ Certified Associate Programmer : Part 11

  1. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    class A
    {
    public:
    void Print(){ cout<<“A”;}
    };
    class B:public A
    {
    public:
    void Print(){ cout<< “B”;}
    };
    int main()
    {
    A *obj;
    A ob1;
    obj = &ob1;
    obj?>Print();
    B ob2;
    obj = &ob2;
    obj?>Print();
    }

    • It prints: AB
    • It prints: AA
    • It prints: BA
    • It prints: BB
  2. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>
    using namespace std;

    class A {
    int x;
    protected:
    int y;
    public:
    int z;
    };

    class B : public A {
    string name;
    public:
    void set() {
    y = 2;
    z = 3;
    }
    void Print() { cout << y << z; }
    };
    int main () {
    B b;
    b.set();
    b.Print();
    return 0;
    }

    • It prints: 123
    • It prints: 000
    • It prints: 23
    • It prints: 12
  3. Which of the following can be checked in a switch?case statement?

    • char
    • int
    • enum
    • double
  4. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    int main()
    {
    int x=2, *y;
    y = &x;
    cout << *y + x;
    return 0;
    }

    • It prints: 1
    • It prints: 2
    • It prints: 4
    • It prints: 0
  5. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    void fun(int*);

    int main()
    {
    int i=2;
    fun(&i);
    cout<<i;
    return 0;
    }

    void fun(int *i)
    {
    *i = *i**i;
    }

    • It prints: 1
    • It prints: 4
    • It prints: 10
    • It prints: 0
  6. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>
    using namespace std;

    class A {
    protected:
    int y;
    public:
    int x;
    int z;
    A() { x=2; y=2; z=3; }
    A(int a, int b) : x(a), y(b) { z = x ? y;}
    void Print() {
    cout << z;
    }
    };

    int main () {
    A a(2,5);
    a.Print();
    return 0;
    }

    • It prints: ?3
    • It prints: 2
    • It prints: 6
    • It prints: 5
  7. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    class BaseClass
    {
    public:
    int *ptr;
    BaseClass(int i) { ptr = new int(i); }
    ~BaseClass() { delete ptr; delete ptr;}
    void Print() { cout << *ptr; }
    };
    void fun(BaseClass x);

    int main()
    {
    BaseClass o(10);
    fun(o);
    o.Print();
    }

    void fun(BaseClass x) {
    cout << “Hello:”;
    }

    • It prints: Hello:1
    • It prints: Hello:
    • It prints: 10
    • Runtime error.
  8. What happens when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;
    class complex{
    double re;
    double im;
    public:
    complex() : re(0),im(0) {}
    complex(double x) { re=x,im=x;};
    complex(double x,double y) { re=x,im=y;}
    void print() { cout << re << ” ” << im;}
    };

    int main(){
    complex c1;
    double i=2;
    c1 = i;
    c1.print();
    return 0;
    }

    • It prints: 0 0
    • It prints: 1 1
    • It prints: 2 0
    • It prints: 2 2
  9. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    int main()
    {
    int i=2;
    switch(i)
    {
    case 1:
    cout<<“Hello”;
    case 2:
    cout<<“world”;
    case 3:
    cout<<“End”;
    } return 0;
    }

    • It prints: Hello
    • It prints: world
    • It prints: worldEnd
    • It prints: End
  10. Point out an error in the program.

    #include <iostream>
    using namespace std;
    int main()
    {
    const int x=1;
    int const *y=&x;
    cout<<*y;
    return 0;
    }

    • No error
    • Error: unknown pointer conversion
    • cannot convert from ‘const int *’ to ‘int *const’
    • Compilation error
  11. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    class BaseC
    {
    int *ptr;
    public:
    BaseC() { ptr = new int(10);}
    BaseC(int i) { ptr = new int(i); }
    ~BaseC() { delete ptr; }
    void Print() { cout << *ptr; }
    };

    int main()
    {
    BaseC *o = new BaseC(5);
    o?>Print();
    }

    • It prints: 5
    • It prints: 10
    • It prints: 1
    • It prints: 0
  12. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    class Test {
    float i,j;
    };

    class Add {
    public:
    int x,y;
    Add (int a=3, int b=3) { x=a; y=b; }
    int result() { return x+y;}
    };

    int main () {
    Test test;
    Add * padd;
    padd = &test;
    cout << padd?>result();
    return 0;
    }

    • It prints: 6
    • It prints: 9
    • Compilation error
    • It prints: 33
  13. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>

    using namespace std;

    int f(int i, int b);

    int main()
    {
    int i=0;
    i++;
    for (i=0; i<=2; i++)
    {
    cout<<f(0,i);
    }
    return 0;
    }

    int f(int a, int b)
    {
    return a+b;
    }

    • It prints: 202020
    • It prints: 012
    • It prints: 0
    • It prints: 2
  14. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    void set(struct person*);

    struct person
    {
    int age;
    };

    int main()
    {
    struct person e = {18};
    set(&e);
    cout<< e.age;
    return 0;
    }

    void set(struct person *p)
    {
    p?>age = p?>age + 1;
    }

    • It prints: 18
    • It prints: 19
    • It prints: 20
    • It prints: 0
  15. What is the output of the program?

    #include <iostream>
    #include <string>

    using namespace std;

    struct Person {
    int age;
    };

    class First
    {
    Person *person;
    public:
    First() {person = new Person;
    person?>age = 20;
    }
    void Print(){
    cout << person?>age;
    }
    };

    int main()
    {
    First t[2];
    for (int i=0; i<2; i++)
    t[i].Print();
    }

    • It prints: 10
    • It prints: 2020
    • It prints: 22
    • It prints: 00
  16. What happens when you attempt to compile and run the following code?#include <iostream>

    #include <string>

    using namespace std;

    class A {

    public:

    A() { cout << “A0 “;}

    A(string s) { cout << “A1”;}

    };

    class B : public A {

    public:

    B() { cout << “B0 “;}

    B(string s) { cout << “B1 “;}

    };

    class C : private B {

    public:

    C() { cout << “C0 “;}

    C(string s) { cout << “C1 “;}

    };

    int main () {

    B b1;

    C c1;

    return 0;

    }

    • It prints: A0 B0 A0 B1 A0 C0 A0 C1
    • It prints: B0 B1 C0 C1
    • It prints: A0 B0 A0 B0 C0
    • It prints: B0 B1
  17. What will happen when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    int getValue();

    int main()
    {
    const int x = getValue();
    cout<<x;
    return 0;
    }
    int getValue()
    {
    return 5;
    }

    • It will print 0
    • The code will not compile.
    • It will print 5
    • It will print garbage value
  18. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    #define DEF_A 0
    #define DEF_B DEF_A+1
    #define DEF_C DEF_B+1

    int main(int argc, char *argv[]) {
    cout << DEF_C;
    return 0;
    }

    • It prints: 2
    • It prints: 10
    • It prints: 0
    • It prints: 1
  19. What is the output of the program?

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    string s1=”Hello”;
    string s2=”World”;
    s1+=s2;
    cout << s1;
    return( 0 );
    }

    • It prints: HelloWorld
    • It prints: Hello
    • It prints: World
    • It prints: HelWorld
  20. What happens when you attempt to compile and run the following code?

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    inline float sum(float a,float b)
    {
    return a+b;
    }

    int main()
    {
    float a,b;
    a = 1.5; b = 3.4;
    cout<<sum(a,b);
    return 0;
    }

    • It prints: 0
    • It prints: 4.9
    • It prints: 5
    • It prints: 4
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments