What happens when you attempt to compile and run the following code?
#include
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();
}
A. It prints: from First
B. It prints: from Firstfrom First
C. It prints: from Firstfrom Second
D. It prints: from Secondfrom Second
Point out an error in the program.
#include
using namespace std;
int main()
{
const int x=1;
int const *y=andx;
cout<<*y;
return 0;
}
A. No error
B. Error: unknown pointer conversion
C. cannot convert from 'const int *' to 'int *const'
D. Compilation error
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main()
{
int i=2;
switch(i)
{
case 1:
cout<<"Hello";
case 2:
cout<<"world";
case 3:
cout<<"End";
} return 0;
}
A. It prints: Hello
B. It prints: world
C. It prints: worldEnd
D. It prints: End
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main() {
float i = 1.0 / 2 * 2 / 1 * 2 / 4 * 4 / 2;
cout << i;
return 0;
}
A. It prints: 0
B. It prints: 1
C. It prints: 2
D. It prints: 0.5
What happens when you attempt to compile and run the following code?
#include
using namespace std;
class Base {
int age;
public:
class C {
int b;
void PrintC() { cout << b; }
};
Base () {age=5;};
void setAge(int a=20) {age = a;}
void Print() { cout << age;}
};
int main () {
Base a;
A. setAge(10);
B. Print(); return 0; }
C. It prints: 1020
D. It prints: 105
E. It prints: 10
F. It prints: 20
What happens when you attempt to compile and run the following code?
#include
using namespace std;
class BaseC
{
public:
int *ptr;
BaseC() { ptr = new int(10);}
BaseC(int i) { ptr = new int(i); }
~BaseC() { delete ptr; }
};
void fun(BaseC x);
int main()
{
BaseC *o = new BaseC(5);
fun(*o);
}
void fun(BaseC x) {
cout << "Hello:"<<*x.ptr;
}
A. It prints: Hello:50
B. It prints: Hello:10
C. It prints: Hello:5
D. Compilation error
Which code, inserted at line 10, generates the output "2?1"?
#include
#include
using namespace std;
class A {
protected:
int y;
public:
int z;
};
//insert code here
public:
void set() {
y = 2;
z = 3;
}
void Print() { cout << y << z; }
};
int main () {
B b;
A. set();
B. z = ?1;
C. Print(); return 0; }
D. class B : private A {
E. class B : public A {
F. class B : protected A {
G. class B {
What happens when you attempt to compile and run the following code?
#include
using namespace std;
class A {
public:
int x;
A() { x=0;}
};
class B : protected A {
public:
int y;
using A::x;
B(int y) {this?>y = y;}
void Print() { cout << x << y; }
};
int main () {
B b(5);
A. Print(); return 0; }
B. It prints: 05
C. It prints: 0
D. It prints: 5
E. It prints: 15
What happens when you attempt to compile and run the following code? #include
#include
using namespace std;
struct Person {
string name;
int age;
};
class First
{
Person *person;
public:
First() {person = new Person;
person?>name = "John";
person?>age = 30;
}
void Print(){
cout<
}
};
int main()
{
First t[2];
for (int i=0; i<2; i++)
t[i].Print();
}
A. It prints: 30
B. It prints: John
C. It prints: John 31
D. It prints: John 30John 30
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
struct Person {
string name;
int age;
};
class First
{
Person *person;
public:
First() {person = new Person;
person?>name = "John";
person?>age = 30;
}
void Print(){
cout<
}
};
int main()
{
First t;
A. Print(); }
B. It prints: 30
C. It prints: John
D. It prints: John 30
E. It prints: John 30John 30