CPA : C++ Certified Associate Programmer : Part 03

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

    #include <iostream>

    using namespace std;

    int main()
    {
    int i = 0;
    do {
    i++;
    if (i==3)
    break;
    cout<<i;
    }
    while(i < 5);
    return 0;
    }

    • It prints: 12
    • It prints: 1
    • It prints: 0
    • No output
  2. 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;
    c1.print();
    return 0;
    }

    • It prints: 1 0
    • It prints: 1 1
    • It prints: 0 0
    • Compilation error
  3. What is not inherited from the base class?

    • constructor
    • destructor
    • operator=()
    • operator+()
  4. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>

    using namespace std;

    string fun(string, string);

    int main()
    {
    string s=”Hello”;
    cout << fun(s, ” World”);
    return 0;
    }

    string fun(string s1, string s2)
    {
    return s1+s2;
    }

    • It will print: Hello World
    • It will print: Hello
    • It will print: World
    • It will print: HW
  5. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    int main()
    {
    int i = 5;
    do {
    i??;
    cout<<i;
    }
    while(i >= 0);
    return 0;
    }

    • It prints: 43210?1
    • It prints: ?1
    • It prints: 4321
    • It prints: 1
  6. What is the output of the program?

    #include <iostream>
    #include <string>

    using namespace std;

    class First
    {
    string name;
    public:
    First() {
    name = “Alan”;
    }
    void setName(string n) {this?>name = n;}
    void setName() {this?>name = “John”;}
    void Print(){
    cout << name;
    }
    };

    int main()
    {
    First ob1,*ob2;
    ob2 = new First();
    First *t;
    t = &ob1;
    t?>setName();
    t?>Print();
    t = ob2;
    t?>setName(“Steve”);
    ob2?>Print();
    }

    • It prints: JohnSteve
    • It prints: AlanAlan
    • It prints: AlanSteve
    • It prints: JohnAlan
  7. What is the output of the program?

    #include <iostream>
    #include <string>

    using namespace std;

    union t
    {
    char c;
    int i;
    };

    class First
    {
    union t u;
    public:
    First() {
    u.c = ‘A’;
    }
    void Print(){
    cout << u.c;
    }
    };

    int main()
    {
    First *t = new First();
    t?>Print();
    }

    • Garbage value
    • It prints: A
    • It prints: A 65
    • Compilation error
  8. How many times will the program print “HELLO” ?

    #include <iostream>

    using namespace std;

    int main()
    {
    cout<<“HELLO”;
    main();
    return 0;
    }

    • 65536
    • 32769
    • 1
    • Till stack overflows
  9. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>

    using namespace std;

    void fun(int i);

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

    void fun(int i)
    {
    if (i==3)
    return;
    cout << i;
    }

    • It prints: 05
    • It prints: 012345
    • It prints: 01245
    • It prints: 0
  10. If there is one, point out an error in the program

    #include <iostream>

    using namespace std;

    int main()
    {
    int c = ‘a’;
    switch(i)
    {
    case ‘2’:
    cout<<“OK”;
    case ‘1’:
    cout<<“Error”;
    default:
    break;
    }
    return 0;
    }

    • No Error
    • Use of undeclared identifier ‘i’
    • Illegal use of ‘continue’
    • Illegal use of ‘break’
  11. What is the output of the program?

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    char str[] = “Hello\0\World\0”;
    cout << str;
    return 0;
    }

    • It prints: Hello
    • It prints: World
    • It prints: HW
    • It prints: World\0World
  12. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    int main()
    {
    int i = 0;
    i++;
    goto lab;
    i++;
    lab:
    cout<<i;
    return 0;
    }

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

    #include <iostream>

    using namespace std;

    class First
    {
    public:
    First() { cout << “Constructor”;}
    void Print(){ cout<<“from First”;}
    };
    int main()
    {
    First FirstObject;
    FirstObject.Print();
    }

    • It prints: Constructorfrom First
    • It prints: Constructor
    • It prints: from First
    • None of these
  14. Which code, inserted at line 10, generate the output “50”?

    #include <iostream>
    using namespace std;

    class Base {
    int age;
    public:
    Base () {
    age=5;
    };
    //insert code here
    void Print() { cout << age;}
    };

    void setAge(Base &ob) {ob.age = 0;}

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

    • friend void setAge(Base ob);
    • friend void setAge(Base *ob);
    • friend void setAge(Base &ob);
    • None of these
  15. 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;
    A() { x=1; y=2; z=3; }
    };
    class B : public A {
    string z;
    public:
    void set() { y = 4; z = “John”; }
    void Print() { cout << y << A::z; }
    };

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

    • It prints: 4John
    • It prints: 2John
    • It prints: 23
    • It prints: 43
  16. What happens when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;

    int fun(int x) {
    return 2*x;
    }

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

    • It prints: 0
    • It prints: 1
    • It prints: -1
    • Compilation error
  17. Which code, inserted at line 8, generates the output “100”?

    #include <iostream>
    using namespace std;
    int fun(int);
    int main()
    {
    int *x = new int;
    *x=10;
    //insert code here
    return 0;
    }

    int fun(int i)
    {
    return i*i;
    }

    • cout << fun(*x) ;
    • cout << fun(10);
    • cout << fun(5) ;
    • cout << fun(y) ;
  18. Which definitions are correct?

    • int age;
    • int double;
    • char c;
    • int char;
  19. Which of the following is a user defined data type?

    1:

    struct person

    {

    char name[20];

    int age;

    };

    2:

    int l=2;

    3:

    enum color {red,blue, green};

    D.

    char c;

    • 1
    • 2
    • 3
    • 4
  20. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    class First
    {
    public:
    First() { cout << “Constructor”;}
    ~First() { cout << “Destructor”;}
    void Print(){ cout<<“from First”;}
    };
    int main()
    {
    First FirstObject;
    FirstObject.Print();
    }

    • It prints: Constructorfrom First
    • It prints: Constructorfrom FirstDestructor
    • It prints: Constructorfrom FirstDestructorDestructor
    • Compilation error at line 16
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments