CPA : C++ Certified Associate Programmer : Part 10

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

    #include <iostream>
    using namespace std;

    int main() {
    float i = 1.0 / 2 * 2 / 1 * 2 / 4 * 4 / 2;
    cout << i;
    return 0;
    }

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

    #include <iostream>
    using namespace std;

    namespace myNamespace1
    {
    int x = 5;
    int y = 10;
    }
    namespace myNamespace2
    {
    float x = 3.14;
    float y = 1.5;
    }
    int main () {
    {
    using namespace myNamespace1;
    cout << x << ” “;
    }{
    using namespace myNamespace2;
    cout << y;
    }
    return 0;
    }

    • It prints: 5 1.5
    • It prints: 3.14 10
    • Compilation error
    • None of these
  3. What is the output of the program given below?

    #include <iostream>

    using namespace std;

    int main (int argc, const char * argv[])
    {
    int i=10;
    {
    int i=0;
    cout<<i;
    }
    {
    int i=5;
    cout << i;
    }
    cout<<i;
    return 0;
    }

    • 1010
    • 101010
    • 0510
    • None of these
  4. What happens when you attempt to compile and run the following code?

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

    • It prints: 20
    • It prints: 10
    • Compilation error at line 8
    • It prints address of ptr
  5. What will the variable “age” 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
  6. What happens when you attempt to compile and run the following code?

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

    class A {
    public:
    int x;
    A() { x=0;}
    };
    class B {
    public:
    int x;
    B() { x=1;}
    };
    class C :public A, public B {
    public:
    int x;
    C(int x) {
    this?>x = x;
    A::x = x + 1;
    }
    void Print() { cout << x << A::x << B::x; }
    };
    int main () {
    C c2(1);
    c2.Print();
    return 0;
    }

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

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

    class myClass : public exception
    {
    virtual const char* what() const throw()
    {
    return “My exception.”;
    }
    } obj;

    int main () {
    try
    {
    throw obj;
    }
    catch (exception& e)
    {
    cout << e.what() << endl;
    }
    return 0;
    }

    • It prints: My exception.
    • It prints: 0
    • It prints: 1
    • Compilation error
  8. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    #define DEF_A 0

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

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

    #include <iostream>

    using namespace std;

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

    • It prints: 36
    • It prints: 14
    • It prints: 16
    • Compilation error
  10. What happens when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;

    int main(){
    int i = 1;
    for(i=10; i>-1; i/=2) {
    if(!i)
    break;
    }
    cout << i;
    return 0;
    }

    • It prints: 0
    • It prints: 1
    • It prints: -1
    • Compilation error
  11. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    int main()
    {
    const int x=0;
    const int *ptr;
    ptr = &x;
    cout<<*ptr;
    return 0;
    }

    • It prints: 0
    • It prints address of x
    • It prints: 1
    • Compilation error
  12. 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”;}
    };
    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
  13. What will happen 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;
    *s++;
    }

    return 0;
    }

    • It will print:”el “
    • The code will not compile.
    • It will print:”Hello “
    • It will print garbage value
  14. How could you pass arguments to functions?

    • by value
    • by reference
    • by pointer
    • by void
  15. Which of the following is a logical operator?

    • &
    • &&
    • ||
    • !
  16. Which of the following statements are correct about an array?

    int tab[10];

    • The array can store 10 elements.
    • The expression tab[1] designates the very first element in the array.
    • The expression tab[9] designates the last element in the array.
    • It is necessary to initialize the array at the time of declaration.
  17. Which code, inserted at line 5, generates the output “ABC”?

    #include <iostream>
    using namespace std;
    class A {
    public:
    //insert code here
    };
    class B:public A {
    public:
    void Print(){ cout<< “B”; }
    };
    class C:public B {
    public:
    void Print(){ cout<< “C”; }
    };
    int main()
    {
    A ob1;
    B ob2;
    C ob3;
    A *obj;
    obj = &ob1;
    obj?>Print();
    obj = &ob2;
    obj?>Print();
    obj = &ob3;
    obj?>Print();
    }

    • void Print(){ cout<<“A”;}
    • virtual void Print(){ cout<<“A”;}
    • virtual void Print(string s){ cout<<s;}
    • None of these
  18. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <cstdarg>

    using namespace std;

    int mult(int f, int s, int t);

    int main()
    {
    cout << mult(1,2,3);
    return 0;
    }

    int mult(int f, int s, int t)
    {
    int mult_res;
    mult_res = f*s*t;
    return mult_res;
    }

    • It prints: 0
    • It prints: 6
    • It prints: 2
    • It prints: 3
  19. 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, j = 1; j < 2, i < 4; i++, j++);
    cout << i << ” ” << j;
    return 0;
    }

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

    #include <iostream>
    using namespace std;

    int main(){
    int *i;
    i = new int;
    *i = 1.0 / 2 * 2 / 1 * 2 / 4 * 4;
    cout << *i;
    return 0;
    }

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