ch 11 CIS 35a

Ace your homework & exams now with Quizwiz!

The instanceof operator is used to determine whether an object is an instance of a certain class. a. true b. false

a. true

The set of all instances of a subclass is a subset of the instances of its superclass. a. true b. false

a. true

You can always successfully cast a subclass to a superclass. a. true b. false

a. true

The visibility of these modifiers increases in this order: a. private, protected, none (if no modifier is used), and public. b. private, none (if no modifier is used), protected, and public. c. none (if no modifier is used), private, protected, and public. d. none (if no modifier is used), protected, private, and public.

private, none (if no modifier is used), protected, and public.

The UML uses _______ before a member name to indicate that the member is public. a. + b. - c. # d. ?

a. +

Which of the following is incorrect? a. A constructor may be static. b. A constructor may be private. c. A constructor may invoke a static method. d. A constructor may invoke an overloaded constructor. e. A constructor invokes its superclass no-arg constructor by default if a constructor does not invoke an overloaded constructor or its superclass's constructor.

a. A constructor may be static.

Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; } a. An object of B contains data fields i, j, k, m. b. An object of B contains data fields j, k, m. c. An object of B contains data fields j, m. d. An object of B contains data fields k, m.

a. An object of B contains data fields i, j, k, m.

____________ can search in an unsorted list. a. Linear search b. Binary search

a. Linear search

If a method is declared protected in the superclass, you may declare the method public in the subclass. a. true b. false

a. true

If a parameter is of the java.lang.Object type, you can pass any object to it. This is known as generic programming. a. true b. false

a. true

A final class can have instances. a. true b. false

a. true

A final class can be extended a. true b. false

b. false

The UML uses _______ before a member name to indicate that the member is protected. a. + b. - c. # d. ?

c. #

The class Date, Calendar, and ArrayList are in the ________ package. a. java.lang b. javax.swing c. java.util d. java.io

c. java.util

Given the following code, find the compile error? 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"; } } a. m(new GraduateStudent()) causes an error b. m(new Student()) causes an error c. m(new Person()) causes an error d. m(new Object()) causes an error

c. m(new Person()) causes an error d. m(new Object()) causes an error

Given two reference variables t1 and t2, if t1. equals(t2) is true, t1 == t2________. a. is always false b. is always true c. may be true or false

c. may be true or false

You can create an ArrayList using _________. a. new ArrayList[] b. new ArrayList[100] c. new ArrayList() d. ArrayList()

c. new ArrayList()

You can assign _________ to a variable of Object[] type. a. new char[100] b. new int[100] c. new double[100] d. new String[100] e. new java.util.Date[100]

d. new String[100] e. new java.util.Date[100]

The UML uses _______ before a member name to indicate that the member is package - private a. + b. - c. # d. ?

none?

Which of the following statements are true? a. Override the methods equals and toString defined in the Object class whenever possible. b. Override the hashCode method whenever the equals method is overridden. By contract, two equal objects must have the same hash code. c. A public default no-arg constructor is assumed if no constructors are defined explicitly. d. You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.

a. Override the toString method defined in the Object class whenever possible. b. Override the equals method defined in the Object class whenever possible. c. A public default no-arg constructor is assumed if no constructors are defined explicitly. d. You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.

In OOP, you declare a data fields to be private. This is called _____. a. encapsulation b. inheritance c. polymorphism d. abstraction

a. encapsulation

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))); } } a. false false b. true true c. false true d. true false

a. false false

Encapsulation means ______________. a. that data fields should be declared private. b. that a class can extend another class. c. that a variable of supertype can refer to a subtype object. d. that a class can contain another class.

a. that data fields should be declared private.

15. You can use the operator == to check whether two variables refer to the same object, and use the equals() method to check the equality of contents of the two objects. a. true b. false

a. true

A instance of a subclass is also an instance of its superclass. a. true b. false

a. true

An interface can be a separate unit and can be compiled into a bytecode file. a. true b. false

a. true

Every class has a toString() method and an equals() method. a. true b. false

a. true

Every object is an instance of the Object class. a. true b. false

a. true

If a data field is declared in the superclass, you may hide it by redeclaring it in the subclass. a. true b. false

a. true

If a method is declared private in the superclass, you may declare the method protected in the subclass. a. true b. false

a. true

If a method is declared private in the superclass, you may declare the method public in the subclass. a. true b. false

a. true

If an object is an instance of class A, and class A is a subclass of class B, then the object is also an instance of class B. a. true b. false

a. true

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing]? a. x.remove("Singapore") b. x.remove(0) c. x.remove(1) d. x.remove(2)

a. x.remove("Singapore") c. x.remove(1)

In OOP, you declare a class that extends another class. This is called _____. a. encapsulation b. inheritance c. polymorphism d. abstraction

b. inheritance

Object-oriented programming allows you to derive new classes from existing classes. This is called ____________. a. encapsulation b. inheritance c. abstraction d. generalization

b. inheritance

Which of the following are Java keywords? a. instanceOf b. instanceof c. cast d. casting

b. instanceof

You use the keyword ____________ to reference a method in the superclass from a subclass. a. this b. that c. super d. superObject

c. super

Polymorphism means ______________. a. that data fields should be declared private. b. that a class can extend another class. c. that a variable of supertype can refer to a subtype object. d. that a class can contain another class.

c. that a variable of supertype can refer to a subtype object.

Invoking _________ returns the number of the elements in an ArrayList x. a. x.getSize() b. x.getLength(0) c. x.length(1) d. x.size()

d. x.size()

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()); } } a. Person Person b. Person Student c. Stduent Student d. Student Person

a. 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(((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; } } a. Program 1 displays true and Program 2 displays true b. Program 1 displays false and Program 2 displays true c. Program 1 displays true and Program 2 displays false d. Program 1 displays false and Program 2 displays false

a. Program 1 displays true and Program 2 displays true

Normally you depend on the JVM to perform garbage collection automatically. However, you can explicitly use ____ to request garbage collection. a. System.gc() b. System.exit() c. System.exit(0) d. System.gc(0)

a. System.gc()

Analyze the following code: Circle c = new Circle (5); Cylinder c = cy; a. The code has a compile error. b. The code has a runtime error. c. The code is fine.

a. The code has a compile error.

Analyze the following code: ArrayList<String> list = new ArrayList<String>(); list.add("Beijing"); list.add("Tokyo"); list.add("Shanghai"); list.set(3, "Hong Kong"); a. The last line in the code causes a runtime error because there is no element at index 3 in the array list. b. The last line in the code has a compile error because there is no element at index 3 in the array list. c. If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine. d. If you replace the last line by list.add(4, "Hong Kong"), the code will compile and run fine.

a. The last line in the code causes a runtime error because there is no element at index 3 in the array list. c. If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine.

Which of the following statements are true? a. To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. b. Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. 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. 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.

a. To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. b. Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. 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. 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.

The UML uses _______ before a member name to indicate that the member is private. a. + b. - c. # d. ?

b. -

Which of the following statements are true? a. A method can be overloaded in the same class. b. A method can be overridden in the same class. c. If a method overloads another method, these two methods must have the same signature. d. If a method overrides another method, these two methods must have the same signature. e. A method in a subclass can overload a method in the superclass.

b. A method can be overridden in the same class. d. If a method overrides another method, these two methods must have the same signature. e. A method in a subclass can overload a method in the superclass.

Which of the following statements are true? a. A subclass is a subset of a superclass. b. A subclass is usually extended to contain more functions and more detailed information than its superclass. c. "class A extends B" means A is a subclass of B. d. "class A extends B" means B is a subclass of A.

b. A subclass is usually extended to contain more functions and more detailed information than its superclass. c. "class A extends B" means A is a subclass of B.

The getValue() method is overridden in two ways. Which one is correct? I: public class Test { public static void main(String[] args) { A a = new A(); System.out.println(a.getValue()); } } class B { public String getValue() { return "Any object"; } } class A extends B { public Object getValue() { return "A string"; } } II: public class Test { public static void main(String[] args) { A a = new A(); System.out.println(a.getValue()); } } class B { public Object getValue() { return "Any object"; } } class A extends B { public String getValue() { return "A string"; } } a. I b. II c. Both I and II d. Neither

b. II

Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; // some methods omitted } a. In the class B, an instance method can only access i, j, k, m. b. In the class B, an instance method can only access j, k, m. c. In the class B, an instance method can only access j, m. d. In the class B, an instance method can only access k, m.

b. In the class B, an instance method can only access j, k, m.

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 { public String getInfo() { return "Student"; } } class Person { public String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } } a. Person Person b. Person Student c. Stduent Student d. Student Person

b. Person Student

Analyze the following code. // Program 1: public class Test { public static void main(String[] args) { Circle circle1 = new Circle(); Circle circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } class Circle { double radius; public boolean equals(Circle circle) { return this.radius == circle.radius; } } // Program 2: public class Test { public static void main(String[] args) { Circle circle1 = new Circle(); Circle circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } class Circle { double radius; public boolean equals(Object circle) { return this.radius == ((Circle)circle).radius; } } a. Program 1 displays true and Program 2 displays true b. Program 1 displays false and Program 2 displays true c. Program 1 displays true and Program 2 displays false d. Program 1 displays false and Program 2 displays false

