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

C++ Institute CPA C++ Certified Associate Programmer Exam Practice Test

Demo: 33 questions
Total 220 questions

C++ Certified Associate Programmer Questions and Answers

Question 1

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

Question 2

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;

}

Options:

A.

25

B.

5

C.

0

D.

1

Question 3

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 A

{

public:

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

};

int main()

{

A *obj;

A ob1;

obj = &ob1;

obj?>Print();

B ob2;

obj = &ob2;

obj?>Print();

}

Options:

A.

It prints: AB

B.

It prints: AA

C.

It prints: BA

D.

It prints: BB

Question 4

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

#include

using namespace std;

void fun(int*);

int main()

{

int i=2;

fun(&i);

cout<

return 0;

}

void fun(int *i)

{

*i = *i**i;

}

Options:

A.

It prints: 1

B.

It prints: 4

C.

It prints: 10

D.

It prints: 0

Question 5

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

#include

using namespace std;

class Test {

float i,j;

};

class Add {

public:

int x,y;

Add (int a=3, int b=3) { x=a; y=b; }

int result() { return x+y;}

};

int main () {

Test test;

Add * padd;

padd = &test;

cout << padd?>result();

return 0;

}

Options:

A.

It prints: 6

B.

It prints: 9

C.

Compilation error

D.

It prints: 33

Question 6

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.

Garbage value

Question 7

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

public:

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

}

Options:

A.

It prints: BBB

B.

It prints: AAA

C.

It prints: ABC

D.

It prints: ABB

Question 8

Which of the following is a logical operator?

Options:

A.

&

B.

&&

C.

||

D.

!

Question 9

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

#include

using namespace std;

int main()

{

int i=2;

switch(i)

{

case 1:

cout<<"Hello";

case 2:

cout<<"world";

case 3:

cout<<"End";

} return 0;

}

Options:

A.

It prints: Hello

B.

It prints: world

C.

It prints: worldEnd

D.

It prints: End

Question 10

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

#include

using namespace std;

int main() {

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

cout << i;

return 0;

}

Options:

A.

It prints: 0

B.

It prints: 1

C.

It prints: 2

D.

It prints: 0.5

Question 11

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

}

Options:

A.

It prints: BC

B.

It prints: CB

C.

It prints: CC

D.

It prints: BB

Question 12

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

Question 13

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;

}

Options:

A.

It prints: 7

B.

It prints: 6

C.

It prints: 7,1

D.

Compilation error

Question 14

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

#include

using namespace std;

int main(){

int i, j;

for(i = 0, j = 1; j < 2, i < 4; i++, j++);

cout << i << " " << j;

return 0;

}

Options:

A.

It prints: 4 5

B.

It prints: 2 3

C.

It prints: 3 2

D.

It prints: 4 3

Question 15

What is the output of the program?

#include

#include

using namespace std;

int main()

{

string s1="Wo";

string s2;

s2 = s1;

string s3;

s3 = s2.append("rldHello");

cout << s3;

return( 0 );

}

Options:

A.

It prints: WorldHello

B.

It prints: HelloWo

C.

It prints: World

D.

It prints: Hello

Question 16

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;

}

Options:

A.

It prints: 0

B.

It prints: 1

C.

It prints: 2

D.

It prints: 4

Question 17

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;

}

};

Options:

A.

public

B.

private

C.

protected

D.

None of these

Question 18

What is the output of the program?

#include

using namespace std;

#define SQR(x)(x*x)

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

int x, y=2;

x = SQR(y);

cout << x << ", " <

return 0;

}

Options:

A.

It prints: 3, 2

B.

It prints: 4, 2

C.

It prints: 3, 3

D.

It prints: 9, 2

Question 19

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

return 0;

}

Options:

A.

It prints: 1 0

B.

It prints: 1 1

C.

It prints: 0 0

D.

Compilation error

Question 20

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

#include

using namespace std;

int min(int a, int b);

int main()

{

int min(int,int);

int b;

b = min(10,20);

cout << b;

return 0;

}

int min(int a, int b)

{

return(b);

}

Options:

A.

It prints: 20

B.

It prints: 10

C.

It prints: 1020

D.

It prints: 2010

Question 21

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

#include

using namespace std;

class Base {

static int age;

public:

Base () {};

~Base () {};

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

void Print() { cout << age;}

};

int Base::age=0;

int main () {

Base a;

a.setAge(10);

a.Print();

a.setAge();

a.Print();

return 0;

}

Options:

A.

It prints: 10

B.

It prints: 20

C.

It prints: 1020

D.

It prints: 2010

Question 22

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

}

Options:

A.

It prints: 10

B.

It prints: ?1

C.

It prints: 0

D.

Compilation error

Question 23

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.90;

cout << f << ", ";

x=f;

cout << x <<", ";

f=y;

cout << f;

return 0;

}

Options:

A.

It prints: 5, 5, 10.00

B.

It prints: 5.9, 5, 10

C.

It prints: 6, 5, 10

D.

It prints: 6, 5, 10.00

Question 24

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

#include

using namespace std;

int compare(int, int);

int main()

{

int x = compare(10, 20);

cout << x;

return 0;

}

int compare(int i, int j)

{

return i

}

Options:

A.

It prints: 0

B.

It prints: 2

C.

It prints: 1

D.

It prints: 10

Question 25

What is the output of the program if character 3 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';

}

}

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;

}

Options:

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.

Question 26

What is the output of the program?

#include

#include

using namespace std;

class First

{

string name;

public:

First() {

name = "Alan";

}

void setName(string n) {this?>name = n;}

void setName() {this?>name = "John";}

void Print(){

cout << name;

}

};

int main()

{

First ob1,*ob2;

ob2 = new First();

First *t;

t = &ob1;

t?>setName();

t?>Print();

t = ob2;

t?>setName("Steve");

ob2?>Print();

}

Options:

A.

It prints: JohnSteve

B.

It prints: AlanAlan

C.

It prints: AlanSteve

D.

It prints: JohnAlan

Question 27

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;

}

Options:

A.

It prints: 4

B.

It prints: 0

C.

It prints: 3

D.

It prints: 2

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;

}

Options:

A.

cout << y << z;

B.

cout << y << A::z;

C.

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

D.

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

Question 29

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;

}

Options:

A.

It prints: 43210?1

B.

It prints: ?1

C.

It prints: 4321

D.

It prints: 1

Question 30

What is the output of the program?

#include

using namespace std;

class Base {

static int age;

public:

Base () {};

~Base () {};

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

void Print() { cout << age;}

};

int Base::age=0;

int main () {

Base a,*b;

b = new Base();

a.setAge();

b?>setAge(20);

a.Print();

b?>Print();

return 0;

}

Options:

A.

It prints: 2020

B.

It prints: 1020

C.

It prints: 20

D.

It prints: 10

Question 31

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

}

Options:

A.

It prints: from First

B.

It prints: from Firstfrom First

C.

It prints: from Secondfrom Second

D.

It prints: from Second

Question 32

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

#include

#include

using namespace std;

struct Person {

string name;

int age;

};

class First

{

Person *person;

public:

First() {person = new Person;

person?>name = "John";

person?>age = 30;

}

void Print(){

cout<name << " "<< person?>age;

}

};

int main()

{

First t[2];

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

t[i].Print();

}

Options:

A.

It prints: 30

B.

It prints: John

C.

It prints: John 31

D.

It prints: John 30John 30

Question 33

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

#include

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

}

Options:

A.

class B : private A {

B.

class B : public A {

C.

class B : protected A {

D.

class B {

Demo: 33 questions
Total 220 questions