Vcehome > C++ Institute > C++ Certified Professional Programmer > CPA > CPA Online Practice Questions and Answers

CPA Online Practice Questions and Answers

Questions 4

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

#include

#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

Browse 220 Q&As
Questions 5

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 anda) { name = a.name;}

};

class B : public A {

public:

int *tab;

B() { tab = new int[size]; for (int i=0; i

B(string s) : A(s) { tab = new int[size]; for (int i=0; i

~B() { delete tab; }

void Print() {

for (int i=0; i

cout << name;

}

};

int main () {

B b1("Alan");

B b2;

b1.tab[0]=0;

b1.Print(); b2.Print();

return 0;

}

A. It prints: Alan

B. It prints: 111

C. It prints: 011Alan111Bob

D. It prints: 0

Browse 220 Q&As
Questions 6

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

#include

#include

using namespace std;

void fun(int i);

int main()

{

int i=0;

i++;

for (i=0; i<=5; i++)

{

fun(i);

}

return 0;

}

void fun(int i)

{

if (i==3)

return;

cout << i;

}

A. It prints: 05

B. It prints: 012345

C. It prints: 01245

D. It prints: 0

Browse 220 Q&As
Questions 7

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

#include

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

Browse 220 Q&As
Questions 8

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

#include

using namespace std;

int main() {

int i, j;

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

for(j = i; j < i + 1; j++)

if(j == i)

continue;

else

break;

}

cout << j;

return 0;

}

A. It prints: 0

B. It prints: 3

C. It prints: 2

D. It prints: 1

Browse 220 Q&As
Questions 9

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

A. insert(1,"ow"); cout << s; } return( 0 ); }

B. It prints: How

C. It prints: Ht

D. It prints: Hoto

E. It prints: Howtow

Browse 220 Q&As
Questions 10

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] = i;

}

cout << t[1];

}

A. It prints: 0

B. It prints: 1

C. It prints: 10

D. It prints: ?1

Browse 220 Q&As
Questions 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";}

};

int main()

{

B ob2;

A *obj;

obj = andob2;

obj?>Print();

}

A. It prints: B

B. It prints: A

C. It prints: AB

D. It prints: BA

Browse 220 Q&As
Questions 12

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

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

Browse 220 Q&As
Questions 13

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;

}

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.

Browse 220 Q&As
Exam Code: CPA
Exam Name: C++ Certified Associate Programmer
Last Update: May 09, 2024
Questions: 220 Q&As

PDF

$49.99

VCE

$59.99

PDF + VCE

$67.99