Question 101

Which code, inserted at line 10, generate the output "50"?
#include <iostream>
using namespace std;
class Base {
int age;
public:
Base () {
age=5;
};
//insert code here
void Print() { cout << age;}
};
void setAge(Base &ob) {ob.age = 0;}
int main () {
Base a;
a.Print();
setAge(a);
a.Print();
return 0;
}
  • Question 102

    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;
    }
  • Question 103

    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;
    }
  • Question 104

    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;
    }
  • Question 105

    What happens when you attempt to compile and run the following code?
    #include <iostream>
    using namespace std;
    int main()
    {
    int x=0;
    int *ptr;
    ptr = &x;
    cout<<x<<" "<<*ptr;
    return 0;
    }