Final

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is subclass in java? A. A subclass is a class that extends another class B. A subclass is a class declared inside a class C. Both above. D. None of the above.

A. A subclass is a class that extends another class

What is polymorphism in Java? A. It is when a single parent class has many child classes. B. It is when a class has several methods with the same name but different parameter types. C. It is when a single variable is used with several different types of related objects at different places in a program. D. It is when a program uses several different types of objects, each with its own variable.

A. It is when a single parent class has many child classes.

What is an advantage of polymorphism? A. The same program logic can be used with objects of several related types. B. Variables can be re-used in order to save memory. C. Constructing new objects from old objects of a similar type saves time. D. Polymorphism is a dangerous aspect of inheritance and should be avoided.

A. The same program logic can be used with objects of several related types.

Can an object of a child type be assigned to a variable of the parent type? For example, Card crd; BirthDay bd = new BirthDay("Lucinda", 42); crd = bd; // is this correct? A. Yes—an object can be assigned to a reference variable of the parent type. B. Yes—any object can be assigned to any reference variable. C. No—there must always be an exact match between the variable and the object types. D. No—but a object of parent type can be assigned to a variable of child type.

A. Yes—an object can be assigned to a reference variable of the parent type.

A subclass may call an overridden superclass method by ______. A. prefixing its name with the super key word and a dot (.) B. prefixing its name with the name of the superclass in parentheses C. using the extends key word before the method is called D. calling the superclass method first and then calling the subclass method

A. prefixing its name with the super key word and a dot (.)

What is an abstract method? A. An abstract method is any method in an abstract class. B. An abstract method is a method which cannot be inherited. C. An abstract method is a method in the child class that overrides a parent method. D. An abstract method is one without a body that is declared with the reserved word abstract.

D. An abstract method is one without a body that is declared with the reserved word abstract.

In order for the following code to be correct, what must be the type of the reference variable card? ________ _________ card; card = new Valentine( "Joe", 14 ) ; card.greeting(); card = new Holiday( "Bob" ) ; card.greeting(); card = new Birthday( "Emily", 12 ) ; card.greeting(); A. Valentine B. Holiday C. Birthday D. Card

D. Card

A finally clause is always required in a try-catch block.

False

A reference variable can refer to an object of a child class, but not any further down the inheritance hierarchy.

False

An abstract class must contain abstract methods.

False

Attempting to divide by zero will result in an Error being thrown, not an Exception.

False

Consider a reference declared in the following manner. Animal a; This reference may only point to an object that created by instantiating the Animal class.

False

Every line in a catch block is guaranteed to be executed in all situations.

False

In an inheritance relationship, the subclass constructor always executes before the superclass constructor.

False

In practice, it is important to catch all exceptions that might be thrown by a program.

False

Inheritance involves a subclass, which is the general class, and a superclass, which is the specialized class.

False

It makes sense to declare most abstract classes as final.

False

Java supports multiple inheritance.

False

Let Animal be an interface. Then it is possible to create an object by instantiating the Animal interface.

False

Polymorphism via inheritance requires that all classes in the inheritance hierarchy are concrete.

False

Recursive solutions to problems should be used whenever possible

False

Some problems can only be solved recursively.

False

The getMessage method of the Exception class prints out the stack trace, which helps the user to track down the source of the exception.

False

Unchecked exceptions must be caught or propogated, or a program will not compile.

False

A child class is allowed to declare a variable with the same name as one that is contained in the parent class.

True

A child class is allowed to define a method with the same name and parameter list as a method in the parent class.

True

A method that calls a different method, which then calls the original calling method is a recursive method.

True

A parameter to a method can be polymorphic.

True

A throw statement is used to begin exception propagation.

True

All recursive methods must have a base case.

True

Because every class directly or indirectly inherits from the Object class, every class inherits the Object class's members.

True

In the Towers of Hanoi puzzle, there are ________________ pegs. a) 2 b) 3 c) 4 d) 5 e) The Towers of Hanoi puzzle can include any number of pegs

b) 3

Suppose that Horse is a subclass of Animal, and neither class is abstract. Which of the following is an invalid declaration and initialization? a) Horse h = new Horse(); b) Horse h = new Animal(); c) Animal a = new Animal(); d) Animal a = new Horse(); e) all of the above are valid

b) Horse h = new Animal();

A(n)______________________ class represents a generic concept in a class hierarchy. a) super b) abstract c) interface d) shadow e) generic

b) abstract

How many base cases must a recursive method have? a) A recursive method does not have to have a base case. b) at least 1 c) more than 1 d) more than 2 e) more than 3

b) at least 1

A(n) ____________________ can be used to find the exact line where an exception was thrown during program execution. a) interface b) call-stack trace c) try block d) catch block e) none of the above

b) call-stack trace

All recursive programs ____________________________________ . a) are more difficult to read than iterative programs. b) can be implemented iteratively. c) are easier to read than iterative programs. d) are shorter than iterative programs. e) none of the above are true.

b) can be implemented iteratively.

A class declared as final _________________________________ . a) cannot be changed. b) cannot have subclasses. c) cannot have superclasses. d) has several abstract methods. e) cannot be used in a program.

b) cannot have subclasses.

A(n) _____________________ is an object that defines an unusual or erroneous situation that is typically recoverable. a) error b) exception c) interface d) try block e) catch block

b) exception

Which of the following key words indicates a method that cannot be overridden in a derived class? a) super b) final c) extends d) inherits e) expands

b) final

A solution with exponential complexity is ____________________ . a) efficient b) inefficient c) easy to implement d) difficult to implement e) none of the above

b) inefficient

When a variable declared in a subclass has the same name as a variable declared in a superclass, it is called a _____________ a) final b) shadow c) static d) dead e) this is not allowed in Java

b) shadow

If an exception is not caught, a program will __________________________ . a) not compile b) terminate abnormally c) print a message and continue executing d) all of the above e) neither a, b nor c

b) terminate abnormally

Which of the following methods are included in every class created in Java by inheritance? a) next b) toString c) compareTo d) charAt e) none of the above

b) toString

You need to create a reference variable that can refer to objects from many different classes. You do not know the inheritance hierarchies of the classes. The safest class to use to declare the reference variable is a) Animal b) String c) Object d) Scanner e) File

c) Object

Which of the following statements is true? a) Recursion should be used in every program. b) Recursion should be avoided all the time. c) Solutions that are easily expressed recursively should be programmed recursively. d) Solutions that are easily expressed recursively should be programmed iteratively. e) None of the above.

c) Solutions that are easily expressed recursively should be programmed recursively.

A(n) ____________________ is used to specify how certain exceptions should be handled. a) finally block b) try block c) catch block d) error e) none of the above

c) catch block

Which of the following exception types must always be caught unless they are contained in methods that throw them in the method header? a) file stream b) IO c) checked d) unchecked e) none of the above

c) checked

____________________ recursion occurs when a method calls itself, while _________________ recursion when a method calls another method that then calls the original method. a) upward, downward b) downward, upward c) direct, indirect d) indirect, direct e) none of the above

c) direct, indirect

A polymorphic reference is one that can refer to _______________ type(s) of object(s). a) exactly one b) zero c) multiple d) abstract e) static

c) multiple

Which of the following methods are part of the Exception class and can be used to give information about a thrown exception? a) getInfo b) printInfo c) printStackTrace d) getStackTrace e) none of the above

c) printStackTrace

In order for derived classed to have access to encapsulated data members and methods of superclasses, the data members and methods should be declared using the ____________________ modifier. a. private b. public c. protected d. final e. static

c. protected

Let Dog be a subclass of Animal, and suppose Animal has a method called speak() that is overridden in the Dog class. Consider the following code. Animal spot = new Dog(); spot.speak(); Which of the following is true? a) This code will result in a compile-time error. b) This code will result in a run-time error. c) The speak method defined in the Animal class will be called. d) The speak method defined in the Dog class will be called. e) The speak method will not be called at all.

d) The speak method defined in the Dog class will be called.

In a recursive solution, _______________ is(are) always necessary. a) short, efficient code b) several variables c) numerous lines of code d) a base case e) none of the above

d) a base case

The Comparable interface contains which of the following methods? a) isGreaterThan b) isLessThan c) equals d) compareTo e) all of the above

d) compareTo

In Java, a(n) ___________________ is a collection of constants and abstract methods. a) polymorphic reference b) abstract class c) implementation d) interface e) iterator

d) interface

A method that calls itself is a __________________ method. a) invalid b) static c) final d) recursive e) public

d) recursive

To invoke a parent's constructor in a subclass, we use the ______________ method. a) abstract b) construct c) parent d) super e) extends

d) super

A(n) ____________________ is used to identify a block of statements that may cause an exception. a) call-stack trace b) error c) catch block d) try block e) none of the above

d) try block

The original class that is used to derive a new class using inheritance is called a. a superclass b. a parent class c. a base class d. all of the above e. neither a, b, nor c

d. all of the above

Every line of a(n) __________________ is executed no matter what exceptions are thrown. a) try block b) call stack trace c) catch block d) interface e) finally block

e) finally block

a child class can access private members of a parent class by a. using super in front of the member name b. using the member name directly c. using this in front of the member name d. using the public accessor and mutator methods defined in the parent class e. a child class cannot access private members of a parent class

e. a child class cannot access private members of a parent class

If a random access file contains a stream of characters, which of the following statements would move the file pointer to the starting byte of the fifth character in the file? a. file.seek(4); c. file.seek(10); b. file.seek(8); d. file.seek(5);

b. file.seek(8);

______________ occurs when a child class defines a method with the same signature as a method in the parent class a. overloading b. overriding c. overwhelming d. substituting e. a child class cannot define a method with the same signature as a parent class method

b. overriding

There is(are) ____________ base case(s) in the recursive solution to finding a path through a maze. a) 0 b) 1 c) 2 d) 3 e) 4

c) 2

Of the classes below, the one that is most likely to be declared abstract is _________________. a) Bat b) Squirrel c) Animal d) Iguana e) Parrot

c) Animal

____________ is the process of catching an exception in the chain of method calls from the method where the exception occurred up to the main method. a) Error handling b) Exception handling c) Exception propagation d) Catch block chaining e) Finally block nesting

c) Exception propagation

__________________ recursion results from the lack of a base case. a) Indirect b) Direct c) Infinite d) Spiral e) none of the above

c) Infinite

Determining whether or not there is a path through a maze could have a recursive solution.

True

Every class has a toString method and an equals method inherited from the Object class.

True

Files that are open for output from a program must be explicitly closed in the program

True

If the class SerializedClass contains references to objects of other classes as fields, those classes must also implement the Serializable interface in order to be serialized.

True

In Java, it is possible for a method to call itself.

True

It is possible to derive a class from an abstract class without overriding all of the parents abstract methods.

True

When a subclass extends a superclass, the public members of the superclass become public members of the subclass

True

When accessing an element of an array, if the index is outside of the range of the indexes of the array, an exception is thrown.

True

When an object is serialized, it is converted into a series of bytes that contain the object's data.

True

You can write a super statement that calls a superclass constructor but only in a subclass's constructor.

True

Which of the following exceptions are unchecked? a) RuntimeException b) IllegalAccessException c) NoSuchMethodException d) ClassNotFoundException e) none of the above

a) RuntimeException

Which of the following represents the standard input stream? a) System.in b) System.out c) System.err d) System.instream e) System.outstream

a) System.in

In Java, polymorphic method binding occurs ____________________ . a) at run time b) at compile time c) never d) when a programmer writes the code e) during the testing phase of software development

a) at run time

When designing a class hierarchy, it is important that common features be ________________________ . a) higher in the class hierarchy. b) lower in the class hierarchy. c) near the middle of the class hierarchy. d) in abstract classes. e) in the Object class.

