Ch. 11 Quiz
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(); } } > Nothing displayed > "The default constructor of B is invoked" > "The default constructor of A is invoked" followded by "The default constructor of B is invoked" > "The default constructor of B is invoked" followed by "The default constructor of A is invoked" > "The default constructor of A is invoked"
"The default constructor of A is invoked" followed by "The default constructor of B is invoked"
What is the output of the following code? public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); } } class Student extends Person { private String getInfo() { return "Student"; } } class Person { private String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } } > Person Person > Person Student > Stduent Student > Student Person
Person Person
Assume Cylinder is a subtype of Circle. Analyze the following code: Cylinder cy = new Cylinder(1, 1); Circle c = cy; > The code has a compile error. > The code has a runtime error. > The code is fine.
The code is fine.
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 public. > The variable should be marked private. > The variable should be marked protected. > The variable should have no special access modifier. > The variable should be marked private and an accessor method provided.
The variable should be marked protected.
Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ___________. > True > False
True
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? > public > private > protected > Use the default modifier.
Use the default modifier.
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? > c1 instanceof C1 > c2 instanceof C1 > c3 instanceof C1 > c4 instanceof C2
c4 instanceof C2
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 > true true > false true > true false
false false
What is the output of the following code: public class Test { public static void main(String[] args) { String s1 = new String("Java"); String s2 = new String("Java"); System.out.print((s1 == s2) + " " + (s1.equals(s2))); } } > false false > true true > false true > true false
false true
Object-oriented programming allows you to derive new classes from existing classes. This is called ____________. > encapsulation > inheritance > abstraction > generalization
inheritance
Which of the following are Java keywords? > instanceOf > instanceof > cast > casting
instanceof
You can create an ArrayList using _________. > new ArrayList[] > new ArrayList[100] > new ArrayList<>() > ArrayList()
new ArrayList<>()
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? > public > private > protected > Use the default modifier.
protected
Inheritance means ______________. > that data fields should be declared private > that a class can extend another class > that a variable of supertype can refer to a subtype object > that a class can contain another class
that a class can extend another class
Polymorphism means ______________. > that data fields should be declared private > that a class can extend another class > that a variable of supertype can refer to a subtype object > that a class can contain another class
that a variable of supertype can refer to a subtype object
Encapsulation means ______________. > that data fields should be declared private > that a class can extend another class > that a variable of supertype can refer to a subtype object > that a class can contain another class
that data fields should be declared private
Invoking _________ removes all elements in an ArrayList x. > x.remove() > x.clean() > x.delete() > x.empty() > x.clear()
x.clear()
Invoking _________ returns the first element in an ArrayList x. > x.first() > x.get(0) > x.get(1) > x.get()
x.get(0)
Invoking _________ returns the number of the elements in an ArrayList x. > x.getSize() > x.getLength(0) > x.length(1) > x.size()
x.size()
Suppose an ArrayList list contains {"red", "green", "red", "green"}. What is the list after the following code? list.remove("red"); > {"red", "green", "red", "green"} > {"green", "red", "green"} > {"green", "green"} > {"red", "green", "green"}
{"green", "red", "green"}