CS122/504 QUIZ 1 Prof. Lixin Tao February 23, 2006 1) Questions 1 through 4 are based on the following definitions: -----------------Comparable.java------------------- package p1; public interface Comparable { int compare(Object o); } -----------------A.java---------------------------- package p1; public class A { protected int a; public void fa1() { ... } protected void fa2() { ... } void fa3() { ... } } -----------------B.java---------------------------- package p1; public class B extends A { private int b; private void fb1() { ... } protected void fb2() { ... } public void fb3() { ... } public static void main(String[] args) { B b1 = new B(); A b2 = b1; ...... } } -----------------C.java---------------------------- package p2; public class C extends p1.B implements p1.Comparable { public int compare(Object o) { ... } private void fc() { ... } public static void main(String[] args) { C c1 = new C(); p1.Comparable c2 = c1; ...... } } -----------------D.java--------------------------- package p2; public class D { public static void main(String[] args) { C d = new C(); ...... } } From reference variable b1, which of the following data fields and methods can be accessed? (multiple answers) [10%] 1) a * 2) fa1() * 3) fa2() * 4) fa3() * 5) b * 6) fb1() * 7) fb2() * 8) fb3() * 9) fc() 10) compare(Object o) 2) Based on the same source code in question 1, from reference variable b2, which of the following data fields and methods can be accessed? (multiple answers) [10%] 1) a * 2) fa1() * 3) fa2() * 4) fa3() * 5) b 6) fb1() 7) fb2() 8) fb3() 9) fc() 10) compare(Object o) 3) Based on the same source code in question 1, from reference variable c1, which of the following data fields and methods can be accessed? (multiple answers) [10%] 1) a * 2) fa1() * 3) fa2() * 4) fa3() 5) b 6) fb1() 7) fb2() * 8) fb3() * 9) fc() * 10) compare(Object o) * 4) Based on the same source code in question 1, from reference variable c2, which of the following data fields and methods can be accessed? (multiple answers) [10%] 1) a 2) fa1() 3) fa2() 4) fa3() 5) b 6) fb1() 7) fb2() 8) fb3() 9) fc() 10) compare(Object o) * 5) Based on the same source code in question 1, from reference variable d, which of the following data fields and methods can be accessed? (multiple answers) [10%] 1) a 2) fa1() * 3) fa2() 4) fa3() 5) b 6) fb1() 7) fb2() 8) fb3() * 9) fc() 10) compare(Object o) * 6) What do we mean that class A implements interface IF? (multiple answers) [10%] 1) Class A will implement a graphic user interface based on IF. 2) Class A promises to implement all methods in IF before it can be concrete. * 3) An object of type A can save its reference in a reference variable of type IF. * 4) From a reference variable of type IF, we can access all public methods defined for the object whose reference the variable is holding. 7) Write a class Stack of integers with array of integers, and let class Stack implement interface Iterator. public interface Iterator { Iterator iterator(); // return an Iterator object for a data structure boolean hasNext(); // whether there are more unread integers in the iterator int next(); // return the next integer in the iterator } [40%]