Quiz
chapter 11 quiz
Create a free account to finish this quiz
Save your progress and see your final score.
Already have an account? Sign in
Questions in this quiz (106)
1Suppose an ArrayList list contains {"red", "green", "red", "green"}. What is the list after the following code?

- A{"red", "green", "red", "green"}
- B{"green", "red", "green"}
- C{"green", "green"}
- D{"red", "green", "green"}
2Which of the following statements is false?
- AA public class can be accessed by a class from a different package.
- BA private method cannot be accessed by a class in a different package.
- CA protected method can be accessed by a subclass in a different package.
- DA method with no visibility modifier can be accessed by a class in a different package.
3Invoking ________ removes all elements in an ArrayList x.
- Ax.remove()
- Bx.clean()
- Cx.delete()
- Dx.empty()
- Ex.clear()
4What is the output of the following code?

- Afalse false
- Btrue true
- Cfalse true
- Dtrue false
5The UML uses ________ before a member name to indicate that the member is private.
- A+
- B-
- C#
- D?
6Analyze the following code.

- AProgram 1 displays true and Program 2 displays true
- BProgram 1 displays false and Program 2 displays true
- CProgram 1 displays true and Program 2 displays false
- DProgram 1 displays false and Program 2 displays false
7You can create an ArrayList using ________.
- Anew ArrayList[]
- Bnew ArrayList[100]
- Cnew ArrayList()
- DArrayList()
8You 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.
- ATrue
- BFalse
9The 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, Dallas]
- C[New York, Dallas]
- D[New York, Atlanta]
10________ can search in an unsorted list.
- ALinear search
- BBinary search
11Which of the following is incorrect?
- AA constructor may be static.
- BA constructor may be private.
- CA constructor may invoke a static method.
- DA constructor may invoke an overloaded constructor.
- EA constructor invokes its superclass no-arg constructor by default if a constructor does not invoke an overloaded constructor or its superclass's constructor.
12Analyze the following code.

- AProgram 1 displays true and Program 2 displays true
- BProgram 1 displays false and Program 2 displays true
- CProgram 1 displays true and Program 2 displays false
- DProgram 1 displays false and Program 2 displays false
13Which of the following statements are true?
- ATo override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass.
- BOverloading a method is to provide more than one method with the same name but with different signatures to distinguish them.
- CIt is a compilation error if two methods differ only in return type in the same class.
- DA private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.
- EA 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.
14Which of the following statements are true?
- AOverride the methods equals and toString defined in the Object class whenever possible.
- BOverride the hashCode method whenever the equals method is overridden. By contract, two equal objects must have the same hash code.
- CA public default no-arg constructor is assumed if no constructors are defined explicitly.
- DYou should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.
15Given 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();- Ac1 instanceof C1
- Bc2 instanceof C1
- Cc3 instanceof C1
- Dc4 instanceof C2
16You can assign ________ to a variable of Object[] type.
- Anew char[100]
- Bnew int[100]
- Cnew double[100]
- Dnew String[100]
- Enew java.util.Date[100]
17In OOP, a reference variable can reference a subtype object. This is called ________.
- Aencapsulation
- Binheritance
- Cpolymorphism
- Dabstraction
18A final class can have instances.
- ATrue
- BFalse
19Analyze the following code.

- AProgram 1 displays true and Program 2 displays true
- BProgram 1 displays false and Program 2 displays true
- CProgram 1 displays true and Program 2 displays false
- DProgram 1 displays false and Program 2 displays false
20Analyze the following code.

- AProgram 1 displays true and Program 2 displays true
- BProgram 1 displays false and Program 2 displays true
- CProgram 1 displays true and Program 2 displays false
- DProgram 1 displays false and Program 2 displays false
21Analyze the following code:

- AThe program has a compilation error, because m is overridden with a different signature in B.
- BThe program has a compilation error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.
- CThe program has a runtime error on b.i, because i is not accessible from b.
- DThe method m is not overridden in b. B inherits the method m from A and defines an overloaded method m in b.
22Analyze the following code:

- AThe code is correct and displays [1, 2, 3].
- BThe code is correct and displays [1.0, 2.0, 3.0].
- CThe 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.
- DThe code has a compile error because asList(array) requires that the array elements are objects.
23What is the output of the following code?

