Summer Sale - Special Limited Time 65% Discount Offer - Ends in 0d 00h 00m 00s - Coupon code: dpt65

CPA Questions and Answers

Note! Following CPA Exam is Retired now. Please select the alternative replacement for your Exam Certification. The new exam code is CPA-21-02

CPA Questions and Answers

Question # 6

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

#include <iostream>

#include

using namespace std;

class complex{

double re;

double im;

public:

complex() : re(1),im(0.4) {}

bool operator==(complex &t);

};

bool complex::operator == (complex &t){

if((this?>re == t.re) && (this?>im == t.im))

return true;

else

return false;

}

int main(){

complex c1,c2;

if (c1==c2)

cout << "OK";

else {

cout << "ERROR";

}

}

A.

It prints: OK

B.

It prints: ERROR

C.

Compilation error

D.

Runtime error.

Full Access
Question # 7

What is the output of the program if character 4 is supplied as input?

#include <iostream>

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;

}

A.

It prints: float exception. Exception Nr.

B.

It prints: int exception. Exception Nr.

C.

It prints: An exception occurred

D.

It prints: No exception

Full Access
Question # 8

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

#include <iostream>

#include

using namespace std;

class Base

{

string s;

public:

Base() { s="Sample text";}

Base(string s) { this?>s=s; }

void Print() { cout << s; }

};

int main()

{

Base *o = new Base();

o?>Print();

}

A.

It prints: Sample text

B.

It prints: Sample

C.

It prints: text

D.

None of these

Full Access
Question # 9

Given:

#include <iostream>

#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?

A.

It prints: Error allocating memory

B.

It prints: Standard exception

C.

It prints: Unknown exception

D.

Compilation error

Full Access
Question # 10

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

#include <iostream>

using namespace std;

int main(){

int *i;

i = new int;

*i = 1.0 / 2 * 2 / 1 * 2 / 4 * 4;

cout << *i;

return 0;

}

A.

It prints: 0

B.

It prints: 1

C.

It prints: 2

D.

It prints: 0.5

Full Access
Question # 11

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

#include <iostream>

#include

using namespace std;

class A {

public:

int x;

A() { x=0;}

A(int x) { this?>x=x;}

};

class B : private A {

public:

using A::x;

B() { x=1;}

B(int x) {this?>x = x;}

};

int main () {

B c1;

B c2(?5);

cout << c1.x;

cout << c2.x;

return 0;

}

A.

It prints: 5

B.

It prints: 1?5

C.

It prints: 05

D.

It prints: 0

Full Access
Question # 12

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

#include <iostream>

using namespace std;

void fun(char*);

int main()

{

char t[4]={'0', '1', '2', '3'};

fun(&t[2]);

return 0;

}

void fun(char *a)

{

cout << *a;

}

A.

It prints: 2

B.

It prints: 21

C.

It prints: 00

D.

It prints: 02

Full Access
Question # 13

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

#include <iostream>

using namespace std;

class BaseC

{

int *ptr;

public:

BaseC() { ptr = new int(10);}

BaseC(int i) { ptr = new int(i); }

~BaseC() { delete ptr; }

void Print() { cout << *ptr; }

};

int main()

{

BaseC *o = new BaseC(5);

o?>Print();

}

A.

It prints: 5

B.

It prints: 10

C.

It prints: 1

D.

It prints: 0

Full Access
Question # 14

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

#include <iostream>

using namespace std;

int main()

{

int x=20;

int *ptr;

ptr = &x;

cout<<*ptr;

return 0;

}

A.

It prints: 20

B.

It prints: 0

C.

It prints address of ptr

D.

It prints: 2

Full Access
Question # 15

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

#include <iostream>

using namespace std;

#define FUN(arg) if(arg) cout<<"Test";

int main()

{

int i=1;

FUN(i<3);

return 0;

}

A.

It prints: 0

B.

It prints: T

C.

It prints: T0

D.

It prints: Test

Full Access
Question # 16

What is the output of the program if character 2 is supplied as input?

#include <iostream>

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;

}

A.

It prints: float exception. Exception Nr.

B.

It prints: int exception. Exception Nr. 20

C.

It prints: An exception occurred

D.

It prints: float exception. Exception Nr. 5.2

Full Access
Question # 17

Which of the following is a user defined data type?

1:

struct person

{

char name[20];

int age;

};

2:

int l=2;

3:

enum color {red,blue, green};

D.

char c;

A.

1

B.

2

C.

3

D.

4

Full Access
Question # 18

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

#include <iostream>

#include

using namespace std;

class A {

int x;

protected:

int y;

public:

int z;

};

class B : private A {

string name;

public:

void set() {

x = 1;

}

void Print() {

cout << x;

}

};

int main () {

B b;

b.set();

b.Print();

return 0;

}

A.

It prints: 123

B.

It prints: 1

C.

It prints: ?123

D.

Compilation error

Full Access
Question # 19

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

#include <iostream>

#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 {

string z;

public:

void set() { y = 4; z = "John"; }

void Print() { cout << y << A::z; }

};

int main () {

B b;

b.set();

b.Print();

return 0;

}

A.

It prints: 4John

B.

It prints: 2John

C.

It prints: 23

D.

It prints: 43

Full Access
Question # 20

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

#include <iostream>

using namespace std;

int main()

{

int i = 4;

while(i >= 0) {

cout<<i;

i??;

}

return 0;

}

A.

It prints:”43210”

B.

It prints:”3210”

C.

It prints: ”3210?1”

D.

None of these

Full Access
Question # 21

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

#include <iostream>

#include

using namespace std;

string fun(string, string);

int main()

{

string s="Hello";

cout << fun(s, " World");

return 0;

}

string fun(string s1, string s2)

{

return s1+s2;

}

A.

It will print: Hello World

B.

It will print: Hello

C.

It will print: World

D.

It will print: HW

Full Access
Question # 22

What is the output of the program?

#include <iostream>

#include

using namespace std;

int main()

{

string s1[]= {"H" , "t" };

string s;

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

s = s1[i];

if (i==0)

s.insert(1,"ow");

else

s.push_back('o');

cout << s;

}

return( 0 );

}

A.

It prints: Hoto

B.

It prints: Ht

C.

It prints: toHo

D.

It prints: Howto

Full Access
Question # 23

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

#include <iostream>

using namespace std;

namespace myNamespace1

{

int x = 5;

int y = 10;

}

namespace myNamespace2

{

float x = 3.14;

float y = 1.5;

}

int main () {

namespace newname = myNamespace1;

using namespace newname;

cout << x << " ";

cout << y;

return 0;

}

A.

It prints: 5 1.5

B.

It prints: 3.14 1.5

C.

It prints: 5 10

D.

It prints: 5

Full Access
Question # 24

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

#include <iostream>

using namespace std;

int fun(int x) {

return 2*x;

}

int main(){

int i;

i = fun(1) || fun(2);

cout << i;

return 0;

}

A.

It prints: 0

B.

It prints: 1

C.

It prints: -1

D.

Compilation error

Full Access
Question # 25

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

#include <iostream>

using namespace std;

#define A 1

int main()

{

#if A

cout<<"Hello";

#endif

cout<<"world";

return 0;

}

A.

It will print: Helloworld

B.

It will print: Hello

C.

It will print: world

D.

It will print: 0

Full Access
Question # 26

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

#include <iostream>

using namespace std;

int main(){

int i = 1;

if (i++==1) {

cout << i;

} else {

cout << i-1;

}

return 0;

}

A.

It prints: 0

B.

It prints: 1

C.

It prints: -1

D.

It prints: 2

Full Access
Question # 27

Which code, inserted at line 10, generates the output "2?1"?

#include <iostream>

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

b.set();

b.z = ?1;

b.Print();

return 0;

}

A.

class B : private A {

B.

class B : public A {

C.

class B : protected A {

D.

class B {

Full Access
Question # 28

What is the output of the program if character 3 is supplied as input?

#include <iostream>

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

}

}

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;

}

A.

It prints: float exception. Exception Nr.

B.

It prints: int exception. Exception Nr.

C.

It prints: An exception occurred.

D.

It prints: float exception. Exception Nr.

Full Access
Question # 29

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

#include <iostream>

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;

}

A.

It prints: 4

B.

It prints: 6

C.

It prints: 3

D.

It prints: 0

Full Access
Question # 30

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

#include <iostream>

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

Full Access
Question # 31

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

#include <iostream>

#include

using namespace std;

class A {

int x;

protected:

int y;

public:

int z;

};

class B : public A {

string name;

public:

void set() {

y = 2;

z = 3;

}

void Print() { cout << y << z; }

};

int main () {

B b;

b.set();

b.Print();

return 0;

}

A.

It prints: 123

B.

It prints: 000

C.

It prints: 23

D.

It prints: 12

Full Access
Question # 32

What is the output of the program if characters 't', 'e', 's' and 't' enter are supplied as input?

#include <iostream>

#include

using namespace std;

int main()

{

string s;

getline( cin, s );

cout << s << " " << s.length();

return( 0 );

}

A.

It prints: test 4

B.

It prints: test

C.

It prints: test 5

D.

It prints: 4

Full Access
Question # 33

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

#include <iostream>

using namespace std;

int fun(int x) {

return 2*x;

}

int main(){

int i;

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

cout << i;

return 0;

}

A.

It prints: 0

B.

It prints: 1

C.

It prints: -1

D.

Compilation error

Full Access