Intro. to Java Programming, Ninth Edition - Ch.11

Ace your homework & exams now with Quizwiz!

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.

Which of the following are Java keywords? A. instanceOf B. instanceof C. cast D. casting

B. instanceof

Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ___________. A. false B. true

B. true

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. //Explanation: See the section on the protected modifier.

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.

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.

The output 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 //Explanation: s1 == s2 is false, since s1 and s2 are two different objects. s1.equals(s2) is true since the equals method returns true if two strings have the same content.

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

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.

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 //Explanation: list.get(0) and list.get(1) point to the same object.

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 = 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"}

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"}

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 of the following statements are true? (choose more than one) 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.

A. A method can be overloaded 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 statements are most accurate regarding the following classes? class A { __private int i; __protected int j; __public int getI() { ____return i; } __public int getJ() { ____return j; } } class B extends A { __private int k; __protected int m; __public int getK() { ____return k; } __public int getM() { ____return 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.

Which of the following code is corrcet? (choose more than one) A. ArrayList<Integer> list = new ArrayList<>(); list.add(3.4); B. ArrayList<Double> list = new ArrayList<>(); list.add(3.4); C. ArrayList<Double> list = new ArrayList<>(); list.add(3); D. ArrayList<Integer> list = new ArrayList<>(); list.add(3);

B. ArrayList<Double> list = new ArrayList<>(); list.add(3.4); D. ArrayList<Integer> list = new ArrayList<>(); list.add(3);

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 //you can't override a string with an object in the first one class A extends B { __public Object getValue() { _____return "A string";

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".

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.

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.

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. //Explanation: Casting object reference variable does not affect the contents of the object.

You can create an ArrayList using _________. A. new ArrayList[] B. new ArrayList[100] C. new ArrayList() D. ArrayList()

C. new ArrayList()

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 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 //Explanation: list.get(0) and list.get(1) point to two different objects with the same string contents.

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.

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)

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 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. Student Student D. Student Person

A. Person Person

What is the output of the following code? public class Test1 { __public static void main(String[] args) { ____ChildClass c = new ChildClass(); ____c.print(); } } class ParentClass { __int id = 1; __void print() { ____System.out.println(id); } } class ChildClass extends ParentClass { __int id = 2; } A. 0 B. 1 C. 2 D. Nothing

B. 1

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. Student Student D. Student Person

B. Person Student

Analyze the following code: public class A extends B { } class B { __public B(String s) { } } (choose more than one) 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. 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. D. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

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 { __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 //Explanation: In Program 1, the equals method in the Object class is overridden. a1.equals(a2) invokes this method. It returns true. In Program 2, the equals method in the Object class is not overridden. a1.equals(a2) invokes the equals method defined in the Object class, which returns false in this case.

Which of the following statements are true? (choose more than one) 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.

Analyze the following code: ArrayList<String> list = new ArrayList<String>(); list.add("Beijing"); list.add("Tokyo"); list.add("Shanghai"); list.set(3, "Hong Kong"); (choose more than one) 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. //Explanation: There is no element at index 3.

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 //Explanation: In Program 1, the equals method in the Object class is invoked. In Program 2, the equals method in the class A is invoked. There are now two overloaded methods available in the class A. i.e. public boolean equals(Object a) and public boolean equals(A a). Which of the two is used by a1.equals(a2) is determined at compilation time. a1.equals(a2) in Program 1 matches the equals method defined in Object and a1.equals(a2) in Program 2 matches the equals method defined in the class A.

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.

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

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; } } (choose more than one) 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. //Explanation: Since a1 is an instance of A, the toString() method in the A class is invoked at runtime. //You can also pass an object to invoke System.out.println(object) or System.out.print(object). This is equivalent to invoking System.out.println(object.toString())

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.

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

You can assign _________ to a variable of Object[] type. (choose more than one) 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] //Explanation: Primitive data type array is not compatible with Object[].

Composition means ______________. A. that data fields should be declared private. B. that a class extends another class. C. that a variable of supertype 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.

Which of the following classes cannot be extended? A. class A { } B. class A { private A();} C. final class A { } D. class A { protected A();}

C. final class A { }

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"; } } (choose more than one) 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

Which of the following statements are true? (choose more than one) 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.

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. //a. Invoking toString() on an object returns a string that describes the object. By default, it returns a string consisting of a class name of which the object is an instance, an at sign (@), and the object's memory address in hexadecimal. Usually you should override the toString method so that it returns a descriptive string representation of the object. //b. The default implementation of the equals method in the Object class is: public boolean equals(Object obj) { _____return (this == obj); } This implementation checks whether two reference variables point to the same object using the == operator. You should override this method in your custom class to test whether two distinct objects have the same content.

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

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.

Which of the following statements are true? (choose more than one) 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.

Which of the following statements are true? (choose more than one) 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.

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. E. Dynamic binding can apply to instance methods.

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.

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing]? (choose more than one) A. x.remove("Singapore") B. x.remove(0) C. x.remove(1) D. x.remove(2)

A. x.remove("Singapore") C. x.remove(1)

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); } } (choose more than one) 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.

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

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)

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors? (choose more than one) 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) //Explanation: There is no element at index 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"} //Explanation: The remove(o) method removes teh first element o from the list.

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); A. {"red", "red", "green"} B. {"red", "green"} C. {"green"} D. {}

B. {"red", "green"} //When you remove an element from the list you move the numbers down. So red = 0, red = 1, and green = 2 with a size of 3. Then you remove the first red. So now red = 0 and green = 1 with a size of 2. Loop 1: i = 0("red"), size = 3, means "red" is removed Loop 2: i = 1 ("green"), size = 2, is now false and loop terminates Means this: if (list.get(i).equals(element)) is now false as it would mean list.get("green").equals("red").

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.

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".

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

Miscellaneous insurance policies

View Set

ATI Dermatological Practice Questions

View Set

Chapter 2 - What is Machine Learning?

View Set

Growth, development, & stages of life practice questions

View Set

Algebra 2: Equations Point Slope Part 2

View Set

Psych 100a: Quiz 9 - Paired-Samples t

View Set

Chapter 40: Management of Patients with Gastric and Duodenal Disorders

View Set