CPA Chapter 6 Assessment Answers 100%
1. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X {
private:
int v;
};
class Y : public X {
Y() : v(0) {}
}
int main() {
Y y;
cout << y.v;
return 0;
}
It prints -1
It prints 0
It prints 1
Compilation fails
2. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X {
protected:
int v;
};
class Y : protected X {
Y() : v(0) {}
}
int main() {
Y *y = new Y();
cout << y->v;
delete y;
return 0;
}
, It prints -1
It prints 0
Compilation fails
It prints 1
3. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X {
public:
int v;
void put(int x) { v = x; }
int get(void) { return v; }
};
class Y : public X {
public:
Y() { put(0); }
void write(int x) { put(x + 1); }
int read(void) { return get() – 1; }
};
int main() {
Y *y = new Y();
y->write(1);
cout << y->read();
delete y;
return 0;
}
It prints -1
Compilation fails
It prints 1
It prints 0
4. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X { };
class Y : public X { };
class Z : public X { };
int main() {
Z *z = new Z();
1. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X {
private:
int v;
};
class Y : public X {
Y() : v(0) {}
}
int main() {
Y y;
cout << y.v;
return 0;
}
It prints -1
It prints 0
It prints 1
Compilation fails
2. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X {
protected:
int v;
};
class Y : protected X {
Y() : v(0) {}
}
int main() {
Y *y = new Y();
cout << y->v;
delete y;
return 0;
}
, It prints -1
It prints 0
Compilation fails
It prints 1
3. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X {
public:
int v;
void put(int x) { v = x; }
int get(void) { return v; }
};
class Y : public X {
public:
Y() { put(0); }
void write(int x) { put(x + 1); }
int read(void) { return get() – 1; }
};
int main() {
Y *y = new Y();
y->write(1);
cout << y->read();
delete y;
return 0;
}
It prints -1
Compilation fails
It prints 1
It prints 0
4. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X { };
class Y : public X { };
class Z : public X { };
int main() {
Z *z = new Z();