文档库 最新最全的文档下载
当前位置:文档库 › java第11章

java第11章

1. The class Date, Calendar, and ArrayList are in the ________ package.

a. https://www.wendangku.net/doc/6b12918122.html,ng
b. javax.swing
c. java.util
d. java.io

#
2. 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(2, "Chicago")
b. x.add("Chicago")
c. x.add(1, "Chicago")
d. x.add(0, "Chicago")

#
3. 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. {"green"}
b. {}
c. {"red", "green"}
d. {"red", "red", "green"}

#
4. If a data field is declared in the superclass, you may hide it by redeclaring it in the subclass.

a. true
b. false

#
5. Which of the following statements are true?

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

#
6. You use the keyword ____________ to reference a method in the superclass from a subclass.

a. superObject
b. super
c. that
d. this

#
7. Invoking _________ returns the number of the elements in an ArrayList x.

a. x.size()
b. x.getSize()
c. x.length(1)
d. x.getLength(0)

#
8. ____________ can search in an unsorted list.

a. Linear search
b. Binary search

#
9. You can always successfully cast a subclass to a superclass.

a. true
b. false

#
10. 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

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

#
12. Which of the following methods override the equals method in the Object class?


a. public boolean equals(SomeType o)
b. public static boolean equals(Object o)
c. public boolean equals(Object o)
d. public void equals(Object o)

#
13. Analyze the following code:

Circle c = new Circle (5);
Cylinder c = cy;

a. The code has a compile error.
b. The code is fine.
c. The code has a runtime error.

#
14. 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 false
b. Program 1 displays true and Program 2 displays true
c. Program 1 displays false and Program 2 displays true
d. Program 1 displays false and Program 2 displays false

#
15. Which of the following methods override the toString method in the Object class?

a. public static String toString()
b. public String toString()
c. public String toString(String s)
d. public void toString(String s)

#
16. 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. The statement is correct.
c. You will get a runtime error because you cannot cast objects from sibling classes.
d. You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form.

#
17. 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 j, k, m.
b. An object of B contains data fields i, j, k, m.
c. An object of B contains data fields k, m.
d. An object of B contains data fields j, m.

#
18. If a method is declared private in the superclass, you may declare the method public in the subclass.

a. true
b. false

#
19. Invoking _________ removes all elements in an ArrayList x.

a. x.remove()
b. x.clear()
c. x.clean()
d. x.delete()
e. x.empty()

#
20. 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 compiles, but it has a runtime error due to the conflict on the method name print.
d. The program would compile if a

default constructor A(){ } is added to class A explicitly.

#
21. A final class can be extended.

a. false
b. true

#
22. 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 Student
b. Stduent Student
c. Person Person
d. Student Person

#
23. The printout from the following code is __________.

java.util.ArrayList list = new java.util.ArrayList();
list.add("New York");
java.util.ArrayList list1 = list;
list.add("Atlanta");
list1.add("Dallas");
System.out.println(list1);


a. [New York, Dallas]
b. [New York, Atlanta]
c. [New York, Atlanta, Dallas]
d. [New York]

#
24. Which of the following classes cannot be extended?


a. final class A { }
b. class A {   private A();}
c. class A {   protected A();}
d. class A { }

#
25. The size of ____________ can grow and shrink at runtime.

a. an array
b. an ArrayList

#
26. 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(2)
c. x.remove(0)
d. x.remove(1)

#
27. 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. c4 instanceof C2
c. c2 instanceof C1
d. c3 instanceof C1

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

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

#


30. The UML uses _______ before a member name to indicate that the member is protected.

a. +
b. #
c. -
d. ?

#
31. 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 false
c. Program 1 displays true and Program 2 displays false
d. Program 1 displays false and Program 2 displays true

#
32. The UML uses _______ before a member name to indicate that the member is public.

a. #
b. +
c. ?
d. -

#
33. 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(Object other)
b. public static boolean equals(Object other)
c. public static boolean equals(String other)
d. public boolean equals(String other)

#
34. Analyze the following code:

ArrayList list = new ArrayList();
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. If you replace the last line by list.add(4, "Hong Kong"), the code will compile and run fine.
c. The last line in the code has a compile error because there is no element at index 3 in the array list.
d. If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine.

#
35. 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. {}
c. {"red", "green"}
d. {"green"}

#
36. Which of the statements regarding the super keyword is incorrect?

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

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

#
38.
Object-oriented programming allows you to derive new classes from existing classes. This is called ____________.

a. ab

straction
b. inheritance
c. generalization
d. encapsulation

#
39. Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors?

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

#
40. The instanceof operator is used to determine whether an object is an instance of a certain class.

a. true
b. false

#
41. An interface can be a separate unit and can be compiled into a bytecode file.

a. true
b. false

#
42. 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. protected
c. private
d. Use the default modifier.

#
43. Every class has a toString() method and an equals() method.

a. false
b. true

#
44. Which of the following statements are true?

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

#
45. What is output of the following code:

ArrayList list = new ArrayList();
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. false true
b. false false
c. true false
d. true true

#
46. Analyze the following code:


public class A extends B {
}

class B {
public B(Strign s) {
}
}

a. 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.
c. The program has a compilation error because A does not have a default constructor.
d. The program would compile fine if you add the following constructor into A: A(String s) { }

#
47. You can always successfully cast a superclass to a subclass.

a. true
b. false

#
48. Consider the following declaration for a class A.

class A {
private int x;
private int y;

public A(int x, int y) {
this.x = x;
this.y = y;
}
}

Class B is a subclass of A. Which of the following can be constructors in B?

I:
public B() {
}

II:
public B(int x, int y) {
super(x, y);
}

III:
public B() {
super(0, 0);
}

IV:
public B(int x, int y) {
this.x = x;
this.y = y;
}

a. III
b. I
c. IV
d. II

#
49. Which of the following statements is f

alse?

a. A method with no visibility modifier can be accessed by a class in 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 public class can be accessed by a class from a different package.

#
50. What is output of the following code:

ArrayList list = new ArrayList();
String s1 = new String("Java");
String s2 = new String("Java");
list.add(s1);
list.add(s2);
System.out.println((list.get(0) == list.get(1)) + " " + (list.get(0)).equals(list.get(1)));


a. true true
b. true false
c. false false
d. false true

#
51. The UML uses _______ before a member name to indicate that the member is package-private.

a. none (blank)
b. -
c. +
d. #

#
52. 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

#
53. The UML uses _______ before a member name to indicate that the member is private.

a. -
b. ?
c. #
d. +

#
54. A instance of a subclass is also an instance of its superclass.

a. false
b. true

#
55. In OOP, a reference variable can reference a subtype object. This is called _____.

a. polymorphism
b. encapsulation
c. abstraction
d. inheritance

#
56. If a parameter is of the https://www.wendangku.net/doc/6b12918122.html,ng.Object type, you can pass any object to it. This is known as generic programming.

a. true
b. false

#
57. The set of all instances of a subclass is a subset of the instances of its superclass.

a. false
b. true

#
58. Normally you depend on the JVM to perform garbage collection automatically. However, you can explicitly use ____ to request garbage collection.

a. System.gc(0)
b. System.exit(0)
c. System.gc()
d. System.exit()

#
59.
A final class can have instances.

a. true
b. false

#
60. You can create an ArrayList using _________.

a. new ArrayList()
b. new ArrayList[]
c. ArrayList()
d. new ArrayList[100]

#
61. Polymorphism means ______________.

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

#
62. 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. {"green"}
c. {}
d. {"red", "green"}

#
63. 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 false
b. Program 1 displays false and Program 2 displays false
c. Program 1 displays false and Program 2 displays true
d. Program 1 displays true and Program 2 displays true

#
64. Which of the following is incorrect?

a. A constructor may be static.
b. A constructor may invoke an overloaded constructor.
c. A constructor may be private.
d. A constructor invokes its superclass no-arg constructor by default if a constructor does not invoke an overloaded constructor or its superclass’s constructor.
e. A constructor may invoke a static method.

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

#
66. If a method is declared public in the superclass, you may declare the method private in the subclass.

a. false
b. true

#
67. 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 k, m.
b. In the class B, an instance method can only access i, j, k, m.
c. In the class B, an instance method can only access j, k, m.
d. In the class B, an instance method can only access j, m.

#
68. Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ___________.

a. false
b. true

#
69. 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.

#
70. 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. "The default constructor of A is invoked"
b. "The default constructor of B is invoked"
c. "The default constructor of A is invoked""The default constructor of B is invoked"
d. Nothing displayed
e. "The default constructor of B is invoked""The default constructor of A is invoked"

#
71. In OOP, you declare a data fields to be private. This is called _____.

a. polymorphism
b. inheritance
c. encapsulation
d. abstraction

#
72. You can assign _________ to a variable of Object[] type.

a. new double[100]
b. new java.util.Date[100]
c. new char[100]
d. new int[100]
e. new String[100]

#
73. Which of the following statements are true?

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

#
74. 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. Both I and II
c. Neither
d. II

#
75. Encapsulation means ______________.

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

#
76. 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. f

alse true
b. true false
c. true true
d. false false

#
77. 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

#
78. If a method is declared protected in the superclass, you may declare the method public in the subclass.

a. false
b. true

#
79. 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.

#
80. If a method is declared private in the superclass, you may declare the method protected in the subclass.

a. false
b. true

#
81. In OOP, you declare a class that extends another class. This is called _____.

a. polymorphism
b. encapsulation
c. inheritance
d. abstraction

#
82. The order in which modifiers appear before a class or a method is important.

a. true
b. false

#
83. Inheritance means ______________.

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

#
84. 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. protected
c. private
d. Use the default modifier.

#
85. Which of the following statements are true?

a. A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.
b. It is a compilation error if two methods differ only in return type in the same class.
c. A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.
d. Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them.
e. To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass.

#
86. Which of the following statements are true?

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

#

87. 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 Person()) causes an error
b. m(new Student()) causes an error
c. m(new GraduateStudent()) causes an error
d. m(new Object()) causes an error

#
88. 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. true true
b. false false
c. true false
d. false true

#
89. Invoking _________ returns the first element in an ArrayList x.

a. x.get(1)
b. x.get(0)
c. x.first()
d. x.get()

#
90. The visibility of these modifiers increases in this order:

a. private, none (if no modifier is used), protected, and public.
b. none (if no modifier is used), protected, private, and public.
c. private, protected, none (if no modifier is used), and public.
d. none (if no modifier is used), private, protected, and public.

#
91. Which of the following are Java keywords?

a. casting
b. instanceof
c. instanceOf
d. cast

#
92. If a method is declared protected in the superclass, you may declare the method private in the subclass.

a. true
b. false

#
93. Superclasses contain more features than their subclasses.

a. true
b. false

#
94. 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. {"red", "green", "green"}
c. {"green", "red", "green"}
d. {"green", "green"}

#
95. Analyze the following code:

Cylinder cy = new Cylinder(1, 1);
Circle c = cy;

a. The code has a runtime error.
b. The code has a compile error.
c. The code is fine.

#
96. 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. Stduent Student
b. Student Person
c. Person Student
d. Person Person

相关文档
相关文档 最新文档