Inheritance Test Java Aplus KEY PDF
Inheritance Test Java Aplus KEY PDF
Inheritance Test Java Aplus KEY PDF
com
A+ Computer Science
Inheritance M/C TEST KEY
Directions :: On your answer sheet, mark the letter of the best answer to each question.
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;
}
}
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
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
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
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;
}
}
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; }
}
a. one=n1;
b. super.one=n1;
c. super.one=n2;
d. setOne(n1);
e. super.setOne()=n2;
ANS: D PTS: 1
a. hello
b. null
c. false
d. a=b
e. true
ANS: E PTS: 1
a. hello
b. null
c. false
d. c=d
e. true
ANS: E PTS: 1
public class V
{
public void one(){
System.out.print("Vone");
}
public void two(){
System.out.print("Vtwo");
}
}
a. Vone
b. Vtwo
c. Wone
d. Wtwo
e. one
ANS: C PTS: 1
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");
}
}
a. WoneVtwo
b. WoneWone
c. VoneWone
d. VoneWtwo
e. WtwoVone
ANS: A PTS: 1
public class X{
public void one(){
System.out.print("Xone");
}
public void two(){
System.out.print("Xtwo");
}
}
a. Xone
b. Xtwo
c. Yone
d. Ytwo
e. one
ANS: C PTS: 1
public class X{
public void one(){
System.out.print("Xone");
}
public void two(){
System.out.print("Xtwo");
}
}
super.two();
}
}
a. XoneXtwoYoneYtwo
b. XoneXoneYoneYone
c. XoneYoneXtwoYtwo
d. YoneXoneYtwoXtwo
e. YoneXtwoYtwoXtwo
ANS: D PTS: 1
super.back();
}
public String toString() {
return super.toString() + " " + x;
}
}
a. 3.0
b. 5.0
c. 7.0
d. 9.0
e. 1.0
ANS: B PTS: 1
a. 5 5
b. 7 7
c. 5 3
d. 5 7
e. 7 5
ANS: E PTS: 1
return x;
}
public void back() {
super.back();
}
public String toString() {
return super.toString() + " " + x;
}
}
a. 5 5 9
b. 7 7 9
c. 5 5 3
d. 9 5 9
e. 7 5 9
ANS: A PTS: 1
a. 3.0
b. 5.0
c. 7.0
d. 9.0
e. 1.0
ANS: A PTS: 1
x=3;
}
public double go() {
return x;
}
public void back() {
super.back();
}
public String toString() {
return super.toString() + " " + x;
}
}
a. 5 5 9
b. 7 7 9
c. 5 5 3
d. 9 5 9
e. 7 5 9
ANS: C PTS: 1
public Whale() {
c++;
}
public void fun() {
x = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y + " " + c;
}
}
System.out.println(back());
}
public int back() {
return 2;
}
public String toString() {
return "class Beluga " + super.toString();
}
}
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
public Whale() {
c++;
}
public void fun() {
x = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y + " " + c;
}
}
return 2;
}
public String toString() {
return "class Beluga " + super.toString();
}
}
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
public Whale() {
c++;
}
public void fun() {
x = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y + " " + c;
}
}
}
public String toString() {
return "class Beluga " + super.toString();
}
}
a. 6 0 2
b. 6 0 3
c. 0 0 4
d. 6 0 4
e. 6 0 6
ANS: D PTS: 1
public Whale() {
c++;
}
public void fun() {
x = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y + " " + c;
}
}
return 2;
}
public String toString() {
return "class Beluga " + super.toString();
}
}
a. 1
b. 2
c. 3
d. 4
e. 5
ANS: B PTS: 1
public Whale() {
c++;
}
public void fun() {
x = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y + " " + c;
}
}
}
public int back() {
return 2;
}
public String toString() {
return "class Beluga " + super.toString();
}
}
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
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 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. 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
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);
}
}
a. 0
b. 3
c. 6
d. 9
e. 12
ANS: E PTS: 1
Give the class declarations above and variable instantiation below, which of the following woud
be valid statements?
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
private int x;
Give the class declarations above and variable instantiation below, which of the following woud
be valid statements?
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
Give the class declarations above and variable instantiation below, which of the following woud
be valid statements?
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
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
public Point()
{
x = 0;
y = 0;
}
© A+ Computer Science – www.apluscompsci.com
© A+ Computer Science – www.apluscompsci.com
public PointThree()
{ /* 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
public Point()
{
x = 0;
y = 0;
}
}
public class PointThree extends Point
{
private int z;
public PointThree()
{ /* 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
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
a. method overloading
b. method overriding
c. method rewriting
d. parent extention
e. static binding
ANS: B PTS: 1
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
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
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
a. dynamic binding
b. composition
c. encapsulation
d. static binding
e. none of these
ANS: A PTS: 1
a. dynamic binding
b. composition
c. encapsulation
d. static binding
e. none of the above
ANS: D PTS: 1