Free CPA Exam Braindumps (page: 19)

Page 18 of 56

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class BaseClass
{
public:
int *ptr;
BaseClass(int i) { ptr = new int(i); }
~BaseClass() { delete ptr; delete ptr;}
void Print() { cout << *ptr; }
};
void fun(BaseClass x);


int main()
{
BaseClass o(10);
fun(o);
o.Print();
}


void fun(BaseClass x) {
cout << "Hello:";
}

  1. It prints: Hello:1
  2. It prints: Hello:
  3. It prints: 10
  4. Runtime error.

Answer(s): D



What will happen when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int getValue();

int main()
{
const int x = getValue();
cout<<x;
return 0;
}
int getValue()
{
return 5;
}

  1. It will print 0
  2. The code will not compile.
  3. It will print 5
  4. It will print garbage value

Answer(s): C



What happens when you attempt to compile and run the following code?

#include <iostream>
#include <string>
using namespace std;


class A {
public:
A() { cout << "A0 ";}
A(string s) { cout << "A1";}
};


class B : public A {
public:
B() { cout << "B0 ";}
B(string s) { cout << "B1 ";}
};


class C : private B {
public:
C() { cout << "C0 ";}
C(string s) { cout << "C1 ";}
};


int main () {
B b1;
C c1;
return 0;
}

  1. It prints: A0 B0 A0 B1 A0 C0 A0 C1
  2. It prints: B0 B1 C0 C1
  3. It prints: A0 B0 A0 B0 C0
  4. It prints: B0 B1

Answer(s): C



What is the output of the program?

#include <iostream>
#include <string>

using namespace std;

struct Person {
int age;
};


class First
{
Person *person;
public:
First() {person = new Person;
person?>age = 20;
}
void Print(){
cout << person?>age;
}
};


int main()
{
First t[2];
for (int i=0; i<2; i++)
t[i].Print();
}

  1. It prints: 10
  2. It prints: 2020
  3. It prints: 22
  4. It prints: 00

Answer(s): B






Post your Comments and Discuss C++ Institute CPA exam with other Community members: