0% found this document useful (0 votes)
436 views7 pages

Java Inheritance and Method Overriding Quiz

The document contains 20 multiple choice questions related to Java programming concepts. It tests knowledge of object-oriented programming concepts like inheritance, polymorphism, abstraction, and encapsulation. Some example questions are about determining valid method declarations, overriding methods, static methods, and abstract classes. The overall document is a quiz that helps evaluate a learner's understanding of fundamental Java concepts.

Uploaded by

nunya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
436 views7 pages

Java Inheritance and Method Overriding Quiz

The document contains 20 multiple choice questions related to Java programming concepts. It tests knowledge of object-oriented programming concepts like inheritance, polymorphism, abstraction, and encapsulation. Some example questions are about determining valid method declarations, overriding methods, static methods, and abstract classes. The overall document is a quiz that helps evaluate a learner's understanding of fundamental Java concepts.

Uploaded by

nunya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Question 1 (1 mark) Attempt 1

If class B extends class A and


ng statement is false?
A
an instance of
B
an instance of
C
an instance of
D
an instance of

class C extends class B then which of the followi


class
class
class
class

A
A
C
C

may
may
may
may

use
use
use
use

all
all
all
all

public
public
public
public

methods
methods
methods
methods

of
of
of
of

class
class
class
class

A
B
B
A

Response was: D
Wrong Incorrect (0 mark)
A subclass instance may use superclass methods
Question 2 (1 mark) Attempt 1
Any instance of a Java class may use methods of the class
A
Object
B
String
C
Applet
D
Stack
Response was: A
Correct Correct (1 mark)
Question 3 (1 mark) Attempt 1
Which of the following pairs of classes could be expressed as a composition rela
tionship ?
A
Employee and Person
B
Project and Manager
C
Manager and Employee
D
Employee and Student
Response was: C
Wrong Incorrect (0 mark)
Composition refers to "has a" relationship
Question 4 (1 mark) Attempt 1
Assume that B is a subclass of A and that A has a constructor with 3 arguments.
Which of the following statements accurately represents a correct call to the su
perclass constructor as in (assuming that the required arguments are being passe
d correctly)?
A
this(...)
B
A(...)
C
super(...)
D
super.A(...)
Response was: D
Wrong Incorrect (0 mark)
Refer to section 7.2
Question 5 (1 mark) Attempt 1
What class relationship(s) ("is-a", "has-a") can be inferred from the class defi
nition below? (select all answers that apply)
public class A extends B

{
C c;
D d;
...
A
B
C
D

A
A
A
B

"is-a" B
"has-a" D
"has-a" C
"has-a" D

Response was: A and D


Wrong Incorrect (0 mark)
Inheritance = "is-a" relationship. Composition = "has-a" relationship
Question 6 (1 mark) Attempt 1
In the SomeOtherClass method increment(), which of the statements below are vali
d ? (select all answers that apply)
class A {
private int a;
int b;
protected int c;
public int d;
}
class SomeOtherClass { // not in same package
void increment() {
A refA = new A(...);
// statements to be inserted here
}
}
A
B
C
D

refA.d++
refA.c++
refA.a++
refA.b++

Response was: A and D


Wrong Incorrect (0 mark)
Default access is limited to the directory. Protected access is limited to direc
tory and subclasses
Question 7 (1 mark) Attempt 1
Assuming that Salesman is a subclass of Employee, which itself is a subclass of
Person (all with default constructors), then which of the statements below are v
alid ? (select all answers that apply)
A
Person p = new Person();
B
Person p = new Employee();
C
Person p = new Salesman();
D
Employee e = new Salesman();
Response was: A and B

Wrong Incorrect (0 mark)


A subclass (and subclass of subclass and so on) reference can be converted to su
perclass reference.
Question 8 (1 mark) Attempt 1
Assume that Salesman and Clerk are subclasses of Employee (both with default con
structors). Which of the statement below will compile correctly and run without
errors (no run-time error) ?
Employee e1 = new Salesman();
Employee e2 = new Clerk();
Clerk c;
..... // statement to assign something to c
A
B
C
D

c
c
c
c

=
=
=
=

e2;
(Clerk) e1;
e1;
(Clerk)e2;

Response was: A
Wrong Incorrect (0 mark)
Superclass references can be cast to subclass reference using an explicit cast.
Unrelated class references cannot be cast nor converted.
Question 9 (1 mark) Attempt 1
Assume that CAccount and SAccount are subclasses of Account both of which have o
verridden the deposit() and withdraw() methods. Which of the statement below is
true regarding the transfer method?
public boolean transfer(Account from, Account to, double amount) {
if (from.withdraw(amount) == true) ) {
to.deposit(amount);
return true;
}
return false;
}
A
The actual method called cannot be determined at compile-time as
it depends on the type of objects being passed
B
It will always call the withdraw() and deposit() of Account clas
s
C

