Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Inheritance Test Java Aplus KEY PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

© A+ Computer Science – www.apluscompsci.

com

A+ Computer Science
Inheritance M/C TEST KEY
Directions :: On your answer sheet, mark the letter of the best answer to each question.

1. What is the output by the code below?

public class J
{
private int x, y;

public J() {
x = y = 3;
}
public void fun() {
x = y = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y;
}
}

public class K extends J {


public void fun() {
out.println(back());
}
public String toString() {
return "class K " + super.toString();
}
}

//test code in a client program


J runner = new K();
out.println(runner);

a. class K 3 3
b. class K 6 6
c. class K 3 6
d. class K 6 3
e. class K 1 3
ANS: A PTS: 1

© A+ Computer Science – www.apluscompsci.com


© A+ Computer Science – www.apluscompsci.com

2. Which of the following lines would correctly fill <blank 1> so that instance variable x would
be set to the value of parameter x?

public class M
{
private int x;
public M(){ x=0; }
public M(int x){
<blank 1>
}
}

a. x=x;
b. x=this.x;
c. this.x=x;
d. super.x=x;
e. that.x=x;
ANS: C PTS: 1

3. Which of the following lines occurs automatically at <blank 1>?

public class N
{
private int one;
public N(){ one=0; }
public N(int num){
one=num;
}
}
public class O extends N
{
public O() {
<blank 1>
}
}

a. super();
b. this();
c. that();
d. sub();
e. parent();
ANS: A PTS: 1

4. Which of the following lines would correctly fill <blank 1> to pass x to the appropriate
parent constructor?

public class P
{
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

private int one;


public P(){ one=0; }
public P(int num){
one=num;
}
}
public class Q extends P
{
public Q() { }
public Q(int x){
<blank 1>
}
}

a. super();
b. this(x);
c. parent(x);
d. super(x);
e. that(x);
ANS: D PTS: 1

5. Which of the following lines would correctly fill <blank 1> to complete the S toString()
method?

public class R
{
private int one;
public R(){ one=0; }
public String toString(){
return "" + one;
}
}

public class S extends R


{
private int two;
public S() { two=1; }
public String toString(){
<blank 1>
}
}

a. return super.toString() + " " + two;


b. return " " + two;
c. return super.toString();
d. return toString() + " " + two;
e. return super() + " " + two;
ANS: A PTS: 1
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

6. Which of the following lines would correctly fill <blank 1> to complete method setEm() so
that one would be assigned the value of n1?

public class T
{
private int one;
public T(){ one=0; }
public void setOne(int x){ one=x; }
}

public class U extends T


{
private int two;
public U() { two=1; }
public void setEm(int n1, int n2)
{
two=n2;
<blank 1>
}
}

a. one=n1;
b. super.one=n1;
c. super.one=n2;
d. setOne(n1);
e. super.setOne()=n2;
ANS: D PTS: 1

7. What is output by the code below?

Object a = new String("Hello");


String b = "Hello";
System.out.println(a.equals(b));

a. hello
b. null
c. false
d. a=b
e. true
ANS: E PTS: 1

8. What is output by the code below?

Object c = new String("Hello");


String d = new String("Hello");
System.out.println(c.equals(d));

© A+ Computer Science – www.apluscompsci.com


© A+ Computer Science – www.apluscompsci.com

a. hello
b. null
c. false
d. c=d
e. true
ANS: E PTS: 1

9. What is the output of line 1?

public class V
{
public void one(){
System.out.print("Vone");
}
public void two(){
System.out.print("Vtwo");
}
}

public class W extends V


{
public void one(){
System.out.print("Wone");
}
public void two(){
System.out.print("Wtwo");
}
public void testIt(){
one();
super.two();
}
}

//code in the main of another class


W codeTest = new W();
codeTest.one(); //line 1
codeTest.testIt(); //line 2

a. Vone
b. Vtwo
c. Wone
d. Wtwo
e. one
ANS: C PTS: 1

10. What is the output of line 2?

public class V
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

{
public void one(){
System.out.print("Vone");
}
public void two(){
System.out.print("Vtwo");
}
}

public class W extends V


{
public void one(){
System.out.print("Wone");
}
public void two(){
System.out.print("Wtwo");
}
public void testIt(){
one();
super.two();
}
}

//code in the main of another class


W codeTest = new W();
codeTest.one(); //line 1
codeTest.testIt(); //line 2

a. WoneVtwo
b. WoneWone
c. VoneWone
d. VoneWtwo
e. WtwoVone
ANS: A PTS: 1

11. What is the output of line 1?

public class X{
public void one(){
System.out.print("Xone");
}
public void two(){
System.out.print("Xtwo");
}
}

public class Y extends X{


© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

public void one(){


System.out.print("Yone");
}
public void two(){
System.out.print("Ytwo");
}
public void testIt(){
one();
super.one();
two();
super.two();
}
}

//code in the main of another class


Y code = new Y();
code.one(); //line 1
code.testIt(); //line 2

a. Xone
b. Xtwo
c. Yone
d. Ytwo
e. one
ANS: C PTS: 1

12. What is the output of line 2?

public class X{
public void one(){
System.out.print("Xone");
}
public void two(){
System.out.print("Xtwo");
}
}

public class Y extends X{


public void one(){
System.out.print("Yone");
}
public void two(){
System.out.print("Ytwo");
}
public void testIt(){
one();
super.one();
two();
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

super.two();
}
}

//code in the main of another class


Y code = new Y();
code.one(); //line 1
code.testIt(); //line 2

a. XoneXtwoYoneYtwo
b. XoneXoneYoneYone
c. XoneYoneXtwoYtwo
d. YoneXoneYtwoXtwo
e. YoneXtwoYtwoXtwo
ANS: D PTS: 1

13. What is output by the code below?

public class Cat


{
private int x, y;
public Cat() {
x=y=5;
}
public void fun() {
x=7;
}
public double go() {
return x;
}
public void back() {
fun();
}
public String toString() {
return x + " " + y;
}
}

public class Lion extends Cat


{
private int x;
public Lion() {
x=9;
}
public void fun() {
x=3;
}
public double go() {
return x;
}
public void back() {
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

super.back();
}
public String toString() {
return super.toString() + " " + x;
}
}

//code in a client program


Cat fred = new Cat();
System.out.println(fred.go());

a. 3.0
b. 5.0
c. 7.0
d. 9.0
e. 1.0
ANS: B PTS: 1

14. What is output by the code below?

public class Cat


{
private int x, y;
public Cat() {
x=y=5;
}
public void fun() {
x=7;
}
public double go() {
return x;
}
public void back() {
fun();
}
public String toString() {
return x + " " + y;
}
}

public class Lion extends Cat


{
private int x;
public Lion() {
x=9;
}
public void fun() {
x=3;
}
public double go() {
return x;
}
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

public void back() {


super.back();
}
public String toString() {
return super.toString() + " " + x;
}
}

//code in a client program


Cat fred = new Cat();
fred.back();
System.out.println(fred);

a. 5 5
b. 7 7
c. 5 3
d. 5 7
e. 7 5
ANS: E PTS: 1

15. What is output by the code below?

public class Cat


{
private int x, y;
public Cat() {
x=y=5;
}
public void fun() {
x=7;
}
public double go() {
return x;
}
public void back() {
fun();
}
public String toString() {
return x + " " + y;
}
}

public class Lion extends Cat


{
private int x;
public Lion() {
x=9;
}
public void fun() {
x=3;
}
public double go() {
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

return x;
}
public void back() {
super.back();
}
public String toString() {
return super.toString() + " " + x;
}
}

//code in a client program


Cat fred = new Lion();
System.out.println(fred);

a. 5 5 9
b. 7 7 9
c. 5 5 3
d. 9 5 9
e. 7 5 9
ANS: A PTS: 1

16. What is output by the code below?

public class Cat


{
private int x, y;
public Cat() {
x=y=5;
}
public void fun() {
x=7;
}
public double go() {
return x;
}
public void back() {
fun();
}
public String toString() {
return x + " " + y;
}
}

public class Lion extends Cat


{
private int x;
public Lion() {
x=9;
}
public void fun() {
x=3;
}
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

public double go() {


return x;
}
public void back() {
super.back();
}
public String toString() {
return super.toString() + " " + x;
}
}

//code in a client program


