Java Final Unit

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

True or false? The constructor of a subclass specifies a call to the constructor of the superclass using the name of the class.

false

True or false? The order in which catch blocks are listed is not important

false

True or false? Making a reference to an object that has not yet been instantiated would throw an exception from the NullPointerException class

true

True or false? On average, a sequential search searches half the list.

true

True or false? One way to handle an exception is to print an error message and exit the program

true

True or false? Selection sort swaps the smallest element in the unsorted portion of the list to a new position.

true

True or false? The block finally is always executed

true

True or false? The length of a list is the number of elements in the list.

true

True or false? The process of solving a problem by reducing it to smaller versions of itself is called recursion

true

True or false? To design a recursive method, you must determine the limiting conditions

true

True or false? A syntax error will result if the control statements of a for loop are omitted

False

True or false? Including a semicolon before the action statement in a one way selection causes a syntax error

False

After completing a particular recursive call, control goes back to the ___ ___

calling environment (which is the previous call)

A subclass can/cannot override the methods of a superclass

can

An abstract class can/cannot contain an abstract method(s)

can

The ___ of a catch block specifies the type of exception it handles

heading

A(n) ___ is a class that contains only abstract methods and/or named constants

interface

An ___is a class that contains only abstract methods and/or named constants

interface

Java allows a class to implement more than one ___. This is, in fact, the way to implement a form of multiple inheritance in Java

interface

Inheritance is an example of what type of relationship? a) is-a b) has-a c) was-a d) had-a

is-a

The classes to deal with number format exceptions and arithmetic exceptions such as division by zero are contained in the package ___

java.lang

The class InputMismatchException is contained in the package ___

java.util

Except for a few special cases, Java uses ___ binding for all methods

late

In ___ ___ a method's definition is associated with its invocation at execution time, that is, when the method is executed

late binding

A ___ is a set of elements of the same type.

list

True or False? An abstract method is a method that has only the heading with no body.

true

True or False? If a class is declared final, then no other class can be derived from this class.

true

True or False? In Java, polymorphism is implemented using late binding.

true

True or False? In multiple inheritance, the subclass is derived from more than one superclass.

true

True or False? The class Object is directly or indirectly the superclass of every class in Java.

true

True or false? To determine whether a given item is in an ordered list of length 1024, binary search makes at most 22 key comparisons.

true ( 2 * log base 2 of n = 22 when n is 1024)

Action events are handled by appropriately implementing the interface ___

ActionListener

If a negative value is used for an array index, what exception is thrown?

ArrayIndexOutOfBoundsException

The class Throwable is the superclass of the class ___

Exception

The class ___ is the superclass of the classes designed to handle exceptions

Exception

The class ___and its subclasses are designed to catch exceptions that should be caught and processed during program execution, and thus make a program more robust.

Exception

The exception class that you define extends the class ___ or one of its subclasses

Exception

The class FileReader is derived from the class ___

InputStreamReader

What happens in a method if there is an exception thrown in a try block but there is no catch block following the try block? a) The program ignores the exception. b) The program will not compile without a complete try/catch structure. c) The program terminates immediately. d) The program throws an exception and proceeds to execute the finally block.

The program throws an exception and proceeds to execute the finally block

The class ___ implements the interface WindowListener by providing empty bodies to the methods

WindowAdapter

If you want to provide your own code to terminate the program when the window is closed, or if you want the program to take a different action when the window closes, then you use the interface ____. a) WindowListener b) ActionListener c) CloseListener d) MouseListener

WindowListener

The class PrinterWriter is derived from the class ___

Writer

A(n) ___ method is a method that has only the heading, not the body. Moreover, the heading of a(n) ___ method is terminated with a semicolon

abstract

An abstract class that is declared with the reserved word ___ in its heading

abstract

The ___ ___ stops the recursion

base case

In ___ ___ a method's definition is associated with its invocation when the code is compiled

early binding

Consider the following definition of a recursive method. public static int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1); } What is the output of the following statement? System.out.println(recFunc(10));

10

If this list was sorted using selection sort, which two elements would be swapped first? {16, 30, 24, 7, 25, 62, 45, 5, 65, 50}

16 and 5 (on first iteration of selection sort, index 0 is swapped with the smallest element of array)