It will always call the deposit of Account and withdraw() of CAc

count
D
It will always call the withdraw() and deposit() of one of the s
ubclasses of Account
Response was: D
Wrong Incorrect (0 mark)
Due to dynamic binding the actual method called cannot be determined at compiletime
Question 10 (1 mark) Attempt 1
In the program below Super method meth1() is overridden in Sub1 and Super method
meth2() in Sub2. Super method meth0()is not overridden in the subclasses. Both
subclasses have a method named meth3(). Which of the statement in the main metho
d of the Test class will result in compilation error ?

public class Test {


public static void main(String[] args) {
Super s[] = new Super[4];
s[0] = new Sub1();
s[1] = new Sub1();
s[2] = new Sub2();
s[3] = new Sub2();
for(int i=0; i<4; i++) {
s[i].meth0();
s[i].meth1();
s[i].meth2();
s[i].meth3();
}
}
}
class Super {
public void meth0() {
public void meth1() {
public void meth2() {
}
class Sub1 extends Super{
public void meth1() {
public void meth3() {
}
class Sub2 extends Super {
public void meth2() {
public void meth3() {
}
A
B
C
D

}
} // overridden in Sub1
} // overridden in Sub2
}
} // only in suclasses
}
} // only in subclasses

s[i].meth0();
s[i].meth3();
s[i].meth1();
s[i].meth2();

Response was: B
Correct Correct (1 mark)
Question 11 (1 mark) Attempt 1
Which of the statement(s) below attempting to override method1() in subclass B i
s valid ?
class A {
void method1(int amount) {
}
}
class B extends A {
// method1 to be overridden here
}
A
B
C
D

private void method1(int amount) { }


protected void method1(int amount) { }
public void method1(int amount) { }
void method1(int amount) { }

Response was: A
Wrong Incorrect (0 mark)
modifier cannot be made more restrictive
Question 12 (1 mark) Attempt 1
Which of the following method is not a static method ?
A
sqrt() method of Math class
B
parseInt() method of Integer class
C
parseDouble() method of Double class
D
readLine() of BufferedReader class
Response was: D
Correct Correct (1 mark)
Question 13 (1 mark) Attempt 1
To be able to call a method using the class name rather than an instance name, t
he method will have to be declared ________
Response was: static
Correct Correct (1 mark)
Question 14 (1 mark) Attempt 1
Answer true or false
when applied to a method, static means that the method
can be called without creating instances of the class
A
B

false
true

Response was: B
Correct Correct (1 mark)
Question 15 (1 mark) Attempt 1
Answer true or false:
A class that has abstract methods must be declared abstract explicitly using
abstract modifier.
A
B

true
false

Response was: A
Correct Correct (1 mark)
Question 16 (1 mark) Attempt 1
Answer true or false:
A class that is declared abstract need not have abstract methods.
A
B
Response was: B

true
false

Wrong Incorrect (0 mark)


Question 17 (1 mark) Attempt 1
Answer true or false:
All methods in an interface must be abstract (no implementation)
A
B

true
false

Response was: B
Wrong Incorrect (0 mark)
Question 18 (1 mark) Attempt 1
Given the declaration
float[] anArray = new float[256];
The following loop prints all the elements of the array back to front; select th
e most appropriate option to fill the blanks with the proper int numbers (such a
s 75, 9):
for (int i = ____; i >= ____ ; i--)
System.out.println(anArray[i]);
A
B
C
D
E

256,
255,
256,
255,
None

1
1
0
0
of the above - a for loop cannot count backwards.

Response was: C
Wrong Incorrect (0 mark)
Question 19 (1 mark) Attempt 1
Which of the following are valid method headers ?
A
public static int[] meth(int[] x)
B
public static int[] meth(int x)
C
public static int meth(int[] x)
D
public static int meth(int x)
Response was: B and D
Wrong Incorrect (0 mark)
an array can be passed as argument and can be returned
Question 20 (1 mark) Attempt 1
Which is the output of the program below ?
public class Test2DArray
{
public static void main (String[] args)
{
int[][] m = new int[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)

if (i == j)
m[i][j] = 1;
else
m[i][j] = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
System.out.print(" " + m[i][j]);
System.out.println();
}
}
}
A
0 1 0
0 1 0
0 1 0
B
0 1 0
1 0 1
0 1 0
C
0 0 0
1 1 1
0 0 0
D
1 0 0
0 1 0
0 0 1
Response was: D
Correct Correct (1 mark)

You might also like