Cat fred = new Lion();
fred.fun();
System.out.println(fred.go());

a. 3.0
b. 5.0
c. 7.0
d. 9.0
e. 1.0
ANS: A PTS: 1

17. What is output by the code below?

public class Cat


{
private int x, y;
public Cat() {
x=y=5;
}
public void fun() {
x=7;
}
public double go() {
return x;
}
public void back() {
fun();
}
public String toString() {
return x + " " + y;
}
}

public class Lion extends Cat


{
private int x;
public Lion() {
x=9;
}
public void fun() {
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

x=3;
}
public double go() {
return x;
}
public void back() {
super.back();
}
public String toString() {
return super.toString() + " " + x;
}
}

//code in a client program


Cat fred = new Lion();
fred.fun();
fred.back();
System.out.println(fred);

a. 5 5 9
b. 7 7 9
c. 5 5 3
d. 9 5 9
e. 7 5 9
ANS: C PTS: 1

18. What is output by line 1?

public class Whale


{
private int x, y;
public static int c;

public Whale() {
c++;
}
public void fun() {
x = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y + " " + c;
}
}

public class Beluga extends Whale


{
public void fun() {
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

System.out.println(back());
}
public int back() {
return 2;
}
public String toString() {
return "class Beluga " + super.toString();
}
}

//code in the main of another class


Whale flipper = new Beluga();
System.out.println(flipper); //line 1

a. class Beluga 0 0 1
b. class Beluga 0 0 2
c. class Beluga 0 0 3
d. class Beluga 0 0 0
e. class Beluga 0 0 5
ANS: A PTS: 1

19. What is output by line 1?

public class Whale


{
private int x, y;
public static int c;

public Whale() {
c++;
}
public void fun() {
x = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y + " " + c;
}
}

public class Beluga extends Whale


{
public void fun() {
System.out.println(back());
}
public int back() {
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

return 2;
}
public String toString() {
return "class Beluga " + super.toString();
}
}

//code in the main of another class


Whale flipper = new Beluga();
flipper = new Whale();
flipper = new Beluga();
System.out.println(flipper); //line 1

a. class Beluga 0 0 1
b. class Beluga 0 0 2
c. class Beluga 0 0 3
d. class Beluga 0 0 0
e. class Beluga 0 0 5
ANS: C PTS: 1

20. What is output by line 1?

public class Whale


{
private int x, y;
public static int c;

public Whale() {
c++;
}
public void fun() {
x = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y + " " + c;
}
}

public class Beluga extends Whale


{
public void fun() {
System.out.println(back());
}
public int back() {
return 2;
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

}
public String toString() {
return "class Beluga " + super.toString();
}
}

//code in the main of another class


Whale flipper = new Beluga();
flipper = new Whale();
flipper = new Beluga();
flipper = new Whale();
flipper.fun();
System.out.println(flipper); //line 1

a. 6 0 2
b. 6 0 3
c. 0 0 4
d. 6 0 4
e. 6 0 6
ANS: D PTS: 1

21. What is output by line 1?

public class Whale


{
private int x, y;
public static int c;

public Whale() {
c++;
}
public void fun() {
x = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y + " " + c;
}
}

public class Beluga extends Whale


{
public void fun() {
System.out.println(back());
}
public int back() {
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

return 2;
}
public String toString() {
return "class Beluga " + super.toString();
}
}

//code in the main of another class


Whale flipper = new Beluga();
flipper = new Whale();
flipper = new Beluga();
flipper = new Whale();
flipper.fun();
flipper = new Beluga();
flipper.fun(); //line 1

a. 1
b. 2
c. 3
d. 4
e. 5
ANS: B PTS: 1

22. What is output by line 1?

public class Whale


{
private int x, y;
public static int c;

public Whale() {
c++;
}
public void fun() {
x = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y + " " + c;
}
}

public class Beluga extends Whale


{
public void fun() {
System.out.println(back());
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

}
public int back() {
return 2;
}
public String toString() {
return "class Beluga " + super.toString();
}
}

//code in the main of another class


Whale flipper = new Beluga();
flipper = new Whale();
flipper = new Beluga();
flipper = new Whale();
flipper.fun();
flipper = new Beluga();
flipper.fun();
System.out.println(flipper); //line 1

