Labour Day Special - 65% Discount Offer - Ends in 0d 00h 00m 00s - Coupon code: c4sdisc65

Note! CPA has been withdrawn. The new exam code is CPA-21-02

CPA Practice Exam Questions with Answers C++ Certified Associate Programmer Certification

Question # 6

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

#include

using namespace std;

namespace myNamespace1

{

int x = 5;

int y = 10;

}

namespace myNamespace2

{

float x = 3.14;

float y = 1.5;

}

int main () {

{

using namespace myNamespace1;

cout << x << " ";

}{

using namespace myNamespace2;

cout << y;

}

return 0;

}

A.

It prints: 5 1.5

B.

It prints: 3.14 10

C.

Compilation error

D.

None of these

Full Access
Question # 7

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;

}

A.

It prints: 0

B.

It prints: 4.9

C.

It prints: 5

D.

It prints: 4

Full Access
Question # 8

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;

}

A.

It prints: A A

B.

It prints: B B

C.

It prints: A B

D.

It prints: B A

Full Access
Question # 9

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;

}

A.

using myNamespace2::y; using myNamespace1::x;

B.

using namespace myNamespace1;

C.

using namespace myNamespace1; using namespace myNamespace2;

D.

using myNamespace1::y; using myNamespace2::x;

Full Access
Question # 10

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();

}

A.

Garbage value

B.

It prints: AlanAlan

C.

It prints: Alan

D.

It prints: Al

Full Access
Question # 11

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

#include

#include

using namespace std;

class B;

class A {

int age;

public:

A () { age=5; };

friend class B;

};

class B {

string name;

public:

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

void Print(A ob) {

cout << name << ob.age;

}

};

int main () {

A a;

B b;

b.Print(a);

return 0;

}

A.

It prints: Bob5

B.

It prints: Bob

C.

It prints: 5

D.

None of these

Full Access
Question # 12

Which of the following statements are correct?

A.

A function can be defined inside another function

B.

A function may have any number of return statements each returning different values.

C.

A function can return floating point value

D.

In a function two return statements should never occur.

Full Access
Question # 13

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

#include

using namespace std;

int main()

{

const int x=0;

const int *ptr;

ptr = &x;

cout<<*ptr;

return 0;

}

A.

It prints: 0

B.

It prints address of x

C.

It prints: 1

D.

Compilation error

Full Access
Question # 14

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;

}

A.

It prints: My exception.

B.

It prints: 0

C.

It prints: 1

D.

Compilation error

Full Access
Question # 15

Which code, inserted at line 5, generates the output "ABC"?

#include

using namespace std;

class A {

public:

//insert code here

};

class B:public A {

public:

void Print(){ cout<< "B"; }

};

class C:public B {

public:

void Print(){ cout<< "C"; }

};

int main()

{

A ob1;

B ob2;

C ob3;

A *obj;

obj = &ob1;

obj?>Print();

obj = &ob2;

obj?>Print();

obj = &ob3;

obj?>Print();

}

A.

void Print(){ cout<<"A";}

B.

virtual void Print(){ cout<<"A";}

C.

virtual void Print(string s){ cout<

D.

None of these

Full Access
Question # 16

How could you pass arguments to functions?

A.

by value

B.

by reference

C.

by pointer

D.

by void

Full Access
Question # 17

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;

}

A.

25

B.

5

C.

0

D.

1

Full Access
Question # 18

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

#include

using namespace std;

int main()

{

int i = 5;

do {

i??;

cout<

}

while(i >= 0);

return 0;

}

A.

It prints: 43210?1

B.

It prints: ?1

C.

It prints: 4321

D.

It prints: 1

Full Access
Question # 19

If there is one, point out an error in the program

#include

using namespace std;

int main()

{

int i=1;

for(;;)

{

cout<

if(i>5)

break;

}

return 0;

}

A.

Error in “if” statement

B.

Error in “for” loop

C.

No error

D.

Error in break statement

Full Access
Question # 20

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;

}

A.

It prints: 1

B.

It prints: 100

C.

It prints: 0

D.

It prints: 10

Full Access
Question # 21

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

#include

#include

using namespace std;

int main()

{

string s1[]= {"How" , "to" };

s1[0].swap(s1[1]);

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

cout << s1[i];

}

return( 0 );

}

A.

It prints: Hoto

B.

It prints: toHow

C.

It prints: Ht

D.

It prints: to

Full Access
Question # 22

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;

}

A.

It prints: 1,2

B.

It prints: ?1,1

C.

It prints: 1,1

D.

It prints: 3,3

Full Access
Question # 23

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

#include

using namespace std;

void fun(char*);

int main()

{

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

fun(&t[0]);

return 0;

}

void fun(char *a)

{

cout << *a;

}

A.

It prints: 01

B.

It prints: 1

C.

It prints: 0

D.

It prints: 0123

Full Access
Question # 24

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(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 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) {}

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

b.Print();

return 0;

}

A.

It prints: 10

B.

It prints: 2

C.

It prints: 5

D.

It prints: 1

Full Access
Question # 26

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

#include

#include

using namespace std;

int main()

{

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

string s;

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

s = s1[i];

s.insert(1,"o");

cout << s;

}

return( 0 );

}

A.

It prints: Hoto

B.

It prints: Ho

C.

It prints: to

D.

It prints: Ht

Full Access
Question # 27

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;

}

A.

It will print: Helloworld

B.

It will print: Hello

C.

It will print: world

D.

It will print: 0

Full Access
Question # 28

Which code, inserted at line 19, generates the output "23"?

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

string z;

public:

int y;

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

void Print() {

//insert code here

}

};

int main () {

B b;

b.set();

b.Print();

return 0;

}

A.

cout << y << z;

B.

cout << y << A::z;

C.

cout << A::y << A::z;

D.

cout << B::y << B::z;

Full Access
Question # 29

Which code, inserted at line 18, generates the output "AB"

#include

using namespace std;

class A

{

public:

void Print(){ cout<< "A";}

void Print2(){ cout<< "a";}

};

class B:public A

{

public:

void Print(){ cout<< "B";}

void Print2(){ cout<< "b";}

};

int main()

{

B ob2;

//insert code here

ob2.Print();

}

A.

ob2?>A::Print();

B.

ob2.B::Print();

C.

ob2?>B::Print();

D.

ob2.A::Print();

Full Access
Question # 30

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;

}

A.

It prints: 202020

B.

It prints: 012

C.

It prints: 024

D.

It prints: 0

Full Access
Question # 31

Which code, inserted at line 12, generates the output "5b"?

#include

using namespace std;

namespace myNamespace1

{

int var = 5;

}

namespace myNamespace2

{

char var = 'b';

}

int main () {

//insert code here

return 0;

}

A.

cout << myNamespace1::var << var;

B.

cout << var << var;

C.

cout << myNamespace1::var << myNamespace2::var;

D.

None of these

Full Access
Question # 32

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

#include

using namespace std;

int f(int a, int b);

int main()

{

float b;

b = f(20,10);

cout << b;

return 0;

}

int f(int a, int b)

{

return a/b;

}

A.

It prints: 2

B.

It prints: 5

C.

It prints: 10

D.

It prints: 0

Full Access
Question # 33

What is the output of the program?

#include

#include

using namespace std;

int main()

{

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

string s;

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

s = s1[i];

s.insert(1,"ow");

cout << s;

}

return( 0 );

}

A.

It prints: How

B.

It prints: Ht

C.

It prints: Hoto

D.

It prints: Howtow

Full Access