a) higher in the class hierarchy.

In Java, polymorphic references can be created through the use of __________________ and ________________. a) inheritance, interfaces b) inheritance, abstract classes c) interfaces, abstract classes d) interfaces, iterators e) none of the above

a) inheritance, interfaces

To write data to a binary file, you create objects from which of the following classes? a. FileOutputStream and DataOutputStream b. BinaryFileWriter and BinaryDataWriter c. File and Scanner d. File and PrintWriter

a. FileOutputStream and DataOutputStream

A file that contains raw binary data is known as a . a. binary file c. serial file b. machine file d. raw data file

a. binary file

If a random access file contains a stream of doubles, which of the following statements would move the file pointer to the starting byte of the fifth double in the file? a. file.seek(5); c. file.seek(32); b. file.seek(0); d. file.seek(40);

a. file.seek(5);

What is an interface? A. An interface is a collection of constants, method headers and/or default methods. B. An interface is a class that a child class can extend. C. An interface is a collection of abstract methods. D. An interface is the collection of public methods of a class.

A. An interface is a collection of constants, method headers and/or default methods.

In practice, is it always possible to know how to interpret the bytes in a given file? A. No - because byte patterns can mean almost anything and often documentation is poor or missing. B. No - files from one type of computer can't be read by any other type. C. Yes - data and file formats are the same with all programs on all computers. D. Yes - all you need to do is try all possible interpretations until you find the one that works.

A. No - because byte patterns can mean almost anything and often documentation is poor or missing.

Can an interface name be used as the type of a variable, like this: public static void main( String[] args ) { SomeInterface x; ... } A. Yes — the variable can refer to any object whose class implements the interface. B. No — a variable must always be an object reference type or a primitive type. C. No — a variable must always be a primitive type. D. No — a variable must always be an object reference type

A. Yes — the variable can refer to any object whose class implements the interface.

A file that contains raw binary data is known as a __________. A. binary file C. serial file B. machine file D. raw data file

A. binary file

An exception will be propagated until it is caught and handled or until it is passed out of the main method.

True

An interface cannot declare any instance variables.

True

What is an abstract class? A. An abstract class is one without any child classes. B. An abstract class is class which cannot be instantiated, but can be a base class. C. An abstract class is any parent class with more than one child class. D. abstract class is another name for "base class."

B. An abstract class is class which cannot be instantiated, but can be a base class.

When a class implements an interface, what must it do A. It must redefine each constant from the interface. B. It must declare and provide a method body for each abstract method in the interface. C. It must declare a variable for each constant in the interface. D. It must include a private method for each method in the interface.

B. It must declare and provide a method body for each abstract method in the interface.

When a class implements an interface, what must it do? A. It must redefine each constant from the interface. B. It must declare and provide a method body for each abstract method in the interface. C. It must declare a variable for each constant in the interface. D. It must include a private method for each method in the interface

B. It must declare and provide a method body for each abstract method in the interface.

What happens when the constructor for FileInputStream fails to open a file for reading? A. It returns null. B. It throws a FileNotFoundException. C. It throws a DataFormatException. D. It throws a ArrayIndexOutOfBoundsException.

B. It throws a FileNotFoundException.

In order for an object to be serialized, its class must implement the __________ interface. A. Serial C. ObjectOutputStream B. Serializable D. Writeable

B. Serializable

What is a method's signature? A. The signature of a method is the name of the method the type of its return value. B. The signature of a method is the name of the method and the names of its parameters. C. The signature of a method is the name of the method and the data types of its parameters. D. The signature of a method is the name of the method, its parameter list, and its return type

B. The signature of a method is the name of the method and the names of its parameters.

An anonymous class must ________ A. be a superclass B. implement an interface C. extend a superclass D. either b or c.

B. implement an interface

A functional interface is an interface with _____ A. name B. only one abstract method. C. no abstract methods. D. only private methods.

B. only one abstract method.

When a class implements an interface, it must A. overload all of the methods listed in the interface B. provide all of the nondefault methods that are listed in the interface, with the exact signatures and return types specified C. not have a constructor D. be an abstract class

