exam
Number: CPA
Passing Score: 800
Time Limit: 120 min
File Version: 4.0
http://www.gratisexam.com/
C++
CPA
Version 4.0
Sections
1. Volume A
2. Volume B
http://www.gratisexam.com/
Exam A
QUESTION 1
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class complex{
double re;
double im;
public:
complex() : re(0),im(0) {}
complex(double x) { re=x,im=x;};
complex(double x,double y) { re=x,im=y;}
void print() { cout << re << " " << im;}
};
int main(){
complex c1(1,2);
c1.print();
return 0;
}
A. It prints: 1 0
B. It prints: 1 1
C. It prints: 1 2
D. Compilation error
Correct Answer: C
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 2
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
http://www.gratisexam.com/
int fun(int x) {
return x<<2;
}
int main(){
int i;
i = fun(1) / 2;
cout << i;
return 0;
}
http://www.gratisexam.com/
A. It prints: 0
B. It prints: 1
C. It prints: 2
D. It prints: 4
Correct Answer: C
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 3
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
class A {
int x;
protected:
int y;
public:
http://www.gratisexam.com/
int z;
A() { x=1; y=2; z=3; }
};
class B : public A {
string z;
public:
void set() {
y = 4;
z = "John";
}
void Print() {
cout << y << z;
}
};
int main () {
B b;
b.set();
b.Print();
return 0;
}
A. It prints: 4John
B. It prints: 2John
C. It prints: 23
D. It prints: 43
Correct Answer: A
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 4
What happens when you attempt to compile and run the following code?
#include <iostream>
http://www.gratisexam.com/
#include <string>
using namespace std;
A. It prints: 111Alan
B. It prints: Bob
C. It prints: Alan
D. It prints: 0
Correct Answer: C
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 5
What is the output of the program given below?
http://www.gratisexam.com/
#include <iostream>
A. 1010
B. 101010
C. 055
D. None of these
Correct Answer: C
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 6
What happens when you attempt to compile and run the following code?
#include <iostream>
http://www.gratisexam.com/
union t
{
char tab[2];
int i;
};
union t u;
u.tab[0] = 1;
u.tab[1] = 2;
u.i = 0;
x = u.tab[0];
y = u.tab[1];
cout << x << "," << y << "," << u.i;
return 0;
}
A. compilation fails
B. It prints: 0,0,0
C. It prints: 1,2,0
D. None of these
Correct Answer: B
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 7
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; }
http://www.gratisexam.com/
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;
b.Print();
return 0;
}
A. It prints: 3
B. It prints: 0
C. It prints: 1
D. It prints: 2
Correct Answer: A
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 8
Which code, inserted at line 10, generates the output "Hello World"?
#include <iostream>
#include <string>
using namespace std;
string fun(string, string);
int main()
{
http://www.gratisexam.com/
string s="Hello";
string *ps;
ps = &s;
//insert code here
return 0;
}
Correct Answer: D
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 9
What happens when you attempt to compile and run the following code?
#include <iostream>
int x=5;
static int y=0;
void myFunction(int a)
{
y=++a;
}
http://www.gratisexam.com/
int main (int argc, const char * argv[])
{
int i=0;
myFunction(i);
cout<<y<<" "<<x;
}
A. It prints: 0 5
B. It prints: 5 1
C. It prints: 1 5
D. It prints: 5 0
Correct Answer: C
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 10
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()
{
http://www.gratisexam.com/
A ob1;
B ob2;
C ob3;
A *obj;
obj = &ob1;
obj>Print();
obj = &ob2;
obj>Print();
obj = &ob3;
obj>Print();
}
http://www.gratisexam.com/
A. It prints: BBB
B. It prints: AAA
C. It prints: ABC
D. It prints: ABB
Correct Answer: B
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 11
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
class A {
public:
int x;
http://www.gratisexam.com/
};
class B : public A {
public:
B() { x=1;}
B(int x) {this?>x = x;}
};
int main () {
B c1;
B c2(10);
cout << c1.x;
cout << c2.x;
return 0;
A. It prints: 010
B. It prints: 110
C. It prints: 00
D. It prints: 1
Correct Answer: B
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 12
What happens when you attempt to compile and run the following code?
#include <iostream>
void fun(char*);
int main()
http://www.gratisexam.com/
{
char t[4]={'0', '1', '2', '3'};
fun(&t[2]);
return 0;
}
void fun(char *a)
{
cout << *a;
}
A. It prints: 2
B. It prints: 21
C. It prints: 00
D. It prints: 02
Correct Answer: A
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 13
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
class A {
public:
A() { cout << "A no parameters";}
A(string s) { cout << "A string parameter";}
A(A &a) { cout << "A object A parameter";}
};
class B : public A {
public:
B() { cout << "B no parameters";}
http://www.gratisexam.com/
B(string s) { cout << "B string parameter";}
};
int main () {
A a2("Test");
B b1("Alan");
B b2(b1);
return 0;
Correct Answer: B
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 14
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
class A {
public:
string s;
A(string s) { this>s = s; }
};
class B {
http://www.gratisexam.com/
public:
string s;
B (A a) { this>s = a.s; }
void print() { cout<<s; }
};
int main()
{
A a("Hello world");
B b=a;
b.print();
}
Correct Answer: A
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 15
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
float *pf;
float f=0.9;
pf=&f;
cout << op(1, *pf);
http://www.gratisexam.com/
return 0;
}
A. It prints: 0
B. It prints: 0.5
C. It prints: 1
D. It prints: ?1
Correct Answer: A
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 16
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
class First
{
string *s;
public:
First() { s = new string("Text");}
~First() { delete s;}
void Print(){ cout<<*s;}
};
int main()
{
First FirstObject;
FirstObject.Print();
http://www.gratisexam.com/
FirstObject.~First();
}
A. It prints: Text
B. Compilation error
C. Runtime error.
D. None of these
Correct Answer: C
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 17
What happens when you attempt to compile and run the following code?
#include <iostream>
class A
{
public:
virtual void Print(){ cout<<"A";}
};
class B:public A
{
public:
void Print(){ cout<< "B";}
};
int main()
{
A *obj;
A ob1;
obj = &ob1;
obj>Print();
B ob2;
obj = &ob2;
obj>Print();
http://www.gratisexam.com/
}
A. It prints: AB
B. It prints: AA
C. It prints: BA
D. It prints: BB
Correct Answer: A
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 18
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <sstream>
#include <string>
int main(void)
{
string s;
s = "Test";
s.resize (s.size() ? 1);
cout<<s<<" "<<s.size();
return 0;
}
A. It prints: Test 4
B. It prints: Test 3
C. Compilation error
D. It prints: Tes 3
Correct Answer: D
Section: Volume A
http://www.gratisexam.com/
Explanation
Explanation/Reference:
QUESTION 19
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class A {
public:
int x;
A() { x=0;}
};
class B : public A {
public:
B() { x=1;}
};
class C : private B {
public:
C() { x=2;}
};
int main () {
C c1;
cout << c1.x;
return 0;
}
A. It prints: 210
B. It prints: 110
C. It prints: 010
D. Compilation error
http://www.gratisexam.com/
Correct Answer: D
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 20
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
class A {
public:
A() { cout << "A no parameters";}
A(string s) { cout << "A string parameter";}
A(A &a) { cout << "A object A parameter";}
};
class B : public A {
public:
B() { cout << "B no parameters";}
B(string s) { cout << "B string parameter";}
B(int s) { cout << "B int parameter";}
};
int main () {
A a2("Test");
B b1(10);
B b2(b1);
return 0;
}
http://www.gratisexam.com/
http://www.gratisexam.com/
Correct Answer: B
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 21
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <iostream>
class First
{
public:
void Print(){ cout<<"from First";}
};
int main()
{
First t[2];
for (int i=0; i<2; i++)
http://www.gratisexam.com/
t[i].Print();
}
Correct Answer: B
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 22
What is the output of the program given below?
#include <iostream>
A. 1010
B. 100
C. 010
D. None of these
Correct Answer: C
Section: Volume A
http://www.gratisexam.com/
Explanation
Explanation/Reference:
QUESTION 23
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;
A. It prints: 5
B. It prints: 1?5
C. It prints: 05
http://www.gratisexam.com/
D. It prints: 0
Correct Answer: B
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 24
What happens when you attempt to compile and run the following code?
#include <iostream>
A. compilation fails
B. It prints: 10
C. It prints: 0
D. It prints: 1
Correct Answer: D
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 25
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
http://www.gratisexam.com/
using namespace std;
class B;
class A {
int age;
public:
A () { age=5; };
friend class B;
};
class B {
string name;
public:
B () { name="Bob"; };
void Print(A ob) {
cout << name << ob.age;
}
};
int main () {
A a;
B b;
b.Print(a);
return 0;
}
A. It prints: Bob5
B. It prints: Bob
C. It prints: 5
D. None of these
Correct Answer: A
Section: Volume A
Explanation
Explanation/Reference:
http://www.gratisexam.com/
QUESTION 26
What happens when you attempt to compile and run the following code?
#include <iostream>
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
Correct Answer: C
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 27
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
http://www.gratisexam.com/
int i=2;
fun(i);
cout<<i;
return 0;
}
A. It prints: 2
B. It prints: 0
C. It prints: 4
D. It prints: 16
Correct Answer: C
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 28
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main() {
cout << fun(0);
return 0;
}
int fun(int x) {
if(x > 0)
return fun(x-1);
else
http://www.gratisexam.com/
return 100;
}
A. It prints: 0
B. It prints: 10
C. It prints: 100
D. It prints: -1
Correct Answer: C
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 29
What is the output of the program if character 2 is supplied as input?
#include <iostream>
int main () {
int c;
cin >> c;
try
{
switch (c)
{
case 1:
throw 20;
case 2:
throw 5.2f;
}
}
catch (int e)
{ cout << "int exception. Exception Nr. " << e; }
catch (float e)
{ cout << "float exception. Exception Nr. " << e; }
catch (...)
{ cout << "An exception occurred."; }
http://www.gratisexam.com/
return 0;
}
Correct Answer: D
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 30
What is the output of the program if character 4 is supplied as input?
#include <iostream>
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';
default:
cout<<"No exception";
}
}
catch (int e)
{ cout << "int exception. Exception Nr. " << e; }
catch (float e)
{ cout << "float exception. Exception Nr. " << e; }
http://www.gratisexam.com/
catch (...)
{ cout << "An exception occurred."; }
return 0;
}
http://www.gratisexam.com/
Correct Answer: D
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 31
Which code, inserted at line 14, generates the output "3.14 10"?
#include <iostream>
using namespace std;
namespace myNamespace1
{
int x = 5;
int y = 10;
}
namespace myNamespace2
{
float x = 3.14;
float y = 1.5;
}
int main () {
//insert code here
cout << x << " " << y;
http://www.gratisexam.com/
return 0;
}
Correct Answer: D
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 32
What is the output of the program?
#include <iostream>
#include <string>
int main()
{
string s1[]= {"Hello" , "World" };
A. It prints: HelloWorld
B. It prints: Hello
C. It prints: WorldHello
D. It prints: World
Correct Answer: A
Section: Volume A
http://www.gratisexam.com/
Explanation
Explanation/Reference:
QUESTION 33
Which code, inserted at line 8, generates the output "0102020"?
#include <iostream>
using namespace std;
class Base {
static int age;
public:
Base () {};
~Base () {};
//insert code here
void Print() { cout << age;}
};
int Base::age=0;
int main () {
Base a,*b;
b = new Base();
a.Print();
a.setAge(10);
a.Print();
b?>setAge();
a.Print();
b?>Print();
return 0;
}
Correct Answer: D
Section: Volume A
Explanation
http://www.gratisexam.com/
Explanation/Reference:
QUESTION 34
What happens when you attempt to compile and run the following code?
#include <iostream>
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();
}
Correct Answer: C
Section: Volume A
Explanation
Explanation/Reference:
http://www.gratisexam.com/
QUESTION 35
What happens when you attempt to compile and run the following code?
#include <iostream>
A. It prints: CDEF
B. It prints: ABCDEF
C. It prints: BCDEF
D. None of these
Correct Answer: A
Section: Volume A
Explanation
Explanation/Reference:
QUESTION 36
Which of the following can be checked in a switch?case statement?
A. char
B. int
C. enum
D. double
Explanation/Reference:
http://www.gratisexam.com/
QUESTION 37
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
int x = compare(10, 20);
cout << x;
return 0;
}
A. It prints: 0
B. It prints: 2
C. It prints: 1
D. It prints: 10
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 38
What happens when you attempt to compile and run the following code?
#include <iostream>
http://www.gratisexam.com/
int main()
{
int x,y=10;
float f;
f = 5.90;
cout << f << ", ";
x=f;
cout << x <<", ";
f=y;
cout << f;
return 0;
}
A. It prints: 5, 5, 10.00
B. It prints: 5.9, 5, 10
C. It prints: 6, 5, 10
D. It prints: 6, 5, 10.00
Correct Answer: B
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 39
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
http://www.gratisexam.com/
};
class B : public A {
public:
int *tab;
B() { tab = new int[size]; for (int i=0; i<size; i++) tab[i]=1;}
B(string s) : A(s) { tab = new int[size]; for (int i=0; i<size; i++) tab[i]=1;}
~B() { delete tab; }
void Print() {
for (int i=0; i<size; i++) cout << tab[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
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 40
What happens when you attempt to compile and run the following code?
#include <iostream>
class A {
http://www.gratisexam.com/
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;
B *obj;
obj = &ob2;
obj?>Print();
obj = &ob3;
obj?>Print();
}
http://www.gratisexam.com/
A. It prints: BB
B. It prints: AA
C. It prints: BC
D. It prints: AB
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
http://www.gratisexam.com/
QUESTION 41
What is the output of the program?
#include <iostream>
using namespace std;
class Base {
static int age;
public:
Base () {};
~Base () {};
void setAge(int a=10) {age = a;}
void Print() { cout << age;}
};
int Base::age=0;
int main () {
Base a,*b;
b = new Base();
a.setAge();
b?>setAge(20);
a.Print();
b?>Print();
return 0;
}
A. It prints: 2020
B. It prints: 1020
C. It prints: 20
D. It prints: 10
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
http://www.gratisexam.com/
QUESTION 42
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
class A {
int x;
protected:
int y;
public:
int z;
};
class B : private A {
string name;
public:
void set() {
x = 1;
}
void Print() {
cout << x;
}
};
int main () {
B b;
b.set();
b.Print();
return 0;
}
A. It prints: 123
B. It prints: 1
C. It prints: ?123
D. Compilation error
Correct Answer: D
http://www.gratisexam.com/
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 43
What is the output of the program?
#include <iostream>
#define SQR(x)(x*x)
x = SQR(y);
cout << x << ", " <<y;
return 0;
}
A. It prints: 3, 2
B. It prints: 4, 2
C. It prints: 3, 3
D. It prints: 9, 2
Correct Answer: B
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 44
What will be the output of the program?
#include <iostream>
http://www.gratisexam.com/
using namespace std;
int main()
{
int i=0;
for(; i<=5; i++)
cout << i;
return 0;
}
A. 012345
B. 0123
C. 5
D. 6
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 45
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class Base {
int age;
public:
class C {
int b;
void PrintC() { cout << b; }
};
Base () {age=5;};
void setAge(int a=20) {age = a;}
void Print() { cout << age;}
http://www.gratisexam.com/
};
int main () {
Base a;
a.setAge(10);
a.Print();
return 0;
}
A. It prints: 1020
B. It prints: 105
C. It prints: 10
D. It prints: 20
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 46
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class complex{
double re;
double im;
public:
complex() : re(0),im(0) {}
complex(double x) { re=x,im=x;};
complex(double x,double y) { re=x,im=y;}
void print() { cout << re << " " << im;}
};
int main(){
complex c1;
c1.print();
return 0;
http://www.gratisexam.com/
}
A. It prints: 1 0
B. It prints: 1 1
C. It prints: 0 0
D. Compilation error
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 47
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
int i = 0;
i++;
goto lab;
i++;
lab:
cout<<i;
return 0;
}
A. It prints: 0
B. It prints: 34
C. It prints: 1
D. It prints: 3
Correct Answer: C
Section: Volume B
Explanation
http://www.gratisexam.com/
Explanation/Reference:
QUESTION 48
What is the output of the program?
#include <iostream>
#include <string>
int main()
{
char str[] = "Hello\0\World\0";
cout << str;
return 0;
}
A. It prints: Hello
B. It prints: World
C. It prints: HW
D. It prints: World\0World
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 49
If there is one, point out an error in the program
#include <iostream>
int main()
{
int c = 'a';
http://www.gratisexam.com/
switch(i)
{
case '2':
cout<<"OK";
case '1':
cout<<"Error";
default:
break;
}
return 0;
}
A. No Error
B. Use of undeclared identifier 'i'
C. Illegal use of 'continue'
D. Illegal use of 'break'
Correct Answer: B
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 50
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
class A {
int x;
protected:
int y;
public:
int z;
A() { x=1; y=2; z=3; }
};
class B : public A {
http://www.gratisexam.com/
string z;
public:
void set() { y = 4; z = "John"; }
void Print() { cout << y << A::z; }
};
int main () {
B b;
b.set();
b.Print();
return 0;
}
http://www.gratisexam.com/
A. It prints: 4John
B. It prints: 2John
C. It prints: 23
D. It prints: 43
Correct Answer: D
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 51
Which code, inserted at line 10, generate the output "50"?
#include <iostream>
using namespace std;
class Base {
int age;
http://www.gratisexam.com/
public:
Base () {
age=5;
};
//insert code here
void Print() { cout << age;}
};
int main () {
Base a;
a.Print();
setAge(a);
a.Print();
return 0;
}
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 52
What happens when you attempt to compile and run the following code?
#include <iostream>
class First
{
http://www.gratisexam.com/
public:
First() { cout << "Constructor";}
void Print(){ cout<<"from First";}
};
int main()
{
First FirstObject;
FirstObject.Print();
}
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 53
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
int main()
{
int i=0;
i++;
for (i=0; i<=5; i++)
{
fun(i);
}
return 0;
http://www.gratisexam.com/
}
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
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 54
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
int i = 5;
do {
i??;
cout<<i;
}
while(i >= 0);
return 0;
}
http://www.gratisexam.com/
A. It prints: 43210?1
B. It prints: ?1
C. It prints: 4321
D. It prints: 1
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 55
What will happen when you attempt to compile and run the following code?
#include <iostream>
#include <string>
int main()
{
string s="Hello";
cout << fun(s, " World");
return 0;
}
http://www.gratisexam.com/
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 56
What is not inherited from the base class?
A. constructor
B. destructor
C. operator=()
D. operator+()
Explanation/Reference:
QUESTION 57
How many times will the program print "HELLO" ?
#include <iostream>
int main()
{
cout<<"HELLO";
main();
return 0;
}
A. 65536
B. 32769
C. 1
http://www.gratisexam.com/
D. Till stack overflows
Correct Answer: D
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 58
What is the output of the program?
#include <iostream>
#include <string>
union t
{
char c;
int i;
};
class First
{
union t u;
public:
First() {
u.c = 'A';
}
void Print(){
cout << u.c;
}
};
int main()
{
First *t = new First();
t?>Print();
}
http://www.gratisexam.com/
A. Garbage value
B. It prints: A
C. It prints: A 65
D. Compilation error
Correct Answer: B
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 59
What is the output of the program?
#include <iostream>
#include <string>
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;
http://www.gratisexam.com/
t?>setName();
t?>Print();
t = ob2;
t?>setName("Steve");
ob2?>Print();
}
A. It prints: JohnSteve
B. It prints: AlanAlan
C. It prints: AlanSteve
D. It prints: JohnAlan
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 60
What is the output of the program given below?
#include <iostream>
http://www.gratisexam.com/
A. 0
B. 11
http://www.gratisexam.com/
C. ?10
D. ?11
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 61
What is the output of the program?
#include <iostream>
#include <string>
int main()
{
string s1[]= {"H" , "t" };
string s;
A. It prints: Hoto
B. It prints: Ht
C. It prints: toHo
D. It prints: Howto
Correct Answer: D
Section: Volume B
http://www.gratisexam.com/
Explanation
Explanation/Reference:
QUESTION 62
How many times will "HELLO" be printed?
#include <iostream>
int main()
{
for(int i=?1; i<=10; i++)
{
if(i < 5)
continue;
else
break;
cout<<"HELLO";
}
return 0;
}
A. 1
B. 2
C. 0
D. 20
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 63
What happens when you attempt to compile and run the following code?
#include <iostream>
http://www.gratisexam.com/
using namespace std;
int main()
{
int i=2, j=2, k;
float f=0.3;
k = op(i, j);
cout<< k << "," << op(1, f);
return 0;
}
A. It prints: 4,1
B. It prints: 4,0.7
C. It prints: 4,0
D. It prints: 0,4
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 64
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main(){
int i = 1;
if (i++==1) {
http://www.gratisexam.com/
cout << i;
} else {
cout << i-1;
}
return 0;
}
A. It prints: 0
B. It prints: 1
C. It prints: -1
D. It prints: 2
Correct Answer: D
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 65
Which code, inserted at line 12, generates the output "5b"?
#include <iostream>
using namespace std;
namespace myNamespace1
{
int var = 5;
}
namespace myNamespace2
{
char var = 'b';
}
int main () {
//insert code here
return 0;
}
http://www.gratisexam.com/
D. None of these
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 66
What will be the output of the program?
#include <iostream>
int main()
{
const int y = 5;
const x = ?10;
cout<<x<<" "<<y;
return 0;
}
A. ?10 5
B. 5 ?10
C. Compilation error
D. None of these
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 67
What happens when you attempt to compile and run the following code?
#include <iostream>
http://www.gratisexam.com/
using namespace std;
class BaseC
{
public:
int *ptr;
BaseC() { ptr = new int(10);}
BaseC(int i) { ptr = new int(i); }
~BaseC() { delete ptr; }
};
void fun(BaseC x);
int main()
{
BaseC *o = new BaseC(5);
fun(*o);
}
void fun(BaseC x) {
cout << "Hello:"<<*x.ptr;
}
A. It prints: Hello:50
B. It prints: Hello:10
C. It prints: Hello:5
D. Compilation error
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 68
What happens when you attempt to compile and run the following code?
#include <iostream>
http://www.gratisexam.com/
using namespace std;
class First
{
public:
virtual void Print(){ cout<<"from First";}
};
class Second:public First
{
public:
void Print(){ cout<< "from Second";}
};
void fun(First *obj);
int main()
{
First FirstObject;
fun(&FirstObject);
Second SecondObject;
fun(&SecondObject);
}
void fun(First *obj)
{
obj?>Print();
}
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 69
What happens when you attempt to compile and run the following code?
#include <iostream>
http://www.gratisexam.com/
using namespace std;
int main()
{
int min(int,int);
int b;
b = min(10,20);
cout << b;
return 0;
}
A. It prints: 20
B. It prints: 10
C. It prints: 1020
D. It prints: 2010
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 70
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
http://www.gratisexam.com/
union un
{
int x;
char c;
};
union un u1 = {10};
union un u2 = {'a'};
union un u3 = {20, 'a'};
cout<<u1.x;
cout<<u2.c;
cout<<u3.c;
return 0;
}
http://www.gratisexam.com/
A. It prints: 10aa
B. It prints: 10a20a
C. It prints: 1a
D. Compilation error
Correct Answer: D
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 71
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main() {
int i, j;
for(i = 0; i < 2; i++) {
http://www.gratisexam.com/
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
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 72
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
int i=1, j=2, k;
float f=0.3;
k = op(i, j);
cout<< k << "," << op(0, f);
return 0;
}
http://www.gratisexam.com/
int op(int x, int y)
{
return x+y;
}
A. It prints: 3,1
B. It prints: 3,?0.3
C. It prints: 3,0
D. It prints: 0,0
Correct Answer: B
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 73
Which code, inserted at line 10, generates the output "2?1"?
#include <iostream>
#include <string>
using namespace std;
class A {
protected:
int y;
public:
int z;
};
//insert code here
public:
void set() {
y = 2;
z = 3;
http://www.gratisexam.com/
}
void Print() { cout << y << z; }
};
int main () {
B b;
b.set();
b.z = ?1;
b.Print();
return 0;
}
A. class B : private A {
B. class B : public A {
C. class B : protected A {
D. class B {
Correct Answer: B
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 74
What happens when you attempt to compile and run the following code?
#include <iostream>
class First
{
public:
void Print(){ cout<<"from First";}
};
class Second:public First
{
public:
void Print(){ cout<< "from Second";}
http://www.gratisexam.com/
};
void fun(First *obj);
int main()
{
First FirstObject;
fun(&FirstObject);
Second SecondObject;
fun(&SecondObject);
}
void fun(First *obj)
{
obj?>Print();
}
Correct Answer: B
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 75
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
float b;
b = f(20,10);
cout << b;
return 0;
http://www.gratisexam.com/
}
A. It prints: 2
B. It prints: 5
C. It prints: 10
D. It prints: 0
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 76
If there is one, point out an error in the program
#include <iostream>
int main()
{
int i=1;
for(;;)
{
cout<<i++;
if(i>5)
break;
}
return 0;
}
http://www.gratisexam.com/
D. Error in break statement
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 77
What happens when you attempt to compile and run the following code?
#include <iostream>
class A
{
public:
virtual void Print(){ cout<<"A";}
};
class B:public A
{
public:
virtual void Print(){ cout<< "B";}
};
int main()
{
A *obj;
A ob1;
obj = &ob1;
obj?>Print();
B ob2;
obj = &ob2;
obj?>Print();
}
A. It prints: AB
B. It prints: AA
C. It prints: BA
D. It prints: BB
http://www.gratisexam.com/
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 78
What happens when you attempt to compile and run the following code?
#include <iostream>
Correct Answer: C
Section: Volume B
Explanation
http://www.gratisexam.com/
Explanation/Reference:
QUESTION 79
What happens if you try to compile and run this program?
#include <iostream>
A. It prints: Test
B. Compilation fails
C. Program terminates abnormally
D. None of these
Correct Answer: B
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 80
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
http://www.gratisexam.com/
class A {
protected:
int y;
public:
int x;
int z;
A() { x=1; y=2; z=3; }
A(int a, int b) : x(a), y(b) { z = x * y;}
void Print() {
cout << z;
}
};
int main () {
A a(2,5);
a.Print();
return 0;
}
http://www.gratisexam.com/
A. It prints: 10
B. It prints: 2
C. It prints: 6
D. It prints: 5
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 81
What is the output of the program?
http://www.gratisexam.com/
#include <iostream>
#include <string>
int main()
{
string s1="World";
string s2;
s2="Hello" + s1;
cout << s2;
return( 0 );
}
A. It prints: HelloWorld
B. It prints: Hello
C. It prints: World
D. Compilation error
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 82
What is the output of the program?
#include <iostream>
#include <string>
int main()
{
string s1[]= {"H" , "t" };
string s;
http://www.gratisexam.com/
for (int i=0; i<2; i++) {
s = s1[i];
s.insert(1,"ow");
cout << s;
}
return( 0 );
}
A. It prints: How
B. It prints: Ht
C. It prints: Hoto
D. It prints: Howtow
Correct Answer: D
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 83
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
int i=1, j=2, k, l;
k = op(i, j);
l = op(j, i);
cout<< k << "," << l;
return 0;
http://www.gratisexam.com/
}
A. It prints: 1,2
B. It prints: ?1,1
C. It prints: 1,1
D. It prints: 3,3
Correct Answer: D
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 84
What will the variable "y" be in class B?
class A {
int x;
protected:
int y;
public:
int age;
};
class B : protected A {
string name;
public:
void Print() {
cout << name << age;
}
};
A. public
B. private
C. protected
D. None of these
Correct Answer: C
http://www.gratisexam.com/
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 85
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;
}
A. 1
B. 2
C. 3
D. All of these
Correct Answer: AB
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 86
What happens when you attempt to compile and run the following code?
http://www.gratisexam.com/
#include <iostream>
#include <string>
using namespace std;
class Second;
class Base {
int age;
public:
Base () { age=5; };
friend void set(Base &ob, Second &so);
void Print() { cout << age;}
};
class Second {
string name;
public:
friend void set(Base &ob, Second &so);
void Print() { cout << name;}
};
A. It prints: 0Bill
B. Compilation error
C. It prints: Bill0
D. None of these
Correct Answer: A
Section: Volume B
Explanation
http://www.gratisexam.com/
Explanation/Reference:
QUESTION 87
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class A {
public:
int x;
A() { x=0;}
};
class B : protected A {
public:
int y;
using A::x;
B(int y) {this?>y = y;}
void Print() { cout << x << y; }
};
int main () {
B b(5);
b.Print();
return 0;
}
A. It prints: 05
B. It prints: 0
C. It prints: 5
D. It prints: 15
Correct Answer: A
Section: Volume B
Explanation
http://www.gratisexam.com/
Explanation/Reference:
QUESTION 88
What happens when you attempt to compile and run the following code?
#include <iostream>
A. It prints: Test
B. It prints: T
C. It prints: st
D. None of these
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 89
What is the output of the program given below?
#include <iostream>
http://www.gratisexam.com/
using namespace std;
http://www.gratisexam.com/
A. 1234
B. compilation fails
C. 0210
D. 1322
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 90
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
http://www.gratisexam.com/
int *t;
t = new int[2];
for (int i=0; i<2; i++) {
t[i] = i;
}
A. It prints: 0
B. It prints: 1
C. It prints: 10
D. It prints: ?1
Correct Answer: B
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 91
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
long int x,y=10;
double d;
d = 3.99;
x=(int) d;
cout << x <<", ";
d=float (y);
cout << d;
return 0;
}
http://www.gratisexam.com/
A. It prints: 3, 10
B. It prints: 3.99, 10
C. It prints: 4, 10.0
D. It prints: 4, 10
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 92
What is the output of the program if character “1” is supplied as input?
#include <iostream>
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;
http://www.gratisexam.com/
}
Correct Answer: B
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 93
Which of the following is a correct way to define the function fun() in the program below?
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int a[2][2];
fun(a);
return 0;
}
Correct Answer: D
Section: Volume B
Explanation
Explanation/Reference:
http://www.gratisexam.com/
QUESTION 94
What happens when you attempt to compile and run the following code?
#include <iostream>
class A
{
public:
virtual void Print()=0;
};
class B:public A
{
public:
virtual void Print(){ cout<< "B";}
};
int main()
{
B ob2;
A *obj;
obj = &ob2;
obj?>Print();
}
A. It prints: B
B. It prints: A
C. It prints: AB
D. It prints: BA
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 95
http://www.gratisexam.com/
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
class A {
public:
int age;
A () { age=5; };
};
class B : private A {
string name;
public:
B () { name="Bob"; };
void Print() {
cout << name << age;
}
};
int main () {
B b,*ob;
ob = &b;
ob?>age = 10;
ob?>Print();
return 0;
}
A. It prints: Bob55
B. It prints: Bob1
C. It prints: 10
D. Compilation error
Correct Answer: D
Section: Volume B
Explanation
http://www.gratisexam.com/
Explanation/Reference:
QUESTION 96
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int fun(int x) {
return 2*x;
}
int main(){
int i;
i = fun(0.5) || fun(0);
cout << i;
return 0;
}
A. It prints: 0
B. It prints: 1
C. It prints: -1
D. Compilation error
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 97
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
http://www.gratisexam.com/
struct Person {
string name;
int age;
};
class First
{
Person *person;
public:
First() {person = new Person;
person?>name = "John";
person?>age = 30;
}
void Print(){
cout<<person?>name << " "<< person?>age;
}
};
int main()
{
First t[2];
for (int i=0; i<2; i++)
t[i].Print();
}
A. It prints: 30
B. It prints: John
C. It prints: John 31
D. It prints: John 30John 30
Correct Answer: D
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 98
What will happen when you attempt to compile and run the following code?
http://www.gratisexam.com/
#include <iostream>
#include <string>
int fun(int);
int main()
{
int *x = new int;
*x=10;
cout << fun(*x);
return 0;
}
int fun(int i)
{
return i*i;
}
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 99
What is the output of the program?
#include <iostream>
http://www.gratisexam.com/
class BaseC
{
int i;
public:
BaseC() { i=?1;}
BaseC(int i) { i=i; }
void seti(int a) { i = a; };
void Print() { cout << i; }
};
int main()
{
BaseC *o = new BaseC();
o?>seti(10);
o?>Print();
}
http://www.gratisexam.com/
A. It prints: 10
B. It prints: ?1
C. It prints: 0
D. Compilation error
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 100
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
http://www.gratisexam.com/
using namespace std;
class Base
{
string s;
public:
Base() { s="Sample text";}
Base(string s) { this?>s=s; }
void Print() { cout << s; }
};
int main()
{
Base *o = new Base();
o?>Print();
}
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 101
What is the output of the program if characters 'h', 'e', 'l', 'l' , 'o' and enter are supplied as input?
#include <iostream>
#include <string>
void f();
int main()
{
http://www.gratisexam.com/
f();
return 0;
}
void f()
{
char c;
c = cin.get();
cout << c;
if(c != '\n')
f();
}
A. It prints: hello
B. It prints: olleh
C. It prints: h
D. It prints: o
Correct Answer: A
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 102
What happens when you attempt to compile and run the following code?
#include <iostream>
int main()
{
int i=2;
switch(i)
{
case 1:
cout<<"Hello";
break;
http://www.gratisexam.com/
case 2:
cout<<"world";
break;
case 3:
printf("End");
break;
}
return 0;
}
A. It prints: Hello
B. It prints: world
C. It prints: End
D. It prints: E
Correct Answer: B
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 103
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
namespace myNamespace1
{
int x = 5;
int y = 10;
}
namespace myNamespace2
{
float x = 3.14;
float y = 1.5;
}
http://www.gratisexam.com/
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
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 104
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
struct Person {
string name;
int age;
};
class First
{
Person *person;
public:
First() {person = new Person;
http://www.gratisexam.com/
person?>name = "John";
person?>age = 30;
}
void Print(){
cout<<person?>name << " "<< person?>age;
}
};
int main()
{
First t;
t.Print();
}
A. It prints: 30
B. It prints: John
C. It prints: John 30
D. It prints: John 30John 30
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
QUESTION 105
What is the output of the program?
#include <iostream>
#include <string>
struct t
{
int tab[2];
};
http://www.gratisexam.com/
class First
{
struct t u;
public:
First() {
u.tab[0] = 1;
u.tab[1] = 0;
}
void Print(){
cout << u.tab[0] << " " << u.tab[1];
}
};
int main()
{
First t;
t.Print();
}
A. It prints: 2 2
B. It prints: 1 1
C. It prints: 1 0
D. It prints: 0 0
Correct Answer: C
Section: Volume B
Explanation
Explanation/Reference:
http://www.gratisexam.com/
http://www.gratisexam.com/