CPA : C++ Certified Associate Programmer : Part 08

  1. What will the variable “y” be in class B?

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

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

    • public
    • private
    • protected
    • None of these
  2. Which statement should be added in the following program to make work it correctly?

    using namespace std;
    int main (int argc, const char * argv[])
    {
    cout<<“Hello”;
    }

    • #include<stdio.h>
    • #include<stdlib.h>
    • #include <iostream>
    • #include<conio.h>
  3. What happens when you attempt to compile and run the following code?

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    float* sum(float a,float b);

    float* sum(float a,float b)
    {
    float *f = new float;
    *f = a+b;
    return f;
    }

    int main()
    {
    float a,b,*f;
    a = 1.5; b = 3.4;
    f = sum(a,b);
    cout<<*f;

    return 0;
    }

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

    #include <iostream>
    #include <string>

    using namespace std;

    class complex{
    double re, im;
    public:
    complex() : re(1),im(0.4) {}
    complex operator+(complex &t);
    void Print() { cout << re << ” ” << im; }
    };

    complex complex::operator+ (complex &t){
    complex temp;
    temp.re = this?>re + t.re;
    temp.im = this?>im + t.im;
    return temp;
    }

    int main(){
    complex c1,c2,c3;
    c3 = c1 + c2;
    c3.Print();
    }

    • It prints: 1 0.4
    • It prints: 2 0.8
    • It prints: 0 0
    • Garbage value
  5. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    #define FUN(arg) if(arg) cout<<“Test”;

    int main()
    {
    int i=1;
    FUN(i<3);
    return 0;
    }

    • It prints: 0
    • It prints: T
    • It prints: T0
    • It prints: Test
  6. 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 = 3.0;
    c1.print();
    return 0;
    }

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

    #include <iostream>
    #include <string>

    using namespace std;

    class complex{
    double re, im;
    public:
    complex() : re(1),im(0.4) {}
    complex operator(complex &t);
    void Print() { cout << re << ” ” << im; }
    }
    complex complex::operator (complex &t){
    complex temp;
    temp.re = this>re t.re;
    temp.im = this>im t.im;
    return temp;
    }

    int main(){
    complex c1,c2,c3;
    c3 = c1 c2;
    c3.Print();
    }

    • It prints: 1 0.4
    • It prints: 2 0.8
    • It prints: 0 0
    • It prints: 1 0.8
  8. What will the variable “age” be in class B?

    class A {
    int x;
    protected:
    int y;
    public:
    int age;
    A () { age=5; };
    };

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

    • public
    • private
    • protected
    • None of these
  9. What will be the output of the program?

    #include <iostream>

    using namespace std;

    int fun(int);

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

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

    • 25
    • 5
    • 0
    • 1
  10. What happens when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;

    int s(int n);

    int main()
    {
    int a;
    a = 3;
    cout << s(a);
    return 0;
    }

    int s(int n)
    {
    if(n == 0) return 1;
    return s(n?1)*n;
    }

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

    #include <iostream>

    using namespace std;

    void fun(int);

    int main()
    {
    int a=0;
    fun(a);
    return 0;
    }

    void fun(int n)
    {
    if(n < 2)
    {
    fun(++n);
    cout << n;
    }
    }

    • It prints: 21
    • It prints: 012
    • It prints: 0
    • None of these
  12. What is the output of the program?

    #include <iostream>

    using namespace std;

    int main()
    {
    int tab[4]={10,20,30,40};
    tab[1]=10;
    int *p;
    p=&tab[0];

    cout<<*p;
    return 0;
    }

    • It prints: 10
    • It prints: 20
    • It prints: 11
    • It prints: 30
  13. What happens when you attempt to compile and run the following code?

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

    const int size = 3;
    class A {
    public:
    string name;
    A() { name = “Bob”;}
    A(string s) { name = s;}
    A(A &a) { name = a.name;}
    };
    class B : public A {
    public:
    B() { }
    B(string s) : A(s) { }
    void Print() {
    cout << name;
    }
    };
    int main () {
    B b1(“Alan”);
    b1.Print();
    return 0;
    }

    • It prints: 111Alan
    • It prints: Bob
    • It prints: Alan
    • It prints: 0
  14. 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 << z;
    }
    };

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

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

    #include <iostream>
    using namespace std;

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

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

    • It prints: 0
    • It prints: 1
    • It prints: 2
    • It prints: 4
  16. 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) { z = x + y; }
    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;
    b.Print();
    return 0;
    }

    • It prints: 3
    • It prints: 0
    • It prints: 1
    • It prints: 2
  17. 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 x,y;
    union t
    {
    char tab[2];
    int i;
    };
    union t u;
    u.tab[0] = 1;
    u.tab[1] = 2;
    u.i = 0;
    x = u.tab[0];
    y = u.tab[1];
    cout << x << “,” << y << “,” << u.i;
    return 0;
    }

    • compilation fails
    • It prints: 0,0,0
    • It prints: 1,2,0
    • None of these
  18. 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;
    }
    {
    i=5;
    cout << i;
    }
    cout<<i;
    return 0;
    }

    • 1010
    • 101010
    • 055
    • None of these
  19. 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(1,2);
    c1.print();
    return 0;
    }

    • It prints: 1 0
    • It prints: 1 1
    • It prints: 1 2
    • Compilation error
  20. What will the variable “age” be in class B?

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

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

    • public
    • private
    • protected
    • None of these
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments