Comp sci 1020 Practice Final Exam

Ace your homework & exams now with Quizwiz!

What type of access does the use of an iterator provide for the elements of a bucket in a hash table? A) sequential B) semi-random C) sorted D) random

A) sequential

With a few exceptions, what access should instance variables of classes always have? A) final B) private C) public D) protected

B) private

Which of the following statements declares Salaried as a subclass of PayType? A) public class Salaried derivedFrom(PayType) B) public class Salaried extends PayType C) public class PayType derives Salaried D) public class Salaried implements PayType

B) public class Salaried extends PayType

Another name for linear search is ____ search. A) random B) sequential C) sorted D) binary

B) sequential

Consider the swap method shown below from the SelectionSorter class. If we modified it as shown in the swap2 method shown below, what would be the effect on the sort method? private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } private static void swap2(int[] a, int i, int j) { a[i] = a[j]; a[j] = a[i]; } A) Some array elements would be overwritten. B) It would sort the array in reverse order. C) It would still be correct, but run a little faster. D) There would be no effect.

A) Some array elements would be overwritten.

Consider the following code snippet for calculating Fibonacci numbers recursively: int fib(int n) { // assumes n >= 0 if (n <= 1) { return n; } else { return (fib(n - 1) + fib(n - 2)); } } Identify the terminating condition in this recursive method. A) n < 1 B) n <= 1 C) fib(n - 1) D) fib(n - 1) + fib(n - 1)

B) n <= 1

Protected class members can be denoted in a UML diagram with the ________ symbol. A) * B) # C) + D) -

B) #

When the reserved word super is followed by a parenthesis, what does it indicate? A) A call to a superclass method. B) A call to a superclass constructor. C) A call to a subclass method. D) A call to a subclass constructor.

B) A call to a superclass constructor.

n a UML diagram, dependency is denoted by ____. A) A dotted/dashed line with a closed arrow tip. B) A dotted/dashed line with an open arrow tip. C) A solid line with an open arrow tip. D) A solid line with a closed arrow tip.

B) A dotted/dashed line with an open arrow tip.

Which process is recommended for calculating the elapsed running time of an algorithm? A) Use a physical stopwatch to record the length of time it takes to run the program. B) Calculate the difference obtained by calls to the method System.currentTimeMillis() just before the start of the algorithm and just after the end of the algorithm. C) Calculate the difference obtained by calls to the method System.currentTimeMillis() at the start of the program and at the end of the program so that the elapsed time includes the display of the result. D) Use the value returned by the method System.currentTimeMillis() just after the end of the algorithm as the elapsed time.

B) Calculate the difference obtained by calls to the method System.currentTimeMillis() just before the start of the algorithm and just after the end of the algorithm.

Before you begin designing a solution, you should ____. A) Document the methods that will be needed. B) Gather and document all requirements for the program in plain English. C) Determine the classes that will be needed. D) Create the UML diagrams to represent the classes.

B) Gather and document all requirements for the program in plain English.

If a call to the Arrays static method binarySearch returns a value of -10, what can be concluded? I the element is not in the array II the element is at index 10 III the element can be inserted at index 9 A) II only B) I and III only C) III only D) I only

B) I and III only

Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt. public static void main(String[] args) __________________ { String inputFileName = "dataIn.txt"; File inputFile = new File(inputFileName); Scanner in = new Scanner(inputFile); . . . } A) finally FileNotFoundException B) throws FileNotFoundException C) extends FileNotFoundException D) catches FileNotFoundException

B) throws FileNotFoundException

When you make a copy of the aggregate object and of the objects that it references, ________. A) you are performing a nested copy B) you are performing a deep copy C) you are performing a shallow copy D) a compiler error will occur

B) you are performing a deep copy

What is the time required to iterate over all elements in a hash table of size n? A) O(n2) B) O(1) C) O(log(n)) D) O(n)

D) O(n)

Consider the recursive method myPrint shown in this code snippet: public void myPrint(int n) { if (n < 10) { System.out.print(n); } else { int m = n % 10; System.out.print(m); myPrint(n / 10); } } What does this method do? A) Prints a positive int value forward, digit by digit. B) Divides the int by 10 and prints out the result. C) Divides the int by 10 and prints out its last digit. D) Prints a positive int value backward, digit by digit.

D) Prints a positive int value backward, digit by digit.

When the reserved word super is followed by a period and a method name, what does it indicate? A) A call to a superclass method. B) A call to a superclass constructor. C) A call to a subclass method. D) A call to a subclass constructor.

A) A call to a superclass method.

