C++ Institute CPA-21-02 Exam Questions
CPA - C++ Certified Associate Programmer (Page 7 )

Updated On: 2-Mar-2026

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

#include <iostream>

using namespace std;

int main (int argc, const char * argv[])
{
int x,y;
union t
{
char tab[2];

int i;
};
union t u;

  1. tab[0] = 1;
  2. tab[1] = 2;
  3. i = 0;
    x = u.tab[0];
    y = u.tab[1];
    cout << x << "," << y << "," << u.i;
    return 0;
    }
  4. compilation fails
  5. It prints: 0,0,0
  6. It prints: 1,2,0
  7. None of these

Answer(s): B



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

#include <iostream>
#include <string>
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;

  1. Print();
    return 0;
    }
  2. It prints: 3
  3. It prints: 0
  4. It prints: 1
  5. It prints: 2

Answer(s): A



Which code, inserted at line 10, generates the output "Hello World"?

#include <iostream>
#include <string>
using namespace std;
string fun(string, string);
int main()
{
string s="Hello";
string *ps;
ps = &s;
//insert code here return 0;
}

string fun(string s1, string s2)
{
return s1+s2;

}

  1. cout << fun(" World");
  2. cout << fun(*ps);
  3. cout << fun("Hello");
  4. cout << fun("Hello", " World");

Answer(s): D



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

#include <iostream>

using namespace std;

int x=5;
static int y=0;

void myFunction(int a)
{
y=++a;
}

int main (int argc, const char * argv[])
{
int i=0;

myFunction(i);
cout<<y<<" "<<x;
}

  1. It prints: 0 5
  2. It prints: 5 1
  3. It prints: 1 5
  4. It prints: 5 0

Answer(s): C



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

#include <iostream>
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();
}

  1. It prints: BBB
  2. It prints: AAA
  3. It prints: ABC
  4. It prints: ABB

Answer(s): B



Viewing page 7 of 53
Viewing questions 31 - 35 out of 257 questions



Post your Comments and Discuss C++ Institute CPA-21-02 exam dumps with other Community members:

CPA-21-02 Exam Discussions & Posts

AI Tutor