b. Program 1 displays false and Program 2 displays true

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(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; } } a. Program 1 displays true and Program 2 displays true b. Program 1 displays false and Program 2 displays true c. Program 1 displays true and Program 2 displays false d. Program 1 displays false and Program 2 displays false

b. Program 1 displays false and Program 2 displays true

Analyze the following code. // Program 1: public class Test { public static void main(String[] args) { Object circle1 = new Circle(); Object circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } class Circle { double radius; public boolean equals(Circle circle) { return this.radius == circle.radius; } } // Program 2: public class Test { public static void main(String[] args) { Object circle1 = new Circle(); Object circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } class Circle { double radius; public boolean equals(Object circle} return this.radius == ((Circle)circle).radius; } } a. Program 1 displays true and Program 2 displays true b. Program 1 displays false and Program 2 displays true c. Program 1 displays true and Program 2 displays false d. Program 1 displays false and Program 2 displays false

b. Program 1 displays false 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; } } a. The constructor of class A is not called. b. The constructor of class A is called and it displays "i from A is 7". c. The constructor of class A is called and it displays "i from A is 40". d. The constructor of class A is called and it displays "i from A is 60".

b. The constructor of class A is called and it displays "i from A is 7".

Analyze the following code: public class A extends B { } class B { public B(Strign s) { } } a. The program has a compilation error because A does not have a default constructor. b. The program has a compilation error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor of constructor. c. The program would compile fine if you add the following constructor into A: A(String s){} d. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

b. The program has a compilation error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor of constructor. d. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

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); } } a. The program compiles fine, but you cannot create an instance of Square because the constructor does not specify the length of the Square. b. The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally. c. The program compiles fine, but it has a runtime error because of invoking the Square class's constructor illegally.

b. 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); } } a. The program does not compile because Test does not have a default constructor Test(). b. 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. c. The program would compile if a default constructor A(){ } is added to class A explicitly. d. The program compiles, but it has a runtime error due to the conflict on the method name print.

b. 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. c. The program would compile if a default constructor A(){ } is added to class A explicitly.

Given the following classes and their objects: class C1 {}; class C2 extends C1 {}; class C3 extends C1 {}; C2 c2 = new C2(); C3 c3 = new C3(); Analyze the following statement: c2 = (C2)((C1)c3); a. c3 is cast into c2 successfully. b. You will get a runtime error because you cannot cast objects from sibling classes. c. You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form. d. The statement is correct.

b. You will get a runtime error because you cannot cast objects from sibling classes.

The size of ____________ can grow and shrink at runtime. a. an array b. an ArrayList

b. an ArrayList

If a method is declared protected in the superclass, you may declare the method private in the subclass. a. true b. false

b. false

If a method is declared public in the superclass, you may declare the method private in the subclass. a. true b. false

b. false

Superclasses contain more features than their subclasses. a. true b. false

b. false

The order in which modifiers appear before a class or a method is important. a. true b. false

b. false

You can always successfully cast a superclass to a subclass. a. true b. false

b. false

What is output of the following code: ArrayList<String> list = new ArrayList<String>(); 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))); a. true false b. false true c. true true d. false false

b. false true

Which of the following methods override the equals method in the Object class? a. public void equals(Object o) b. public boolean equals(Object o) c. public boolean equals(SomeType o) d. public static boolean equals(Object o)

b. public boolean equals(Object o)

The equals method is defined in the Object class. Which of the following is correct to override it in the String class? a. public boolean equals(String other) b. public boolean equals(Object other) c. public static boolean equals(String other) d. public static boolean equals(Object other)

b. public boolean equals(Object other)

Inheritance means ______________. a. that data fields should be declared private. b. that a class can extend another class. c. that a variable of supertype can refer to a subtype object. d. that a class can contain another class.

b. that a class can extend another class.

Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be___________________. a. true b. false

b. true

Invoking _________ returns the first element in an ArrayList x. a. x.first() b. x.get(0) c. x.get(1) d. x.get()

b. x.get(0)

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors? a. x.get(1) b. x.set(2, "New York"); c. x.get(2) d. x.remove(2) e. x.size()

b. x.set(2, "New York"); c. x.get(2) d. x.remove(2)

Suppose an ArrayList list contains {"red", "green", "red", "green"}.What is list after the following code? list.remove("red"); a. {"red", "green", "red", "green"} b. {"green", "red", "green"} c. {"green", "green"} d. {"red", "green", "green"}

b. {"green", "red", "green"}

Suppose an ArrayList list contains {"red", "red", "green"}. What is ist after the following code? String element = "red"; for (int i = 0; i < list.size(); i++) if (list.get(i).equals(element)) list.remove(element); a. {"red", "red", "green"} b. {"red", "green"} c. {"green"} d. {}

b. {"red", "green"}

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(); } } a. Nothing displayed b. "The default constructor of B is invoked" c. "The default constructor of A is invoked""The default constructor of B is invoked" d. "The default constructor of B is invoked""The default constructor of A is invoked" e. "The default constructor of A is invoked"

c. "The default constructor of A is invoked""The default constructor of B is invoked"

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 { reference variables t1 and t2, if t1 == t2 is true, must be ___________. reference variables t1 and t2, if t1.equals(t2) is true, t1 int x; public boolean equals(A a) { return this.x == a.x; } } a. Program 1 displays true and Program 2 displays true b. Program 1 displays false and Program 2 displays true c. Program 1 displays true and Program 2 displays false d. Program 1 displays false and Program 2 displays false

c. Program 1 displays true and Program 2 displays false

Analyze the following code: Double[] array = {1, 2, 3}; ArrayList<Double> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list); a. The code is correct and displays [1, 2, 3]. b. The code is correct and displays [1.0, 2.0, 3.0]. c. The code has a compile error because an integer such as 1 is automatically converted into an Integer object, but the array element type is Double. d. The code has a compile error because asList(array) requires that the array elements are objects.

c. The code has a compile error because an integer such as 1 is automatically converted into an Integer object, but the array element type is Double.

Analyze the following code: double[] c = {1, 2, 3}; System.out.println(java.util.Collections.max(c)); a. The code is correct and displays 3. b. The code is correct and displays 3.0. c. The code has a compile error on Collections.max(c) . c cannot be an array d. The code has a compile error on Integer[] c = {1, 2, 3].

c. The code has a compile error on Collections.max(c) . c cannot be an array

Analyze the following code: Integer[] c = {3, 5}; java.util.Collections.shuffle(c); System.out.println(java.util.Arrays.toString(c)); a. The code is correct and displays [3, 5]. b. The code is correct and displays [5, 3]. c. The code has a compile error on Collections.shuffle(c). c cannot be an array. d. The code has a compile error on Integer[] c = {3, 5}.

c. The code has a compile error on Collections.shuffle(c). c cannot be an array.

Analyze the following code: Cylinder cy = new Cylinder(1, 1); Circle c = cy; a. The code has a compile error. b. The code has a runtime error. c. The code is fine.

c. 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? a. The variable should be marked public. b. The variable should be marked private. c. The variable should be marked protected. d. The variable should have no special access modifier e. The variable should be marked private and an accessor method provided.

c. The variable should be marked protected.

Which of the following methods override the toString method in the Object class? a. public void toString(String s) b. public static String toString() c. public String toString() d. public String toString(String s)

c. public String toString()

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; } } a. The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString()); b. When executing System.out.println(a1), the toString() method in the Object class is invoked. c. When executing System.out.println(a2), the toString() method in the Object class is invoked. d. When executing System.out.println(a1), the toString() method in the A class is invoked.

c. When executing System.out.println(a2), the toString() method in the Object class is invoked. d. When executing System.out.println(a1), the toString() method in the A class is invoked.

Which of the statements regarding the super keyword is incorrect? a. You can use super to invoke a super class constructor. b. You can use super to invoke a super class method. c. You can use super.super.p to invoke a method in superclass's parent class. d. You cannot invoke a method in superclass's parent class.

c. You can use super.super.p to invoke a method in superclass's parent class.

Show the output of the following code: String[] array = {"red", "green", "blue"}; ArrayList<String> list = new ArrayList<>(Arrays.asList(array)); list.add(0, "red"); System.out.println(list); a. ["red", "green", "blue", "red"] b. ["red", "green", "blue"] c. ["red", "red", "green", "blue"] d. ["red", "green", "red", "blue"]

c. ["red", "red", "green", "blue"]

The printout from the following code is __________. java.util.ArrayList<String> list = new java.util.ArrayList<String>(); list.add("New York"); java.util.ArrayList<String> list1 = list; list.add("Atlanta"); list1.add("Dallas"); System.out.println(list1); a. [New York] b. [New York, Atlanta] c. [New York, Atlanta, Dallas] d. [New York, Dallas]

c. [New York, Atlanta, Dallas]

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))); } } a. false false b. true true c. false true d. true false

