CPA : C++ Certified Associate Programmer : Part 03
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
int i = 0;
do {
i++;
if (i==3)
break;
cout<<i;
}
while(i < 5);
return 0;
}- It prints: 12
- It prints: 1
- It prints: 0
- No output
-
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;
}- It prints: 1 0
- It prints: 1 1
- It prints: 0 0
- Compilation error
-
What is not inherited from the base class?
- constructor
- destructor
- operator=()
- operator+()
-
What will happen when you attempt to compile and run the following code?
#include <iostream>
#include <string>using namespace std;
string fun(string, string);
int main()
{
string s=”Hello”;
cout << fun(s, ” World”);
return 0;
}string fun(string s1, string s2)
{
return s1+s2;
}- It will print: Hello World
- It will print: Hello
- It will print: World
- It will print: HW
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
int i = 5;
do {
i??;
cout<<i;
}
while(i >= 0);
return 0;
}- It prints: 43210?1
- It prints: ?1
- It prints: 4321
- It prints: 1
-
What is the output of the program?
#include <iostream>
#include <string>using namespace std;
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;
t?>setName();
t?>Print();
t = ob2;
t?>setName(“Steve”);
ob2?>Print();
}- It prints: JohnSteve
- It prints: AlanAlan
- It prints: AlanSteve
- It prints: JohnAlan
-
What is the output of the program?
#include <iostream>
#include <string>using namespace std;
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();
}- Garbage value
- It prints: A
- It prints: A 65
- Compilation error
-
How many times will the program print “HELLO” ?
#include <iostream>
using namespace std;
int main()
{
cout<<“HELLO”;
main();
return 0;
}- 65536
- 32769
- 1
- Till stack overflows
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>using namespace std;
void fun(int i);
int main()
{
int i=0;
i++;
for (i=0; i<=5; i++)
{
fun(i);
}
return 0;
}void fun(int i)
{
if (i==3)
return;
cout << i;
}- It prints: 05
- It prints: 012345
- It prints: 01245
- It prints: 0
-
If there is one, point out an error in the program
#include <iostream>
using namespace std;
int main()
{
int c = ‘a’;
switch(i)
{
case ‘2’:
cout<<“OK”;
case ‘1’:
cout<<“Error”;
default:
break;
}
return 0;
}- No Error
- Use of undeclared identifier ‘i’
- Illegal use of ‘continue’
- Illegal use of ‘break’
-
What is the output of the program?
#include <iostream>
#include <string>using namespace std;
int main()
{
char str[] = “Hello\0\World\0”;
cout << str;
return 0;
}- It prints: Hello
- It prints: World
- It prints: HW
- It prints: World\0World
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
int i = 0;
i++;
goto lab;
i++;
lab:
cout<<i;
return 0;
}- It prints: 0
- It prints: 34
- It prints: 1
- It prints: 3
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class First
{
public:
First() { cout << “Constructor”;}
void Print(){ cout<<“from First”;}
};
int main()
{
First FirstObject;
FirstObject.Print();
}- It prints: Constructorfrom First
- It prints: Constructor
- It prints: from First
- None of these
-
Which code, inserted at line 10, generate the output “50”?
#include <iostream>
using namespace std;class Base {
int age;
public:
Base () {
age=5;
};
//insert code here
void Print() { cout << age;}
};void setAge(Base &ob) {ob.age = 0;}
int main () {
Base a;
a.Print();
setAge(a);
a.Print();
return 0;
}- friend void setAge(Base ob);
- friend void setAge(Base *ob);
- friend void setAge(Base &ob);
- None of these
-
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 {
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;
}- It prints: 4John
- It prints: 2John
- It prints: 23
- It prints: 43
-
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(2);
cout << i;
return 0;
}- It prints: 0
- It prints: 1
- It prints: -1
- Compilation error
-
Which code, inserted at line 8, generates the output “100”?
#include <iostream>
using namespace std;
int fun(int);
int main()
{
int *x = new int;
*x=10;
//insert code here
return 0;
}int fun(int i)
{
return i*i;
}- cout << fun(*x) ;
- cout << fun(10);
- cout << fun(5) ;
- cout << fun(y) ;
-
Which definitions are correct?
- int age;
- int double;
- char c;
- int char;
-
Which of the following is a user defined data type?
1:
struct person
{
char name[20];
int age;
};
2:
int l=2;
3:
enum color {red,blue, green};
D.
char c;
- 1
- 2
- 3
- 4
-
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class First
{
public:
First() { cout << “Constructor”;}
~First() { cout << “Destructor”;}
void Print(){ cout<<“from First”;}
};
int main()
{
First FirstObject;
FirstObject.Print();
}- It prints: Constructorfrom First
- It prints: Constructorfrom FirstDestructor
- It prints: Constructorfrom FirstDestructorDestructor
- Compilation error at line 16
Subscribe
0 Comments
Newest