If the following list was sorted, what would be the middle element? {16, 30, 24, 7, 25, 62, 45, 5, 65, 50}

25 ({5, 7, 16, 24, 25, 30, 45, 50, 62, 65}) (on canvas the answer is 24)

Consider the following list. list = {16, 30, 24, 7, 25, 62, 45, 5, 65, 50}; Suppose that sequential search is used to determine whether 24 is in list. Exactly how many key comparisons are executed by the sequential search algorithm?

3 (sequential search makes key comparisons one by one down the list, 24 is the 3rd element)

If a binary search was used on this list, which element would the search element be compared to first? {4, 18, 29, 35, 44, 59, 65, 98}

35 (binary search looks at the middle element first)

Consider the following list. list = {20, 10, 17, 2, 18, 35, 30, 90, 48, 47}; Suppose that sequential search is used to determine whether 2 is in list. Exactly how many key comparisons are executed by the sequential search algorithm?

4

public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } What is the output of exampleRecursion(3)? a) 25 b) 32 c) 36 d) 42

42 ( 3 cubed + 2 cubed + 1 cubed = 27 +8 + 1 =36 )

Consider the following definition of a recursive method. public static int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1); } What is the output of the following statement? System.out.println(recFunc(8)); a) 8 b) 72 c) 720 d) none

720 ( 8 * 9 * 10 = 720)

Key events are handled by appropriately implementing the interface ___

KeyListener

The class ___ implements the interface MouseListenerby providing empty bodies to the methods

MouseAdapter

Mouse events are handled by appropriately implementing the interface ___

MouseListener

The class RuntimeException is the superclass of which of the following classes? a) NullPointerException b) NoSuchMethodException c) IllegalAccessException d) NoSuchFileException

NullPointerException

Which of the following exceptions might be thrown by the methods of the class String? a) NullPointerException b) FileNotFoundException c) NoSuchElementsException d) NumberFormatException

NullPointerException

The Java classes Reader and Writer are derived from the class ____.

Object

The class Throwable is derived from the class ___

Object

The class ___ directly or indirectly becomes the superclass of every class in Java

Object

The classes Scanner, Reader, and Writer are derived from the class ___

Object

The class InputStreamReader is derived from the class ___

Reader

Which class of exceptions is NOT checked? a) FileNotFoundException b) ArithmeticException c) RuntimeException d) All exceptions are checked

RuntimeException

Consider the following definition of a recursive method. public static int foo(int n) //Line 1 { //Line 2 if (n == 0) //Line 3 return 0; //Line 4 else //Line 5 return n + foo(n - 1); //Line 6 } Which of the statements represent the base case? a) statements in lines 3 and 4 b) statements in lines 5 and 6 c) statements in lines 3, 4, and 5 d) none of these

Statements in lines 3 and 4

Window events are handled by appropriately implementing the interface ___

WindowListener

To register a mouse listener object to a GUI component, you use the method ___. The mouse listener object being registered is passed as a parameter to the method ___.

addMouseListener

To register a window listener object to a GUI component, you use the method ___. The window listener object being registered is passed as a parameter to the method ___.

addWindowListener

In Java, the mechanism that allows you to combine data and operations on the data into a single unit is called a(n) ____.

algorithm

When is a finally block executed? a) Only when an exception is thrown by a try block b) Only when there are no exceptions thrown c) At the end of a program d) Always after the execution of a try block, regardless of whether or not an exception is thrown

always after the execution of a try block, regardless of whether or not an exception is thrown

To run a Java application program, a program called a(n) ____ loads the .class file into computer memory.

assembler

Consider the following list. list = {24, 20, 10, 75, 70, 18, 60, 35} Suppose that list is sorted using the insertion sort algorithm. What is the resulting list after two passes of the sorting phase; that is, after three iterations of the for loop? (type in letter for corresponding answer) a) list = {10, 18, 20, 24, 75, 70, 60, 35} b) list = {10, 20, 24, 75, 70, 18, 60, 35} c) list = {10, 18, 20, 24, 35, 70, 60, 75} d) list = {10, 20, 20, 18, 35, 70, 60, 75}

b

