CPA : C++ Certified Associate Programmer : Part 02

  1. What is the output of the program?

    #include <iostream>
    #include <string>

    using namespace std;

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

    • It prints: HelloWorld

    • It prints: Hello
    • It prints: World
    • Compilation error
  2. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    class A
    {
    public:
    virtual void Print(){ cout<<“A”;}
    };
    class B:public A
    {
    public:
    virtual 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
  3. If there is one, point out an error in the program

    #include <iostream>

    using namespace std;

    int main()
    {
    int i=1;
    for(;;)
    {
    cout<<i++;
    if(i>5)
    break;
    }
    return 0;
    }

    • Error in “if” statement
    • Error in “for” loop
    • No error
    • Error in break statement
  4. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    int f(int a, int b);

    int main()
    {
    float b;
    b = f(20,10);
    cout << b;
    return 0;
    }

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

    • It prints: 2
    • It prints: 5
    • It prints: 10
    • It prints: 0
  5. 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=1; 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: 10
    • It prints: 2
    • It prints: 6
    • It prints: 5
  6. What happens if you try to compile and run this program?

    #include <iostream>

    using namespace std;

    int main (int argc, const char * argv[])
    {
    print(“Test”);
    return 0;
    }
    void print(int c[])
    {
    cout<<c;
    }

    • It prints: Test
    • Compilation fails
    • Program terminates abnormally
    • None of these
  7. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    void set(struct person*);
    struct person
    {
    char name[25];
    int age;
    };
    int main()
    {
    struct person e = {“Steve”, 30};
    set(&e);
    cout<< e.name << ” ” << e.age;
    return 0;
    }
    void set(struct person *p)
    {
    p?>age = p?>age + 1;
    }

    • Error: in prototype declaration unknown struct person
    • Error: in structure
    • It prints: Steve 31
    • None of these
  8. What is the output of the program given below?

    #include <iostream>

    using namespace std;

    int main (int argc, const char * argv[])
    {
    float f=?10.501;
    cout<<(int)f;
    }

    • 0
    • 11
    • ?10
    • ?11
  9. 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 : private A {
    string name;
    public:
    void set() {
    x = 1;
    }
    void Print() {
    cout << x;
    }
    };

    int main () {
    B b;
    b.set();
    b.Print();
    return 0;
    }

    • It prints: 123
    • It prints: 1
    • It prints: ?123
    • Compilation error
  10. What is the output of the program?

    #include <iostream>
    using namespace std;

    class Base {
    static int age;
    public:
    Base () {};
    ~Base () {};
    void setAge(int a=10) {age = a;}
    void Print() { cout << age;}
    };

    int Base::age=0;

    int main () {
    Base a,*b;
    b = new Base();
    a.setAge();
    b?>setAge(20);
    a.Print();
    b?>Print();
    return 0;
    }

    • It prints: 2020
    • It prints: 1020
    • It prints: 20
    • It prints: 10
  11. 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:
    virtual void Print(){ cout<< “B”;}
    };
    class C:public B {
    public:
    void Print(){ cout<< “C”;}
    };
    int main()
    {
    A ob1;
    B ob2;
    C ob3;
    B *obj;
    obj = &ob2;
    obj?>Print();
    obj = &ob3;
    obj?>Print();
    }

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

    #include <iostream>
    using namespace std;

    class Base {
    int age;
    public:
    class C {
    int b;
    void PrintC() { cout << b; }
    };
    Base () {age=5;};
    void setAge(int a=20) {age = a;}
    void Print() { cout << age;}
    };

    int main () {
    Base a;
    a.setAge(10);
    a.Print();
    return 0;
    }

    • It prints: 1020
    • It prints: 105
    • It prints: 10
    • It prints: 20
  13. What will be the output of the program?

    #include <iostream>

    using namespace std;

    int main()
    {
    int i=0;
    for(; i<=5; i++)
    cout << i;
    return 0;
    }

    • 012345
    • 0123
    • 5
    • 6
  14. What is the output of the program?

    #include <iostream>

    using namespace std;

    #define SQR(x)(x*x)

    int main(int argc, char *argv[]) {
    int x, y=2;

    x = SQR(y);
    cout << x << “, ” <<y;
    return 0;
    }

    • It prints: 3, 2
    • It prints: 4, 2
    • It prints: 3, 3
    • It prints: 9, 2
  15. What happens when you attempt to compile and run the following code?

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

    const int size = 3;
    class A {
    public:
    string name;
    A() { name = “Bob”;}
    A(string s) { name = s;}
    A(A &a) { name = a.name;}
    };
    class B : public A {
    public:
    int *tab;
    B() { tab = new int[size]; for (int i=0; i<size; i++) tab[i]=1;}
    B(string s) : A(s) { tab = new int[size]; for (int i=0; i<size; i++) tab[i]=1;}
    ~B() { delete tab; }
    void Print() {
    for (int i=0; i<size; i++) cout << tab[i];
    cout << name;
    }
    };
    int main () {
    B b1(“Alan”);
    B b2;
    b1.tab[0]=0;
    b1.Print(); b2.Print();
    return 0;
    }

    • It prints: Alan
    • It prints: 111
    • It prints: 011Alan111Bob
    • It prints: 0
  16. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>

    using namespace std;

    class complex{
    double re, im;
    public:
    complex() : re(1),im(0.3) {}
    complex(double n) { re=n,im=n;};
    complex(int m,int n) { re=m,im=n;}
    complex operator+(complex &t);
    void Print() { cout << re << ” ” << im; }

    };

    complex complex::operator+ (complex &t){
    complex temp;
    temp.re = this?>re + t.re;
    temp.im = this?>im + t.im;
    return temp;
    }

    int main(){
    complex c1(1),c2(2),c3;
    c3 = c1 + c2;
    c3.Print();
    }

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

    #include <iostream>

    using namespace std;

    void fun(int*);

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

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

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

    #include <iostream>

    using namespace std;

    class First
    {
    public:
    void Print(){ cout<<“from First”;}
    };
    class Second
    {
    public:
    void Print(){ cout<< “from Second”;}
    };
    int main()
    {
    Second t[2];
    for (int i=0; i<2; i++)
    t[i].Print();
    }

    • It prints: from First
    • It prints: from Firstfrom First
    • It prints: from Secondfrom Second
    • It prints: from Second
  19. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    int main()
    {
    int x,y=10;
    float f;
    f = 5.90;
    cout << f << “, “;
    x=f;
    cout << x <<“, “;
    f=y;
    cout << f;
    return 0;
    }

    • It prints: 5, 5, 10.00
    • It prints: 5.9, 5, 10
    • It prints: 6, 5, 10
    • It prints: 6, 5, 10.00
  20. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    int compare(int, int);

    int main()
    {
    int x = compare(10, 20);
    cout << x;
    return 0;
    }

    int compare(int i, int j)
    {
    return i<j;
    }

    • It prints: 0
    • It prints: 2
    • It prints: 1
    • It prints: 10
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments