cs

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

2) The set of all instances of a subclass is a subset of the instances of its superclass. A) true B) false

A

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

b

35) Every class has a toString() method and an equals() method. A) true B) false

a

49) 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

11) Invoking ________ removes all elements in an ArrayList x. A) x.empty() B) x.delete() C) x.remove() D) x.clear() E) x.clean()

d

18) 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(String s) D) public String toString()

D

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

a

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

a

20) Which of the statements regarding the super keyword is incorrect? A) You can use super.super.p to invoke a method in superclass's parent class. B) You can use super to invoke a super class constructor. C) You cannot invoke a method in superclass's parent class. D) You can use super to invoke a super class method.

a

31) What is the 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 true B) true false C) false true D) false false

a

32) 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 true B) false false C) true true D) true false

a

34) If a method is declared private in the superclass, you may declare the method public in the subclass. A) true B) false

a

29) 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) Override the methods equals and toString defined in the Object class whenever possible. D) You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.

abcd

37) 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(a2), the toString() method in the Object class is invoked B) When executing System.out.println(a1), the toString() method in the Object class is invoked. C) When executing System.out.println(a1), the toString() method in the A 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());

ac

16) 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.0. B) The code has a compile error on Collections.max(c). c cannot be an array. C) The code has a compile error on Integer[] c = {1, 2, 3}. D) The code is correct and displays 3.

b

43) If a method is declared public in the superclass, you may declare the method private in the subclass. A) true B) false

b

17) 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) I B) II C) III D) IV

b,c

40) Which of the following methods override the equals method in the Object class? A) public static boolean equals(Object o) B) public void equals(Object o) C) public boolean equals(Object o) D) public boolean equals(SomeType o)

c

8) 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) You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form. B) c3 is cast into c2 successfully. C) You will get a runtime error because you cannot cast objects from sibling classes. D) The statement is correct.

c

13) 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, Dallas] C) [New York, Atlanta] D) [New York, Atlanta, Dallas]

d

14) 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 k, m. C) An object of B contains data fields j, m. D) An object of B contains data fields i, j, k, m.

d

26) 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, m. C) that a variable of supertype can refer to a subtype object D) that a class can contain another class 26) 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, m. D) In the class B, an instance method can only access j, k, m.

d

6) 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 60". 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 7".

B

50) Which of the following statements are true? A) Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. B) To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. C) It is a compilation error if two methods differ only in return type in the same class. D) A 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. E) A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.

abcde

30) 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 Object()) causes an error D) m(new Person()) causes an error

c,d

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 called and it displays "i from A is 40". B) The constructor of class A is called and it displays "i from A is 60". C) The constructor of class A is not called. D) The constructor of class A is called and it displays "i from A is 7".

d

24) The order in which modifiers appear before a class or a method is important. A) true B) false

b

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

d

3) The equals method is defined in the Object class. Which of the following is correct to override it in the String class? A) public static boolean equals(String other) B) public boolean equals(Object other) C) public boolean equals(String other) D) public static boolean equals(Object other)

B

33) You can always successfully cast a superclass to a subclass. A) true B) false

b

4) 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) true false B) true true C) false true D) false false

d

6) Which of the following statements are true? A) "class A extends B" means B is a subclass of A. B) A subclass is a subset of a superclass. 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.

C,D

28) 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" "The default constructor of A 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"

c

44) 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

7) Which of the following statements is false? A) A private method cannot be accessed by a class in a different package. B) A protected method can be accessed by a subclass in a different package. C) A method with no visibility modifier can be accessed by a class in a different package. D) A public class can be accessed by a class from a different package.

c

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

d

36) 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.gc(0) C) System.exit(0) D) System.exit()

a

39) If a method is declared protected in the superclass, you may declare the method public in the subclass. A) true B) false

a

42) 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

47) 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

10) The visibility of these modifiers increases in this order ________. A) none (if no modifier is used), protected, private, and public B) private, none (if no modifier is used), protected, and public C) private, protected, none (if no modifier is used), and public D) none (if no modifier is used), private, protected, and public

b

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

a, d

23) Every object is an instance of the Object class. A) true B) false

a

21) Analyze the following code. // Program 1: public class Test { public static void main(String[] args) { Object 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

25) Encapsulation means ________. A) that a class can extend another class B) that data fields should be declared private C) that a variable of supertype can refer to a subtype object D) that a class can contain another class

b

38) 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 protected. C) The variable should be marked private. D) The variable should be marked private and an accessor method provided. E) The variable should have no special access modifier.

b

41) 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) c3 instanceof C1 D) c2 instanceof C1

b

48) Suppose an ArrayList list contains {"red", "red", "green"}. What is the 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) {"green"} C) {} D) {"red", "green"}

b

5) 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 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 does not compile because Test does not have a default constructor Test(). D) The program would compile if a default constructor A(){ } is added to class A explicitly.

b

9) The UML uses ________ before a member name to indicate that the member is private. A) + B) - C) # D) ?

b


संबंधित स्टडी सेट्स

Exam 1: Health Promotion of Adolescent

View Set

Chapter 7 Creating a Flexible Organization

View Set

n) Chapter 7 Brooker - using chapter 9 Campbell, chapter 8 Mader & others**

View Set