B. provide all of the nondefault methods that are listed in the interface, with the exact signatures and return types specified

A car dealership needs a program to store information about the cars for sale. For each car, they want to keep track of the following information: number of doors (2 or 4), whether the car has air conditioning, and its average number of miles per gallon. Which of the following is the best design? A. Use four unrelated classes: Car, Doors, AirConditioning, and MilesPerGallon. B. Use a class Car with three subclasses: Doors, AirConditioning, and MilesPerGallon. C. Use a class Car, with fields: numDoors, hasAir, and milesPerGallon. D. Use a class Car, with subclasses of Doors, AirConditioning, and MilesPerGallon. E. Use classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass Car.

C. Use a class Car, with fields: numDoors, hasAir, and milesPerGallon.

You can use a lambda expression to instantiate an object that _____ A. that has no constructor. B. extends any superclass. C. implements a functional interface D. does not implement an interface.

C. implements a functional interface

How many patterns can be formed from 8 bits? A. 8 B. 16 C. 64 D. 256 == 2^8

D. 256 == 2^8

What happens when DataInputStream.readLong() reaches the end of the input file? A. Control automatically leaves the program. B. A null value is returned. C. The program crashes. D. An EOFException is thrown.

D. An EOFException is thrown.

Is the following a correct way to start out a class definition: public class SomeClass implements MyInterface A. No — SomeClass must also extend a base class. B. No — SomeClass cannot be public if it implements an interface C. Yes — SomeClass is a child of MyInterface D. Yes — SomeClass is automatically a child of the class Object.

D. Yes — SomeClass is automatically a child of the class Object.

An interface name may be used as a reference type.

True

An anonymous class must ______ A. be a superclass B. implement an interface C. extend a superclass D. either b or c.

D. either b or c.

You use the _________ operator to define an anonymous inner class. A. class B. inner C. anonymous D. new

D. new

A program with infinite recursion will act like a program with an infinite loop.

False

Recursive solutions are always more efficient than iterative solutions

False

A class must implement the Serializable interface in order for the objects of the class to be serialized

True

To serialize an object and write it to the file, use the _____ method of the ObjectOutputStream class. a. writeObject c. serialize b. serializeObject d. serializeAndWrite

a. writeObject

If you want to append data to an existing binary file, BinaryFile.dat, which of the following statements would you use to open the file? a. FileOutputStream fstream = new FileOutputStream("BinaryFile.dat"); DataOutputStream binary OutputFileWriter = new DataOutputStream(fstream); b. FileOutputStream fstream = new FileOutputStream("BinaryFile.dat", false); DataOutputStream binaryOutputFileWriter = new DataOutputStream(fstream); c. FileOutputStream fstream = new FileOutputStream("BinaryFile.dat", true); DataOutputStream binaryOutputFileWriter = new DataOutputStream(fstream); d. FileOutputStream fstream = new FileOutputStream("BinaryFile.dat"); DataOutputStream binaryOutputFileWriter = new DataOutputStream(fstream, true);

c. FileOutputStream fstream = new FileOutputStream("BinaryFile.dat", true); DataOutputStream binaryOutputFileWriter = new DataOutputStream(fstream);

To read data from a binary file, you create objects from which of the following classes? a. File and Scanner b. File and PrintWriter c. BinaryFileReader and BinaryDataReader d. FileInputStream and DataInputStream

d. FileInputStream and DataInputStream

All Java classes are subclasses of the ___________________ class. a) String b) java.lang c) Java d) Class e) Object

e) Object

A car dealership needs a program to store information about the cars for sale. For each car, they want to keep track of the following information: number of doors (2 or 4), whether the car has air conditioning, and its average number of miles per gallon. Which of the following is the best design? A. Use four unrelated classes: Car, Doors, AirConditioning, and MilesPerGallon. B. Use a class Car with three subclasses: Doors, AirConditioning, and MilesPerGallon. C. Use a class Car, with fields: numDoors, hasAir, and milesPerGallon. D. Use a class Car, with subclasses of Doors, AirConditioning, and MilesPerGallon. E. Use classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass Car.

B. Use a class Car with three subclasses: Doors, AirConditioning, and MilesPerGallon.

When a class implements an interface, it must ________ A. overload all of the methods listed in the interface B. provide all of the nondefault methods that are listed in the interface, with the exact signatures and return types specified C. not have a constructor D. be an abstract class

B. provide all of the nondefault methods that are listed in the interface, with the exact signatures and return types specified

In an inheritance relationship ______. A. the subclass constructor always executes before the superclass constructor B. the superclass constructor always executes before the subclass constructor C. the constructor with the lowest overhead always executes first regardless of inheritance in subclasses D. the unified constructor always executes first regardless of inheritance

B. the superclass constructor always executes before the subclass constructor

What is an interface A. An interface is a collection of constants, method headers and/or default methods. B. An interface is a class that a child class can extend. C. An interface is a collection of abstract methods. D. An interface is the collection of public methods of a class.

C. An interface is a collection of abstract methods.

Can an abstract parent class have non-abstract children? A. Yes— all children of an abstract parent must be non-abstract. B. Yes— an abstract parent can have both abstract and non-abstract children. C. No—an abstract parent must have only abstract children. D. No—an abstract parent must have no children at all.

C. No—an abstract parent must have only abstract children.

Can an abstract method be defined in a non-abstract class? A. Yes—a method can be declared abstract in any parent as long as the child classes also declare it abstract. B. Yes—there is no restriction on where abstract methods can be defined. C. No—if a class defines an abstract method the class itself must be abstract. D. No—only classes are abstract, not methods.

C. No—if a class defines an abstract method the class itself must be abstract.

If a subclass constructor does not explicitly call a superclass constructor, ______. A. the superclass's fields will be set to the default values for their data types B. Java will automatically call the superclass's default or no-arg constructor immediately after the code in the subclass's constructor executes C. it must include the code necessary to initialize the superclass fields D. Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes

D. Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes

What is a method's signature? A. The signature of a method is the name of the method the type of its return value. B. The signature of a method is the name of the method and the names of its parameters. C. The signature of a method is the name of the method, its parameter list, and its return type. D. The signature of a method is the name of the method and the data types of its parameters.

D. The signature of a method is the name of the method and the data types of its parameters.

When an "is a" relationship exists between objects, the specialized object has ______. A. some of the characteristics of the general class, but not all, plus additional characteristics B. some, but not all, of the characteristics of the general object C. none of the characteristics of the general object D. all of the characteristics of the general object plus additional characteristics

D. all of the characteristics of the general object plus additional characteristics

You use the ________ operator to define an anonymous inner class. A. class B. inner C. anonymous D. new

D. new

Suppose Animal is an interface that specifies a single method - speak. Now suppose the Dog class implements the Animal interface. In addition to the speak method, the Dog class also has a method called wagTail. Now consider the following code. Animal a = new Dog(); a.wagTail(); Which of the following is true about this code? a) It will result in a compile-time error. b) It will result in a run-time error. c) It will call the speak method defined in the Animal interface. d) It will call the wagTail method defined in the Dog class. e) none of the above are true.

a) It will result in a compile-time error.

The process of inheritance should establish a(n) ____________________ relationship a. is-a b. has-a c. static d. not-a e. none of the above

a. is-a


Set pelajaran terkait

Chapter 6 Key Terms & Concepts - Sociological Theories

View Set

Chapter 14 The End of Life: Death, Dying, Grief and Loss

View Set

Life and Health Insurance Study Guide

View Set

La Celestina Intro - Spanish 4 Honors

View Set

Talent Development Ch. 1 Questions

View Set

Auditing the Revenue Cycle Day 10

View Set

F2: Determine the need for Behavior-Analytic Services

View Set

Management Test 2 Answers answered incorrectly

View Set

Chapter 13- Cardiovascular System

View Set