What happens when you attempt to compile and run the following code?
#include
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();
}
What happens when you attempt to compile and run the following code?
#include
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;
}
}
Point out an error in the program.
#include
using namespace std;
int main()
{
char s1[] = "Hello";
char s2[] = "world";
char *const ptr = s1;
*ptr = 'a';
ptr = s2;
return 0;
}
What happens when you attempt to compile and run the following code?
#include
#include
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;
}
What will happen when you attempt to compile and run the following code?
#include
using namespace std;
int main()
{
const char *s;
char str[] = "Hello ";
s = str;
while(*s) {
cout << *++s;
*s++;
}
return 0;
}
What is the output of the program if characters 't', 'e', 's' and 't' enter are supplied as input?
#include
#include
using namespace std;
int main()
{
string s;
getline( cin, s );
cout << s << " " << s.length();
return( 0 );
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main(){
int *i;
i = new int;
*i = 1.0 / 2 * 2 / 1 * 2 / 4 * 4;
cout << *i;
return 0;
}
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
class A {
public:
string s;
A(string s) { this?>s = s; }
};
class B {
public:
string s;
B (A a) { this?>s = a.s; }
void print() { cout<
};
int main()
{
A a("Hello world");
B b=a;
b.print();
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
void fun(int &i);
int main()
{
int i=2;
fun(i);
cout<
return 0;
}
void fun(int &i)
{
i+=2;
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
class A {
public:
virtual void Print()=0;
};
class B:public A {
public:
virtual void Print() { cout<< "B"; }
};
class C:public A {
public:
virtual void Print() { cout<< "C"; }
};
int main()
{
B ob2;
C ob3;
A *obj;
obj = &ob2;
obj?>Print();
obj = &ob3;
obj?>Print();
}
What is the output of the program given below?
#include
using namespace std;
int main (int argc, const char * argv[])
{
int i=10;
{
int i=0;
cout<
}
{
int i=5;
cout << i;
}
cout<
return 0;
}
What will happen when you attempt to compile and run the following code?
#include
using namespace std;
int getValue();
int main()
{
const int x = getValue();
cout< return 0; } int getValue() { return 5; }
Which code, inserted at line 15, generates the output "5 Bob"?
#include
#include
using namespace std;
class B;
class A {
int age;
public:
A () { age=5; };
friend void Print(A &ob, B &so);
};
class B {
string name;
public:
B () { name="Bob"; };
//insert code here
};
void Print(A &ob, B &so) {
cout< } int main () { A a; B b; Print(a,b); return 0; }
Point out an error in the program.
#include
using namespace std;
int main()
{
const int x=1;
int const *y=&x;
cout<<*y;
return 0;
}
What is the output of the program given below?
#include
using namespace std;
int main (int argc, const char * argv[])
{
int i=10;
{
int i=0;
cout<
}
cout<
return 0;
}
Which code line inserted instead of the comment below will cause the program to produce the expected output?
What happens when you attempt to compile and run the following code?
#include
using namespace std;
class First
{
public:
First() { cout << "Constructor";}
~First() { cout << "Destructor";}
void Print(){ cout<<"from First";}
};
int main()
{
First FirstObject;
FirstObject.Print();
}
What is the output of the program?
#include
using namespace std;
class BaseC
{
int i;
public:
BaseC() { i=?1;}
BaseC(int i) { i=i; }
void seti(int a) { i = a; };
void Print() { cout << i; }
};
int main()
{
BaseC *o = new BaseC();
o?>seti(10);
o?>Print();
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int op(int x, int y)
{
int i;
i = x + y;
return i;
}
int main()
{
int i=1, j=2, k, l;
k = op(i, j);
l = op(j, i);
cout<< k << "," << l;
return 0;
}
What will happen when you attempt to compile and run the following code?
#include
using namespace std;
#define A 1
int main()
{
#if A
cout<<"Hello";
#endif
cout<<"world";
return 0;
}
Analyze the code below. If it contains an error, indicate its place in the program.
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
class A {
int x;
protected:
int y;
public:
int z;
A() { x=1; y=2; z=3; }
};
class B : public A {
public:
void set() {
y = 4; z = 2;
}
void Print() {
cout << y << z;
}
};
int main () {
B b;
b.set();
b.Print();
return 0;
}
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
class complex{
double re, im;
public:
complex() : re(1),im(0.3) {}
complex(double n) { re=n,im=n;};
complex(int m,int n) { re=m,im=n;}
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(1),c2(2),c3;
c3 = c1 + c2;
c3.Print();
}
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()
{
Second t[2];
for (int i=0; i<2; i++)
t[i].Print();
}
What will be the output of the program?
#include
#include
using namespace std;
int fun(int);
int main()
{
float k=3;
k = fun(k);
cout< return 0; } int fun(int i) { i++; return i; }
What happens when you attempt to compile and run the following code?
#include
#include
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();
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main()
{
int *a= new int;
*a=100;
cout << *a;
delete a;
}
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
int f(int i);
int main()
{
int i=0;
i++;
for (i=0; i<=2; i++)
{
cout< } return 0; } int f(int a) { return a+a; }
Which code lines inserted independently instead of the comment will make the following program work correctly? (Choose three.)
What happens when you attempt to compile and run the following code?
#include
using namespace std;
class First
{
public:
virtual void Print(){ cout<<"from First";}
};
class Second:public First
{
public:
void Print(){ cout<< "from Second";}
};
void fun(First *obj);
int main()
{
First FirstObject;
fun(&FirstObject);
Second SecondObject;
fun(&SecondObject);
}
void fun(First *obj)
{
obj?>Print();
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main()
{
int i = 4;
while(i >= 0) {
cout<
i??;
}
return 0;
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
void print(char *c);
int main (int argc, const char * argv[])
{
print("Test");
return 0;
}
void print(char *c)
{
cout< }
What happens when you attempt to compile and run the following code?
#include
using namespace std;
class A {
public :
void print() {
cout << "A ";
}
};
class B {
public :
void print() {
cout << "B ";
}
};
int main() {
B sc[2];
B *bc = (B*)sc;
for (int i=0; i<2;i++)
(bc++)->print();
return 0;
}
Which code, inserted at line 8, generates the output "100"?
#include
using namespace std;
int fun(int);
int main()
{
int *x = new int;
*x=10;
//insert code here
return 0;
}
int fun(int i)
{
return i*i;
}
If a function, which is not a method, needs to save any value between its subsequent invocations, this can be done by: (Choose two.)
Which line of code inserted instead of the comment will make the following code run properly without causing memory leaks?
What will happen when you attempt to compile and run the following code?
#include
using namespace std;
int main (int argc, const char * argv[])
{
enum state { ok, error, warning};
enum state s1, s2, s3;
s1 = ok;
s2 = warning;
s3 = error;
s4 = ok;
cout << s1<< s2<< s3;
return 0;
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main()
{
int i = 0;
i++;
goto lab;
i++;
lab:
cout<
return 0;
}
Which code line instead of the comment below will cause the program to produce the expected output?
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
class A {
protected:
int y;
public:
int x,z;
A() : x(2), y(2), z(1) { 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;
}
Which of the following is a correct way to define the function fun() in the program below?
#include
#include
#include
using namespace std;
int main()
{
int a[2][2];
fun(a);
return 0;
}
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
inline float sum(float a,float b)
{
return a+b;
}
int main()
{
float a,b;
a = 1.5; b = 3.4;
cout< return 0; }
What is the output of the program if character 2 is supplied as input?
#include
using namespace std;
int main () {
int c;
cin >> c;
try
{
switch (c)
{
case 1:
throw 20;
case 2:
throw 5.2f;
}
}
catch (int e)
{ cout << "int exception. Exception Nr. " << e; }
catch (float e)
{ cout << "float exception. Exception Nr. " << e; }
catch (...)
{ cout << "An exception occurred."; }
return 0;
}
What happens when you attempt to compile and run the following code?
#include
#include
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;
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main()
{
int *t;
t = new int[2];
for (int i=0; i<2; i++) {
t[i]=0;
}
cout << t[1];
}
What is the output of the program given below?
#include
using namespace std;
int main (int argc, const char * argv[])
{
int i=10;
{
int i=0;
cout<
}
{
i=5;
cout << i;
}
cout<
return 0;
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int op(int x, int y);
int main()
{
float *pf;
float f=0.9;
pf=&f;
cout << op(1, *pf);
return 0;
}
int op(int x, int y)
{
return x*y;
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int fun(int x) {
return x<<2;
}
int main(){
int i;
i = fun(1) / 2;
cout << i;
return 0;
}
Which code, inserted at line 14, generates the output "3.14 10"?
#include
using namespace std;
namespace myNamespace1
{
int x = 5;
int y = 10;
}
namespace myNamespace2
{
float x = 3.14;
float y = 1.5;
}
int main () {
//insert code here
cout << x << " " << y;
return 0;
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main()
{
int x,y=10;
float f;
f = 5.20;
x=(int) f;
cout << x <<", ";
f=float (y);
cout << f;
return 0;
}
What is the output of the program?
#include
#include
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();
}
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;
}
};
What is the output of the program?
#include
#include
using namespace std;
int main () {
string s1 = "Hello", s2 = "World";
s2 = s1 + s2;
cout << s2;
return 0;
}
What happens when you attempt to compile and run the following code?
#include
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;
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main()
{
float x=3.5,y=1.6;
int i,j=2;
i = x + j + y;
cout << i;
return 0;
}
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int fun(int x);
int main() {
cout << fun(0);
return 0;
}
int fun(int x) {
if(x > 0)
return fun(x-1);
else
return 100;
}
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
class A {
public:
int x;
};
class B : public A {
public:
B() { x=1;}
B(int x) {this?>x = x;}
};
int main () {
B c1;
B c2(10);
cout << c1.x;
cout << c2.x;
return 0;
}
What is the output of the program if character 4 is supplied as input?
#include
using namespace std;
int main () {
int c;
cin >> c;
try
{
switch (c)
{
case 1:
throw 20;
case 2:
throw 5.2f;
case 3:
throw 'a';
default:
cout<<"No exception";
}
}
catch (int e)
{ cout << "int exception. Exception Nr. " << e; }
catch (float e)
{ cout << "float exception. Exception Nr. " << e; }
catch (...)
{ cout << "An exception occurred."; }
return 0;
}
What will be the output of the program?
#include
using namespace std;
int fun(int);
int main()
{
cout << fun(5);
return 0;
}
int fun(int i)
{
return i*i;
}
What is the output of the program?
#include
#include
using namespace std;
int main()
{
string s1="Hello";
string s2="World";
s1+=s2;
cout << s1;
return( 0 );
}
Which code, inserted at line 8, generates the output "0102020"?
#include
using namespace std;
class Base {
static int age;
public:
Base () {};
~Base () {};
//insert code here
void Print() { cout << age;}
};
int Base::age=0;
int main () {
Base a,*b;
b = new Base();
a.Print();
a.setAge(10);
a.Print();
b?>setAge();
a.Print();
b?>Print();
return 0;
}