a. class Beluga 0 0 3
b. class Beluga 0 0 4
c. class Beluga 0 0 6
d. class Beluga 0 0 5
e. class Beluga 0 0 8
ANS: D PTS: 1

23. Which of the following reserved words is used to when calling a parent class method?

a. parent
b. super
c. implements
d. extends
e. inherits
ANS: B PTS: 1

24. What is wrong with line 1?

public class G
{
private int x;
public G() { x=3;}
public void setX(int val){
x=val;
}
public String toString(){
return ""+x;
}
}
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

public class H extends G


{
private int y;
public H() { y=4;}
public void setY(int val){
y=val;
}
public String toString() {
return ""+y+super.toString();
}
}

//test code in the main method


G bad = new H();
bad.setY(9); //line 1

a. You cannot call an H method on a G reference


b. You cannot call an H method on a H reference
c. You cannot call an G method on a G reference
d. You cannot call an G method on a H reference
e. There is nothing wrong with line 1.
ANS: A PTS: 1

25. Which of the following lines would correctly fix line 1?

public class G{
private int x;
public G() { x=3;}
public void setX(int val){
x=val;
}
public String toString(){
return ""+x;
}
}

class H extends G{
private int y;
public H() { y=4;}
public void setY(int val){
y=val;
}
public String toString() {
return ""+y+super.toString();
}
}

© A+ Computer Science – www.apluscompsci.com


© A+ Computer Science – www.apluscompsci.com

//test code in the main method


G bad = new H();
bad.setY(9); //line 1

a. bad.setY(9);
b. ((H)bad).setY(9);
c. ((G)bad).setY(9);
d. (H)bad.setY(9);
e. more than one of these
ANS: B PTS: 1

26. What is output by the code below?

public class E
{
private int x;

public E() {
x=12;
}
public E(int val){
x=val;
}
public String toString() {
return "" + x;
}
}

class F extends E
{
public F(){
}
public F(int num){
super(num);
}
}

//test code in a client program


E what = new F();
out.println( what );

a. 0
b. 3
c. 6
d. 9
e. 12
ANS: E PTS: 1

© A+ Computer Science – www.apluscompsci.com


© A+ Computer Science – www.apluscompsci.com

27. public class G


{
private int x;

public G() { x=3;}

public void setX(int val){


x=val;
}

public int getX(){


return x;
}
}

public class H extends G


{
private int y;

public H() { y=4;}

public void setY(int val){


y=val;
}

public int getY(){


return y;
}
}

Give the class declarations above and variable instantiation below, which of the following woud
be valid statements?

G bob = new G();

I. System.out.println( bob.y );
II. bob.setX(9);
III. System.out.println( bob.getX() );

a. I only
b. II only
c. III only
d. I and II only
e. II and III only
ANS: E PTS: 1

28. public class G


{
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

private int x;

public G() { x=3;}

public void setX(int val){


x=val;
}

public int getX(){


return x;
}
}

public class H extends G


{
private int y;

public H() { y=4;}

public void setY(int val){


y=val;
}

public int getY(){


return y;
}
}

Give the class declarations above and variable instantiation below, which of the following woud
be valid statements?

H bob = new H();

I. System.out.println( bob.y );
II. bob.setY(9);
III. System.out.println( bob.getX() );

a. I only
b. II only
c. III only
d. I and II only
e. II and III only
ANS: E PTS: 1

29. public class G


{
private int x;

© A+ Computer Science – www.apluscompsci.com


© A+ Computer Science – www.apluscompsci.com

public G() { x=3;}

public void setX(int val){


x=val;
}

public int getX(){


return x;
}
}

public class H extends G


{
private int y;

public H() { y=4;}

public void setY(int val){


y=val;
}

public int getY(){


return y;
}
}

Give the class declarations above and variable instantiation below, which of the following woud
be valid statements?

G bob = new H();

I. System.out.println( bob.x );
II. bob.setY(9);
III. System.out.println( bob.getX() );

a. I only
b. II only
c. III only
d. I and II only
e. II and III only
ANS: C PTS: 1

30. public class G


{
private int x;

public G() { x=3;}

© A+ Computer Science – www.apluscompsci.com


© A+ Computer Science – www.apluscompsci.com

public void setX(int val){


x=val;
}

public int getX(){


return x;
}
}

