Inheritance Quiz

By Mickey Arnold
Last updated 10 months ago
10 Questions

Which reserved word is used to indicate that one class is a child or descendant of another class?

Consider the classes and client code below to determine what is printed by the client code.
public class N
{
private int one;
public N(){
one=5;
}
public String toString(){
return ""+ one;
}
}
public class O extends N
{
}

//client code
N it = new O();
System.out.println( it );

Consider the classes and client code below to determine the output of the client code.
public class N
{
private int one;
public N( int x ){
one=x;
}
public String toString(){
return ""+ one;
}
}
public class O extends N
{
public O( int x ){
super( x );
}
}

////////////////////////////////////////////////////////////////////////////
//client code
N it = new O(7);
System.out.println( it );

Consider the classes below and determine what is printed out by the client code.
public class V
{
public void one(){
System.out.print("it");
}
public void two(){
System.out.print("go");
}
}
public class W extends V
{
public void one(){
System.out.print("up");
}
public void two(){
System.out.print("at");
}
public void testIt(){
one();
super.two();
}
}
///////////////////////////////////////////////////////////////
//client code in the main of another class
V x = new V();
x.one();

Consider the classes below and determine what is printed out by the client code.
public class V
{
public void one(){
System.out.print("it");
}
public void two(){
System.out.print("go");
}
}
public class W extends V
{
public void one(){
System.out.print("up");
}
public void two(){
System.out.print("at");
}
public void testIt(){
one();
super.two();
}
}

///////////////////////////////////////////////////////////////
//client code in the main of another class
V x = new W();
x.one();

Consider the classes below and determine what is printed out by the client code.
public class V
{
public void one(){
System.out.print("it");
}
public void two(){
System.out.print("go");
}
}
public class W extends V
{
public void one(){
System.out.print("up");
}
public void two(){
System.out.print("at");
}
public void testIt(){
one();
super.two();
}
}
///////////////////////////////////////////////////////////////
//client code in the main of another class
V x = new W();
x.one();
x.two();

Consider the classes below and determine what is printed out by the client code.
public class V
{
public void one(){
System.out.print("it");
}
public void two(){
System.out.print("go");
}
}
public class W extends V
{
public void one(){
System.out.print("up");
}
public void two(){
System.out.print("at");
}
public void testIt(){
one();
super.two();
}
}
///////////////////////////////////////////////////////////////
//client code in the main of another class
V x = new W();
x.one();
x = new V();
x.two();

Consider the classes below and determine what is printed out by the client code.
public class V
{
public void one(){
System.out.print("it");
}
public void two(){
System.out.print("go");
}
public void testIt(){
one();
}
}
public class W extends V
{
public void one(){
System.out.print("up");
}
public void two(){
System.out.print("at");
}
public void testIt(){
one();
super.testIt();
super.one();
}
}
///////////////////////////////////////////////////////////////
//client code in the main of another class
W x = new W();
x.testIt();

What is the output by the code below?
public class J
{
private int x, y;
public J() {
x = y = 4;
}
public void fun() {
x = y = 2;
}
public int back() {
return 7;
}
public String toString() {
return x + " " + y;
}
}
public class K extends J {
public void fun() {
System.out.print(back() + " ");
}
public String toString() {
return "" + super.toString();
}
}
//test code in a client program
J runner = new K();
System.out.println(runner);

What is the output by the code below?
public class J
{
private int x, y;
public J() {
x = y = 4;
}
public void fun() {
x = y = 2;
}
public int back() {
return 7;
}
public String toString() {
return x + " " + y;
}
}
public class K extends J {
public void fun() {
System.out.print(back() + " ");
}
public String toString() {
return "" + super.toString();
}
}
//test code in a client program
J runner = new K();
runner.fun();
System.out.println(runner);