Jordan University of Science and Technology

Jordan University of Science and Technology
Computer Science Department
CS112 - Introduction to computer programming language C++
Review Questions for Chapter 14
Prepared by Malak Abdullah
Q1 What is the Output of the following code:
class A
{
public:
virtual void fun1()=0;
void fun2(){cout<<"Fun2 A \n";}
};
class B:public A
{
public:
void fun1(){cout<<"Fun1 B\n";}
virtual fun2(){cout<<"Fun2 B\n";}
};
void MyFun(A &obj)
{
obj.fun1();
obj.fun2();
}
void main()
{
A *p;
p=new B;
p->fun1();
p->fun2();
cout<<"///////////////////\n";
MyFun(*p);
}
Fun1 B
Fun2 A
///////////////////
Fun1 B
Fun2 A
Q2 What is the Output of the following code:
class A
{
public:
int *p;
void fun(){int var1=3; p=&var1;}
};
void main()
{
int x=10;
A obj;
obj.p=&x;
obj.fun();
*obj.p +=5;
cout<<*obj.p<<" "<<x<<endl;
}
8 10
Q3 With the following code
class A
{
public:
int *p;
};
void main()
{
int x=10;
A *obj=new A;
obj->p=&x;
}
How to print the contents of x
cout<<x<<"
"<<*obj->p<<"
"<<*(*obj).p<<endl;
Q4 What is the Output of the following code:
void main()
{
int* p1, p2, *p3;
int x=3, y=8, &z=x;
p1=&x;
p3=&z;
*p3=*p3+3;
*p1=*p1+10;
x=x+5;
z=z+10;
p2=y;
p2=p2+4;
cout<<x<<" "<<y<<" "<<z<<endl;
}
31 8
31
Q5 What are the 6 errors in the following code
void main()
{
1) int* p1, p2, *p3;
2) int x=3, y=8, //&z;
3) p1=x//p1=&x;;
4) p2=&y;//
5) p3=&p2;
6) z=&x;
7) p3=& new int;//
8) *p1=*p2;//
}
line 2 &z
line 3 p1 is a pointer
line 4 p2 is not a pointer
line 6 z is not a pointer
line 7 new returns address
line 8 p2 is not a pointer
Q6 What is the Output of the following code:
void main()
{
int *p1, **p2,**p3;
int x=10;
p1=new int (5);
cout<<*p1<<endl;
p2=& new int;
**p2=7;
cout<<**p2<<endl;
p3=&*&p1;
//p3=&p1;
cout<<*p3<<endl;
}
5
7
00480040
Address
Q7 What is the Output of the following code:
void main()
{
int **p;
int x[4]={1,2,3,4};
p=new int* [3];
p[1]=new int[5];
p[1][0]=10;
p[1][1]=20;
p[1][2]=30;
p[2]=x;
p[3]=new int [6];
for(int i=0;i<=5;i++)
p[3][i]=i*100;
p++;
p[1]++;
cout<<p[1][1]<<endl;
cout<<p[1][-1]<<endl;
}
3
1
Q8 What is the Output of the following code:
class A
{
public:
int x;
int y;
int fun() {x=y;}
};
void main()
{
A obj1;
obj1.x=4;
obj1.y=3;
A obj2(obj1);
cout<<obj2.x<<" "<<obj2.y<<endl;
}
3
4
Q9 What is the Output of the following code:
class A
{
public:
int x;
int y;
int *p;
};
void main()
{
A obj1,obj3;
A *obj2=&obj1;
obj1.p=new int;
*obj1.p=10;
if(obj1.p==obj2->p)
cout<<"Weclome\n";
else
cout<<"Good Bye\n";
obj3=*obj2;
obj3.p=new int;
*obj3.p=20;
if(obj1.p==obj3.p)
cout<<"Weclome\n";
else
cout<<"Good Bye\n";
}
Welcome
Good Bye
Q10 What is the output of the following code
class A
{
public:
int *p;
A (A obj) {cout<<"Copy Constructor\n";}
};
void main()
{
A obj1;
A *obj2=new A;
A obj3(obj1);
}
No default constructor
Copy constructor takes parameter by reference only
Q11 What is the output of the following code
class A
{
public:
int *p;
int x;
A (A &obj) {cout<<"Copy Constructor\n"; p=&x; x=5;}
A(){cout<<"Default costructor\n"; p=&x; x=3;}
};
void main()
{
A obj1;
A *obj2=new A(obj1);
A obj3(obj1);
A *obj4=new A;
if(obj1.p==obj3.p)
cout<<"Welcome\n";
else
cout<<"Good bye\n";
cout<<*obj1.p<<" "<<*obj2->p<<" "<<*obj3.p<<" "<<*obj4->p<<endl;
}
Default costructor
Copy Constructor
Copy Constructor
Default costructor
Good bye
3 5 5 3
Q12 What is the output of the following code
class A
{
public:
int *p;
int x;
A (A &obj) {cout<<"Copy Constructor\n"; p=new int; *p=5;}
A(){cout<<"Default costructor\n"; p=new int; *p=5;}
~A(){cout<<"Destructor\n"; delete p;}
};
void main()
{
A obj1;
A *obj2=new A(obj1);
if(obj2->p==obj1.p)
cout<<"Equal pointers\n";
else
cout<<"Not Equal pointers\n";
if ((*obj2->p)==(*obj1.p))
cout<<"Equal Values\n";
else
cout<<"Not Equal Values\n";
delete obj2;
obj2=&obj1;
if(obj2->p==obj1.p)
cout<<"Equal pointers\n";
else
cout<<"Not Equal pointers\n";
}
Default costructor
Copy Constructor
Not Equal pointers
Equal Values
Destructor
Equal pointers
Destructor
Q13 What is the output of the following code
Q14 What is the output of the following code
"Dictionary is the only place that success comes before work. Hard work is
the price we must pay for success. I think you can accomplish anything if
you're willing to pay the price."
Do your Best