CS - 2013: 11. Java: Chapter 11 - Inheritance And Polymorphism

Ace your homework & exams now with Quizwiz!

How do you create an ArrayList for storing double values? 1. ArrayList<Double> list = new ArrayList<Double>(); 2. ArrayList<double> list = new ArrayList<>();

1

Suppose you have an ArrayList called "list: How do you append an object to a list? 1. list.add(object); 2. list.add(0, object); 3. list.append(object); 4. list.append(0, object);

1

Suppose you have an ArrayList called "list: How do you retrieve an object at a specified index from a list? 1. list.get(index); 2. list[index]

1

Which line will be executed to show the output? (in order) 1 public class Test { 2 public static void main(String[] args) { 3 A a = new A(3); 4 } 5 } 6 7 class A extends B { 8 public A(int t) { 9 System.out.println("A's constructor is invoked"); 10 } 11 } 12 13 class B { 14 public B() { 15 System.out.println("B's constructor is invoked"); 16 } 17 }

15, 9

Suppose the ArrayList list contains {"Dallas", "Dallas", "Houston", "Dallas"}. Which of the following code correctly remove all elements with value "Dallas" from the list? 1. for (int i = 0; i < list.size(); i++) list.remove("Dallas"); 2. for (int i = 0; i < list.size(); i++) { if (list.remove(element)) i--; }

2

Suppose you have an ArrayList called "list: How do you check whether a given object is in a list? 1. list.contains(0, object); 2. list.contains(object);

2

Suppose you have an ArrayList called "list: How do you insert an object at the beginning of a list? 1. list.add(object); 2. list.add(0, object); 3. list.append(object); 4. list.append(0, object);

2

Which of the following statements is correct? 1. ArrayList<int> list = new ArrayList<>(); 2. ArrayList<Integer> list = new ArrayList<>();

2

Suppose you have an ArrayList called "list: How do you find the number of objects in a list? 1. list.length(); 2. list.length; 3. list.size();

3

Suppose you have an ArrayList called "list: How do you remove the last object from the list? 1. list.delete(object); 2. list.delete(list.size() - 1); 3. list.remove(list.size() - 1); 4. list.remove(object);

3

Suppose you have an ArrayList called "list: How do you remove a given object from a list? 1. list.delete(object); 2. list.delete(list.size() - 1); 3. list.remove(list.size() - 1); 4. list.remove(object);

4

Which lines cause the following code error? 1 public class Test { 2 public static void main(String[] args) { 3 Object fruit = new Fruit(); 4 Object apple = (Apple)fruit; 5 } 6 } 7 8 class Apple extends Fruit { 9 } 10 11 class Fruit { 12 }

4

Which lines of the following program cause error? 1. ArrayList<String> list = new ArrayList<>(); 2. list.add("Denver"); 3. list.add("Austin"); 4. list.add(new java.util.Date()); 5. String city = list.get(0); 6. list.set(3, "Dallas"); 7. System.out.println(list.get(3));

4, 6, 7

public class Test { public static void main(String[] args) { new A(); 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; } } => Output: i from A is ..... i from A is ...... i from B is .......

40, 60, 60

Which line causes the following code error? 1 public class Test { 2 public static void main(String[] args) { 3 Integer[] list1 = {12, 24, 55, 1}; 4 Double[] list2 = {12.4, 24.0, 55.2, 1.0}; 5 int[] list3 = {1, 2, 3}; 6 printArray(list1); 7 printArray(list2); 8 printArray(list3); 9 } 10 11 public static void printArray(Object[] list) { 12 for (Object o: list) 13 System.out.print(o + " "); 14 System.out.println(); 15 } 16 }

8

Show 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()); } }

Person Person ( new Student().printPerson() invokes the printPerson() method definedin the Person class, which then invokes the getInfo() method in the Person class. It does not invoke the getInfo() method in the Student class, because it is private and can only be invoked from a method in the Student class. Note that the getInfo() method is not overridden because it is private. You can only override a non-private method)

Show 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 { @Override public String getInfo() { return "Student"; } } class Person { public String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } }

Person Student (new Person().printPerson() invokes the printPerson() method definedin the Person class, which then invokes the getInfo() method in the Person class.new Student().printPerson() invokes the printPerson() method definedin the Person class, which then invokes the getInfo() method in the Student class, because the calling object is a Student. )

......... means that a variable of a supertype can refer to a subtype object.

Polymorphism

If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. The hidden static methods can be invoked using the syntax "........."

SuperClassName.staticMethodName

Show the result of the following program: ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.remove(1); System.out.println(list);

[1, 3]

Show the result of the following program: ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.remove(new Integer(1)); System.out.println(list);

[2,3]

An instance method can be overridden only if it is ...... Thus a ..... method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its ......, the two methods are completely unrelated.

accessible, private, superclass

In ArrayList class from Java API, " clear(): void" removes ...... the elements from this list.

all

The static method ..... in the Arrays class returns an ArrayList from an Array

asList

The syntax to call a superclass's constructor is: 1. super(); OR 2. super(parameters);

both of them

One object reference can be typecast into another object reference. This is called ......

casting object.

Method overloading defines methods of the same name in a ...... Method overriding modifies the methods that are defined in the .......

class, superclasses

Overridden methods are in different ...... related by ......; ....... methods can be either in the same class or different classes related by inheritance.

classes, inheritance, overloaded

The declared type decides which method to match at ...... time

compile

When constructing an object of a subclass, the subclass constructor first invokes its superclass constructor before performing its own tasks. If the superclass is derived from another class, the superclass constructor invokes its parent-class constructor before performing its own tasks. This process continues until the last constructor along the inheritance hierarchy is called. This is called .......

constructor chaining

A ..... is used to construct an instance of a class. Unlike properties and methods, the constructors of a superclass are not ..... by a subclass. They can only be invoked from the constructors of the subclasses using the keyword .....

constructor, inherited, super

The keyword super refers to the superclass of the class in which super appears. It can be used in two ways: 1. To call a superclass ....... 2. To call a superclass .......

constructor, method

Matching a method signature and binding a method implementation are two separate issues. The ...... type of the reference variable decides which method to match at compile time. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compile time. A method may be implemented in several subclasses. The JVM dynamically binds the implementation of the method at ...., decided by the actual class of the object referenced by the ......

declared, runtime, variable

A method may be implemented in several classes along the inheritance chain. The JVM decides which method is invoked at runtime. This is known as ......

dynamic binding

What keyword do you use to define a subclass?

extends

Assume are circle and object1 created as follows: Circle circle = new Circle(1); GeometricObject object1 = new GeometricObject(); => (object instanceof Circle) returns .....

false

True or False? "A subclass inherits accessible data fields and methods from its superclass, but it can't add new data fields and methods."

false

True or False? "Java allows multiple inheritance."

false

True or False? "Private data fields in a superclass are accessible outside the class."

false

True or False? "You can always successfully cast an instance of a superclass to a subclass."

false

True or False? "a subclass is a subset of its superclass"

false

True or False? "casting an object reference creates a new object"

false

True or False? "the constructors of a superclass could be inherited by a subclass"

false

True or False? "You can override a private method defined in a superclass."

false

True or False? "You can override a static method defined in a superclass."

false

True or false? "When invoking a constructor from a subclass, its superclass's no-arg constructor is always invoked."

false

The statement super() or super(arguments) must be the ....... statement of the subclass's constructor; this is the only way to explicitly invoke a superclass constructor.

first

In ArrayList class from Java API, "remove(o: Object): boolean" removes the ...... element o from this list. Returns ..... if an element is removed.

first, true

In ArrayList class from Java API, "set(index: int, o: E)" sets the element at the specified .....

index

In ArrayList class from Java API, "indexOf(o: Object)" returns the ....... of the .... matching element in this list.

index, first

In ArrayList class from Java API, "lastIndexOf(o: Object)" returns the ..... of the ..... matching element in this list.

index, last

Object-oriented programming allows you to define new classes from existing classes. This is called .......

inheritance

In "downcasting", for the casting to be successful, you must make sure that the object to be cast is an ...... of the subclass. If the superclass object is not an instance of the subclass, a runtime ....... occurs.

instance, ClassCastException

to ensure that the object is an instance of another object before attempting a casting. This can be accomplished by using the ..... operator.

instanceof

instanceof is a Java keyword. Every letter in a Java keyword is in .......

lowercase

You can use the static ..... and ..... in the java.util.Collections class to return the maximum and minimal element in a list.

max, min

Some programming languages allow you to derive a subclass from several classes. This capability is known as ....... inheritance.

multiple

Assume are circle and object1 created as follows: Circle circle = new Circle(1); GeometricObject object1 = new GeometricObject(); Can the following statements be compiled? GeometricObject object = new GeometricObject(); Circle circle = (Circle)object;

no

Can the following code be executed? ArrayList<Double> list = new ArrayList<>(); list.add(1);

no

Can the following program be executed? class A { public A(int x) { System.out.println("A's no-arg constructor is invoked"); } } class B extends A { public B() { } } public class C { public static void main(String[] args) { B b = new B(); } }

no

A variable of a reference type can hold a ..... value or a reference to an instance of the declared type. The instance may be created using the constructor of the declared type or its subtype.

null

An ArrayList object can be used to store a list of ......

objects

If a method in a subclass has the same name as a method in its superclass with different parameter types, is the method overridden or overloaded?

overloader

Method ..... defines methods of the same name in a class. Method ...... modifies the methods that are defined in the superclasses.

overloading, overriding

