CPA : C++ Certified Associate Programmer : Part 09
-
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”; }
};
class C:public A {
public:
virtual void Print() { cout<< “C”; }
};
int main()
{
B ob2;
C ob3;
A *obj;
obj = &ob2;
obj>Print();
obj = &ob3;
obj>Print();
}- It prints: BC
- It prints: CB
- It prints: CC
- It prints: BB
-
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(1) & fun(0);
cout << i;
return 0;
}- It prints: 0
- It prints: 1
- It prints: -1
- Compilation error
-
What is the output of the program?
#include <iostream>
#include <string>using namespace std;
int main()
{
string s1=”Wo”;
string s2;
s2 = s1;
string s3;
s3 = s2.append(“rldHello”);
cout << s3;
return( 0 );
}- It prints: WorldHello
- It prints: HelloWo
- It prints: World
- It prints: Hello
-
Which of the structures is incorrect?
1:
struct s1{
int x;
long int li;
};2:
struct s2{
float f;
struct s2 *s;
};3:
struct s3{
float f;
struct s3 s;
};- 1
- 2
- 3
- 2,3
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int x=5;
static int y;
int i=0;void static myFunction()
{
y=x++ + ++i;
}int main (int argc, const char * argv[])
{
x++;
myFunction();
cout<<y<<” “<<x<< ” ” << i;
}- Compilation fails
- It prints: 5 5 0
- It prints: 7 7 1
- It prints: 6 5 1
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
int x=0;
int *ptr;ptr = &x;
cout<<x<<” “<<*ptr;return 0;
}- It prints: 0 0
- It prints address of ptr
- It prints: 1
- It prints: 2
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
int x=20;
int *ptr;
ptr = &x;
cout<<*ptr;
return 0;
}- It prints: 20
- It prints: 0
- It prints address of ptr
- It prints: 2
-
Point out an error in the program.
#include <iostream>
using namespace std;
int main()
{
char s1[] = “Hello”;
char s2[] = “world”;
char *const ptr = s1;
*ptr = ‘a’;
ptr = s2;
return 0;
}- No error
- Cannot modify a const object
- Compilation error at line 9
- None of these
-
What is the output of the program?
#include <iostream>
#include <string>using namespace std;
int main () {
string s1 = “Hello”, s2 = “World”;
s2 = s1 + s2;
cout << s2;
return 0;
}- It prints: Hello
- It prints: HelloWorld
- It prints: WorldHello
- It prints: WorldHelloWorld
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
struct {
int x;
char c;
union {
float f;
int i;
};
} s;int main (int argc, const char * argv[])
{
s.x=10;
s.i=0;
cout << s.i << ” ” << s.x;
}- It prints: 0 10
- It prints: 11
- Compilation error
- None of these
-
Given:
#include <iostream>
#include <exception>
using namespace std;int main () {
try
{
int * myarray= new int[1000];
}
catch (bad_alloc&)
{
cout << “Error allocating memory”;
}
catch (exception& e)
{
cout << “Standard exception”;
}
catch (…)
{
cout << “Unknown exception”;
}
return 0;
}What will happen if we use the operator “new” and the memory cannot be allocated?
- It prints: Error allocating memory
- It prints: Standard exception
- It prints: Unknown exception
- Compilation error
-
What happens if character 3 is entered as input?
#include <iostream>
using namespace std;
class A {
public:
int i;
};
int main () {
int c;
A obj;
obj.i = 5;
cin >> c;
try
{
switch (c)
{
case A. throw 20;
case B. throw 5.2f;
case C. throw obj;
default: cout<<“No exception”;
}
}
catch (int e)
{ cout << “int exception. Exception Nr. ” << e; }
catch (A e)
{ cout << “object exception. Exception Nr. ” << e.i; }
catch (…)
{ cout << “An exception occurred.”; }
return 0;
}- It prints: object exception. Exception Nr. 5
- It prints: int exception. Exception Nr.
- It prints: An exception occurred
- It prints: No exception
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int op(int x, int y)
{
return x?y;
}int op(int x, float y)
{
return x+y;
}int main()
{
int i=1, j=2, k, l;
float f=0.23;
k = op(i, j);
l = op(j, f);
cout<< k << “,” << l;
return 0;
}- It prints: 1,1
- It prints: 1,3
- It prints: 1,2
- Compilation fails
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
int x,y=10;
float f;
f = 5.20;
x=(int) f;
cout << x <<“, “;
f=float (y);
cout << f;
return 0;
}- It prints: 5, 10
- It prints: 5.2, 10
- It prints: 5.20, 10.0
- It prints: 5.2, 10.00
-
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 :
void print() {
cout << “B “;
}
};
int main() {
B sc[2];
A *bc = (A*)sc;
for (int i=0; i<2;i++)
(bc++)->print();
return 0;
}- It prints: A A
- It prints: B B
- It prints: A B
- It prints: B A
-
What is the output of the program if characters ‘t’, ‘e’, ‘s’ and ‘t’ enter are supplied as input?
#include <iostream>
#include <string>using namespace std;
int main()
{
string s;
getline( cin, s );
cout << s << ” ” << s.length();
return( 0 );
}- It prints: test 4
- It prints: test
- It prints: test 5
- It prints: 4
-
Which code, inserted at line 15, generates the output “5 Bob”?
#include <iostream>
#include <string>
using namespace std;
class B;
class A {
int age;
public:
A () { age=5; };
friend void Print(A &ob, B &so);
};
class B {
string name;
public:
B () { name=”Bob”; };
//insert code here
};void Print(A &ob, B &so) {
cout<<ob.age << ” ” << so.name;
}int main () {
A a;
B b;
Print(a,b);
return 0;
}- friend void Print(A ob, B so);
- friend void Print(A &ob, B &so);
- friend void Print(A *ob, B *so);
- None of these
-
Which of the following statements are correct?
- A function can be defined inside another function
- A function may have any number of return statements each returning different values.
- A function can return floating point value
- In a function two return statements should never occur.
-
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]=0;
}cout << t[1];
}- It prints: 0
- It prints: 1
- It prints: 2
- It prints: 3
-
What is the output of the program?
#include <iostream>
#include <string>using namespace std;
class First
{
string name;
public:
First() {
name = “Alan”;
}
void Print(){
cout << name;
}
};int main()
{
First ob1,*ob2;
ob2 = new First();
ob1.Print();
ob2?>Print();
}- Garbage value
- It prints: AlanAlan
- It prints: Alan
- It prints: Al
Subscribe
0 Comments
Newest