Which of the following classifications of method behavior is accurate? A) Methods that change parameter variables always have side effects. B) Methods that change parameter variables do not have side effects. C) Methods that do not change parameter variables never have side effects. D) Methods that do not change parameter variables always have side effects.

A) Methods that change parameter variables always have side effects.

Suppose we maintain a linked list of length n in random element order. What would be the big-Oh notation for an algorithm that prints each list element and the number of times it occurs in the list (without sorting the list)? A) O(n log n) A) O(n2) A) O(1) A_ O(n)

A) O(n2)

Using the textbook's implementation of a linked list, which of the following statements about changing the data stored in a previously visited element is correct? A) The node that will be updated is the most recently visited node. B) The position.next reference must be updated when the data is updated. C) The set method can be called again immediately after calling the add method. D) The previous reference must be updated when the node is updated.

A) The node that will be updated is the most recently visited node.

What is the name of a class that has only accessor methods and no mutators methods? A) Static B) Immutable C) Utility D) Actor

B) Immutable

Consider the following code snippet: public interface Measurable { double getMeasure(); ____________ boolean largerThan(Measurable other) { return getMeasure() > other.getMeasure(); } } Which of the following completes the interface declaration correctly? A) final B) private C) default D) public

C) default

Suppose an algorithm requires a total of 3n^3 + 2n^2 - 3^n + 4 visits. In big-Oh notation, the total number of visits is of what order? A) n^2 * n^2 B) n^2 C) n^3 D) n^6

C) n^3

The actions performed by the JVM that take place with each method call are sometimes referred to as ________. A) allocation B) overflow C) overhead D) retention

C) overhead

Consider the following code snippet, assuming that description is a String and totalPrice is a double: System.out.printf("%-12s%8.2f",description,totalPrice); Which of the following statements is correct? A) This code will produce 2 columns, with the description field left justified and the totalPrice field left justified. B) This code will produce 2 columns, with the description field right justified and the totalPrice field left justified. C) This code will produce 2 columns, with the description field right justified and the totalPrice field right justified. D) This code will produce 2 columns, with the description field left justified and the totalPrice field right justified.

D) This code will produce 2 columns, with the description field left justified and the totalPrice field right justified.

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(5); B) file.seek(10); C) file.seek(4); D) file.seek(8);

D) file.seek(8);

If you don't provide an access specifier for a class member, the class member is given ________ access by default. A) protected B) private C) public D) package

D) package

On average, how many elements of an array list of size n need to be moved when an element is added? A) n B) 2n C) n / 2 D) n2

C) n / 2

What technique is used to store elements that hash to the same location? A) sharing B) colliding C) deletion D) bucketing

D) bucketing

The RandomAccessFile class treats a file as a stream of ________. A) data B) integers C) characters D) bytes

D) bytes

Assume that you have a hash table in which there are an average number of collisions. What is the time required to remove an element from this hash table? 1) O(1)+ 2) O(n2) 3) O(1) 4) O(n)

1) O(1)+

Which of the following is the most important consideration when designing a class? A) Each class should represent an appropriate mathematical concept. B) Each class should represent a single concept or object from the problem domain. C) Each class should represent no more than three specific concepts. D) Each class should represent multiple concepts only if they are closely related.

B) Each class should represent a single concept or object from the problem domain.

Which of the following is an example of an immutable class? A) Scanner B) String C) Math D) System

B) String

The method below is designed to return the smaller of two Comparable objects received as arguments. Assume that the objects are instances of the same class. Select the correct expression to complete the method. public static Comparable smaller(Comparable value1, Comparable value2) { if (_________________________ ) return value1; else return value2); } A) value1.compareTo(value2) > 0 B) value1.compareTo(value2) == 0 C) value1.compareTo(value2) < 0 D) value1 < value2

C) value1.compareTo(value2) < 0

Insert the missing code in the following code fragment. This fragment is intended to write an output file named dataOut.txt that resides in a folder named reports on the C: drive of a Windows system. public static void main(String[] args) throws IOException { PrintWriter outputFile = _______; . . . } A) new PrintWriter("c:\\reports\\dataOut.txt") B) new PrintWriter("c:/reports/dataOut.txt") C) new PrintWriter("c://reports//dataOut.txt") D) new PrintWriter("c:\reports\dataOut.txt")

A) new PrintWriter("c:\\reports\\dataOut.txt")

If a method in a subclass has the same signature as a method in the superclass, the subclass method ________ the superclass method. A) overrides B) inherits C) overloads D) implements

A) overrides

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? A) 3 B) 4 C) 5 D) 6

C) 5

How large does n need to be so 0.3n2 is greater than 2.5n - 3? A) 9 B) 5 C) 7 D) 3

C) 7

Which of the following statements about a mock class is true? A) A mock class does not provide an implementation of the services of the actual class. B) A mock class must be an interface. C) A mock class provides a simplified implementation of the services of the actual class. D) A mock class provides a complete implementation of the services of the actual class.

C) A mock class provides a simplified implementation of the services of the actual class.

Which statement(s) about recursion are true? I Recursion is faster than iteration. II Recursion is often easier to understand than iteration. III Recursive design has an economy of thought. A) I only B) II only C) II and III only D) I and III only

C) II and III only

In the hierarchy of Exception classes, the NumberFormatException class is a subclass of the ____ class. A) ClassCastException B) IllegalStateException C) IllegalArgumentException D) ArithmeticException

C) IllegalArgumentException

The catch clause ________. A) starts with the word catch followed by a parameter list in parentheses containing an ExceptionType parameter variable B) follows the try clause C) contains code to gracefully handle the exception type listed in the parameter list D) The catch clause does all of these

D) The catch clause does all of these

Consider the getArea method from the textbook shown below. public int getArea() { if (width <= 0) { return 0; } // line #1 else if (width == 1) { return 1; } // line #2 else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } } Where is/are the terminating condition(s)? A) line #2 B) line #4 C) line #1 D) lines #1 and #2

D) lines #1 and #2

All methods specified by an interface are ________. A) static B) private C) protected D) public

D) public

The final step of the design phase recommended by the textbook is to ____. A) Write the documentation of the discovered classes and methods B) Write the code for each class and all of its methods. C) Identify the classes and methods that will be required. D) Create the UML diagrams to represent the classes.

A) Write the documentation of the discovered classes and methods

The JVM periodically performs the ________ process to remove unreferenced objects from memory. A) garbage collection B) memory sweeping C) memory shuffling D) system restore

A) garbage collection

An exception object's default error message can be retrieved using the ________ method. A) getMessage B) getDefaultMessage C) getDefaultErrorMessage D) getErrorMessage

A) getMessage

Suppose you are developing a payroll application that computes and displays weekly paycheck amounts for various employees. As a result of the design phase, the partial Employee class below is developed. Select the method header that best completes the class, according to the method comments. public class Employee { private double hourlyRate; private int hoursWorked; /** Modifies the hourly rate for this employee. @param newHourlyRate the rate per hour earned by the employee */ _________________________________________ { // method body } } A) public void changeRate(double newHourlyRate) B) public void changeRate(double newHourlyRate, int hoursWorked) C) public double changeRate(double newHourlyRate) D) public double changeRate(double newHourlyRate, int hoursWorked)

A) public void changeRate(double newHourlyRate)

Which of the following types of methods are invoked on objects? A) Either static or instance methods B) Static methods C) Class methods D) Instance methods

D) Instance methods

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

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

Suppose we maintain a linked list of length n in sorted order. What would be the big-Oh notation for printing out those elements that occur exactly once in the list? A) O(n log n) B) O(1) C) O(n) D) O(n2)

C) O(n)

If many classes of a program depend on each other, we say that ____. A) cohesiveness is high. B) cohesiveness is low. C) coupling is high. D) coupling is low.

C) coupling is high.

When a field is declared static there will be ________. A) two reference copies of the field for each method in the class B) a copy of the field in each class object C) only one copy of the field in memory D) a copy of the field for each method in the class

C) only one copy of the field in memory

Which of the following is true about protected access? A)Protected members may be accessed by methods in the same package or in a subclass, but only if the subclass is in the same package. B)Protected members are actually named constants. C)Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package. D)Protected members cannot be accessed by methods in any other classes.

C)Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package.

Which of the following problems can be solved recursively? A) binary search B) greatest common denominator C) Towers of Hanoi D) All of these.

D) All of these.

In Java it is possible to write a method that will return ________. A) a reference to an object B) a string of characters C) a whole number D) Any of these

D) Any of these

When does quicksort's worst-case run-time behavior occur? I when the data is randomly initialized in the array II when the data is in ascending order III when the data is in descending order A) III only B) I only C) II only D) II and III only

D) II and III only

Which of the sorts in the textbook can be characterized by the fact that even in the worst case the running time will be O(n log(n)))? I quicksort II selection sort III merge sort A) I and III only B) I only C) II only D) III only

D) III only

________ occurs when method A calls method B which in turn calls method A. A) Dynamic recursion B) Linear recursion C) Direct recursion D) Indirect recusion

D) Indirect recusion


Related study sets

Biology Ch 16 HUMAN ANATOMY AND PHYSIOLOGY II

View Set

Chapter 29: Management of Patients With Complications from Heart Disease

View Set

Lesson 8: Civil Rights Unit Test

View Set