- Afalse false
- Btrue true
- Cfalse true
- Dtrue false
24Which of the statements regarding the super keyword is incorrect?
- AYou can use super to invoke a super class constructor.
- BYou can use super to invoke a super class method.
- CYou can use super.super.p to invoke a method in superclass's parent class.
- DYou cannot invoke a method in superclass's parent class.
25A 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?
- AThe variable should be marked public.
- BThe variable should be marked private.
- CThe variable should be marked protected.
- DThe variable should have no special access modifier.
- EThe variable should be marked private and an accessor method provided.
26If a method is declared public in the superclass, you may declare the method private in the subclass.
- ATrue
- BFalse
27Analyze the following code:

- AThe program does not compile because Test does not have a default constructor Test().
- BThe 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.
- CThe program would compile if a default constructor A(){ } is added to class A explicitly.
- DThe program compiles, but it has a runtime error due to the conflict on the method name print.
28Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors?
- Ax.get(1)
- Bx.set(2, "New York");
- Cx.get(2)
- Dx.remove(2)
- Ex.size()
29Consider the following declaration for a class A.

- AI
- BII
- CIII
- DIV
30The visibility of these modifiers increases in this order ________.
- Aprivate, protected, none (if no modifier is used), and public
- Bprivate, none (if no modifier is used), protected, and public
- Cnone (if no modifier is used), private, protected, and public
- Dnone (if no modifier is used), protected, private, and public
31Show the output of the following code:

- A["red", "green", "blue", "red"]
- B["red", "green", "blue"]
- C["red", "red", "green", "blue"]
- D["red", "green", "red", "blue"]
32What is the output of the following code?

- Afalse true
- Btrue false
- Ctrue true
- Dfalse false
33Analyze the following code:

- AThe constructor of class A is not called.
- BThe constructor of class A is called and it displays "i from A is 7".
- CThe constructor of class A is called and it displays "i from A is 40".
- DThe constructor of class A is called and it displays "i from A is 60".
34The UML uses ________ before a member name to indicate that the member is protected.
- A+
- B-
- C#
- D?
35Analyze the following code:

- AThe constructor of class A is not called.
- BThe constructor of class A is called and it displays "i from A is 7".
- CThe constructor of class A is called and it displays "i from A is 40".
- DThe constructor of class A is called and it displays "i from A is 60".
36If a data field is declared in the superclass, you may hide it by redeclaring it in the subclass.
- ATrue
- BFalse
37The size of ________ can grow and shrink at runtime.
- Aan array
- Ban ArrayList
38Analyze the following code:

- AThe code is correct and displays 3.
- BThe code is correct and displays 3.0.
- CThe code has a compile error on Collections.max(c). c cannot be an array.
- DThe code has a compile error on Integer[] c = {1, 2, 3}.
39A final class can be extended.
- ATrue
- BFalse
40Analyze the following code.

- AProgram 1 displays true and Program 2 displays true
- BProgram 1 displays false and Program 2 displays true
- CProgram 1 displays true and Program 2 displays false
- DProgram 1 displays false and Program 2 displays false
41What is the output of the following code?

- APerson Person
- BPerson Student
- CStduent Student
- DStudent Person
42The UML uses ________ before a member name to indicate that the member is protected.
- A+
- B-
- C#
- D?
43If a method is declared private in the superclass, you may declare the method protected in the subclass.
- ATrue
- BFalse
44A final class can have instances.
- ATrue
- BFalse
45A final class can be extended.
- ATrue
- BFalse
46Analyze the following code:

- AThe program has a compilation error, because m is overridden with a different signature in B.
- BThe program has a compilation error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.
- CThe program has a runtime error on b.i, because i is not accessible from b.
- DThe method m is not overridden in b. B inherits the method m from A and defines an overloaded method m in b.
47The UML uses ________ before a member name to indicate that the member is public.
- A+
- B-
- C#
- D?
48Which of the following classes cannot be extended?
- Aclass A { }
- Bclass A { private A();}
- Cfinal class A { }
- Dclass A { protected A();}
49If a method is declared protected in the superclass, you may declare the method private in the subclass.
- ATrue
- BFalse
50Analyze the following code.

- AProgram 1 displays true and Program 2 displays true
- BProgram 1 displays false and Program 2 displays true
- CProgram 1 displays true and Program 2 displays false
- DProgram 1 displays false and Program 2 displays false
51Superclasses contain more features than their subclasses.
- ATrue
- BFalse
52If a method is declared protected in the superclass, you may declare the method public in the subclass.
- ATrue
- BFalse
53Analyze the following code:

- AWhen assigning s to o in Object o = s, a new object is created.
- BWhen casting o to s in String d = (String)o, a new object is created.
- CWhen casting o to s in String d = (String)o, the contents of o is changed.
- Ds, o, and d reference the same String object.
54Analyze the following code.

- AProgram 1 displays true and Program 2 displays true
- BProgram 1 displays false and Program 2 displays true
- CProgram 1 displays true and Program 2 displays false
- DProgram 1 displays false and Program 2 displays false
55Consider the following declaration for a class A.

- AI
- BII
- CIII
- DIV
56You can always successfully cast a subclass to a superclass.
- ATrue
- BFalse
57In OOP, you declare a class that extends another class. This is called ________.
- Aencapsulation
- Binheritance
- Cpolymorphism
- Dabstraction
58You can always successfully cast a superclass to a subclass.
- ATrue
- BFalse
59Which of the following statements are true?
- AA method can be overloaded in the same class.
- BA method can be overridden in the same class.
- CIf a method overloads another method, these two methods must have the same signature.
- DIf a method overrides another method, these two methods must have the same signature.
- EA method in a subclass can overload a method in the superclass.
60Analyze the following code.

- AProgram 1 displays true and Program 2 displays true
- BProgram 1 displays false and Program 2 displays true
- CProgram 1 displays true and Program 2 displays false
- DProgram 1 displays false and Program 2 displays false
61Consider the following declaration for a class A.

- AI
- BII
- CIII
- DIV
62Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]?
- Ax.add("Chicago")
- Bx.add(0, "Chicago")
- Cx.add(1, "Chicago")
- Dx.add(2, "Chicago")
63A 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?
- AThe variable should be marked public.
- BThe variable should be marked private.
- CThe variable should be marked protected.
- DThe variable should have no special access modifier.
- EThe variable should be marked private and an accessor method provided.
64Given 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?- Ac1 instanceof C1
- Bc2 instanceof C1
- Cc3 instanceof C1
- Dc4 instanceof C2
65Which of the following methods override the equals method in the Object class?
- Apublic void equals(Object o)
- Bpublic boolean equals(Object o)
- Cpublic boolean equals(SomeType o)
- Dpublic static boolean equals(Object o)
66Which of the following is incorrect?
- AA constructor may be static.
- BA constructor may be private.
- CA constructor may invoke a static method.
- DA constructor may invoke an overloaded constructor.
- EA constructor invokes its superclass no-arg constructor by default if a constructor does not invoke an overloaded constructor or its superclass's constructor.
67Superclasses contain more features than their subclasses.
- ATrue
- BFalse
68The getValue() method is overridden in two ways. Which one is correct?

- AI
- BII
- CBoth I and II
- DNeither
69The visibility of these modifiers increases in this order ________.
- Aprivate, protected, none (if no modifier is used), and public
- Bprivate, none (if no modifier is used), protected, and public
- Cnone (if no modifier is used), private, protected, and public
- Dnone (if no modifier is used), protected, private, and public
70Analyze the following code:

- AThe code is correct and displays 3.
- BThe code is correct and displays 3.0.
- CThe code has a compile error on Collections.max(c). c cannot be an array.
- DThe code has a compile error on Integer[] c = {1, 2, 3}.
71The UML uses ________ before a member name to indicate that the member is protected.
- A#
- B+
- C-
- D?
72You can always successfully cast a subclass to a superclass.
- ATrue
- BFalse
73Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ________.
- ATrue
- BFalse
74Object-oriented programming allows you to derive new classes from existing classes. This is called ________.
- Aencapsulation
- Binheritance
- Cabstraction
- Dgeneralization
75Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code?

- A{"red", "red", "green"}
- B{"red", "green"}
- C{"green"}
- D{}
76Normally you depend on the JVM to perform garbage collection automatically. However, you can explicitly use ________ to request garbage collection.
- ASystem.gc()
- BSystem.exit()
- CSystem.exit(0)
- DSystem.gc(0)
77Analyze the following code:

- AThe code is correct and displays [3, 5].
- BThe code is correct and displays [5, 3].
- CThe code has a compile error on Collections.shuffle(c). c cannot be an array.
- DThe code has a compile error on Integer[] c = {3, 5}.
78Which statements are most accurate regarding the following classes?

- AAn object of B contains data fields i, j, k, m.
- BAn object of B contains data fields j, k, m.
- CAn object of B contains data fields j, m.
- DAn object of B contains data fields k, m.
79Which of the following are Java keywords?
- AinstanceOf
- Binstanceof
- Ccast
- Dcasting
80The UML uses ________ before a member name to indicate that the member is package-private.
- A+
- B-
- C#
- Dnone (blank)
81Which statements are most accurate regarding the following classes?

- AAn object of B contains data fields i, j, k, m.
- BAn object of B contains data fields j, k, m.
- CAn object of B contains data fields j, m.
- DAn object of B contains data fields k, m.
82Analyze the following code:

- AThe code is correct and displays [1, 2, 3].
- BThe code is correct and displays [1.0, 2.0, 3.0].
- CThe 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.
- DThe code has a compile error because asList(array) requires that the array elements are objects.
83Object-oriented programming allows you to derive new classes from existing classes. This is called ________.
- Aencapsulation
- Binheritance
- Cabstraction
- Dgeneralization
84Which statements are most accurate regarding the following classes?

- AIn the class B, an instance method can only access i, j, k, m.
- BIn the class B, an instance method can only access j, k, m.
- CIn the class B, an instance method can only access j, m.
- DIn the class B, an instance method can only access k, m.
85The instanceof operator is used to determine whether an object is an instance of a certain class.
- ATrue
- BFalse
86Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code?

- A{"red", "red", "green"}
- B{"red", "green"}
- C{"green"}
- D{}
87You can create an ArrayList using ________.
- Anew ArrayList[]
- Bnew ArrayList[100]
- Cnew ArrayList()
- DArrayList()
88Analyze the following code.

- AProgram 1 displays true and Program 2 displays true
- BProgram 1 displays false and Program 2 displays true
- CProgram 1 displays true and Program 2 displays false
- DProgram 1 displays false and Program 2 displays false
89Analyze the following code:

- AThe program has a compilation error, because m is overridden with a different signature in B.
- BThe program has a compilation error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.
- CThe program has a runtime error on b.i, because i is not accessible from b.
- DThe method m is not overridden in b. B inherits the method m from A and defines an overloaded method m in b.
90If a method is declared protected in the superclass, you may declare the method private in the subclass.
- ATrue
- BFalse
91If 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.
- ATrue
- BFalse
92What is the output of the following code?

- APerson Person
- BPerson Student
- CStduent Student
- DStudent Person
93Show the output of the following code:

- A["red", "green", "blue", "red"]
- B["red", "green", "blue"]
- C["red", "red", "green", "blue"]
- D["red", "green", "red", "blue"]
94A final class can have instances.
- ATrue
- BFalse
95The getValue() method is overridden in two ways. Which one is correct?

- AI
- BII
- CBoth I and II
- DNeither
96Which of the statements regarding the super keyword is incorrect?
- AYou can use super to invoke a super class constructor.
- BYou can use super to invoke a super class method.
- CYou can use super.super.p to invoke a method in superclass's parent class.
- DYou cannot invoke a method in superclass's parent class.
97The equals method is defined in the Object class. Which of the following is correct to override it in the String class?
- Apublic boolean equals(String other)
- Bpublic boolean equals(Object other)
- Cpublic static boolean equals(String other)
- Dpublic static boolean equals(Object other)
98Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code?

- A{"red", "red", "green"}
- B{"red", "green"}
- C{"green"}
- D{}
99A final class can be extended.
- ATrue
- BFalse
100Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code?

- A{"red", "red", "green"}
- B{"red", "green"}
- C{"green"}
- D{}
101Analyze the following code:
ArrayList<String> list = new ArrayList<String>();
list.add("Beijing");
list.add("Tokyo");
list.add("Shanghai");
list.set(3, "Hong Kong");- AThe last line in the code causes a runtime error because there is no element at index 3 in the array list.
- BThe last line in the code has a compile error because there is no element at index 3 in the array list.
- CIf you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine.
- DIf you replace the last line by list.add(4, "Hong Kong"), the code will compile and run fine.
102Given the following code, find the compile error.

- Am(new GraduateStudent()) causes an error
- Bm(new Student()) causes an error
- Cm(new Person()) causes an error
- Dm(new Object()) causes an error
103Analyze the following code:

- AThe code is correct and displays [1, 2, 3].
- BThe code is correct and displays [1.0, 2.0, 3.0].
- CThe 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.
- DThe code has a compile error because asList(array) requires that the array elements are objects.
104Given 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);- Ac3 is cast into c2 successfully.
- BYou will get a runtime error because you cannot cast objects from sibling classes.
- CYou will get a runtime error because the Java runtime system cannot perform multiple casting in nested form.
- DThe statement is correct.
105You can always successfully cast a superclass to a subclass.
- ATrue
- BFalse
106What 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?
- Apublic
- Bprivate
- Cprotected
- DUse the default modifier.
Information
- Description
- Quiz on chapter 11 quiz, created by Joe Delisle on 10/02/2018.
- Author
- Joe Delisle
- Language
- English
- Views
- 2
- Rating
- No ratings yet Rate this quiz