Pass the C++ Institute C++ Certified Professional Programmer CPA-21-02 Questions and answers with CertsForce

Viewing page 6 out of 8 pages
Viewing questions 51-60 out of questions
Questions # 51:

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];

A *bc = (A*)sc;

for (int i=0; i<2;i++)

(bc++)->print();

return 0;

}

Options:

A.

It prints: A A


B.

It prints: B B


C.

It prints: A B


D.

It prints: B A


Expert Solution
Questions # 52:

What happens when you attempt to compile and run the following code?

#include

using namespace std;

int x=5;

static int y=0;

void myFunction(int a)

{

y=++a;

}

int main (int argc, const char * argv[])

{

int i=0;

myFunction(i);

cout<

}

Options:

A.

It prints: 0 5


B.

It prints: 5 1


C.

It prints: 1 5


D.

It prints: 5 0


Expert Solution
Questions # 53:

What happens when you attempt to compile and run the following code?

#include

#include

using namespace std;

const int size = 3;

class A {

public:

string name;

A() { name = "Bob";}

A(string s) { name = s;}

A(A &a) { name = a.name;}

};

class B : public A {

public:

B() { }

B(string s) : A(s) { }

void Print() {

cout << name;

}

};

int main () {

B b1("Alan");

b1.Print();

return 0;

}

Options:

A.

It prints: 111Alan


B.

It prints: Bob


C.

It prints: Alan


D.

It prints: 0


Expert Solution
Questions # 54:

What happens when you attempt to compile and run the following code?

#include

#include

using namespace std;

float* sum(float a,float b);

float* sum(float a,float b)

{

float *f = new float;

*f = a+b;

return f;

}

int main()

{

float a,b,*f;

a = 1.5; b = 3.4;

f = sum(a,b);

cout<<*f;

return 0;

}

Options:

A.

It prints: 0


B.

It prints: 4.9


C.

It prints: 5


D.

It prints: 4


Expert Solution
Questions # 55:

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;

}

Options:

A.

It prints: 0


B.

It prints: 0.5


C.

It prints: 1


D.

It prints: ?1


Expert Solution
Questions # 56:

What happens when you attempt to compile and run the following code?

#include

#include

using namespace std;

class myClass : public exception

{

virtual const char* what() const throw()

{

return "My exception.";

}

} obj;

int main () {

try

{

throw obj;

}

catch (exception& e)

{

cout << e.what() << endl;

}

return 0;

}

Options:

A.

It prints: My exception.


B.

It prints: 0


C.

It prints: 1


D.

Compilation error


Expert Solution
Questions # 57:

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.4) {}

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,c2,c3;

c3 = c1 ? c2;

c3.Print();

}

Options:

A.

It prints: 1 0.4


B.

It prints: 2 0.8


C.

It prints: 0 0


D.

It prints: 1 0.8


Expert Solution
Questions # 58:

What happens when you attempt to compile and run the following code?

#include

using namespace std;

int s(int n);

int main()

{

int a;

a = 3;

cout << s(a);

return 0;

}

int s(int n)

{

if(n == 0) return 1;

return s(n?1)*n;

}

Options:

A.

It prints: 4


B.

It prints: 6


C.

It prints: 3


D.

It prints: 0


Expert Solution
Questions # 59:

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(1,2);

c1.print();

return 0;

}

Options:

A.

It prints: 1 0


B.

It prints: 1 1


C.

It prints: 1 2


D.

Compilation error


Expert Solution
Questions # 60:

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;

}

Options:

A.

It prints: 0


B.

It prints: 1


C.

It prints: 2


D.

It prints: 0.5


Expert Solution
Viewing page 6 out of 8 pages
Viewing questions 51-60 out of questions