If a method in a subclass has the same signature as a method in its superclass with the same return type, is the method overridden or overloaded?

overridden

A subclass inherits methods from a superclass. Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method ......

overriding

The == comparison operator is used for comparing two primitive data type values or for determining whether two objects have the same ...... The equals method is intended to test whether two objects have the same ......., provided that the method is overridden in the defining class of the objects.

references, contents

This annotation denotes that the annotated method is ...... to override a method in the superclass. If a method with this annotation does not override its superclass's method, the compiler will report an .....

required, error

The JVM dynamically binds the implementation of the method at ......, decided by the actual type of the variable.

runtime

The actual type decides which method to match at ...... time

runtime

The @Override annotation forces the compiler to check the ..... of the overridden method to ensure that the method is defined correctly.

signature

Overridden methods have the same .... and .......; overloaded methods have the same ..... but a different .......

signature, return type, name, parameter list

Overloading means to define multiple methods with the same name but different ....... Overriding means to provide a new implementation for a ...... in the subclass.

signatures, method

A Java class may inherit directly from only one superclass. This restriction is known as ...... inheritance. If you use the ...... keyword to define a subclass, it allows only one parent class.

single, extends

you can use the static ...... method in the java.util.Collections class to sort the elements.

sort

To override a method, the method must be defined in the ...... using the same signature and the same return type.

subclass

To override a method, the method must be defined in the ...... using the same ...... and the same ..... as in its superclass.

subclass, signature, return type

A class defines a type. A type defined by a subclass is called a ...., and a type defined by its superclass is called a .......

subtype, supertype

The keyword ..... refers to the superclass and can be used to invoke the superclass's methods and constructors.

super

You must use the keyword ..... to call the superclass constructor, and the call must be the ..... statement in the constructor. Invoking a superclass constructor's name in a subclass causes a ...... error.

super, first, syntax

A ..... is also referred to as a parent class or a base class, and a ..... as a child class, an extended class, or a derived class.

superclass, subclass

Inheritance enables you to define a general class (i.e., a ......) and later extend it to more specialized classes (i.e., ....).

superclass, subclasses

Assume are circle and object1 created as follows: Circle circle = new Circle(1); GeometricObject object1 = new GeometricObject(); => (circle instanceof Circle) returns .....

true

Assume are circle and object1 created as follows: Circle circle = new Circle(1); GeometricObject object1 = new GeometricObject(); => (circle instanceof GeometricObject) returns .....

true

Assume are circle and object1 created as follows: Circle circle = new Circle(1); GeometricObject object1 = new GeometricObject(); => (object instanceof GeometricObject) returns .....

true

True of False? "A subclass inherits accessible data fields and methods from its superclass and may also add new data fields and methods."

true

True of False? "Casting a primitive type value returns a new value"

true

True or False? "A constructor may invoke an overloaded constructor or its superclass constructor. If neither is invoked explicitly, the compiler automatically puts super() as the first statement in the constructor."

true

True or False? "A method can be implemented in several classes along the inheritance chain."

true

True or False? "Java does not allow multiple inheritance."

true

True or False? "Private data fields in a superclass are not accessible outside the class."

true

True or False? "You can always successfully cast an instance of a subclass to a superclass. "

true

True or False? "casting an object reference does not create a new object"

true

True or False? "the constructors of a superclass are not inherited by a subclass"

true

True or False? "we can use a special Java syntax, called override annotation, to place @Override before the method in the subclass."

true

It is always possible to cast an instance of a subclass to a variable of a superclass (known as .....), because an instance of a subclass is always an instance of its superclass. When casting an instance of a superclass to a variable of its subclass (known as .....).

upcasting, downcasting

Assume are circle and object1 created as follows: Circle circle = new Circle(1); GeometricObject object1 = new GeometricObject(); Can the following statements be compiled? Circle circle = new Circle(5); GeometricObject object = circle;

yes

Can the following code be executed? String[] array = {"red", "green", "blue"}; ArrayList<String> list = new ArrayList<>(Arrays.asList(array));

yes

Can the following program be executed? class A { public A() { System.out.println("A's no-arg constructor is invoked"); } } class B extends A { } public class C { public static void main(String[] args) { B b = new B(); } }

yes

If a method in a subclass has the same signature as a method in its superclass with a different return type, will this be a problem?

yes


Related study sets

4.1 DIFFERENCES BETWEEN MANAGERS AND LEADERS

View Set

Unit 4 AP-Microeconomics Test Study Guide

View Set

Chapter 15 Global Production and SCM

View Set

chapter 15 Neurological emergencies

View Set

Chapter 6 Input and Output Devices

View Set

FBLA Help Desk Problems and Solutions (scenarios)

View Set

Prep U Ch 45 Digestive and Gastrointestinal Tx Modalities

View Set

Select-All-That-Apply Questions and General Test Taking Skills

View Set