If class Dog has a subclass Retriever, which of the following is true? (type in the letter of option choice) a) Because of single inheritance, Dog can have no other subclasses b) Because of single inheritance, Retriever can extend no other class except Dog c) The relationship between these classes implies that Dog "is-a" Retriever d) The relationship between these classes implies that Retriever "has-a" Dog

b

True or false? To design a recursive method, you must identify the ___ cases and provide a direct solution to each ___ case

base

The solution to a problem in a ___ ___ is obtained directly

base case

Consider the following list. list = {24, 20, 10, 75, 70, 18, 60, 35} Suppose that list is sorted using the selection sort algorithm. What is the resulting list after two passes of the sorting phase; that is, after two iterations of the outer for loop? (type in letter for corresponding answer) a) list = {10, 18, 24, 20, 75, 70, 60, 35} b) list = {10, 18, 20, 24, 75, 70, 60, 35} c) list = {10, 18, 24, 75, 70, 20, 60, 35} d) list = {10, 20, 24, 75, 70, 20, 60, 35}

c

An abstract class can/cannot contain instance variables, constructors, a finalizer, and nonabstract methods

can

An abstract class can/cannot be instantiated

cannot

You can/cannot automatically consider a superclass object to be an object of a subclass

cannot

You can/cannot automatically make a reference variable of a subclass type point to an object of a superclass type

cannot

An abstract class ____ a) does not have any subclasses b) is a superclass with many subclasses c) cannot be instantiated d) is the base class of all other classes

cannot be instantiated

A ___ block specifies the type of exception it can catch and contains an exception handler

catch

A try block is followed by zero or more ___ blocks

catch

Any exception that can be recognized by the compiler is called a ___ exception

checked

Java's predefined exceptions are divided into two categories: ___ and ___ exceptions

checked and unchecked

An exception that can be recognized by the compiler is a(n) ___

checked exception

What is the correct syntax for defining a new class Parakeet based on the superclass Bird?

class Parakeet extends Bird {} (class className extends superclassName {} )

In ___, one or more members of a class are objects of one or more other classes

composition (aggregation)

___ is a "has-a" relationship

composition (aggregation)

A method is ___ ___ if it calls itself

directly recursive

Generally, ___ classes are placed in the package that contains the methods that throw these exceptions

exceptions

If you define a class and do not use the reserved word ___ to derive it from an existing class, then the class you define is automatically considered to be derived from the class ___

extends, Object

A binary search is faster on ordered lists and slower on unordered lists

false

True or False? Interfaces are defined using the reserved word interface and the reserved word class.

false

True or False? Redefining a method of a superclass is also known as overloading a method

false

True or False? The private members of a superclass can be accessed by a subclass.

false

True or False? To override a public method of a superclass in a subclass, the corresponding method in the subclass must have the same name but a different number of parameters.

false

True or false? A GUI component can generate only one type of event

false

True or false? A binary search can be used on an unsorted list.

false

True or false? A list is a set of related values that do not necessarily have the same type.

false

True or false? A selection sort always starts with the middle element of the list

false

True or false? A sequential search is faster than a binary search on sorted lists.

false

True or false? A sequential search of a list assumes that the list is in ascending order

false

True or false? A subclass must define a constructor

false

True or false? All exceptions must be reported to avoid compilation errors

false

True or false? An unchecked exception is any exception that causes a program to terminate

false

True or false? On average, the number of comparisons made by a sequential search is equal to one-third the size of the list.

false

True or false? Suppose that you have the following list. int[] list = {1, 3, 5, 7, 9, 11, 13, 15, 17}; Further assume that binary search is used to determine whether 8 is in list. When the loop terminates, the value of the index variable first is 1.

false

True or false? The constructor of a subclass specifies a call to the constructor of the superclass in the heading of the constructor's definition.

false

True or false? The order in which catch blocks are placed in a program has no impact on which catch block is executed.

false

True or false? The try block contains statements that should be executed regardless of whether or not an exceptions occurs.

false

True or false? a recursive method always returns a value.

false

True or false? Java does not allow users to create and implement their own exception classes.

false (Java does allow users to create their own exception classes)

True or false? The general case stops the recursion.

false (base case is where the recursion stops)

True or false? In the general case, the solution to the problem is obtained directly.

false (base case is where the solution to the problem is obtained directly)

True or false? File not found is an unchecked exception

false (it is checked)

True or false? Division by zero is a checked exception

false (it is unchecked)

True or false? A binary search can be used on an unsorted list

false (it must be sorted for binary search)

True or false? Every recursive definition can have zero or more base cases

false (one or more base cases)

True or false? The methods getMessage and printStackTrace are private methods of the class Throwable.

false (they are Public methods of the class Throwable)

In general, a binary search is much ___ than a sequential search

faster

The code contained in the ___ block always executes, regardless of whether or not an exception occurs (except when the program exits early from a __ block by calling the method System.exit

finally

The last catch block may or may not be followed by a ___ block

finally

A sequential search algorithm searches the list for a given item, starting with the ___ element in the list.

first

The MouseListener interface defines how many methods?

five

True or false? To design a recursive method, you must identify the ___ case(s) and provide a solution to each ___ case in terms of a smaller version of itself

general

The ___ ___ must eventually be reduced to a base case

general case

The ___ ___ breaks a problem into smaller versions of itself

general solution

The method ___ returns the string containing the detailed message stored in the exception object.

getMessage

An abstract method ___ a) is any method in the abstract class b) cannot be inherited c) has no body d) is found in a subclass and overrides methods in a superclass using the reserved word abstract

has no body

A method that calls another method and eventually results in the original method call is ___ ___

indirectly recursive

___ is an "is-a" relationship

inheritance

The ___ ___ algorithm sorts the list by inserting each element in its proper place

insertion sort

The ___ operator is used to determine if an object is of a particular class type

instanceof

A method is recursive if it calls ___

itself

The classes to deal with I/O exceptions, such as the file not found exception, are contained in the package ___

java.io

The class Exception is contained in the package ___

java.lang

What are the three exception handling techniques?

log the error and continue, fix the error and continue, terminate the program (LFT)

In ___ ___, a subclass is derived from more than one superclass. Java does not support this. (in Java, a class can only extend the definition of one class)

multiple inheritance

An exception is a(n) ___ of a specific exception class

object

When an exception occurs an ___ of a specific exception class is created

object

Every recursive algorithm has ___ or more base cases

one

Every recursive definition has ___ or more base cases

one

Every recursive method must have ___ or more base cases

one

A ___ ___ ___ is a convenient data structure for storing and processing lists

one dimensional array

Consider the following class definitions. public class BClass { private int x; public void set(int a) { x = a; } public void print() { System.out.print(x); } } public class DClass extends BClass { private int y; public void set(int a, int b) { //Postcondition: x = a; y = b; } public void print(){ } } Which of the following correctly redefines the method print of DClass? (i) public void print() { System.out.print(x + " " + y); } (ii) public void print() { super.print(); System.out.print(" " + y); }

only ii

The term ___ means assigning multiple meanings to the same method. In Java, ___ is implemented using late binding

polymorphism

The method ____ is used to determine the order in which the methods were called and where the exception was handled

printStackTrace

The ___ members of a superclass are ___ to the superclass. The subclass cannot directly access them.

private

For a superclass to give direct access to its member(s), to its subclass(es), and still prevent its direct access outside the class, such as in a user program, you must declare that member using the modifier ___

protected

The methods getMessage, printStackTrace, and toString of the class Throwable are ___ and so ___ inherited by the subclasses of the class Throwable.

public, are

A ___ algorithm solves a problem by reducing it to smaller versions of itself

recursive

A ___ definition defines the problem in terms of smaller versions of itself

recursive

Recursive algorithms are implemented using ____ methods

recursive

Logically, you can think of a ___ ___ as having unlimited copies of itself

recursive method

A ___ ___ of a class can refer to either an object of its own class or an object of its subclass

reference variable

public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } What does the code in the code above do? a) returns the cube of the number n b) returns the sum of the cubes of the numbers, 0 to n c) returns three times the number n d) returns the next number in a fibonacci sequence

returns the sum of the cubes of the numbers, 0 to n

In a ___ ___ a list is sorted by selecting elements in the list, one at a time, and moving them to their proper positions

selection sort

This algorithm finds the location of the smallest element in the unsorted portion of the list and moves it to the top of the unsorted portion (i.e., the whole list) of the list.

selection sort

In ___ ___, the subclass is derived from only one existing class, called the superclass

single inheritance

What type of inheritance does Java support? a. single inheritance b. double inheritance c. multiple inheritance d. Java does not support inheritance

single inheritance

If the ___ does not override a public method of the superclass, you can specify a call to that public method by using the name of the method and an appropriate parameter list.

subclass

When a subclass overrides the method of a superclass, the redefinition is only available to objects of the ___

subclass

If the subclass overrides a public method of a superclass, then you specify a call to that public method of the superclass by using the reserved word ___, followed by the dot operator, followed by the method name with an appropriate parameter list

super

While writing the definition of a constructor of a subclass, a call to a constructor of the superclass is specified using the reserved word ___ with an appropriate parameter list. (the call to a constructor of the superclass must be the first statement)

super

A recursive method in which the last statement executed is the recursive call is called a ___ ___ ___

tail recursive method (TRM)

Which operator is used to determine if an object is of a particular class type? a) The operator new b) The dot(.) operator c) The instanceof operator d) The + operator

the instanceof operator

Consider the following class definitions. public class BClass { private int x; private double y; public void print() { } } public class DClass extends BClass { private int a; private int b; public void print() { } } Suppose that you have the following statement. DClass dObject = new DClass(); How many instance variables does dObject have?

three

Which of the following is NOT a method of the class Throwable? a) getMessage b) throwMessage c) printStackTrace d) toString

throwMessage

The method ___ (in class Exception) returns the detailed message stored in the exception object as well as the name of the exception class.

toString

A binary search of a list assumes that the list is in sorted order

true

True or False? To determine whether a reference variable that points to an object is of a particular class type, Java provides the operator instanceof.

true

True or false? A catch block can catch either all exceptions of a specific type or all types of exceptions

true

True or false? A reference variable of a superclass type can point to an object of a subclass type

true

True or false? An abstract method has no body.

true

True or false? An event handler is a method

true

True or false? An exception can be caught either in the method where it occurred or in any one of the methods that led to the invocation of this method

true

True or false? During program execution, if division by zero occurs with integer values and is not addressed by the program, then the program terminates with an error message indicating an attempt to divide by zero.

true

True or false? Every recursive definition must have one or more base cases.

true

True or false? Every recursive method must have one or more base cases.

true

True or false? Exceptions are thrown either in a try block in a method or from a method called directly or indirectly from a try block

true

True or false? If a class contains an abstract method, then the class must be declared abstract

true

True or false? If a try block is not followed by a catch block, then it must have a finally block.

true

True or false? If an exception occurs in a try block and that exception is caught by a catch block, then the remaining catch blocks associated with that try block are ignored.

true

True or false? In Java, polymorphism is implemented using late binding

true

True or false? In a class hierarchy several methods can have the same name and the same formal parameter list

true

True or false? Java provides extensive support for exception handling by providing several exception classes.

true

True or false? To design a recursive method, you must understand the problem requirements

true

True or false? To handle window events, you first create a class that implements the interface WindowListener and then create and register objects of that class

true

True or false? When an exception occurs in a method, you can use the method printStackTrace to determine the order in which the methods were called and where the exception was handled.

true

True or false? You can instantiate an object of a subclass of an abstract class, but only if the subclass gives the definitions of all the abstract methods of the superclass

true

True or false? You cannot instantiate an object of an abstract class. You can only declare a reference variable of an abstract class type

true

The ___ block contains statements that should not be executed if an exception occurs

try

statements that may generate an exception are placed in a ___ block

try

the ___ / ___ / ___ block is used to handle exceptions within a program

try catch finally (TCF)

How many constructors does the class Exception have?

two

Inheritance and composition(aggregation) are meaningful ways to relate ___ or more classes

two

The class Exception contains ___ constructors.

two

___ exceptions are exceptions that are not recognized by the compiler

unchecked


Kaugnay na mga set ng pag-aaral

Career Planning and Skill Development Unit 1 Lesson 2: Career Planning

View Set

POST First Responder First Aid/CPR/AED

View Set

BARBARISMES SOBRE L'ALIMENTACIÓ

View Set