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

Viewing page 5 out of 8 pages
Viewing questions 41-50 out of questions
Questions # 41:

What will the variable "age" be in class B?

class A {

int x;

protected:

int y;

public:

int age;

A () { age=5; };

};

class B : public A {

string name;

public:

B () { name="Bob"; };

void Print() {

cout << name << age;

}

};

Options:

A.

public


B.

private


C.

protected


D.

None of these


Expert Solution
Questions # 42:

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;

}

Options:

A.

It prints: 5, 10


B.

It prints: 5.2, 10


C.

It prints: 5.20, 10.0


D.

It prints: 5.2, 10.00


Expert Solution
Questions # 43:

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;

}

Options:

A.

It prints: 0


B.

It prints: 10


C.

It prints: 100


D.

It prints: -1


Expert Solution
Questions # 44:

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;

int z;

A() { x=2; y=2; z=3; }

A(int a, int b) : x(a), y(b) { z = x ? y;}

void Print() {

cout << z;

}

};

int main () {

A a(2,5);

a.Print();

return 0;

}

Options:

A.

It prints: ?3


B.

It prints: 2


C.

It prints: 6


D.

It prints: 5


Expert Solution
Questions # 45:

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;

double i=2;

c1 = i;

c1.print();

return 0;

}

Options:

A.

It prints: 0 0


B.

It prints: 1 1


C.

It prints: 2 0


D.

It prints: 2 2


Expert Solution
Questions # 46:

What is the output of the program?

#include

using namespace std;

int main()

{

int tab[4]={10,20,30,40};

tab[1]=10;

int *p;

p=&tab[0];

cout<<*p;

return 0;

}

Options:

A.

It prints: 10


B.

It prints: 20


C.

It prints: 11


D.

It prints: 30


Expert Solution
Questions # 47:

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

#include

using namespace std;

int fun(int x) {

return 2*x;

}

int main(){

int i;

i = fun(1) & fun(0);

cout << i;

return 0;

}

Options:

A.

It prints: 0


B.

It prints: 1


C.

It prints: -1


D.

Compilation error


Expert Solution
Questions # 48:

Given:

#include

#include

using namespace std;

int main () {

try

{

int * myarray= new int[1000];

}

catch (bad_alloc&)

{

cout << "Error allocating memory";

}

catch (exception& e)

{

cout << "Standard exception";

}

catch (...)

{

cout << "Unknown exception";

}

return 0;

}

What will happen if we use the operator “new” and the memory cannot be allocated?

Options:

A.

It prints: Error allocating memory


B.

It prints: Standard exception


C.

It prints: Unknown exception


D.

Compilation error


Expert Solution
Questions # 49:

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;

}

Options:

A.

It will print:"el "


B.

The code will not compile.


C.

It will print:"Hello "


D.

It will print garbage value


Expert Solution
Questions # 50:

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;

}

Options:

A.

void setAge(int a) {age = a;}


B.

void setAge() {age = 20;}


C.

void setAge() {age = 10;}


D.

void setAge(int a=20) {age = a;}


Expert Solution
Viewing page 5 out of 8 pages
Viewing questions 41-50 out of questions