CPA : C++ Certified Associate Programmer : Part 04

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

    #include <iostream>

    using namespace std;

    int main()
    {
    int i = 5;
    cout<<“Hello World” << ++i;
    return 0;
    }

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

    #include <iostream>

    using namespace std;

    int main()
    {
    int a=5;
    cout << ((a < 5) ? 9.9 : 9);
    }

    • It prints: 9
    • It prints: 9.9
    • Compilation error
    • None of these
  3. What happens when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;
    int main()
    {
    int i=5;
    switch(i)
    {
    case 1:
    cout<<“Hello”;
    break;
    case 2:
    cout<<“world”;
    break;
    case 3:
    break;
    default:
    cout<<“End”;
    }
    return 0;
    }

    • It prints: Hello
    • It prints: world
    • It prints: End
    • It prints: Helloworld
  4. What happens when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;
    int main()
    {
    int *a= new int;
    *a=100;
    cout << *a;
    delete a;
    }

    • It prints: 1
    • It prints: 100
    • It prints: 0
    • It prints: 10
  5. Which of the following operations is INCORRECT?

    • int i=15;
    • long int k=123
    • float f=12,2;
    • double d=12;
  6. Which code, inserted at line 18, generates the output “AB”

    #include <iostream>
    using namespace std;
    class A
    {
    public:
    void Print(){ cout<< “A”;}
    void Print2(){ cout<< “a”;}
    };
    class B:public A
    {
    public:
    void Print(){ cout<< “B”;}
    void Print2(){ cout<< “b”;}
    };
    int main()
    {
    B ob2;
    //insert code here
    ob2.Print();
    }

    • ob2?>A::Print();
    • ob2.B::Print();
    • ob2?>B::Print();
    • ob2.A::Print();
  7. 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, z;
    A() : x(1), y(2), z(0) {}
    A(int a, int b) : x(a), y(b) { z = x * y;}
    void Print() { cout << z; }
    };

    class B : public A {
    public:
    int y;
    B() : A() {}
    B(int a, int b) : A(a,b) {}
    void Print() { cout << z; }
    };

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

    • It prints: 10
    • It prints: 2
    • It prints: 5
    • It prints: 1
  8. What is the output of the program?

    #include <iostream>
    using namespace std;
    #define PRINT(i) cout<<i;
    int main()
    {
    int y=2, z=3;
    PRINT(y);
    PRINT(z);
    return 0;
    }

    • It prints: 123
    • It prints: 23
    • It prints: 3
    • It prints: 2
  9. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;
    int main (int argc, const char * argv[])
    {
    enum state { ok, error, warning};
    enum state s1, s2, s3;
    s1 = ok;
    s2 = warning;
    s3 = error;
    s4 = ok;
    cout << s1<< s2<< s3;
    return 0;
    }

    • It will print:”123”
    • compilation error
    • It will print:”021”
    • It will print:”132”
  10. What will be the output of the program?

    #include <iostream>
    #include <string>
    using namespace std;
    int fun(int);
    int main()
    {
    float k=3;
    k = fun(k);
    cout<<k;
    return 0;
    }
    int fun(int i)
    {
    i++;
    return i;
    }

    • 3
    • 5
    • 4
    • 5
  11. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;
    #define A 1
    int main()
    {
    #if A
    cout<<“Hello”;
    #endif
    cout<<“world”;
    return 0;
    }

    • It will print: Helloworld
    • It will print: Hello
    • It will print: world
    • It will print: 0
  12. What happens when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;
    int main()
    {
    int i = 4;
    while(i >= 0) {
    cout<<i;
    i;
    }
    return 0;
    }

    • It prints:”43210”
    • It prints:”3210”
    • It prints: ”3210?1”
    • None of these
  13. What will variable “y” be in class B?

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

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

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

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    string s1[]= {“How” , “to” };

    s1[0].swap(s1[1]);
    for (int i=0; i<2; i++) {
    cout << s1[i];
    }
    return( 0 );
    }

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

    #include <iostream>
    using namespace std;
    int main()
    {
    const char *s;
    char str[] = “Hello”;
    s = str;
    while(*s) {
    cout << *s++;
    }

    return 0;
    }

    • It prints: el
    • It prints: Hello
    • It prints: H
    • It prints: o
  16. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>

    using namespace std;
    class complex{
    double re;
    double im;
    public:
    complex() : re(1),im(0.4) {}
    bool operator==(complex &t);
    };

    bool complex::operator == (complex &t){
    if((this?>re == t.re) && (this?>im == t.im))
    return true;
    else
    return false;
    }

    int main(){
    complex c1,c2;
    if (c1==c2)
    cout << “OK”;
    else {
    cout << “ERROR”;
    }
    }

    • It prints: OK
    • It prints: ERROR
    • Compilation error
    • Runtime error.
  17. What happens when you attempt to compile and run the following code?

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

    class A {
    public:
    A() { cout << “A no parameters”;}
    A(string s) { cout << “A string parameter”;}
    A(A &a) { cout << “A object A parameter”;}
    };

    class B : public A {
    public:
    B() { cout << “B no parameters”;}
    B(string s) { cout << “B string parameter”;}
    };

    int main () {
    A a1;
    A a2(“Test”);
    B b1(“Alan”);
    return 0;

    }

    • It prints: A no parametersA string parameterA no parametersB string parameter
    • It prints: A no parametersB string parameter
    • It prints: B string parameter
    • It prints: B no parameter
  18. What happens when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;
    int main (int argc, const char * argv[])
    {
    int tab[5]={1,2,3};
    for (int i=0; i<5; i++)
    cout <<tab[i];
    return 0;
    }

    • compilation fails
    • It prints: 12300
    • It prints: 12345
    • It prints: 00000

  19. 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 C:public A {
    public:
    virtual void Print()=0;
    };
    int main()
    {
    C obj3;
    obj3?>Print();
    }

    • It prints: BB
    • It prints: A
    • It prints: AB
    • Compilation error
  20. 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 :
    void print() {
    cout << “B “;
    }
    };
    int main() {
    B sc[2];
    B *bc = (B*)sc;
    for (int i=0; i<2;i++)
    (bc++)->print();
    return 0;
    }

    • It prints: A A
    • It prints: B B
    • It prints: A B
    • It prints: B A
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments