Test 3

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

What is the return value for xMethod(4) after calling the following method? static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); }

10

How many times is the factorial method in Listing 18.1 invoked for factorial(5)?

6

Which of the following statements are true? A. The Fibonacci series begins with 0 and 1, and each subsequent number is the sum of the preceding two numbers in the series. B. The Fibonacci series begins with 1 and 1, and each subsequent number is the sum of the preceding two numbers in the series. C. The Fibonacci series begins with 1 and 2, and each subsequent number is the sum of the preceding two numbers in the series. D. The Fibonacci series begins with 2 and 3, and each subsequent number is the sum of the preceding two numbers in the series.

A

What is a try/ catch block?

A try/ catch block is used to handle exceptions. The try block defines a set of statements that may lead to an error. The catch block basically catches the exception.

Which of the following statements are true? (Select All that Apply) A. You use the keyword throws to declare exceptions in the method heading. B. If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method. C. To throw an exception, use the key word throw. D. A method may declare to throw multiple exceptions.

A, B, C, D

Which of the following statements are true? (Select All that Apply) A. Recursive methods usually take more memory space than non-recursive methods. B. In some cases, however, using recursion enables you to give a natural, straightforward, simple solution to a program that would otherwise be difficult to solve. C. Recursive methods run faster than non-recursive methods. D. A recursive method can always be replaced by a non-recursive method.

A, B, D

Which of the following statements are true? (Select All that Apply) A. Infinite recursion can occur if recursion does not reduce the problem in a manner that allows it to eventually converge into the base case. B. Every recursive method must have a return value. C. Every recursive method must have a base case or a stopping condition. D. A recursive method is invoked differently from a non-recursive method. E. Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case.

A, C, E

You can use the methods in the Arrays class to ________. (Select All that Apply) A.do a binary search on an array B.shuffle an array C.find the maximum object in an array based on the compareTo method D.find the maximum object in an array using a Comparator object E.sort an array

A, E

What are Java exceptions?

An unwanted event that interrupts the normal flow of the program.

What exception type does the following program throw? public class Test { public static void main(String[] args) { System.out.println(1 / 0); } }

ArithmeticException

What exception does the following program throw? public class Test { public static void main(String[] args) { int[] a = new int[5]; for (int i = 0; i <= 5; i++) { a[i] = i; } } }

ArrayIndexOutOfBounds

Which of the following is not an advantage of Java exception handling? A. Java separates exception handling from normal processing tasks. B. Exception handling improves performance. C. Exception handling simplifies programming because the error-reporting and error-handling code can be placed at the catch block. D. Exception handling makes it possible for the caller's caller to handle the exception.

B

What exception type does the following program throw? public class Test { public static void main(String[] args) { Object o = new Object(); String d = (String)o; } }

ClassCastException

An instance of ________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully.

Error

An instance of ________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.

Exception

What are Queues?

First-in First-out (FIFO) collections, the first element inserted must be the first element removed.

What are stacks?

Last-in First-out (LIFO) collections, the last element added is the first removed.

What are some Queue examples?

Netflix, Spotify, YouTube, Online Gaming Lobby

What exception type does the following program throw? public class Test { public static void main(String[] args) { Object o = null; System.out.println(o); } }

No exception

What exception type does the following program throw? public class Test { public static void main(String[] args) { Object o = null; System.out.println(o.toString()); } }

NullPointerException

An instance of ________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.

RuntimeException

Which class do you use to read data from a text file?

Scanner

Analyze the following recursive method. public static long factorial(int n) { return n * factorial(n - 1); }

The method runs infinitely and causes a StackOverflowError.

What is a base case?

The smallest possible problem the functions can resolve, simply return the known answer

A Java exception is an instance of ________.

Throwable

What are some Stack Examples?

Undo (Ctrl-Z), Back button on browser

What is a general (recursive) case?

Where the problem is reduced before performing a recursive call, reducing to the point that it reaches the base case, breaking the recursive call chain.

What is wrong in the following program? public class Test { public static void main (String[] args) { try { System.out.println("Welcome to Java"); } } }

You cannot have a try block without a catch block or a finally block.

What is a recursive function?

a function that calls itself

The ________ method in the Queue interface retrieves, but does not remove, the head of this queue, throwing an exception if this queue is empty.

element()

Fill in the code to complete the following method for computing factorial. /** Return the factorial for a specified index */ public static long factorial(int n) { if (n == 0) // Base case return 1; else return ________; // Recursive call }

factorial(n - 1) * n

Fill in the code to complete the following method for computing a Fibonacci number. public static long fib(long index) { if (index == 0) // Base case return 0; else if (index == 1) // Base case return 1; else // Reduction and recursive calls return ________; }

fib(index - 1) + fib(index - 2)

What are the base cases in the following recursive method? public static void xMethod(int n) { if (n > 0) { System.out.print(n % 10); xMethod(n / 10); } }

n <= 0

In the following method, what is the base case? static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); }

n is 1

Which method can be used to read a whole line from the file?

nextLine

The following code causes Java to throw ________. int number = Integer.MAX_VALUE + 1;

no exceptions

The ________ method in the Queue interface retrieves and removes the head of this queue, or null if this queue is empty.

poll()

The ________ method in the Queue interface retrieves and removes the head of this queue and throws an exception if this queue is empty.

remove()


Kaugnay na mga set ng pag-aaral

List #2: North America/U.S. Geography Basics

View Set

Aggregate Demand and Aggregate Supply

View Set

Chapter 10: Security in Network Design

View Set

Appl in Exercise and Sports Science Ch 3

View Set

AP Psychology Unit 7: Cognition Part 2

View Set

SAFe Scrum Master Study Guide 5.0

View Set

Mr. Zarra- AP Enivro Energy Test (Pt 5)

View Set

Final Exam Review - Business Law

View Set

Principles of Accounting - D074 UNIT:6

View Set