Chapter 11 Flashcards
A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this?
The variable should be marked protected.
Invoking _________ returns the first element in an ArrayList x.
x.get(0)
Invoking _________ returns the number of the elements in an ArrayList x.
x.size()
Assume Cylinder is a subtype of Circle. Analyze the following code: Cylinder cy = new Cylinder(1, 1); Circle c = cy;
The code is fine.
What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package?
protected
Which of the following are Java keywords?
instanceof
What is the output of running class C? class A { public A() { System.out.println( "The default constructor of A is invoked"); } } class B extends A { public B() { System.out.println( "The default constructor of B is invoked"); } } public class C { public static void main(String[] args) { B b = new B(); } }
“The default constructor of A is invoked” followded by “The default constructor of B is invoked”
Given the following code: class C1 {} class C2 extends C1 { } class C3 extends C2 { } class C4 extends C1 {} C1 c1 = new C1(); C2 c2 = new C2(); C3 c3 = new C3(); C4 c4 = new C4(); Which of the following expressions evaluates to false?
c4 instanceof C2
Inheritance means ______________.
that a class can extend another class
Polymorphism means ______________.
that a variable of supertype can refer to a subtype object
Invoking _________ removes all elements in an ArrayList x.
x.clear()
What modifier should you use on a class so that a class in the same package can access it but a class (including a subclass) in a different package cannot access it?
Use the default modifier.
Suppose an ArrayList list contains {“red”, “green”, “red”, “green”}. What is the list after the following code?
list.remove(“red”);
{“green”, “red”, “green”}
What is the output of the following code: public class Test { public static void main(String[] args) { Object o1 = new Object(); Object o2 = new Object(); System.out.print((o1 == o2) + " " + (o1.equals(o2))); } }
false false
Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ___________.
True