Winter Special Flat 65% Limited Time Discount offer - Ends in 0d 00h 00m 00s - Coupon code: suredis

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 if character 3 is entered as input?

#include

using namespace std;

class A {

public:

int i;

};

int main () {

int c;

A obj;

obj.i = 5;

cin >> c;

try

{

switch (c)

{

case A. throw 20;

case B. throw 5.2f;

case C. throw obj;

default: cout<<"No exception";

}

}

catch (int e)

{ cout << "int exception. Exception Nr. " << e; }

catch (A e)

{ cout << "object exception. Exception Nr. " << e.i; }

catch (...)

{ cout << "An exception occurred."; }

return 0;

}

Options:

A.

It prints: object exception. Exception Nr. 5

B.

It prints: int exception. Exception Nr.

C.

It prints: An exception occurred

D.

It prints: No exception

Question 2

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

Question 3

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

#include

using namespace std;

int main(){

int i = 1;

for(i=10; i>-1; i/=2) {

if(!i)

break;

}

cout << i;

return 0;

}

Options:

A.

It prints: 0

B.

It prints: 1

C.

It prints: -1

D.

Compilation error

Question 4

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

#include

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

}

Options:

A.

It prints: 5

B.

It prints: 10

C.

It prints: 1

D.

It prints: 0

Question 5

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

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

void set() {

y = 4;

z = "John";

}

void Print() {

cout << y << z;

}

};

int main () {

B b;

b.set();

b.Print();

return 0;

}

Options:

A.

It prints: 4John

B.

It prints: 2John

C.

It prints: 23

D.

It prints: 43

Question 6

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

#include

using namespace std;

class A

{

public:

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

};

class B:public A

{

public:

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 7

What is the output of the program?

#include

#include

using namespace std;

int main()

{

string s1="Hello";

string s2="World";

s1+=s2;

cout << s1;

return( 0 );

}

Options:

A.

It prints: HelloWorld

B.

It prints: Hello

C.

It prints: World

D.

It prints: HelWorld

Question 8

What is the output of the program given below?

#include

using namespace std;

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

{

int i=10;

{

int i=0;

cout<

}

{

int i=5;

cout << i;

}

cout<

return 0;

}

Options:

A.

1010

B.

101010

C.

0510

D.

None of these

Question 9

Which statement should be added in the following program to make work it correctly?

using namespace std;

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

{

cout<<"Hello";

}

Options:

A.

#include

B.

#include

C.

#include

D.

#include

Question 10

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

#include

using namespace std;

int getValue();

int main()

{

const int x = getValue();

cout<

return 0;

}

int getValue()

{

return 5;

}

Options:

A.

It will print 0

B.

The code will not compile.

C.

It will print 5

D.

It will print garbage value

Question 11

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 12

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

#include

using namespace std;

int main()

{

int *t;

t = new int[2];

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

t[i]=0;

}

cout << t[1];

}

Options:

A.

It prints: 0

B.

It prints: 1

C.

It prints: 2

D.

It prints: 3

Question 13

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

#include

using namespace std;

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

{

int x,y;

union t

{

char tab[2];

int i;

};

union t u;

u.tab[0] = 1;

u.tab[1] = 2;

u.i = 0;

x = u.tab[0];

y = u.tab[1];

cout << x << "," << y << "," << u.i;

return 0;

}

Options:

A.

compilation fails

B.

It prints: 0,0,0

C.

It prints: 1,2,0

D.

None of these

Question 14

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

#include

#include

#include

using namespace std;

int main(void)

{

string s;

s = "Test";

s.resize (s.size() ? 1);

cout<

return 0;

}

Options:

A.

It prints: Test 4

B.

It prints: Test 3

C.

Compilation error

D.

It prints: Tes 3

Question 15

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

Question 16

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

#include

using namespace std;

int main()

{

int x=20;

int *ptr;

ptr = &x;

cout<<*ptr;

return 0;

}

Options:

A.

It prints: 20

B.

It prints: 0

C.

It prints address of ptr

D.

It prints: 2

Question 17

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) { 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: 3

B.

It prints: 0

C.

It prints: 1

D.

It prints: 2

Question 18

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

class A {

int x;

protected:

int y;

public:

int age;

};

class B : protected A {

string name;

public:

void Print() {

cout << name << age;

}

};

Options:

A.

public

B.

private

C.

protected

D.

None of these

Question 19

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

#include

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

}

}

Options:

A.

It prints: OK

B.

It prints: ERROR

C.

Compilation error

D.

Runtime error.

Question 20

What is the output of the program if character “1” 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. 5.2

B.

It prints: int exception. Exception Nr. 20

C.

It prints: An exception occurred

D.

Compilation Error

Question 21

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 22

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

#include

#include

using namespace std;

int fun(int);

int main()

{

int *x = new int;

*x=10;

cout << fun(*x);

return 0;

}

int fun(int i)

{

return i*i;

}

Options:

A.

It will print: 100

B.

It will print: 101

C.

It will print: 10

D.

It will print: 1

Question 23

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;

}

Options:

A.

cout << myNamespace1::var << var;

B.

cout << var << var;

C.

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

D.

None of these

Question 24

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

#include

#include

using namespace std;

class SampleClass

{

string *s;

public:

SampleClass() { s = new string("Text");}

SampleClass(string s) { this?>s = new string(s);}

~SampleClass() { delete s;}

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

};

int main()

{

SampleClass *obj;

obj = new SampleClass("Test");

obj?>Print();

}

Options:

A.

It prints: Text

B.

It prints: Test

C.

It prints: TextTest

D.

Garbage value.

Question 25

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

#include

using namespace std;

int main()

{

int i = 5;

cout<<"Hello World" << ++i;

return 0;

}

Options:

A.

It prints: Hello World6

B.

It prints: Hello

C.

It prints: World

D.

It prints: Hello World5

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

}

Options:

A.

It prints: Hoto

B.

It prints: Ho

C.

It prints: to

D.

It prints: Ht

Question 27

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

break;

case 2:

cout<<"world";

break;

case 3:

printf("End");

break;

}

return 0;

}

Options:

A.

It prints: Hello

B.

It prints: world

C.

It prints: End

D.

It prints: E

Question 28

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

}

Options:

A.

It prints: Hoto

B.

It prints: toHow

C.

It prints: Ht

D.

It prints: to

Question 29

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

#include

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

}

Options:

A.

It prints: 123

B.

It prints: 1

C.

It prints: ?123

D.

Compilation error

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;

}

Options:

A.

It prints: 202020

B.

It prints: 012

C.

It prints: 024

D.

It prints: 0

Question 31

What is the output of the program?

#include

#include

using namespace std;

int main()

{

char str[] = "Hello\0\World\0";

cout << str;

return 0;

}

Options:

A.

It prints: Hello

B.

It prints: World

C.

It prints: HW

D.

It prints: World\0World

Question 32

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

#include

using namespace std;

int main()

{

union un

{

int x;

char c;

};

union un u1 = {10};

union un u2 = {'a'};

union un u3 = {20, 'a'};

cout<

cout<

cout<

return 0;

}

Options:

A.

It prints: 10aa

B.

It prints: 10a20a

C.

It prints: 1a

D.

Compilation error

Question 33

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;

t.Print();

}

Options:

A.

It prints: 30

B.

It prints: John

C.

It prints: John 30

D.

It prints: John 30John 30

Demo: 33 questions
Total 220 questions