Free CPA-21-02 Practice Test Questions and Answers (2026)
Last Update Check
Q: 1
What happens when you attempt to compile and run the following code?
#include
using namespace std;
class BaseClass
{
public:
int *ptr;
BaseClass(int i) { ptr = new int(i); }
~BaseClass() { delete ptr; delete ptr;}
void Print() { cout << *ptr; }
};
void fun(BaseClass x);
int main()
{
BaseClass o(10);
fun(o);
o.Print();
}
void fun(BaseClass x) {
cout << "Hello:";
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 2
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 :
void print() {
cout << "B ";
}
};
int main() {
B sc[2];
A *bc = (A*)sc;
for (int i=0; iprint();
return 0;
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 3
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
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 4
Which of the following is a user defined data type?
1:
struct person
{
char name[20];
int age;
};
2:
int l=2;
3:
enum color {red,blue, green};
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 5
What happens when you attempt to compile and run the following code?
#include
#include
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.Print();
FirstObject.~First();
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 6
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main (int argc, const char * argv[])
{
int tab[5]={1,2,3};
for (int i=0; i<5; i++)
cout <<tab[i];
return 0;
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 7
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main()
{
const char *s;
char str[] = "Hello";
s = str;
while(*s) {
cout << *s++;
}
return 0;
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 8
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
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 9
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 <setName();
t?>Print();
t = ob2;
t?>setName("Steve");
ob2?>Print();
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 10
Which code, inserted at line 18, generates the output "AB"
#include
using namespace std;
class A
{
public:
void Print(){ cout<< "A";}
void Print2(){ cout<< "a";}
};
class B:public A
{
public:
void Print(){ cout<< "B";}
void Print2(){ cout<< "b";}
};
int main()
{
B ob2;
//insert code here
ob2.Print();
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 11
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()
{
First FirstObject;
FirstObject.Print();
Second SecondObject;
SecondObject.Print();
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 12
What will be the output of the program?
#include
#include
using namespace std;
int fun(int);
int main()
{
float k=3;
k = fun(k);
cout<<k;
return 0;
}
int fun(int i)
{
i++;
return i;
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 13
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<Print();
B ob2;
obj = &ob2;
obj?>Print();
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 14
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
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 15
Which code line inserted instead of the comment below will fix the program so that it produces the
expected output and does not cause any memory leak?


Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 16
What will happen if the memory cannot be allocated?


Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 17
Which line of code inserted instead of the comment will make the following code run properly
without causing memory leaks?


Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 18
What happens if characters 'w', 'o', 'r', 'l' and 'd' are entered as input?
#include
#include
using namespace std;
int main()
{
string s1 = "Hello";
string s2;
getline( cin, s2 );
cout << s1 + s2;
return( 0 );
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 19
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;
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Q: 20
What happens when you attempt to compile and run the following code?
#include
using namespace std;
void print(char *c);
int main (int argc, const char * argv[])
{
print("Test");
return 0;
}
void print(char *c)
{
cout<<c;
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Question 1 of 20