Question 46

What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class A
{
public:
virtual void Print(){ cout<<"A";}
};
class B:public A
{
public:
void Print(){ cout<< "B";}
};
int main()
{
A *obj;
A ob1;
obj = &ob1;
obj>Print();
B ob2;
obj = &ob2;
obj>Print();
}
  • Question 47

    What happens when you attempt to compile and run the following code?
    #include <iostream>
    #include <string>
    using namespace std;
    class SampleClass
    {
    string *s;
    public:
    SampleClass() { s = new string("Text");}
    SampleClass(string s) { this->s = new string(s);}
    ~SampleClass() { delete s;}
    void Print(){ cout<<*s;}
    };
    int main()
    {
    SampleClass *obj;
    obj = new SampleClass("Test");
    obj->Print();
    }
  • Question 48

    What is the output of the program?
    #include <iostream>
    #include <string>
    using namespace std;
    class First
    {
    string name;
    public:
    First() {
    name = "Alan";
    }
    void Print(){
    cout << name;
    }
    };
    int main()
    {
    First ob1,*ob2;
    ob2 = new First();
    ob1.Print(); ob2->Print(); }
  • Question 49

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

    If there is one, point out an error in the program
    #include <iostream>
    using namespace std;
    int main() {
    int c = 'a';
    switch(i)
    {
    case '2':
    cout<<"OK";
    case '1':
    cout<<"Error";
    default:
    break;
    }
    return 0;
    }