Skip to main content

CPA-21-02 Real Exam Questions

CPA - C++ Certified Associate Programmer

257 questions available · Page 1 of 26

Updated Exam DumpsVerified AnswersPass Guarantee

Get Complete Exam Dumps
Question 1 Single choice

What is the output of the program?

#include <iostream>

#include <string>

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

}

  1. A

    It prints: JohnSteve

  2. B

    It prints: AlanAlan

  3. C

    It prints: AlanSteve

  4. D

    It prints: JohnAlan

Show answer and explanation

Correct answer: A

Question 2 Multiple choice

Which of the following structures are correct?

1:

struct s1{

int x;

char c;

};

2:

struct s2{

float f;

struct s2 *s;

};

3:

struct s3{

float f;

in i;

}

  1. A

    1

  2. B

    2

  3. C

    3

  4. D

    All of these

Show answer and explanation

Correct answers: A, B

Question 3 Single choice

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

#include <iostream>
using namespace std;
struct {
int x;
char c;
union {
float f;
int i;
};
} s;
int main (int argc, const char * argv[])
{
s.x=10;
s.i=0;
cout << s.i << " " << s.x;
}

  1. A

    It prints: 0 10

  2. B

    It prints: 11

  3. C

    Compilation error

  4. D

    None of these

Show answer and explanation

Correct answer: A

Question 4 Single choice

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

#include <iostream>

#include <string>

using namespace std;

class First

{

string *s;

public:

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

~First() { delete s;}

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

};

int main()

{

First FirstObject;
FirstObject.~First();

}

  1. A

    It prints: Text

  2. B

    Compilation error

  3. C

    Runtime error.

  4. D

    None of these

Show answer and explanation

Correct answer: C

Question 5 Single choice

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

#include <iostream>

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;

}

  1. A

    It prints: 2

  2. B

    It prints: 5

  3. C

    It prints: 10

  4. D

    It prints: 0

Show answer and explanation

Correct answer: A

Question 6 Single choice

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

#include <iostream>

#include <string>

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;

}

  1. A

    It will print: Hello World

  2. B

    It will print: Hello

  3. C

    It will print: World

  4. D

    It will print: HW

Show answer and explanation

Correct answer: A

Question 7 Single choice

What is the output of the program?
#include <iostream>

using namespace std;

#define SQR(x)(x*x)

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

int x, y=2;

x = SQR(y);

cout << x << ", " <<y;

return 0;

}

  1. A

    It prints: 3, 2

  2. B

    It prints: 4, 2

  3. C

    It prints: 3, 3

  4. D

    It prints: 9, 2

Show answer and explanation

Correct answer: B

Question 8 Single choice

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

}

  1. A

    It prints: from First

  2. B

    It prints: from Firstfrom First

  3. C

    It prints: from Firstfrom Second

  4. D

    It prints: from Secondfrom Second

Show answer and explanation

Correct answer: C

Question 9 Single choice

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

#include <iostream>

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

}

  1. A

    It prints: 20

  2. B

    It prints: 10

  3. C

    It prints: 1020

  4. D

    It prints: 2010

Show answer and explanation

Correct answer: A

Question 10 Single choice

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

#include <iostream>

#include <string>

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;

}

  1. A

    It prints: 5

  2. B

    It prints: 1?5

  3. C

    It prints: 05

  4. D

    It prints: 0

Show answer and explanation

Correct answer: B