CPA : C++ Certified Associate Programmer : Part 07

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

    #include <iostream>
    #include <string>

    using namespace std;

    class A {
    public:
    string s;
    A(string s) { this>s = s; }
    };

    class B {
    public:
    string s;
    B (A a) { this>s = a.s; }
    void print() { cout<<s; }
    };

    int main()
    {
    A a(“Hello world”);
    B b=a;
    b.print();
    }

    • It prints: Hello world
    • It prints: Hello
    • Compilation error
    • None of these
  2. 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 a2(“Test”);
    B b1(“Alan”);
    B b2(b1);
    return 0;
    }

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

    #include <iostream>

    using namespace std;

    void fun(char*);

    int main()
    {
    char t[4]={‘0’, ‘1’, ‘2’, ‘3’};
    fun(&t[2]);
    return 0;
    }
    void fun(char *a)
    {
    cout << *a;
    }

    • It prints: 2
    • It prints: 21
    • It prints: 00
    • It prints: 02
  4. 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;
    }
    cout<<i;
    return 0;
    }

    • 1010
    • 100
    • 010
    • None of these
  5. What is the output of the program?

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    string s1[]= {“Hello” , “World” };

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

    • It prints: HelloWorld
    • It prints: Hello
    • It prints: WorldHello
    • It prints: World
  6. Which code, inserted at line 14, generates the output “3.14 10”?

    #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 () {
    //insert code here
    cout << x << ” ” << y;
    return 0;
    }

    • using myNamespace2::y; using myNamespace1::x;
    • using namespace myNamespace1;
    • using namespace myNamespace1; using namespace myNamespace2;
    • using myNamespace1::y; using myNamespace2::x;
  7. What is the output of the program if character 4 is supplied as input?

    #include <iostream>
    using namespace std;
    int main () {
    int c;
    cin >> c;
    try
    {
    switch (c)
    {
    case 1:
    throw 20;
    case 2:
    throw 5.2f;
    case 3:
    throw ‘a’;
    default:
    cout<<“No exception”;
    }
    }
    catch (int e)
    { cout << “int exception. Exception Nr. ” << e; }
    catch (float e)
    { cout << “float exception. Exception Nr. ” << e; }
    catch (…)
    { cout << “An exception occurred.”; }
    return 0;
    }

    • It prints: float exception. Exception Nr.
    • It prints: int exception. Exception Nr.
    • It prints: An exception occurred
    • It prints: No exception
  8. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    int main(int argc, char *argv[]) {
    char *s = “ABCDEF”;
    cout << s+2;
    return 0;
    }

    • It prints: CDEF
    • It prints: ABCDEF
    • It prints: BCDEF
    • None of these
  9. 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()
    {
    First FirstObject;
    FirstObject.Print();
    Second SecondObject;
    SecondObject.Print();
    }

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

    #include <iostream>
    using namespace std;
    class Base {
    static int age;
    public:
    Base () {};
    ~Base () {};
    //insert code here
    void Print() { cout << age;}
    };

    int Base::age=0;

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

    • void setAge(int a) {age = a;}
    • void setAge() {age = 20;}
    • void setAge() {age = 10;}
    • void setAge(int a=20) {age = a;}
  11. What is the output of the program if character 2 is supplied as input?

    #include <iostream>

    using namespace std;

    int main () {
    int c;
    cin >> c;
    try
    {
    switch (c)
    {
    case 1:
    throw 20;
    case 2:
    throw 5.2f;
    }
    }
    catch (int e)
    { cout << “int exception. Exception Nr. ” << e; }
    catch (float e)
    { cout << “float exception. Exception Nr. ” << e; }
    catch (…)
    { cout << “An exception occurred.”; }
    return 0;
    }

    • It prints: float exception. Exception Nr.
    • It prints: int exception. Exception Nr. 20
    • It prints: An exception occurred
    • It prints: float exception. Exception Nr. 5.2
  12. What happens when you attempt to compile and run the following code?

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

    class B;

    class A {
    int age;
    public:
    A () { age=5; };
    friend class B;
    };

    class B {
    string name;
    public:
    B () { name=”Bob”; };
    void Print(A ob) {
    cout << name << ob.age;
    }
    };

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

    • It prints: Bob5
    • It prints: Bob
    • It prints: 5
    • None of these
  13. 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 a = 30, b = 1, c = 5, i=10;
    i = b < a < c;
    cout << i;
    return 0;
    }

    • compilation fails
    • It prints: 10
    • It prints: 0
    • It prints: 1
  14. 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;}
    A(int x) { this?>x=x;}
    };

    class B : private A {
    public:
    using A::x;
    B() { x=1;}
    B(int x) {this?>x = x;}
    };

    int main () {
    B c1;
    B c2(?5);
    cout << c1.x;
    cout << c2.x;
    return 0;

    }

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

    #include <iostream>
    using namespace std;

    int fun(int x);

    int main() {
    cout << fun(0);
    return 0;
    }

    int fun(int x) {
    if(x > 0)
    return fun(x-1);
    else
    return 100;
    }

    • It prints: 0
    • It prints: 10
    • It prints: 100
    • It prints: -1
  16. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    void fun(int &i);

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

    void fun(int &i)
    {
    i+=2;
    }

    • It prints: 2
    • It prints: 0
    • It prints: 4
    • It prints: 16
  17. 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
  18. Which code, inserted at line 10, generates the output “Hello World”?

    #include <iostream>
    #include <string>
    using namespace std;
    string fun(string, string);
    int main()
    {
    string s=”Hello”;
    string *ps;
    ps = &s;
    //insert code here
    return 0;
    }

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

    • cout << fun(” World”);
    • cout << fun(*ps);
    • cout << fun(“Hello”);
    • cout << fun(“Hello”, ” World”);
  19. 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
  20. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    int main()
    {
    float x=3.5,y=1.6;
    int i,j=2;
    i = x + j + y;
    cout << i;
    return 0;
    }

    • It prints: 7
    • It prints: 6
    • It prints: 7,1
    • Compilation error
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments