CPA : C++ Certified Associate Programmer : Part 01

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

     #include <iostream>

    using namespace std;

    int main()
    {
    union un
    {
    int x;
    char c;
    };
    union un u1 = {10};
    union un u2 = {‘a’};
    union un u3 = {20, ‘a’};
    cout<<u1.x;
    cout<<u2.c;
    cout<<u3.c;
    return 0;
    }

    • It prints: 10aa
    • It prints: 10a20a
    • It prints: 1a
    • Compilation error
  2. What happens when you attempt to compile and run the following code?

     #include <iostream>

    using namespace std;

    int min(int a, int b);

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

    int min(int a, int b)
    {
    return(b);
    }

    • It prints: 20
    • It prints: 10
    • It prints: 1020
    • It prints: 2010
  3. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    class First
    {
    public:
    virtual void Print(){ cout<<“from First”;}
    };
    class Second:public First
    {
    public:
    void Print(){ cout<< “from Second”;}
    };
    void fun(First *obj);
    int main()
    {
    First FirstObject;
    fun(&FirstObject);
    Second SecondObject;
    fun(&SecondObject);
    }
    void fun(First *obj)
    {
    obj?>Print();
    }

    • It prints: from First
    • It prints: from Firstfrom First
    • It prints: from Firstfrom Second
    • It prints: from Secondfrom Second
  4. Which code, inserted at line 10, generates the output “2?1”?

     #include <iostream>
    #include <string>
    using namespace std;
    class A {
    protected:
    int y;
    public:
    int z;
    };
    //insert code here
    public:
    void set() {
    y = 2;
    z = 3;
    }
    void Print() { cout << y << z; }
    };

    int main () {
    B b;
    b.set();
    b.z = ?1;
    b.Print();
    return 0;
    }

    • class B : private A {
    • class B : public A {
    • class B : protected A {
    • class B {
  5. What happens when you attempt to compile and run the following code?

     #include <iostream>

    using namespace std;

    int op(int x, int y);
    float op(int x, float y);

    int main()
    {
    int i=1, j=2, k;
    float f=0.3;
    k = op(i, j);
    cout<< k << “,” << op(0, f);
    return 0;
    }

    int op(int x, int y)
    {
    return x+y;
    }

    float op(int x, float y)
    {
    return x?y;
    }

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

     #include <iostream>
    using namespace std;

    int main() {
    int i, j;
    for(i = 0; i < 2; i++) {
    for(j = i; j < i + 1; j++)
    if(j == i)
    continue;
    else
    break;
    }
    cout << j;
    return 0;
    }

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

     #include <iostream>

    using namespace std;

    class BaseC
    {
    public:
    int *ptr;
    BaseC() { ptr = new int(10);}
    BaseC(int i) { ptr = new int(i); }
    ~BaseC() { delete ptr; }
    };
    void fun(BaseC x);

    int main()
    {
    BaseC *o = new BaseC(5);
    fun(*o);
    }

    void fun(BaseC x) {
    cout << “Hello:”<<*x.ptr;
    }

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

     #include <iostream>

    using namespace std;

    int op(int x, int y);

    int main()
    {
    int i=2, j=2, k;
    float f=0.3;
    k = op(i, j);
    cout<< k << “,” << op(1, f);
    return 0;
    }

    int op(int x, int y)
    {
    return x+y;
    }

    • It prints: 4,1
    • It prints: 4,0.7
    • It prints: 4,0
    • It prints: 0,4
  9. How many times will “HELLO” be printed?

    #include <iostream>

    using namespace std;

    int main()
    {
    for(int i=?1; i<=10; i++)
    {
    if(i < 5)
    continue;
    else
    break;
    cout<<“HELLO”;
    }
    return 0;
    }

    • 1
    • 2
    • 0
    • 20
  10. What is the output of the program?

     #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    string s1[]= {“H” , “t” };
    string s;

    for (int i=0; i<2; i++) {
    s = s1[i];
    if (i==0)
    s.insert(1,”ow”);
    else
    s.push_back(‘o’);
    cout << s;
    }
    return( 0 );
    }

    • It prints: Hoto
    • It prints: Ht
    • It prints: toHo
    • It prints: Howto
  11. What will be the output of the program?

     #include <iostream>

    using namespace std;

    int main()
    {
    const int y = 5;
    const x = ?10;
    cout<<x<<” “<<y;
    return 0;
    }

    • ?10 5
    • 5 ?10
    • Compilation error
    • None of these
  12. Which code, inserted at line 12, generates the output “5b”?

     #include <iostream>
    using namespace std;
    namespace myNamespace1
    {
    int var = 5;
    }
    namespace myNamespace2
    {
    char var = ‘b’;
    }
    int main () {
    //insert code here
    return 0;
    }

    • cout << myNamespace1::var << var;
    • cout << var << var;
    • cout << myNamespace1::var << myNamespace2::var;
    • None of these
  13. What happens when you attempt to compile and run the following code?

     #include <iostream>
    using namespace std;

    int main(){
    int i = 1;
    if (i++==1) {
    cout << i;
    } else {
    cout << i-1;
    }
    return 0;
    }

    • It prints: 0
    • It prints: 1
    • It prints: -1
    • It prints: 2
  14. 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 First
    {
    public:
    void Print(){ cout<< “from Second”;}
    };
    void fun(First *obj);
    int main()
    {
    First FirstObject;
    fun(&FirstObject);
    Second SecondObject;
    fun(&SecondObject);
    }
    void fun(First *obj)
    {
    obj?>Print();
    }

    • It prints: from First
    • It prints: from Firstfrom First
    • It prints: from Firstfrom Second
    • It prints: from Secondfrom Second
  15. What will the variable “y” be in class B?

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

    class B : protected A {
    string name;
    public:
    void Print() {
    cout << name << age;
    }
    };

    • public
    • private
    • protected
    • None of these
  16. What happens when you attempt to compile and run the following code?

     #include <iostream>

    using namespace std;

    int op(int x, int y)
    {
    int i;
    i = x + y;
    return i;
    }

    int main()
    {
    int i=1, j=2, k, l;
    k = op(i, j);
    l = op(j, i);
    cout<< k << “,” << l;
    return 0;
    }

    • It prints: 1,2
    • It prints: ?1,1
    • It prints: 1,1
    • It prints: 3,3
  17. What is the output of the program?

     #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    string s1[]= {“H” , “t” };
    string s;

    for (int i=0; i<2; i++) {
    s = s1[i];
    s.insert(1,”ow”);
    cout << s;
    }
    return( 0 );
    }

    • It prints: How
    • It prints: Ht
    • It prints: Hoto
    • It prints: Howtow
  18. What happens when you attempt to compile and run the following code?

     #include <iostream>
    using namespace std;

    class A {
    public:
    int x;
    A() { x=0;}
    };

    class B : protected A {
    public:
    int y;
    using A::x;
    B(int y) {this?>y = y;}
    void Print() { cout << x << y; }
    };

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

    • It prints: 05
    • It prints: 0
    • It prints: 5
    • It prints: 15
  19. What happens when you attempt to compile and run the following code?

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

    class Second;

    class Base {
    int age;
    public:
    Base () { age=5; };
    friend void set(Base &ob, Second &so);
    void Print() { cout << age;}
    };
    class Second {
    string name;
    public:
    friend void set(Base &ob, Second &so);
    void Print() { cout << name;}
    };

    void set(Base &ob, Second &so) {
    ob.age = 0; so.name = “Bill”;
    }
    int main () {
    Base a;
    Second b;
    set(a,b);
    a.Print();
    b.Print();
    return 0;
    }

    • It prints: 0Bill
    • Compilation error
    • It prints: Bill0
    • None of these
  20. Which of the following structures are correct?

    1:
    struct s1{
    int x;
    char c;
    };

    2:
    struct s2{
    float f;
    struct s2 *s;
    };

    3:
    struct s3{
    float f;
    in i;
    }

    • 1
    • 2
    • 3
    • All of these
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments