quiz ch 11
What is the output of the following code? ArrayList list = new ArrayList(); String s1 = new String("Java"); String s2 = new String("Java"); list.add(s1); list.add(s2); System.out.println((list.get(0) == list.get(1)) + " " + (list.get(0)).equals(list.get(1)));
false true
Assume Circle is the superclass of Cylinder. Analyze the following code: Cylinder cy = new Cylinder(1, 1); Circle c = cy;
The code is fine.
Analyze the following code: ArrayList list = new ArrayList(); list.add("Beijing"); list.add("Tokyo"); list.add("Shanghai"); list.set(3, "Hong Kong");
The last line in the code causes a runtime error because there is no element at index 3 in the array list. If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine.
Analyze the following code: public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new Object(); System.out.println(a1); System.out.println(a2); } } class A { int x; public String toString() { return "A's x is " + x; } }
When executing System.out.println(a1), the toString() method in the A class is invoked. When executing System.out.println(a2), the toString() method in the Object class is invoked.
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
Object-oriented programming allows you to derive new classes from existing classes. This is called ________.
inheritance
Given the following code, find the compile error(s). public class Test { public static void main(String[] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); } public static void m(Student x) { System.out.println(x.toString()); } } class GraduateStudent extends Student { } class Student extends Person { public String toString() { return "Student"; } } class Person extends Object { public String toString() { return "Person"; } }
m(new Person()) causes an error m(new Object()) causes an error
Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ________.
true
Show the output of the following code: String[] array = {"red", "green", "blue"}; ArrayList list = new ArrayList(Arrays.asList(array)); list.add(0, "red"); System.out.println(list);
["red", "red", "green", "blue"]
What is the output? ArrayList list = new ArrayList( Arrays.asList(new String[] {"red", "red", "green"})); String element = "red"; for (int i = list.size() - 1; i >= 0; i--) if (list.get(i).equals(element)) list.remove(element); System.out.println(list);
{"green"}
Which of the following is INcorrect?
A constructor may be static.
Which of the following statements are true? A. A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. B. To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. C. It is a compilation error if two methods differ only in return type in the same class. D. A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. E. Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them.
All of the above
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
Analyze the following code. // Program 1: public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(Object a) { return this.x == ((A)a).x; } } // Program 2: public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } }
Program 1 displays true and Program 2 displays false
Analyze the following code. // Program 1 public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(((A)a1).equals((A)a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } // Program 2 public class Test { public static void main(String[] args) { A a1 = new A(); A a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } }
Program 1 displays true and Program 2 displays true
Analyze the following code: public class Test { public static void main(String[] args) { new B(); } } class A { int i = 7; public A() { System.out.println("i from A is " + i); } public void setI(int i) { this.i = 2 * i; } } class B extends A { public B() { setI(20); // System.out.println("i from B is " + i); } public void setI(int i) { this.i = 3 * i; } }
The constructor of class A is called and it displays "i from A is 7".
Analyze the following code: public class Test { public static void main(String[] args) { B b = new B(); b.m(5); System.out.println("i is " + b.i); } } class A { int i; public void m(int i) { this.i = i; } } class B extends A { public void m(String s) { } }
The method m is not overridden in class B. B inherits the method m of class A and defines an overloaded method m in class B.
Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code: class Square extends GeometricObject { double length; Square(double length) { GeometricObject(length); } }
The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally.
Analyze the following code: public class Test extends A { public static void main(String[] args) { Test t = new Test(); t.print(); } } class A { String s; A(String s) { this.s = s; } public void print() { System.out.println(s); } }
The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed. The program would compile if a default constructor A(){ } is added to class A explicitly.
Given the following classes and their objects: class C1 { public static void main(String...ar) { C2 c2 = new C2(); C3 c3 = new C3(); c2 = (C2)((C1)c3); } }; class C2 extends C1 {}; class C3 extends C1 {};
You will get a runtime error because you cannot cast objects from sibling classes.
The equals method is defined in the Object class. Which of the following is correct to override it in the String class?
public boolean equals( Object other)
Analyze the following code: public class Test { public static void main(String[] args) { String s = new String("Welcome to Java"); Object o = s; String d = (String)o; } }
s, o, and d reference the same String object.
Invoking ________ removes all elements in an ArrayList x.
x.clear()
Invoking ________ returns the first element in an ArrayList x.
x.get(0)
Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing]?
x.remove("Singapore") and x.remove(1)