CPA : C++ Certified Associate Programmer : Part 05
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;class Base {
static int age;
public:
Base () {};
~Base () {};
void setAge(int a=20) {age = a;}
void Print() { cout << age;}
};int Base::age=0;
int main () {
Base a;
a.setAge(10);
a.Print();
a.setAge();
a.Print();
return 0;
}- It prints: 10
- It prints: 20
- It prints: 1020
-
It prints: 2010
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
void fun(char*);int main()
{
char t[4]={‘0’, ‘1’, ‘2’, ‘3’};
fun(&t[0]);
return 0;
}
void fun(char *a)
{
cout << *a;
}- It prints: 01
- It prints: 1
- It prints: 0
- It prints: 0123
-
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(2), y(2), z(1) { 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;
b.Print();
return 0;
}- It prints: 4
- It prints: 0
- It prints: 3
- It prints: 2
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>using namespace std;
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();
}- It prints: 30
- It prints: John
- It prints: John 31
- It prints: John 30John 30
-
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;
}- It prints: 0
- It prints: 1
- It prints: -1
- Compilation error
-
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;
}- It prints: Bob55
- It prints: Bob1
- It prints: 10
- Compilation error
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
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();
}- It prints: Sample text
- It prints: Sample
- It prints: text
- None of these
-
What is the output of the program?
#include <iostream>
using namespace std;
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();
}- It prints: 10
- It prints: ?1
- It prints: 0
- Compilation error
-
What will happen when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
int fun(int);
int main()
{
int *x = new int;
*x=10;
cout << fun(*x);
return 0;
}int fun(int i)
{
return i*i;
}- It will print: 100
- It will print: 101
- It will print: 10
- It will print: 1
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
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();
}- It prints: B
- It prints: A
- It prints: AB
- It prints: BA
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
int *t;t = new int[2];
for (int i=0; i<2; i++) {
t[i] = i;
}cout << t[1];
}- It prints: 0
- It prints: 1
- It prints: 10
- It prints: ?1
-
What is the output of the program given below?
#include <iostream>
using namespace std;
int main (int argc, const char * argv[])
{
enum state { ok, error, warning};
enum state s1, s2, s3, s4;
s1 = ok;
s2 = warning;
s3 = error;
s4 = ok;
cout << s1<< s2<< s3<< s4;
return 0;
}- 1234
- compilation fails
- 0210
- 1322
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
void print(char *c);
int main (int argc, const char * argv[])
{
print(“Test”);
return 0;
}void print(char *c)
{
cout<<c;
}- It prints: Test
- It prints: T
- It prints: st
- None of these
-
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;
}- void fun(int *p[2]) {}
- void fun(int *p[2][2]) {}
- void fun(int *p[][2]) {}
- void fun(int p[][2]) {}
-
What is the output of the program if character “1” 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’;
}
}
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;
}- It prints: float exception. Exception Nr. 5.2
- It prints: int exception. Exception Nr. 20
- It prints: An exception occurred
- Compilation Error
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
long int x,y=10;
double d;
d = 3.99;
x=(int) d;
cout << x <<“, “;
d=float (y);
cout << d;
return 0;
}- It prints: 3, 10
- It prints: 3.99, 10
- It prints: 4, 10.0
- It prints: 4, 10
-
What is the output of the program if characters ‘h’, ‘e’, ‘l’, ‘l’ , ‘o’ and enter are supplied as input?
#include <iostream>
#include <string>using namespace std;
void f();
int main()
{
f();
return 0;
}void f()
{
char c;
c = cin.get();
cout << c;
if(c != ‘\n’)
f();
}- It prints: hello
- It prints: olleh
- It prints: h
- It prints: o
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>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 );
}- It prints: Hoto
- It prints: Ho
- It prints: to
- It prints: Ht
-
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 b=10;
b = min(5,20);
cout << b;
return 0;
}int min(int a, int b)
{
if (a<b)
return(a);
else
return(b);
}- It prints: 10
- It prints: 20
- It prints: 5
- It prints: 0
-
Which code, inserted at line 19, generates the output “23”?
#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 {
string z;
public:
int y;
void set() { y = 4; z = “John”; }
void Print() {
//insert code here
}
};
int main () {
B b;
b.set();
b.Print();
return 0;
}- cout << y << z;
- cout << y << A::z;
- cout << A::y << A::z;
- cout << B::y << B::z;