CSCI-C201 Final Exam

Ace your homework & exams now with Quizwiz!

A(n) __________ is an action that takes place in an application, such as the clicking of a button.

event

Programs that operate in a GUI environment must be __________.

event driven

Given the following recursive function z, what happens when the following two function calls are made. Indicate the line number that made the call. int z(int k, int n) { L1 if (k == n) L2 return k L3 else { L4 if (k < n) L5 return z(k, n-k); L6 else L7 return z(k-n, n); } } z(6,-1) = ? z(6,8) = ?

1. It will fall into line 7 every time and recursively call it infinitely. It will make k larger every time because n is a negative number. 2. The answer is 2 because k will equal 2 at the end of the program. The first time it will fall into line 5 and it will call z again with (6,2). The second time it will fall into line 7 and it will call z again with (4,2). The third time it will fall into line 7 and calls z again with (2,2). The last time it will fall into line 2 and return k which equals 2.

In the following code, assume that inputFile references a Scanner object that has been successfully used to open a file: double totalIncome = 0.0;while (inputFile.hasNext()){ try{ totalIncome += inputFile.nextDouble(); }catch(InputMismatchException e){ System.out.println("Non-numeric data encountered " + "in the file."); inputFile.nextLine(); }finally{ totalIncome = 35.5; }} What will be the value of totalIncome after the following values are read from the file? 2.58.53.05.5abc1.0 35.5

35.5

Given the following code that uses recursion to find the factorial of a number, how many times will the else clause be executed if n = 5? private static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }

5

When would you choose recursion over an iterative solution?

A recursive approach would be best if you are interested in a simpler design. You may also use recursion if the code is too complex for an iterative solution. An iterative solution would be better if you are interested in efficiency.

What is the difference between direct and indirect recursion? Give an example.

Direct recursion is when methods directly call themselves. An example of direct recursion would be when method A calls method A. Indirect recursion is when a method calls another method which will call the original method. An example of indirect recursion is when method A calls method B, which in turn calls method A.

If an exception is thrown and the program does not handle it, what happens?

If an exception is thrown and the program does not handle it, it is thrown up to calling method. It is handled by the default exception handler which will print an error message and crash the program.

If you set a scene's size to a width and height that is smaller than the width and height of the image to be displayed, What happens?

If you set a scene's size to a width and height that is smaller than the width and height of the image to be displayed, the image will be cut off and only partially displayed.

Assume that the classes BlankISBN, NegativePrice, and NegativeNumberOrdered are exception classes that inherit from Exception. The following code is a constructor for the Book class. What must be true about any method that instantiates the Book class with this constructor?

It must handle all of the possible exceptions thrown by the constructor or have its own throws clause specifying them.

What is Random Access File? Explain the I/O operations of Random Access Files.

Random Access File is a file that allows a program to jump to any location in the file. Items in a Random Access File are accessed in any order. The Random Access File class treats a file as a stream of bytes. Bytes are numbered, starting at 0, similar to an array. Java accesses the file through a file pointer. When the file is first opened, the file pointer is set to 0. When an item is read it reads from that byte. This allows data to be read and written at any byte location in the file.

What will be the result of the following statements? FileInputStream fstream = new FileInputStream("Input.dat"); DataInputStream inFile = new DataInputStream(fstream);

The DataInputStream object is stored in the variable called inFile. The inFile variable will reference an object that is able to read binary data from the file.

When an exception is thrown by code in its try block, the JVM begins searching the try statement for a catch clause that can handle it and passes control of the program to _____________.

The first catch clause that is able to handle the exception

What is the purpose of Serializable interface? How one could use it?

The purpose of the serializable interface is to allow for objects to be saved easier. When an object is serialized, it is converted into a series of bytes that contain the object's data. The resulting set of bytes can be saved to a file for later retrieval. You can use the serializable interface by implementing it into your Java class.

What does the following statement do? Image puppy = new Image("file:C:\\images\terrier.jpg");

This statement will load terrier.jpg from the hard drive into memory and into the puppy variable.


Related study sets

Medical Terminology - Chapter 1 Quiz - Introduction

View Set

Introduction to Networks (Version 6.00) - ITN Chapter 11 Exam

View Set

John Locke: The Second Treatise on Civil Government

View Set

CMS 2 Assignment 7: Designing Pay Levels, Mix, and Pay Structures

View Set

Fyzika MO - mechanika, kmitání, akustika

View Set

Audit Chapter 7 concepts (material)

View Set