public class H extends G


{
private int y;

public H() { y=4;}

public void setY(int val){


y=val;
}

public int getY(){


return y;
}
}

Give the class declarations above and variable instantiation below, which of the following woud
be valid statements?
G bob = new H();

I. System.out.println( bob.x );
II. bob.setX(9);
III. System.out.println( ((H)bob).getY() );

a. I only
b. II only
c. III only
d. I and II only
e. II and III only
ANS: E PTS: 1

31. public class Point


{
private int x,y;

public Point()
{
x = 0;
y = 0;
}
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

public Point(int nx, int ny)


{
x = nx;
y = ny;
}
}
public class PointThree extends Point
{
private int z;

public PointThree()
{ /* Implementation not shown */ }

public PointThree(int nx, int ny, int nz)


{ /* Implementation not shown */ }

}
Which of the following correctly implements the default PointThree construtor?

a. super();
z = 0;
b. z = 0;
super();
c. x = 0;
y = 0;
z = 0;
d. super();
y = 0;
z = 0;
e. super();
ANS: A PTS: 1

32. public class Point


{
private int x,y;

public Point()
{
x = 0;
y = 0;
}

public Point(int nx, int ny)


{
x = nx;
y = ny;
}
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

}
public class PointThree extends Point
{
private int z;

public PointThree()
{ /* Implementation not shown */ }

public PointThree(int nx, int ny, int nz)


{ /* Implementation not shown */ }

}
Which of the following correctly implements the PointThree construtor with parameters?

a. super();
z = nz;
b. z = nz;
super();
c. super(nx,ny,nz);
d. super(nx,ny);
z = nz;
e. super();
ANS: D PTS: 1

33. Consider the following classes.

public class Dog


{
/* code */
}

public class Dachshund extends Dog


{
/* code */
}
Assuming that each class has a default constructor, which of the following are valid
declarations?

I. Dog sadie = new Dachshund();


II. Dachshund aldo = new Dachshund();
III. Dachshund doug = new Dog();

a. I only
b. II only
c. III only
d. I and II only
e. II and III only
ANS: D PTS: 1
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

34. Which of the following access priveleges makes instance variables visible only to the class
where they are defined?

a. parent public
b. packet public
c. protected
d. private
e. public
ANS: D PTS: 1

35. A child having a different version of a parent’s method is an example of what?

a. method overloading
b. method overriding
c. method rewriting
d. parent extention
e. static binding
ANS: B PTS: 1

36. What is output by the client code below?

class A{
public static int x = 0;
public A(){
x++;
}
public static int getX()
{
return x;
}
}
class B extends A{}

///////////////////////////////////////////////////////////////////
//client code in the main of a runner class
A a = new A();
a = new A();
System.out.println(A.getX());

a. 0
b. 1
c. 2
d. 3
e. 4
ANS: C PTS: 1

© A+ Computer Science – www.apluscompsci.com


© A+ Computer Science – www.apluscompsci.com

37. What is output by the client code below?

class A{
public static int x = 0;
public A(){
x++;
}
public static int getX()
{
return x;
}
}
class B extends A{}

///////////////////////////////////////////////////////////////////
//client code in the main of a runner class
A a = new A();
a = new B();
System.out.println(A.getX());

a. 0
b. 1
c. 2
d. 3
e. 4
ANS: C PTS: 1

38. What is output by the client code below?

class A{
public static int x = 0;
public A(){
x++;
}
public static int getX()
{
return x;
}
}
class B extends A{
public B(){
x++;
}
}

///////////////////////////////////////////////////////////////////
//client code in the main of a runner class
A a = new A();
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com

a = new B();
System.out.println(A.getX());

a. 0
b. 1
c. 2
d. 3
e. there is no output due to a syntax error
ANS: D PTS: 1

39. Overriding methods is an example of which concept?

a. dynamic binding
b. composition
c. encapsulation
d. static binding
e. none of these
ANS: A PTS: 1

40. Private variables of a class are an example of which concept?

a. dynamic binding
b. composition
c. encapsulation
d. static binding
e. none of the above
ANS: D PTS: 1

© A+ Computer Science – www.apluscompsci.com

You might also like