CPA – Programming Essentials in C++ Final Exam Answers
Question 1: What is the output of the following snippet?
#include <iostream>
using namespace std;
namespace OuterSpace
{
int x = 1;
int y = 2;
}
namespace InnerSpace
{
float x = 3.0;
float y = 4.0;
}
int main () {
{ using namespace InnerSpace;
cout << x << ” “;
}{
using namespace OuterSpace; using InnerSpace::y;
cout << y;
}
return 0;
}
- 3 2
- Compilation error
- 3 1
- 3 4
Question 2: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
class Uno {
public: ~Uno() { cout << “X”; }
};
void foo(Uno *d)
{
Uno e;
*d = e;
}
int main()
{
Uno *u = new Uno;
foo(u);
delete u;
return 0;
}
- XXX
- XXXX
- XX
- X
Question 3: Which code, inserted into class C, generates the output by? Choose all correct answers.
#include <iostream>
#include <string>
using namespace std;
class Uno {
protected: char y;
public: char z;
};
// insert code here
{
public:
void set() {
y = ‘a’; z = ‘z’;
}
void out() { cout << ++y << –z; }
};
int main () {
Due b;
b.set();
b.out();
return 0;
}
- class Due : public Uno
- class Due : private Uno
- class Due
- class Due : protected Uno
Question 4: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
int cnt = 10;
do {
cnt–;
if (cnt % 3 == 2)
break;
cout << cnt;
}
while(cnt);
return 0;
}
- 109
- 9
- 987654321
- 10987654321
Question 5: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
int i = 2;
float f = 1.4;
char c = ‘a’;
bool b = true;
c += i + f + b;
cout << c;
return 0;
}
- e
- f
- d
- c
Question 6: What is the output of the following snippet?
#include <iostream>
using namespace std;
class A
{
public: void out(){ cout << “A”; }
};
class B : public A
{
public: void out(){ cout << “B”; }
};
int main()
{
A *a;
a = new A();
a -> out();
a = new B();
a -> out();
return 0;
- AA
- BB
- BA
- AB
Question 7: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
struct Who
{
string nick;
};
class She
{
Who *who;
public:
She() {
who = new Who;
who -> nick = “Jane”;
}
string out (){
return who -> nick;
}
};
int main()
{
She they[2];
for(int i = 0; i < 2; i++)
cout << they[i].out();
return 0;
}
- It prints nothing
- Jane
- Runtime error
- JaneJane
Question 8: Which statement will you add in the following program to make it correct?
#include <string>
int main() {
std::string s = “Here I am!”;
std::cout << s;
return 0;
}
- #include
- #include
- #include
- #include
Question 9: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
char *abc;
abc = new char[26];
for(int i = 0; i < 26; i++)
abc[i] = ‘a’ + i;
cout << *(abc + 2);
return 0;
}
- a
- d
- b
- c
Question 10: Variable r in class C3, will be…
#include <iostream>
using namespace std;
class C1
{
public int p;
private int q;
protected int r;
};
class C2 : private C1 {};
class C3 : public C2 {};
- private
- None of these
- public
- protected
Question 11: What is the output of the following snippet?
#include <iostream>
using namespace std;
#define CALL(param) { if(param) cout << param++; }
int main()
{
int i = 1;
CALL(i);
cout << i;
return 0;
}
- 11
- 1
- 12
- 2
Question 12: What is the output of the following snippet?
#include <iostream>
using namespace std;
int doit(int i, int j = 0)
{
return (i * j);
}
int main ()
{
cout << doit(doit(1,2));
return 0;
}
- 12
- 1
- 112
- 0
Question 13: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
struct S
{
char *p;
};
class C
{
S s;
public:
C() { s.p = new char; *s.p = ‘A’; }
void p() { cout << ++(*s.p); }
};
int main()
{
C *c = new C();
c->p();
return 0;
}
- B
- It prints garbage value
- Compilation error
- A
Question 14: Variable y in class Y, will be…
class X {
private: int x;
protected: int y;
public: int z;
};
class Y : protected X {
};
- public
- private
- protected
- none of these
Question 15: What is the output of the following snippet?
#include <iostream>
using namespace std;
int doit(int a, float b)
{
return a / b;
}
int main()
{
float x = doit(1.5f, 2l);
cout << x << “:” << doit(1, 1.f);
return 0;
}
- 0:1
- 1:1
- 1:0
- 0:0
Question 16: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
const PI = 3.14;
const PI2 = PI * PI;
cout << PI2;
return 0;
}
- 3
- 9.8596
- 3.14
- Compilation fails
Question 17: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
const int size = 3;
class Uno {
public: int n;
Uno() { n = 1; }
Uno(int v) { n = v;}
};
class Due : public Uno {
public:
int *arr;
Due() : Uno() {
arr = new int[n];
}
Due(int a) : Uno(a) {
arr = new int[n];
}
~Due() { delete arr; }
};
int main () {
Due d(2);
Due e;
cout << d.n + e.n;
return 0;
}
- 2
- 3
- 4
- 1
Question 18: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
const char c = ‘!’;
const char *p;
p = &c;
*p = ‘?’;
cout << *p;
return 0;
}
- Compilation fails
- ?
- !
- Prints address of c
Question 19: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
class Uno {
public: int Int;
};
class Due : public Uno {
public: Due() { Int = 2;}
Due(int x) { Int = x == 0 ? 2 : x – 2; }
};
int main () {
Due d,d2(0);
cout << d.Int – d2.Int;
return 0;
}
- 1
- 0
- 3
- 2
Question 20: What is the output of the following snippet?
#include <iostream>
using namespace std;
float doit(int a, int b)
{
return a * b;
}
float doit(float a, float b)
{
return a + b;
}
int main()
{
cout << doit(doit(1,2),doit(3.f,4.f));
return 0;
}
- 14
- 9
- Compilation error
- 21
Question 21: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
class One
{
char value;
public:
One() { value = ‘A’; }
One(char v) : value(v) {}
void set(char c) {this -> value = c; }
void set() { this -> value = ‘d’; }
char get(){ return value; }
};
int main()
{
One o1,*o2;
o2 = new One(‘b’);
One *p;
p = &o1;
p -> set();
p = o2;
p -> set(‘c’);
cout << o2->get() – o1.get();
return 0;
}
- -1
- Compilation fails
- 1
- 0
Question 22: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
class cmplx{
double re, im;
public:
cmplx() : re(1),im(1) {}
cmplx(double r, double i) : re(r),im(i) {}
cmplx operator+(cmplx &);
void out() { cout << “(” << re << “,” << im << “)”; }
};
cmplx cmplx::operator+ (cmplx &a){
cmplx c(this->re + a.re, this->im + a.im);
return c;
}
int main(){
cmplx x(1,2),y,z;
z = x + y;
z.out();
return 0;
}
- (2,3)
- (3,3)
- (2,2)
- (3,2)
Question 23: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
int i = 8;
do {
i–;
cout << i–;
}
while(i);
return 0;
}
- 6420
- 6543210
- 76543210
- 7531
Question 24: What is the output of the following snippet?
#include <iostream>
using namespace std;
struct S {
int a;
char b;
struct {
float a;
int b;
} c;
};
int main (int argc, const char *argv[])
{
S s = { 1, 2, 3, 4 };
cout << s.c.a << s.c.b;
}
- 14
- 34
- 12
- 23
Question 25: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
class Uno {
int val;
public: Uno(int x) { val = x; }
int out() { return val; }
void operator++(int var) {
val += val;
}
};
ostream &operator<<(ostream &o, Uno u)
{
return o << u.out();
}
int main()
{
Uno i(2);
i++;
cout << i;
return 0;
}
- 4
- 3
- 2
- 1
Question 26: What is the output of the following snippet?
#include <iostream>
using namespace std;
int doit(int x) {
return x << 1;
}
int main(){
int i;
i = doit(1) || doit(0);
cout << i;
return 0;
}
- 1
- false
- true
- 0
Question 27: What is the output of the following snippet?
#include <iostream>
using namespace std;
int doit(int x) {
return x << 1;
}
int main(){
int i;
i = doit(1) || doit(0);
cout << i;
return 0;
}
- 1
- false
- true
- 0
Question 28: What is the output of the following snippet?
#include <iostream>
using namespace std;
class Sup {
public: virtual void out() { cout << “p”; }
};
class Sub : public Sup {
public: virtual void out() { cout << “b”; }
};
int main()
{
Sub sub;
Sup *sup;
sup = ⊂
sup->out();
sub.out();
return 0;
}
- bb
- pb
- pp
- bp
Question 29: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
int x = -2, y;
float f = 2.5, g;
g = x;
y = f;
cout << (int)g / y;
return 0;
}
- 0.8
- -0.8
- 1
- -1
Question 30: What is the output of the following snippet?
#include <iostream>
using namespace std;
char max(char x, char y) {
if(x > y)
return y;
else
return x;
}
int main()
{
char chr = max(‘a’, ‘z’);
cout << chr;
return 0;
}
- z
- Compilation error
- a
- az
Question 31: Which code, inserted into the main function, generates the output 12?
#include <iostream>
#include <string>
using namespace std;
string fun(string s1, string s2)
{
return s1 + s2;
}
int main()
{
string s=”1″, *t = &s;
//insert code here
return 0;
}
- fun(*t,”2″);
- fun(“1”,*t);
- fun(“1+2”);
- fun(*t,s);
Question 32: How many times will the program print HI!?
#include <iostream>
using namespace std;
int X = 5;
int main()
{
cout << “HI!”;
if(X– > 0)
main();
return 0;
}
- Six
- One
- Five
- Two
Question 33: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
class One {
public: float f;
One(float f) { this -> f = f; }
};
class Two {
public: float f;
Two (One o) { this -> f = o.f; }
void foo() { cout << (int)f; }
};
int main()
{
One o1(3.14);
Two o2 = o1;
o2.foo();
}
- 3.14
- 3
- Compilation fails
- 0
Question 34: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
bool t[] = {false, true, false & true};
string u[2] = {“false”, “true”};
bool *p;
p = t + 2;
cout << u[*p];
return 0;
}
- 1
- true
- 0
- false
Question 35: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int boo(int v)
{
v++;
return ++v;
}
int main()
{
float x = 3;
x = boo(x);
cout << x;
return 0;
}
- 3
- 5
- -1
- 1
Question 36: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
int i = 10;
float f = 2.5;
cout << float(i) / int(f);
return 0;
}
- 5
- 1
- 0.5
- 4
Question 37: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
class Uno {
public: Uno() { cout << “X”; }
};
Uno foo(Uno d)
{
Uno e = d;
return e;
}
int main()
{
Uno u;
foo(u);
return 0;
}
- XX
- X
- XXXX
- XXX
Question 38: Which code, inserted into the function main, generates the output 03?
#include <iostream>
using namespace std;
class Uno
{ public: void foo(){ cout << “0”;}
void bar(){ cout << “1”;}
};
class Due : public Uno
{ public: void foo(){ cout << “2”;}
void bar(){ cout << “3”;}
};
int main()
{
Due d;
// insert code here
d.bar();
}
- d->Due::foo();
- d.Uno::foo();
- d.Due::foo();
- d->Uno::foo();
Question 39: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
class Class1 {
char a;
protected:
char b;
public:
char c;
Class1() { a=’a’; b=’b’; c=’c’; }
};
class Class2 : public Class1 {
char d;
public:
void set() {
c = ‘e’;
d = ‘d’;
}
};
int main () {
Class2 a;
a.set();
cout << a.c << a.d;
return 0;
}
- ed
- cd
- Compilation fails
- bd
Question 40: What is the output of the following snippet?
#include <iostream>
using namespace std;
class cmplx{
double re,im;
public:
cmplx() : re(0),im(0) {}
cmplx(double x) { re = im = x; }
cmplx(double x,double y) { re = x; im = y; }
void out() { cout << “(” << re << “,” << im << “)”; }
};
int main(){
cmplx c(1,2), cc(c);
cc.out();
return 0;
}
- (2,2)
- (1,1)
- (2,1)
- (1,2)
Question 41: What is the output of the following snippet?
#include <iostream>
#include <cstdarg>
using namespace std;
int calculate(int &val, int arg)
{
val *= arg;
return arg;
}
int main()
{
int i = 1;
int j = calculate(i,2);
cout << i << j;
return 0;
}
- 11
- 21
- 12
- 22
Question 42: What happens if we use the operator new and the memory cannot be allocated?
#include <iostream>
#include <exception>
using namespace std;
int main () {
long i = 2000000000;
try
{ char *text = new char[i]; }
catch (bad_alloc& e)
{ cout << “1”; }
catch (exception& e)
{ cout << “2”; }
catch (…)
{ cout << “3”; }
return 0;
}
- It prints 1
- It prints 2
- None of these
- It prints 3
Question 43: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main(){
int a = 0;
if (++a == 1) {
cout << (a >> 1);
} else {
cout << (a);
}
return 0;
}
- 1
- 0
- 2
- None of these
Question 44: What is the output of the program if the value of 1 is supplied as input?
#include <iostream>
using namespace std;
class Uno {
public: char Char;
};
int main () {
int swtch;
Uno u;
u.Char = ‘5’;
cin >> swtch;
try
{
switch (swtch)
{
case 3: throw 1;
case 2: throw 3.f;
case 1: throw u;
}
}
catch (int e)
{ cout << e; }
catch (Uno e)
{ cout << e.Char; }
catch (…)
{ cout << “?”; }
return 0;
}
- 5
- 1
- Compilation error
- 3
Question 45: What is the output of the following snippet?
#include <iostream>
using namespace std;
bool compare(bool t, bool u)
{
return t < u;
}
int main()
{
cout << compare(true,false);
return 0;
}
- true
- false
- 0
- 1
Question 46: What is the output of the following snippet?
#include <iostream>
using namespace std;
int k = -1;
class Class
{
public: char *adr;
Class() { adr = new char[k]; }
~Class() { delete [] adr; }
};
int fun(void)
{
Class object;
return 0.5f;
}
int main()
{
fun();
return 0;
}
- 1
- 0.5
- 0
- Runtime error
Question 47: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
int i = 0;
for(; i < 5; i++);
cout << i;
return 0;
}
- 4
- 01234
- 5
- 012345
Question 48: What is the output of the following snippet?
#include <iostream>
using namespace std;
class Alpha {
public: char out(){ return ‘A’; }
};
class Beta : public Alpha {
public: virtual char out(){ return ‘B’; }
};
class Gamma : public Beta {
public: char out(){ return ‘G’; }
};
int main()
{
Alpha *a = new Alpha();
Alpha *b = new Beta();
Alpha *c = new Gamma();
cout << (a->out()) << (b->out()) << (c->out());
return 0;
}
- AAA
- BBB
- ABG
- GGG
Question 49: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = “top”;
string s2;
s2.append(s1).append(“down”);
cout << s2;
return( 0 );
}
- down
- top
- downtop
- topdown
Question 50: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main (int argc, const char *argv[])
{
float B = 32;
{ char B = ‘1’; cout << B; }
{ int B = 2; cout << B; }
cout << B;
return 0;
}
- 1322
- None of these
- 3122
- 3212
Question 51: What is the output of the following snippet?
#include <iostream>
using namespace std;
class X1 {
public: virtual void foo() = 0;
};
class X2 : public X1 {
public: virtual void foo() { cout << “X2”; }
};
class X3 : public X1 {
public: virtual void foo() { cout << “X3”; }
};
int main()
{
X1 *a = new X2(), *b = new X3();
b->foo();
a->foo();
return 0;
}
- X3X3
- X2X3
- X3X2
- X2X2