c. false true

Which of the following classes cannot be extended? a. class A { } b. class A {&nbsp;&nbsp; private A();} c. final class A { } d. class A {&nbsp;&nbsp; protected A();}

c. final class A { }

In OOP, a reference variable can reference a subtype object. This is called _____. a. encapsulation b. inheritance c. polymorphism d. abstraction

c. polymorphism

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? a. public b. private c. protected d. Use the default modifier.

c. protected

What is output of the following code: ArrayList<java.util.Date> list = new ArrayList<java.util.Date>(); java.util.Date d = new java.util.Date(); list.add(d); list.add(d); System.out.println((list.get(0) == list.get(1)) + " " +(list.get(0)).equals(list.get(1))); a. true false b. false true c. true true d. false false

c. true true

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]? a. x.add("Chicago") b. x.add(0, "Chicago") c. x.add(1, "Chicago") d. x.add(2, "Chicago")

c. x.add(1, "Chicago")

Suppose an ArrayList list contains {"red", "red", "green"}. What is list after the following code? String element = "red"; for (int i = list.size() - 1; i >= 0; i--) if (list.get(i).equals(element)) list.remove(element); a. {"red", "red", "green"} b. {"red", "green"} c. {"green"} d. {}

c. {"green"}

Suppose an ArrayList list contains {"red", "red", "green"}. What is list after the following code? String element = "red"; for (int i = 0; i < list.size(); i++) if (list.get(i).equals(element)) { list.remove(element); i--; } a. {"red", "red", "green"} b. {"red", "green"} c. {"green"} d. {}

c. {"green"}

Which of the following statements is false? a. A public class can be accessed by a class from a different package. b. A private method cannot be accessed by a class in a different package. c. A protected method can be accessed by a subclass in a different package. d. A method with no visibility modifier can be accessed by a class in a different package.

d. A method with no visibility modifier can be accessed by a class in a different package.

Which of the following statements are true? a. You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as polymorphism. b. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compilation time. c. A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime. d. Dynamic binding can apply to static methods. e. Dynamic binding can apply to instance methods.

d. Dynamic binding can apply to static methods.

Analyze the following code: double[] array = {1, 2, 3}; ArrayList<Double> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list); a. The code is correct and displays [1, 2, 3]. b. The code is correct and displays [1.0, 2.0, 3.0]. c. The code has a compile error because an integer such as 1 is automatically converted into an Integer object, but the array element type is Double. d. The code has a compile error because asList(array) requires that the array elements are objects.

d. The code has a compile error because asList(array) requires that the array elements are objects.

Analyze the following code: public class Test { public static void main(String[] args) { new B(); } } class A { int i = 7; public A() { setI(20); System.out.println("i from A is " + i); } public void setI(int i) { this.i = 2 * i; } } class B extends A { public B() { // System.out.println("i from B is " + i); } public void setI(int i) { this.i = 3 * i; } } a. The constructor of class A is not called. b. The constructor of class A is called and it displays "i from A is 7". c. The constructor of class A is called and it displays "i from A is 40". d. The constructor of class A is called and it displays "i from A is 60".

d. The constructor of class A is called and it displays "i from A is 60".

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) { } } a. The program has a compilation error, because m is overridden with a different signature in B. b. The program has a compilation error, because b.m(5) cannot be invoked since the method m(int) is hidden in B. c. The program has a runtime error on b.i, because i is not accessible from b. d. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

d. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it? a. public b. private c. protected d. Use the default modifier.

d. 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? a. c1 instanceof C1 b. c2 instanceof C1 c. c3 instanceof C1 d. c4 instanceof C2

d. c4 instanceof C2

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; } } a. When assigning s to o in Object o = s, a new object is created. b. When casting o to s in String d = (String)o, a new object is created. c. When casting o to s in String d = (String)o, the contents of o is changed. d. s, o, and d reference the same String object.

d. s, o, and d reference the same String object.

Composition means ______________. a. that data fields should be declared private. b. that a class extends another class. c. that a variable of super type refers to a subtype object. d. that a class contains a data field that references another object.

d. that a class contains a data field that references another object.

Invoking _________ removes all elements in an ArrayList x. a. x.remove() b. x.clean() c. x.delete() d. x.empty() e. x.clear()

e. x.clear()


Related study sets

PrepU Ch 40 Drug Therapy for Diarrhea

View Set

Microbiology Ch 3 & 4 Study Guide

View Set

Exam 2 Naming and Writing Formulas Need to know

View Set

MKT 605 Chapter 16 Video Exercise: Blu Dot

View Set