CSC Chapter 11

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

a. A constructor may be static

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

a. Linear search

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

a. true

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

a. true

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

a. true

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

a. true

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

a. true

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

a. true

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

a. true

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

a. true

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

b. inheritance

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

b. inheritance

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

c. super

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

a, c

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

a. +

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

a. System.gc()

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

a. The code has a compile error.

Analyze the following code: public class Test extends A{ public static void main(String[] args){ Test t = new Test(); t. print();}} class A{ A(String s){ this.s = s;} public void print(){ System.out.println(s);}} a. The program does not compile because Test does not have a default constructor Test(). b. The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed. c. The program would compile if a default constructor A(){ } is added to class A explicitly. d. The program compiles, but it has a runtime error due to the conflict on the method name print.

b, c

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

b, c

Analyze the following code: public class A extends B{} class B{ public B(String s){}} a. The program has a compilation error because A does not have a default constructor. b. The program has a compilation error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor. 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, d

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

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.

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

b. false

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

b. false

Analyze the follow code: public class Test{ public static void main(String[] args){ Object a1 = new A(); Object a2 = new Object(); System.out.println(a1); System.out.println(a2);}} class A{ int x; public String toString(){ return "A's x is " + x;}} a. The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString()); b. When executing System.out.println(a1), the toString() method in the Object class is invoked. c. When executing System.out.println(a2), the toString() method in the Object class is invoked. d. When executing System.out.println(a1), the toString() method in the A class is invoked.

c, d

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"

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

c. #

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

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

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

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

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

c. The code is fine.

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 supperclass's parent class.

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

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: 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

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

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

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.

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

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


Ensembles d'études connexes

Need a New Drug? Exam 2 Study Guide

View Set

Chapter 3: Collecting Objective Data: The Physical Examination PrepU

View Set

CH. 11-15 Final BA201 principles of management

View Set

Unit 1 - One-party and Multi- Party Governments

View Set

Human Growth and Devp Chap 10 CM

View Set