Free C++ Institute CPA Practice Test & Real Exam Questions
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main(){
int i = 1;
for(i=10; i>-1; i/=2) {
if(!i)
break;
}
cout << i;
return 0;
}
#include <iostream>
using namespace std;
int main(){
int i = 1;
for(i=10; i>-1; i/=2) {
if(!i)
break;
}
cout << i;
return 0;
}
Correct Answer: B
Vote an answer
What happens when you attempt to compile and run the following code?
#include <iostream> #include <string>
using namespace std;
class complex{
double re, im;
public:
complex() : re(1),im(0.4) {}
complex operator+(complex &t);
void Print() { cout << re << " " << im; }
};
complex complex::operator+ (complex &t){
complex temp;
temp.re = this->re + t.re;
temp.im = this->im + t.im;
return temp;
}
int main(){
complex c1,c2,c3;
c3 = c1 + c2;
c3.Print();
}
#include <iostream> #include <string>
using namespace std;
class complex{
double re, im;
public:
complex() : re(1),im(0.4) {}
complex operator+(complex &t);
void Print() { cout << re << " " << im; }
};
complex complex::operator+ (complex &t){
complex temp;
temp.re = this->re + t.re;
temp.im = this->im + t.im;
return temp;
}
int main(){
complex c1,c2,c3;
c3 = c1 + c2;
c3.Print();
}
Correct Answer: B
Vote an answer
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class Test {
float i,j;
};
class Add {
public:
int x,y;
Add (int a=3, int b=3) { x=a; y=b; }
int result() { return x+y;}
};
int main () {
Test test;
Add * padd;
padd = &test;
cout << padd->result();
return 0;
}
#include <iostream>
using namespace std;
class Test {
float i,j;
};
class Add {
public:
int x,y;
Add (int a=3, int b=3) { x=a; y=b; }
int result() { return x+y;}
};
int main () {
Test test;
Add * padd;
padd = &test;
cout << padd->result();
return 0;
}
Correct Answer: B
Vote an answer
What will happen when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
#define A 1
int main()
{
#if A
cout<<"Hello";
#endif
cout<<"world";
return 0;
}
#include <iostream>
using namespace std;
#define A 1
int main()
{
#if A
cout<<"Hello";
#endif
cout<<"world";
return 0;
}
Correct Answer: C
Vote an answer
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;
}
#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;
}
Correct Answer: A,D
Vote an answer
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
int i = 5;
cout<<"Hello World" << ++i;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i = 5;
cout<<"Hello World" << ++i;
return 0;
}
Correct Answer: A
Vote an answer
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;
}
#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: B
Vote an answer
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, j = 1; j < 2, i < 4; i++, j++);
cout << i << " " << j; return 0;
}
#include <iostream>
using namespace std;
int main(){
int i, j;
for(i = 0, j = 1; j < 2, i < 4; i++, j++);
cout << i << " " << j; return 0;
}
Correct Answer: D
Vote an answer
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:
struct s1{ int x;
long int li; };
2:
struct s2{ float f; struct s2 *s;
};
3:
struct s3{ float f; struct s3 s;
};
Correct Answer: C
Vote an answer
