CPA : C++ Certified Associate Programmer : Part 06
-
What is the output of the program if character 3 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.
- It prints: int exception. Exception Nr.
- It prints: An exception occurred.
- It prints: float exception. Exception Nr.
-
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 {
public:
void set() {
y = 4; z = 2;
}
void Print() {
cout << y << z;
}
};int main () {
B b;
b.set();
b.Print();
return 0;
}- It prints: 42
- It prints: 44
- It prints: 22
- It prints: 2
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>using namespace std;
int f(int i);
int main()
{
int i=0;
i++;
for (i=0; i<=2; i++)
{
cout<<f(i);
}
return 0;
}int f(int a)
{
return a+a;
}- It prints: 202020
- It prints: 012
- It prints: 024
- It prints: 0
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;int mul (int a, int b=2)
{
int r;
r=a*b;
return (r);
}int main ()
{
cout << mul(1) << mul(2,4);
return 0;
}- It prints: 2
- It prints: 28
- It prints: 8
- It prints: 6
-
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;
t.Print();
}- It prints: 30
- It prints: John
- It prints: John 30
- It prints: John 30John 30
-
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;
}int main () {
namespace newname = myNamespace1;
using namespace newname;
cout << x << ” “;
cout << y;
return 0;
}- It prints: 5 1.5
- It prints: 3.14 1.5
- It prints: 5 10
- It prints: 5
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
int i=2;
switch(i)
{
case 1:
cout<<“Hello”;
break;
case 2:
cout<<“world”;
break;
case 3:
printf(“End”);
break;
}
return 0;
}- It prints: Hello
- It prints: world
- It prints: End
- It prints: E
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>using namespace std;
class SampleClass
{
string *s;
public:
SampleClass() { s = new string(“Text”);}
SampleClass(string s) { this?>s = new string(s);}
~SampleClass() { delete s;}
void Print(){ cout<<*s;}
};
int main()
{
SampleClass *obj;
obj = new SampleClass(“Test”);
obj?>Print();
}- It prints: Text
- It prints: Test
- It prints: TextTest
- Garbage value.
-
What happens if characters ‘w’, ‘o’, ‘r’, ‘l’ and ‘d’ are entered as input?
#include <iostream>
#include <string>using namespace std;
int main()
{
string s1 = “Hello”;
string s2;
getline( cin, s2 );
cout << s1 + s2;
return( 0 );
}- It prints: Helloworld
- It prints: Hello
- It prints: world
- Compilation error
-
What is the output of the program?
#include <iostream>
#include <string>using namespace std;
struct t
{
int tab[2];
};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();
}- It prints: 2 2
- It prints: 1 1
- It prints: 1 0
- It prints: 0 0
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <sstream>
#include <string>using namespace std;
int main(void)
{
string s;
s = “Test”;
s.resize (s.size() ? 1);
cout<<s<<” “<<s.size();return 0;
}- It prints: Test 4
- It prints: Test 3
- Compilation error
- It prints: Tes 3
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
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();
}- It prints: AB
- It prints: AA
- It prints: BA
- It prints: BB
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>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();
}- It prints: Text
- Compilation error
- Runtime error.
- None of these
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
class First
{
public:
void Print(){ cout<<“from First”;}
};int main()
{
First t[2];
for (int i=0; i<2; i++)
t[i].Print();
}- It prints: from First
- It prints: from Firstfrom First
- Compilation error
- Runtime error.
-
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;
}- It prints: A no parametersA no parametersB string parameter
- It prints: A string parameterA no parametersB int parameterA object A parameter
- It prints: A no parametersB string parameter
- It prints: A no parametersA no parameters
-
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;
}- It prints: 210
- It prints: 110
- It prints: 010
- Compilation error
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int op(int x, int y);
int main()
{
float *pf;
float f=0.9;
pf=&f;
cout << op(1, *pf);
return 0;
}int op(int x, int y)
{
return x*y;
}- It prints: 0
- It prints: 0.5
- It prints: 1
- It prints: ?1
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;class A {
public:
int x;
};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;}
- It prints: 010
- It prints: 110
- It prints: 00
- It prints: 1
-
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();
}- It prints: BBB
- It prints: AAA
- It prints: ABC
- It prints: ABB
-
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;
}- It prints: 0 5
- It prints: 5 1
- It prints: 1 5
- It prints: 5 0
Subscribe
0 Comments
Newest