CS 030 Final Review

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the nickname for the graphical user interface library in Java? Select one: a. GUI b. Swing c. JComponent d. Applet

B

Adding an element to a balanced binary search tree takes ____ time. Select one: a. O(1) b. O(n) c. O(n2) d. O(log (n))

D

A method header consists of which of the following parts? Select one: a. an access specifier, a return type, a method name, and a list of the parameters (if any) b. an access specifier, the type of the instance variable, and the name of the instance variable c. the return type, the name of the method, and a list of the parameters (if any) d. the type of the instance variable, an access specifier, and a list of the parameters (if any)

A

A method name is ____________________ if a class has more than one method with that name (but different parameter types). Select one: a. overloaded b. overimplemented c. overridden d. overwhelmed

A

A portion of your program implements a loop over n elements in which each step contains a fixed number of actions. What can you conclude about the running time of this section of code? Select one: a. Its running time will be O(n). b. Its running time will be O(log (n)). c. Its running time will be O(n log (n)). d. Its running time will be O(n2).

A

Assume the class Circle has an accessor called getRadius and a mutator called setRadius. What is the output of the following code? Circle c1 = new Circle(3); Circle c2 = c1; c1.setRadius(4); System.out.println(c2.getRadius()); Select one: a. 4 b. 3 c. 8 d. 6

A

Consider an array with n elements. If we visit each element n times, how many total visits will there be? Select one: a. n^2 b. 2^n c. n d. n^n

A

Find the simplest order of growth of the following expression: (n^3 + n + 3)^2. Select one: a. θ(n^6) b. θ(2^n) c. θ(n^5) d. θ(n^9)

A

Given the following constructor for the BankAccount class, which references the instance variable balance declared to be of type double, what output is generated by a call to new BankAccount()? public BankAccount() { System.out.println(balance); } Select one: a. 0.0 b. 1000.0 c. You cannot print out the value of an uninitialized instance variable. d. The code fragment has a syntax error and does not compile.

A

In Big-Oh notation, selection sort is a(n) ____ algorithm. Select one: a. O(n^2) b. O(log n2) c. O(1) d. log n

A

In a _____________, a set of cooperating methods calls each other repeatedly. Select one: a. mutual recursion b. permutation c. stack overflow error d. infinite recursion

A

Recursion does NOT take place if any of the following happen: I method A calls method B, which calls method C, which calls method B II method A calls method B, which calls method A III method A calls method B, B returns, and A calls B again Select one: a. III only b. II only c. I and II only d. I only

A

The black boxes from which a program is manufactured are called ___. Select one: a. objects b. access specifiers c. methods d. instance variables

A

The line public class HelloPrinter indicates which declaration below? a. Declaration of the class HelloPrinter. b. Declaration of the variable public. c. Declaration of the class public. d. Declaration of the variable class.

A

We want to create a class that represents a geometric sequence. A geometric sequence is a sequence of numbers that begin at some value and then multiplies each value by some constant to get the next value. For example, the geometric sequence 1, 2, 4, 8, 16 starts at 1 and multiplies each term by 2 to get the next. The geometric sequence 10.8, 5.4, 2.7, 1.35 starts at 10.8 and multiplies each term by 0.5 to get the next. The basic framework of a geometric sequence class is below: public class GeometricSequence { private double currentValue; private double multiplier; } We want to create a geometric sequence using code like: GeometricSequence first = new GeometricSequence (1, 2); // Creates 1, 2, 4, 8, 16... GeometricSequence second = new GeometricSequence (10.8, 0.5); // Creates 10.8, 5.4, 2.7, 1.35 ... We want to produce elements of the geometric sequence using code code like: System.out.println (first.next()); // Prints 1 and advances System.out.println (first.next()); // Prints 2 and advances System.out.println (first.next()); // Prints 4 and advances System.out.println (first.next()); // Prints 8 and advances System.out.println (second.next()); //Prints 10.8 and advances System.out.println (second.next()); //Prints 5.4 and advances System.out.println (second.next()); //Prints 2.7 and advances What should the body of the next method be? Select one: a. double result = currentValue; currentValue = currentValue * multiplier; return result; b. double result = currentValue; multiplier = currentValue * multiplier; return result; c. currentValue = currentValue * multiplier; return currentValue; d. return currentValue; currentValue = currentValue * multiplier;

A

What is the argument in the given method call? System.out.println("Welcome"); Select one: a. "Welcome" b. println c. System d. out

A

What is the name of the = operator in Java? Select one: a. assignment b. identity c. equality d. inequality

A

What is wrong with the following code snippet? int average; average = 78A; Select one: a. The average variable is assigned a non-numeric value. b. The data type for the average variable is not specified. c. The average variable is never initialized. d. The average variable is never assigned a value.

A

What term is used to refer to an individual instruction inside a method? a. statement b. comment c. object d. constant

A

What term is used to refer to an informal description of a sequence of steps for solving a problem? Select one: a. pseudocode b. assembly language instructions c. Java virtual machine instructions d. machine instructions for a specific CPU

A

What will be the value stored in the variable x after the execution of the following code snippet? int a = 10; int b = 20; int c = 2; int x = b / a /*c*/; Select one: a. 2 b. The code has a syntax error c. 1 d. 4

A

When a recursive method is called correctly, and it does not perform recursion, what must be true? Select one: a. A terminating condition was true. b. One recursive case condition was true. c. All recursive case conditions were true. d. Both a terminating condition and a recursive case condition were true.

A

When tracing the execution of an object by hand, what is one way to indicate the change of an instance variable when a mutator method is executed? Select one: a. Cross out the old value and write down the new value. b. Turn the card over and check the "mutator" box. c. Add a new index card for each change. d. Put a check mark in the upper left corner of the index card.

A

Whenever a method is called in Java, what must be specified? Select one: a. method name, arguments b. program name, method name c. strings, method name d. the main method, arguments

A

Which of the following statements about recursion is correct? Select one: a. In many cases, a recursive solution may be easier to understand and to implement than an iterative solution. b. A recursive solution will always run faster than an equivalent iterative solution. c. It is not necessary to have a special terminating case in all recursions. d. It is not necessary to simplify the argument in the recursive call.

A

Which of the following statements about the heapsort algorithm is correct? Select one: a. The heapsort algorithm is based on inserting elements into a heap and removing them in sorted order. b. Each insertion and removal is O(n log(n)). c. The heapsort sorts an array that is already sorted in O(n). d. The heapsort algorithm requires more space than the mergesort.

A

Which package is automatically imported in any Java program? Select one: a. java.lang b. java.util c. java.system d. java.language

A

Who or what is responsible for inspecting and testing the program to guard against logic errors? Select one: a. programmer b. compiler c. end-user d. JVM

A

You wish to traverse a binary search tree in sorted order. Arrange the following actions in the correct order to accomplish this. I Print the right subtree recursively II Print the root III Print the left subtree recursively Select one: a. III, II, I b. II, III, I c. I, II, III d. III, I, II

A

A balanced binary tree with 260 nodes has a height of approximately ____. Select one: a. 12 b. 8 c. 13 d. 10

B

Assume that recursive method search returns true if argument value is one of the elements in the section of the array limited by the firstIndex and lastIndex arguments. What statement can be used in main to determine if the value 7 is one of the elements in array values? public static boolean search(int value, int[] array, int firstIndex, int lastIndex) { if (firstIndex <= lastIndex) { if (array[firstIndex] == value) { return true; } else { return search(value, array, firstIndex + 1, lastIndex); } } return false; } public static void main(String[] args) { int [] values = { 4, 7, 1, 0, 2, 7 }; if (_________________________________ ) { System.out.println("7 is in the array"); } } Select one: a. search(7, values, 1, values.length) b. search(7, values, 0, values.length - 1) c. search(7, values, 1, values.length - 1) d. search(7, values, 0, values.length)

B

Assume that the variable count has been declared as type int. Which statement adds 10 to count? Select one: a. count + 10; b. count = count + 10; c. count == count + 10; d. count = 10;

B

Assume the following variable has been declared and given values as shown: String name = "Mamey, Jean"; Which statement will print the name as "Jean Mamey"? Select one: a. System.out.print(name.substring(2) + " " + name.substring(1)); b. System.out.print(name.substring(7) + " " + name.substring(0, 5)); c. System.out.print(name.substring(8, 4) + " " + name.substring(1, 5)); d. System.out.print(name.substring(8) + " " + name.substring(1, 4));

B

Assuming the following Java statement: int num = 10; What does the variable num store? Select one: a. A reference to the memory location where the value 10 is stored. b. The numeric value 10. c. A reference to the int primitive type. d. An object representing the number 10.

B

Assuming the programmer wishes to output the phrase "Welcome!", Which statement is true about the following Java statement: System.out.println("Welcome!"); Select one: a. There is a compile-time error. b. There are no errors. c. There are multiple errors. d. There is a run-time error.

B

Can you search the following array using binary search? int[] A = {6, 5, 4, 2, 0, 1, -1, -17}; Select one: a. No, negative numbers are not allowed because they indicate that a value is not present. b. No. Binary search can be applied to a sorted array only. c. Yes. Binary search can be applied to any array. d. Yes, but the algorithm runs slower because the array is in descending order.

B

Complete this code fragment to ensure that the frame is shown: JFrame frame = new JFrame(); Select one: a. frame.setVisible(); b. frame.setVisible(true); c. frame.visible = true; d. JFrame.setVisible();

B

Consider the following method comment and method header: /** Converts from a source measurement to a target measurement. @param _______________ the measurement @return the input value converted to the target unit */ public double convertTo(double fromMeasurement) { . . . } Fill in the blank. Select one: a. convertTo b. fromMeasurement c. return d. double

B

Consider the following method comment and method header: /** Converts from a source measurement to a target measurement. @param fromMeasurement the measurement __________ the input value converted to the target unit */ public double convertTo(double fromMeasurement) { . . . } Fill in the blank. Select one: a. return double b. @return c. return d. @return double

B

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]; } Select one: a. There would be no effect. b. Some array elements would be overwritten. c. It would still be correct, but run a little faster. d. It would sort the array in reverse order.

B

Fill in the blank in the comment for this method header. /** Gets the interest for the bank account _________________________________ */ public double getInterest() . . . Select one: a. @return double the interest b. @return the interest c. return double the interest d. return the interest

B

Given the following code snippet: public static int newCalc(int n) { if (n < 0) { return -1; } else if (n < 10) { return n; } else { return (1 + newCalc(n / 10)); } } What value will be returned when this code is executed with a call to newCalc(15)? Select one: a. 5.5 b. 2 c. 5 d. 2.5

B

Given this method comment, fill in the blank in the method implementation. /** Gets the current balance of the bank account @return the current balance */ public double getBalance() { __________ balance; } Select one: a. double b. return c. balance d. @return

B

If the postorder traversal of an expression tree is 8, 2, +, 5, /, what is the preorder traversal? Select one: a. 8, +, 2, /, 5 b. /, +, 8, 2, 5 c. +, /, 8, 2, 5 d. /, +, 2, 8, 5

B

Removing an element from a balanced binary search tree takes ____ time. Select one: a. O(n) b. O(log (n)) c. O(1) d. O(n2)

B

The Java compiler ignores any text between ____. Select one: a. {* and *} b. /* and */ c. // and // d. (* and *)

B

The ____ class contains a sort method that can sort array lists. Select one: a. Linear b. Collections c. Sorting d. Arrays

B

The error message "cannot find symbol" is usually a good clue that what kind of error has been made? a. division by zero b. spelling c. logic d. run-time

B

The following code is an example of a ___ search. public static int search(int[] a, int v) { for (int i = 0; i < a.length; i++) { if (a[i] == v) { return i; } } return -1; } Select one: a. binary b. linear c. sorted d. random

B

The method checkArray examines an array arr: public static boolean checkArray(int[] arr) { if (arr[0] >= arr[arr.length -1]) { return true; } return false; } What can you conclude about the running time of this section of code? Select one: a. Its running time will be O(n2). b. Its running time will be O(1). c. Its running time will be O(n). d. Its running time will be O(log (n)).

B

The private implementation of a class consists of ___. Select one: a. parameter variables and the method bodies b. instance variables and the implementation of the constructors and methods c. local variables and the method headers d. instance variables and the method headers

B

What is the efficiency of adding an element to a red-black tree? Select one: a. O(1) b. O(log (n)) c. O(n) d. O(n2)

B

What is the name of the constructor for the BankAccount class? Select one: a. withdraw b. BankAccount c. balance d. deposit

B

What is the output of the following Java statement? System.out.println("4 + 6"); Select one: a. 10 b. 4 + 6 c. 46 d. 4

B

What is the output of the following code snippet? int num1 = 10; int num2 = 5; int num3 = 200; num3 = num3 % (num1 * num2); System.out.println(num3); Select one: a. 10 b. 0 c. 250 d. 4

B

When are instance variables initialized? Select one: a. You must initialize instance variables in a method body. b. Instance variables are initialized with a default value before a constructor is invoked. c. Instance variables are initialized when the method is called. d. You must initialize instance variables in the constructor.

B

When the size of an array increases by a factor of 100, the time required by selection sort increases by a factor of ____. Select one: a. 5,000 b. 10,000 c. 12,000 d. 2,000

B

Which method call represents the invocation of a method that does not have arguments? Select one: a. greeting.length b. greeting.length() c. System.out.println(greeting); d. greeting.replace("Hello", "Welcome");

B

Which of the following is the Java equivalent of the following mathematical expression? p = 2 × π × radius3 Select one: a. p = Math.PI * Math.pow(3,radius); b. p = 2 * Math.PI * Math.pow(radius, 3); c. p = 2 * Math.PI * (radius * 3); d. p = 2 * Math.pow(Math.PI * radius, 3);

B

Which one of the following statements gives the absolute value of the floating-point number x = -25.50? Select one: a. x.absolute(); b. Math.abs(x); c. x.abs(); d. abs(x);

B

Which statement about private instance variables is true? Select one: a. They can only be accessed by methods of a different class b. They can only be accessed by methods of the same class c. They can only be accessed by the constructor of the class d. They cannot be accessed by methods of the same class

B

Which statement regarding computer programs is correct? Select one: a. Computer programs can decide what task to perform. b. Computer programs are composed of extremely primitive operations. c. Small computer programs are not documented. d. Large and complex computer programs are generally written by only one programmer.

B

A sequence of steps that eventually comes to an end is ______________. Select one: a. executable b. documented c. terminating d. unambiguous

C

An algorithm that cuts the work in half in each step is an ____ algorithm. Select one: a. O(n log (n)) b. O(n) c. O(log (n)) d. O(n^2)

C

An instance variable declaration consists of which of the following parts? Select one: a. the type of the instance variable, an access specifier, a list of the parameters (if any), and the body of the method. b. the return type, the name of the method, and a list of the parameters (if any). c. an access specifier, the type of the instance variable, and the name of the instance variable. d. an access specifier, a list of the parameters (if any), and the body of the method.

C

Assuming the programmer wishes to output the phrase "Welcome!", which of the following is true about the following Java statement. System.out.Println("Wlcome!"); Select one: a. There is a compile-time error. b. There is a run-time error. c. There are multiple errors. d. There are no errors.

C

Based on the following statement, which of the following statements sets the title of the frame: JFrame frame = new JFrame(); Select one: a. frame.addTitle("An Empty Frame"); b. frame.setTitle(JFrame.EMPTY); c. frame.setTitle("An Empty Frame"); d. frame.title = "An Empty Frame";

C

Complete the following code snippet, which is intended to be a recursive method that reverses a String value: public static String reverseIt(String s) { if (s.length() <= 1) { return s; } else { ________________________ } } Select one: a. return reverseIt(s.substring(0)) + s.charAt(1); b. return reverseIt(s.substring(1)) + s.charAt(1); c. return reverseIt(s.substring(1)) + s.charAt(0); d. return reverseIt(s.substring(0)) + s.charAt(0);

C

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 recursive call(s)? Select one: a. line #1 b. lines #1 and #2 c. line #4 d. line #2

C

Consider the recursive version of the fib method from the textbook shown below: public static long fib(int n) { if (n <= 2) { return 1; } else { return fib(n - 1) + fib(n - 2); } } How many recursive calls to fib(2) will be made from the original call of fib(6)? Select one: a. 3 b. 2 c. 5 d. 4

C

Evaluate the given pseudocode to calculate the weighted score for a student: The program score (program) = 92 The weight of programs (pgmWeight) = 40% The exam score (exams) = 85 The weight of exams(exWeight) = 60% input program input pgmWeight input exams input exWeight output program*pgmWeight + exams*exWeight What is the final output? a. 89.80 b. 89.20 c. 87.80 d. 92.20

C

Fill in the third line of this SquareTester program so that it prints out the expected outcome. public class SquareTester { public static void main(String[] args) { /* Step 1: declare and initialize a variable mySquare as an instance of a Square class with a side length of 6 */ /* Step 2: print out the area of the object referenced by the variable mySquare using the getArea method */ /* Step 3: print the expected outcome */ _____________________________________ } } Select one: a. System.out.println("Expected: 18"); b. System.out.println("Expected: 6"); c. System.out.println("Expected: 36"); d. System.out.println("Expected: 12");

C

How do you compute the length of the string str? Select one: a. length(str) b. str.length c. str.length() d. length.str

C

If greeting refers to a String object, which of the following is a syntactically correct Java statement? Select one: a. System.out.println(length().greeting); b. greeting.println("Hello"); c. System.out.println(greeting.length()); d. System.out.println(greeting());

C

In the statement below, amount is referred to as the ____ parameter. public void deposit(double amount) Select one: a. implicit b. public c. explicit d. private

C

In the worst case, quicksort is a(n) ____ algorithm. Select one: a. O(log(n)) b. O(n) c. O(n2) d. O(n log n)

C

Suppose objects a and b are from a user-defined class that implements the Comparable interface. What must be true about the return value of a.compareTo(b) for the compareTo method that this class implements? Select one: a. It must return 1 if a comes before b, 0 if they are the same, and -1 if a comes after b. b. It must return -1 if a comes before b, 0 if they are the same, and 1 if a comes after b. c. It must return a negative value if a comes before b, 0 if they are the same, and a positive value if a comes after b. d. It must return a positive value if a comes before b, 0 if they are the same, and a negative value if a comes after b.

C

Suppose that a computer virus infects your computer and corrupts the files you were going to submit for your current homework assignment. What precaution could have saved you from a disastrously bad grade for this assignment? Select one: a. Purchase an extended warranty for your computer. b. Purchase an anti-virus program to remove the virus from your computer. c. Make regular backups of all your important files. d. Defragment the hard drive.

C

Suppose we are using binary search on an array with approximately 1,000,000 elements. How many visits should we expect to make in the worst case? Select one: a. 1 b. 30 c. 20 d. 16

C

The height of a tree can be obtained by recursively computing the heights of its subtrees, while keeping track of the height of the deepest subtree. Given the Node class (partially shown below), select an expression to complete the recursive method height, which is designed to return the height of the tree rooted at a node. class Node { public Object data; public List<Node> children; . . . public int height() { int maxChildHeight = 0; for (Node child : children) { int childHeight = child.height(); if (childHeight > maxChildHeight) maxChildHeight = childHeight; } return _________________; } } Select one: a. maxChildHeight + 2 b. maxChildHeight c. maxChildHeight + 1 d. maxChildHeight + height()

C

What (if any) type of error occurs with the following code if the user input is ABC? Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String str = in.next(); int count = Integer.parseInt(str); System.out.println("Input is " + count); Select one: a. Illegal expression b. Overflow error c. Run-time error d. Compile-time error

C

What happens to the fractional part when a division is performed on two integer variables? Select one: a. Instead of using an integer division, you should use the modulus operator to perform floating-point division. b. The fractional part is rounded off to the nearest integer value. c. The fractional part is discarded. d. Two integers cannot be used in division; at least one of the operands should be a floating-point number.

C

What is the declared return type for a method that does not have a return value? Select one: a. String b. There is no declared return type when a method does not return a value. c. void d. A method must return a value.

C

What is the output of the following code: int num1 = 6; int num2 = num1; num2 = num2 + 10; System.out.println(num1); Select one: a. 16 b. 10 c. 6 d. 4

C

What is the term used to specify the collection of things you can do with objects that belong to a class? Select one: a. private implementation b. hidden implementation c. public interface d. private interface

C

What is the value of the following expression? 1 % 12 Select one: a. -11 b. This is an error because 12 is greater than 1 c. 1 d. 0

C

What is the worst-case performance of insertion sort? Select one: a. O(n log (n)) b. O(log (n)) c. O(n2) d. O(n)

C

What will be the value of the variables x and y after the given set of assignments? int x = 20; int y = 10; x = (x - y) * 2; y = x / 2; Select one: a. x = 40, y = 20 b. x = 10, y = 20 c. x = 20, y = 10 d. x = 20, y = 20

C

Which of the following guidelines will make code more explanatory for others? Select one: a. Use shorter statements and shorter variable names in source code. b. Avoid usage of complex calculations in source code. c. Add comments to source code. d. Always enclose the statements in curly braces in source code.

C

Why is the double type not appropriate for financial calculations? Select one: a. The calculations are too slow. b. There are too few significant digits. c. Roundoff errors occur when an exact representation of a floating point number is not possible. d. The range is too small.

C

he method findLargest examines the elements of an array arr which contains non-negative values public static int findLargest(int[] arr) { int curLargest = -1; for (int i = 0; i < arr.length; i++) { if (arr[i] >= curLargest) { curLargest = arr[i]; } } return curLargest; } What can you conclude about the running time of this section of code? Select one: a. Its running time will be O(n2). b. Its running time will be O(log (n)). c. Its running time will be O(n). d. Its running time will be O(n log (n)).

C

A completely filled binary tree with a height of 4 has ____ nodes. Select one: a. 8 b. 16 c. 12 d. 15

D

Assume the following variables have been declared and given values as shown: int i = 2345; double m = 67.8; What will be printed by the statement below? System.out.printf("Values are %10d and %7.2f", i, j); Select one: a. Values are 2345 and 67.8 b. Values are %10d and %7.2f i j c. Values are %10d and %7.2f 2345 67.8 d. Values are 2345 and 67.80

D

Consider the sort method for selection sort shown below: public static void sort(int[] a) { for (int i = 0; i < a.length - 1; i++) { int minPos = minimumPosition(i); swap(minPos, i); } } Suppose we modify the loop control to read int i = 1; i < a.length - 1; i++. What would be the result? Select one: a. The sort would not consider the last array element. b. The sort would still work correctly. c. An exception would occur d. The sort would not consider the first array element.

D

Evaluate the given pseudocode to calculate the efficiency of a vehicle's fuel consumption using the following test values: The trip odometer reading (odometer) = 300 The amount to fill the gas tank (amount) = 15 input odometer input amount output odometer/amount What is the final output? Select one: a. 15 b. 30 c. 10 d. 20

D

Given an ordered array with 15 elements, how many elements must be visited in the worst case of binary search? Select one: a. 2 b. 3 c. 8 d. 4

D

If the child references of a binary tree node are both null, the node is ____. Select one: a. a parent node b. a root node c. an interior node d. a leaf node

D

In Java, every statement must end with which symbol? a. ! b. . c. ) d. ;

D

The javadoc utility is used to Select one: a. convert the Java program into machine-executable code. b. ensure that comments are appropriately descriptive. c. assist the compiler in checking the syntax of the Java program. d. automatically generate HTML pages that describe classes.

D

The partial binary search method below is designed to search an array of String objects sorted in ascending order. Select the expression that would be needed to complete the method. public static int search(String[] a, int low, int high, String item) { if(low <= high) { int mid = (low + high) / 2; int result = ____________________________; if (result == 0) { return mid; } else if (result < 0) { return search(a, mid + 1, high, item); } else { return search(a, low, mid - 1, item); } } return -1; } Select one: a. a[low].compareTo(item) b. item.compareTo(a[mid]) c. item.equals(a[mid]) d. a[mid].compareTo(item)

D

What does the following statement sequence print if the user input is 123? Scanner in = new Scanner(System.in); System.out.print("Enter a number "); int myInt = in.nextInt(); myInt += 456; System.out.println(myInt); Select one: a. Run-time error b. Compile-time error c. 123456 d. 579

D

What is the efficiency of the heapsort algorithm? Select one: a. O(log (n)) b. O(n2) c. O(1) d. O(n log (n))

D

What is the name of the instance variable for a Counter object? Select one: a. concertCounter b. click c. getValue d. value

D

What is the name of the type that denotes a string of characters? Select one: a. char b. Characters c. charString d. String

D

What is the output of the following code: int num1 = 6; int num2 = 10; num1 = num2; num2 = num1; System.out.println(num1 + ", " + num2); Select one: a. 10, 6 b. 6, 6 c. 6, 10 d. 10, 10

D

What is the purpose of the following algorithm? input num Repeat the following steps for 9 times input var1 if var1 > num then num = var1 print num Select one: a. To find the smallest among 10 numbers b. To search for a particular number among 10 numbers c. To print out the 10 numbers d. To find the largest among 10 numbers

D

What is the purpose of the following algorithm? input somenum Repeat the following steps for 14 times input variable1 if variable1 < somenum then somenum = variable1 print somenum Select one: a. To search for a particular number among 15 numbers. b. To find the largest among 15 numbers. c. To print out the 15 numbers. d. To find the smallest among 15 numbers.

D

What term is used to refer to text in a program that is an explanation for human readers of the code? Select one: a. [* and *] b. constants c. methods d. comments

D

What will be printed by the statements below? final int BONUS = 20; int salary = 100; int netSalary = salary + BONUS; System.out.print(netSalary); Select one: a. salaryBONUS b. Nothing will be printed because constants cannot be used in expressions. c. 100BONUS d. 120

D

Which is the Java equivalent of the following mathematical expression? c = √(a2 + b2) Select one: a. c = Math.sqrt(a * 2) + Math.sqrt(b * 2); b. c = Math.sqrt(a * 2 + b * 2); c. c = Math.sqrt(Math.pow(a, 2)) + Math.sqrt(Math.pow(b, 2)); d. c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));

D

Which of the following corresponds to the getArea method header for a Square class assuming an integer value for a side length? Select one: a. public integer getArea() b. public int getArea c. public integer getArea d. public int getArea()

D

Which of the following declares a variable that will store a measurement with fractional parts? Select one: a. int measure; b. integer measure; c. String measure; d. double measure;

D

Which of the following is NOT true about debugging a recursive method by setting a breakpoint on the line containing a return statement? Select one: a. The debugger's call stack will show all the calls to the recursive method that are pending. b. The last call on the stack represents the first recursive call to reach the return statement. c. You can select a pending call and then inspect the value of its variables. d. You cannot debug a recursive method with the debugger.

D

Which of the following options is true about algorithms? Select one: a. Algorithms are described informally and can contain ambiguous steps. b. Algorithms can replace the source code in programs. c. Algorithms are written in a programming language. d. You must create an algorithm for a problem before you can create a program to solve the problem.

D

Which of the following represents a method declaration with a void return type? Select one: a. void int getValue() { ... } b. public void int getValue() { ... } c. void public setValue(int value) { ... } d. public void setValue(int value) { ... }

D

Which one of the following statements displays the output as -1.23e+02? Select one: a. System.out.printf("%5.2E", -123.0); b. System.out.printf("^5.2e", -123.0); c. System.out.printf("%5.1e", -123.0); d. System.out.printf("%5.2e", -123.0);

D

Which sort algorithm starts by partitioning the array and selecting a pivot element? Select one: a. selection sort b. merge sort c. insertion sort d. quicksort

D

Which statement is true about the following constructor of the BankAccount class? public BankAccount(double balance) { this.balance = balance; } Select one: a. You can't have an instance variable and a parameter variable with the same name. b. The code has a logic error. c. The code has a syntax error. d. The code sets the instance variable balance to the parameter variable balance.

D

What are Collisions in computer science? Select one: a. Identical hash codes for different objects b. Attempting to assign a list as a map c. A NullPointerException d. When an iterator points into the middle of a node chain

a

Which of the following actions must be taken to remove a node X from the middle of a doubly-linked list? I Update the next reference in the node before X II Update the previous reference in the node after X III Update the list's first reference Select one: a. I and II b. II c. I d. II and III

a

Which of the following statements about removing a node from a linked list is correct? Select one: a. A node's data is returned to the program when the node is removed from the linked list, and its memory space is reclaimed later by the garbage collector. b. A node's data is discarded when it is removed from the linked list, and its memory space is immediately reclaimed. c. A node's data is returned to the program when the node is removed from the linked list, and its memory space is immediately reclaimed. d. A node's data is discarded when it is removed from the linked list, and its memory space is reclaimed later by the garbage collector.

a

In Big-Oh notation, selection sort is a(n) ____ algorithm. Select one: a. O(n^2) b. log n c. O(log n^2) d. O(1)

a. 0(n^2)

A completely filled binary tree with a height of 4 has ____ nodes. Select one: a. 15 b. 16 c. 8 d. 12

a. 15

If the postorder traversal of an expression tree is, 8, 2, +, 5, /, what is the result of an inorder traversal? Select one: a. 8, +, 2, /, 5 b. /, +, 8, 2, 5 c. +, /, 8, 2, 5 d. /, +, 2, 8, 5

a. 8, +, 2, /, 5

Assume that names is an array of String objects that has been initialized with a large number of elements. Select the statement that would sort the elements in names in ascending alphabetic order. Select one: a. Arrays.sort(names); b. Collections.sort(names); c. Arrays.sort(names, 0, names.length - 1); d. Collections.sort(names, 0, names.length - 1);

a. Arrays.sort(names);

Which statement is true about backtracking? Select one: a. Backtracking starts with a partial solution and builds it up to get closer to the goal. b. Backtracking never abandons a partial solution. c. Backtracking starts from the end of the program and works backward to the beginning. d. Backtracking explores only one path toward a solution

a. Backtracking starts with a partial solution and builds it up to get closer to the goal.

Suppose a generic method accepts a generic unconstrained argument p. What restrictions are placed on p by the compiler? Select one: a. Can only execute Object class methods. b. There are no restrictions. c. Can not execute any methods. d. Can only execute methods from its own class.

a. Can only execute Object class methods.

Which of the sorts in the textbook are based on the strategy of divide and conquer? I quicksort II mergesort III insertion sort Select one: a. I and II only b. II only c. I only d. III only

a. I and II only

Would switching the special case order affect the return value of the following method? public int mystery(int n, int m) { if (n == 0) // special case #1 { return 0; } if (n == 1) // special case #2 { return m; } return m + mystery(n - 1, m); } Select one: a. No b. It is impossible to tell. c. Yes d. An exception will be thrown.

a. No

Merge sort is a(n) ____ algorithm. Select one: a. O(n log(n)) b. O(n2) c. O(log n) d. O(n)

a. O(n log(n))

What is the worst-case performance of insertion sort? Select one: a. O(n^2) b. O(n log (n)) c. O(n) d. O(log (n))

a. O(n^2)

Given the following declaration, what is the type parameter in Stack<E> replaced with? private Stack<String> myStack = new Stack<>(); Select one: a. String b. int c. Object d. Stack<String>

a. String

The partial binary search method below is designed to search an array of String objects sorted in ascending order. Select the expression that would be needed to complete the method. public static int search(String[] a, int low, int high, String item) { if(low <= high) { int mid = (low + high) / 2; int result = ____________________________; if (result == 0) { return mid; } else if (result < 0) { return search(a, mid + 1, high, item); } else { return search(a, low, mid - 1, item); } } return -1; } Select one: a. a[mid].compareTo(item) b. item.equals(a[mid]) c. item.compareTo(a[mid]) d. a[low].compareTo(item)

a. a[mid].compareTo(item)

In a binary search tree, where the root node data value = 45, what do we know about the values of all the descendants in the right subtree of the root? Select one: a. all will be > 45 b. approximately half the values are < 45, the other half are > 45 c. the root's right child value > 45, but the left child of the root's right child key is < 45 d. some values will be > 45, but there may be a few values < 45

a. all will be > 45

Complete the code for the calcPower recursive method shown below, which is intended to raise the base number passed into the method to the exponent power passed into the method: public static int calcPower(int baseNum, int exponent) { int answer = 0; if (exponent == 0) { answer = 1; } else { _______________________________________ } return answer; } Select one: a. answer = baseNum * calcPower (baseNum, exponent - 1); b. answer = baseNum * calcPower (baseNum -1, exponent - 1); c. answer = baseNum * calcPower (baseNum -1, exponent); d. answer = baseNum * calcPower (baseNum, exponent);

a. answer = baseNum * calcPower (baseNum, exponent - 1);

A search technique where, in each step, you split the size of the search in half is called a____ search. Select one: a. binary b. random c. linear d. merging

a. binary

The Comparable interface consists of a single method called ____. Select one: a. compareTo b. comparable c. compare d. comparator

a. compareTo

A binary search is generally ____ a linear search. Select one: a. faster than b. less efficient than c. equal to d. slower than

a. faster than

Consider the following code snippet for recursive addition: int add(int i, int j) { // assumes i >= 0 if (i == 0) { return j; } else { return add(i - 1, j + 1); } } Identify the terminating condition in this recursive method. Select one: a. i == 0 b. return add(i - 1, j + 1) c. return j d. there is no terminating condition

a. i == 0

Complete the code for the calcPower recursive method shown below, which is intended to raise the base number passed into the method to the exponent power passed into the method: public static int calcPower(int baseNum, int exponent) { int answer = 0; ________________________ { answer = 1; } else { answer = baseNum * calcPower (baseNum, exponent - 1); } return answer; } Select one: a. if (exponent == 0) b. if (exponent == -1) c. if (exponent != 1) d. if (exponent == 1)

a. if (exponent == 0)

If an element is present in an array of length n, how many element visits, on average, are necessary to find it using a linear search? Select one: a. n / 2 b. n c. 2n d. n^2

a. n / 2

Which of the following satisfies the wildcard ? extends Component? Select one: a. JButton b. String c. Scanner d. Color

a.JButton

Consider the method below, which prints the digits of an arbitrary positive integer in reverse order, one digit per line. The method should print the last digit first. Then, it should recursively print the integer obtained by removing the last digit. Select the statements that should be used to complete the method. public static void printReverse(int value) { if (value > 0) { _____________________ // print last digit _____________________ // recursive call to print value without last digit } } Select one: a. System.out.println(value % 10); printReverse(value / 10); b. System.out.println(value / 10); printReverse(value / 10); c. System.out.println(value / 10); printReverse(value % 10); d. System.out.println(value % 10); printReverse(value % 10);

a.System.out.println(value % 10); printReverse(value / 10);

Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. Which line contains the terminating condition in the permutations recursive method? public static ArrayList<String> permutations(String word) { ArrayList<String> result = new ArrayList<String>(); if (word.length() == 0) // line #1 { result.add(word); // line #2 return result; // line #3 } else { for (int i = 0; i < word.length(); i++) // line #4 { String shorter = word.substring(0, i) + word(substring(i + 1); // line #5 ArrayList<String> shorterPermutations = permutations(shorter); // line #6 for (String s : shorterPermutations) // line #7 { result.add(word.charAt(i) + s); // line #8 } } return result; // line #9 } } Select one: a. line #1 b. line #6 c. line #8 d. line #9

a.line #1

A hash function is considered good if it ____. Select one: a. does not require compression. b. minimizes collisions. c. results in low integer values. d. detects duplicate elements.

b

An array list maintains a reference to an array of elements called a ____. Select one: a. tree map b. buffer c. bucket d. hash table

b

Assume that you have a hash table in which there are few or no collisions. What is the time required to remove an element from this hash table? Select one: a. O(log (n)) b. O(1) c. O(n2) d. O(n)

b

Complete the following code, which is intended to add an element to the top of a stack implemented as a linked list. Node newNode = new Node(); newNode.data = element; _________________ _________________ Select one: a. first = newNode; newNode.previous = first; b. newNode.next = first; first = newNode; c. newNode.previous = first; first.next = newNode; d. first = newNode; newNode.next = first;

b

Consider the following code snippet: LinkedList<String> words = new LinkedList<String>(); words.addFirst("xyz"); words.addLast("jkl"); words.addLast("def"); System.out.print(words.removeFirst()); System.out.print(words.removeLast()); System.out.print(words.removeLast()); What does this code print? Select one: a. defxyzjkl b. xyzdefjkl c. xyzjkldef d. defjklxyz

b

Suppose we maintain an array A of n int values as follows: A[0] < A[1] < . . . < A[i] > A[i + 1] > A[i + 2] > . . . > A[n - 1] The ith element is the maximum in the array. What would be the lowest big-Oh notation for finding that element? Consider a variation of the binary search. Select one: a. O(n2) b. O(log n) c. O(1) d. O(n)

b

The advantage of using the open addressing technique over the separate chaining technique for handling collisions in a hash table is that open addressing ____. Select one: a. is simpler in implementation b. requires less memory storage for the hash table c. allows for faster retrieval of elements d. allows for faster addition of elements

b

Which of the following actions must be taken to add a node X at the end of a doubly-linked list? I Update the next reference in the node before the position where X will be placed II Update the previous reference in the node before the position where X will be placed III Update the list's last reference Select one: a. I and II b. I and III c. I d. II

b

Which of the following statements are true about the Node class? I The instance variables are private II It holds a reference first to the first node III It is a private inner class of the LinkedList class Select one: a. All of the above b. III c. II d. I

b

Which statement about handling collisions in a hash table using the open addressing technique is correct? Select one: a. To find an element, you must search from the hash code location until a match or an element with a different hash code is found. b. There may be some elements with different hash codes that lie on the same probing sequence. c. A colliding element will always be contiguous to the location in which it would normally be placed. d. To remove an element, you simply empty the slot at which you find it.

b

The code segment below displays a pattern of asterisks. Select an expression to complete the code segment so that the resulting algorithm has O(n) running time. for (int k = 0; k < n; k++) { for _______________________ { System.out.print("*"); } System.out.println(); } Select one: a. (int j = n; j > 0; j = j / 2) b. (int j = 1; j <= 10; j++) c. (int j = 1; j < k; j++) d. (int j = n; j > 0; j--)

b. (int j = 1; j <= 10; j++)

Consider the following tree diagram: What is the size of this tree? Select one: a. 4 b. 13 c. 6 d. 5

b. 13

Given the following class code: public class RecurseMore { public static void main(String[] args) { recurse(4); } public static int recurse(int n) { int total = 0; if (n == 0) { return 0; } else { total = 4 + recurse(n - 2); } System.out.println(total); return total; } } What values will be printed when this code is executed? Select one: a. 4 b. 4 and 8 c. 0, 4, and 8 d. 8

b. 4 and 8

Consider the square method shown below that takes a non-negative int argument: public int square(int n) { return squareHelper(n, n); } public int squareHelper(int c, int n) { if (c == 1) { return n; } else { return n + squareHelper(c - 1, n); } } Assume that the last return statement in the squareHelper method is changed to this: return squareHelper(c - 1, n); What would a call to square(7) return? Select one: a. 13 b. 7 c. 49 d. 14

b. 7

A balanced binary tree with 260 nodes has a height of approximately ____. Select one: a. 10 b. 8 c. 12 d. 13

b. 8

Consider the following code snippet: public class LinkedList<E> { private E defaultValue; public static List<E> replicate(E value, int n) { . . . } private class Node { public String data; public Node next;) . . . } What is wrong with this code? Select one: a. Cannot declare the variable defaultValue as a generic type. b. Cannot have a static method in a generic class as shown. c. Cannot have an inner class in a generic class. d. Cannot pass a generic type as an argument to a method.

b. Cannot have a static method in a generic class as shown.

Why does the best recursive method usually run slightly slower than its iterative counterpart? Select one: a. Multiple recursive cases must be considered. b. Each recursive method call takes processor time. c. Testing the terminating condition takes longer. d. Checking multiple terminating conditions take more processor time.

b. Each recursive method call takes processor time.

If a min-heap has 14 nodes, what is known for certain when we add a new node? I every level of the tree will be fully occupied II the tree does not grow a new level III the root contains the smallest element Select one: a. I only b. I, II and III c. I and III only d. I and II only

b. I, II and III

Suppose a linked-list class called MyLinkedList with a generic E type has been instantiated with a java.awt.Component type variable. Consider its instance method locate with the following header: public void locate(MyLinkedList<? extends E>) { . . . } Which type cannot be passed to method locate? I MyLinkedList<JButton> II MyLinkedList<Component> III MyLinkedList<JTextField> Select one: a. I only b. I, II, and III c. II and III only d. I and II only

b. I, II, and III

Which of the following statements about the three tree traversal schemes studied is correct? Select one: a. Postorder traversal is used for copying file directories. b. Postorder traversal is used for removing file directories by removing subdirectories first. c. Inorder traversal is used for copying file directories. d. Inorder traversal is used for evaluating arithmetic expression trees on a stack-based calculator.

b. Postorder traversal is used for removing file directories by removing subdirectories first.

The performance of an algorithm is most closely related to what? Select one: a. The type of elements b. The total number of element visits c. The total number of elements d. The number of lines of code in the method

b. The total number of element visits

Consider the following class declaration: public class SavingsAccount extends BankAccount { . . . } Which of the following statements about these classes is correct? Select one: a. ArrayList<BankAccount> is a subclass of ArrayList<SavingsAccount>. b. There is no relationship between ArrayList<BankAccount> and ArrayList<SavingsAccount> c. ArrayList<SavingsAccount> is a subclass of ArrayList<BankAccount>. d. ArrayList<SavingsAccount> extends ArrayList<BankAccount>.

b. There is no relationship between ArrayList<BankAccount> and ArrayList<SavingsAccount>

Given the BinarySearchTree and Node classes (partially shown below), select an expression to complete the recursive method smallest in the Node class. The method returns the smallest data value in the binary search tree rooted at a node. public class BinarySearchTree { private Node root; public BinarySearchTree() {...} public void add(Comparable obj) {...} public Comparable smallest() { if (root == null) throw new NoSuchElementException(); else return root.smallest(); } class Node { public Comparable data; public Node left; public Node right; public Comparable smallest() { if (left == null) return data; else return _______________; } } } Select one: a. right.smallest() b. left.smallest() c. data.smallest() d. Math.min(left.smallest(), right.smallest())

b. left.smallest()

Select the correct header for this generic print method. public static void print(E[] a) { for (int i = 0; i < a.length; i++) { System.out.println(a[i] + " "); } } Select one: a. public void print(E [] a) b. public static <E> void print(E[] a) c. public static <E> void print(E a) d. The header is correct

b. public static <E> void print(E[] a)

Consider the following code snippet: public static <E extends Measurable> E min(E[] objects) Which of the following represents the result of type erasure on this code? Select one: a. This code is illegal and type erasure will not occur. b. public static Measurable min(Measurable[] objects) c. public static <Measurable E> E min(Measurable E[] objects) d. public static Measurable E min(Measurable E[] objects)

b. public static Measurable min(Measurable[] objects)

Consider the following code snippet: public static <T> void fun(T[] t) { . . . } Erasure by the compiler of method fun will generate which result? Select one: a. public static void fun(Object t) { . . . } b. public static void fun(Object[] t) { . . . } c. public static <T> void fun(Object[] t) { . . . } d. public static <Object> void fun(Object t) { . . . }

b. public static void fun(Object[] t) {...}

Complete the following code snippet, which is intended to be a recursive method that will find the sum of all elements in an array of double values from the beginning of the array to index: // return the sum of all elements in arr[] public static double findSum(double arr[], int index) { if (index == 0) { _____________________ } else { return (arr[index] + findSum(arr, index - 1)); } } Assume that this method would be called using an existing array named myArray as follows: findSum(myArray,myArray.length - 1); Select one: a. return arr[index + 1]; b. return arr[index]; c. return arr[1]; d. return arr[index - 1];

b. return arr[index];

Consider the following code snippet: public class Box<E> { private E data; public Box() { . . . } public void insert(E value) { . . . } public E getData(){ . . . } } What will result from the following code? Box<String> box = new Box<>(); . . . box.insert("blue Box"); Double myDouble = (Double) box.getData(); Select one: a. correct, but unnecessary cast b. run-time error c. compiler error d. correct, with necessary cast

b. run-time error

Consider the sort method shown below for selection sort: public static void sort(int[] a) { for (int i = 0; i < a.length - 1; i++) { int minPos = minimumPosition(i); swap(minPos, i); } } Suppose we modify the call to the swap method call to read swap(i, minPos). What would be the result? Select one: a. The sort would work, but sort backwards. b. The sort would produce correct results. c. An exception would occur. d. The sort would produce incorrect results.

b.The sort would produce correct results.

If we want a create a doubly-linked list data structure so that we can move from a node to the next node as well as to a previous node, we need to add a previous reference to the Node class. Each such DNode (doubly-linked Node) will have a data portion and two DNode references, next and previous. How many references need to be updated when we remove a node from the middle of such a list? Consider the neighboring nodes. Select one: a. 3 b. 1 c. 2 d. 4

c

Which of the following statements about using iterators with hash tables is NOT correct? Select one: a. The iterator must track the bucket number. b. The iterator must skip over empty buckets. c. Two iterators are required: one to traverse the buckets, and another to traverse the nodes within a bucket. d. The iterator must track its position within a node chain in a bucket.

c

The code segment below prints some of the elements in an array with size n. Select an expression to complete the code segment so that the resulting algorithm has O(log n) running time. for __________________________ { System.out.println(array[j]); } Select one: a. (int j = 0; j < array.length / 2; j++) b. (int j = 0; j < array.length; j = j + 2) c. (int j = 1; j < array.length; j = j * 2) d. (int j = 0; j < array.length; j++)

c. (int j = 1; j < array.length; j = j * 2)

Question 3Given the following class code: public class RecurseMore { private static int total; public static void main(String[] args) { System.out.println(recurse(4)); } public static int recurse(int n) { int total = 0; if (n == 0) { return 0; } else { total = 4 + recurse(n - 2); } return total; } } What values will be printed when this code is executed? Select one: a. 4 and 8 b. 0, 4, and 8 c. 8 d. 4

c. 8

Which of the following statements about breadth-first and depth-first traversal is NOT correct? Select one: a. Depth-first search uses a stack to track the nodes that it visits. b. Breadth-first search first visits all nodes on the same level before visiting the children. c. Breadth-first and depth-first search only work on a binary tree. d. Depth-first search goes deeply into the tree and then backtracks when it reaches the leaves.

c. Breadth-first and depth-first search only work on a binary tree.

Consider the following code snippet: public interface MyInterface<E> { . . . } public interface YourInterface<E, T> extends MyInterface<E> { . . . } Which of these are legal class declarations? I public class SomeClass implements YourInterface<String, Double> { . . . } II public class SomeClass implements YourInterface { . . . } III public class SomeClass implements YourInterface<String> { . . . } Select one: a. III only b. I only c. I and II only d. I and III only

c. I and II only

Which of the following necessitates the type erasure process? I The Java virtual machine does not work with generic types II To maintain backward compatibility to older versions of Java III Generics do not work with primitives Select one: a. I only b. II only c. I and II only d. I and III only

c. I and II only

Which of these Java library classes are implemented using type variables? I HashMap II LinkedList III ArrayList Select one: a. I and III only b. I and II only c. I, II and III d. II and III only

c. I, II and III

Consider the following code snippet that declares the GraduateStudent class: public GraduateStudent extends Student { . . .} Which of these statements are false? I GraduateStudent is a subclass of Student II Stack<GraduateStudent> is a subclass of Stack<Student> III Stack<Student> is a subclass of Stack<GraduateStudent> Select one: a. II only b. I only c. II and III only d. III only

c. 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 Select one: a. I only b. I and III only c. III only d. II only

c. III only

Suppose objects a and b are from a user-defined class that implements the Comparable interface. What must be true about the return value of a.compareTo(b) for the compareTo method that this class implements? Select one: a. It must return -1 if a comes before b, 0 if they are the same, and 1 if a comes after b. b. It must return 1 if a comes before b, 0 if they are the same, and -1 if a comes after b. c. It must return a negative value if a comes before b, 0 if they are the same, and a positive value if a comes after b. d. It must return a positive value if a comes before b, 0 if they are the same, and a negative value if a comes after b.

c. It must return a negative value if a comes before b, 0 if they are the same, and a positive value if a comes after b.

Binary search is an ____ algorithm. Select one: a. O(n2) b. O(n) c. O(log n) d. O(n log n)

c. O(log n)

What does the following code snippet mean: <E extends Comparable<E> & Measurable> Select one: a. The class represented by E extends Comparable and Measurable. b. The class represented by E implements Comparable and extends Measurable. c. The class represented by E implements Comparable and Measurable. d. The class represented by E extends Comparable and implements Measurable.

c. The class represented by E implements Comparable and Measurable.

A termination condition in a loop is analogous to _____________ in a recursive method. Select one: a. an initialization condition b. a recursive call c. a special case d. iteration

c. a special case

Consider the following code snippet: public class Box<E> { private E data; public Box(){ . . . } public void insert(E value) { . . . } public E getData() { . . . } } What will result from executing the following code? Box<String> box = new Box<>(); . . . box.insert("blue Box"); String b = (String) box.getData(); Select one: a. compiler error b. correct, with necessary cast c. correct, but unnecessary cast d. run-time error

c. correct, but unnecessary cast

Assume we are using quicksort to sort an array in ascending order. Into which array location does quicksort's strategy place a pivot element after partitioning? Select one: a. highest index in array still available b. closer to its correct final location c. its final correct location d. lowest index in array still available

c. its final correct location

Consider the mutually recursive methods below. Select the method call that could be used to generate the output sequence: A5 B4 A3 B2 A1 public static void methodA(int value) { if (value > 0) { System.out.print(" A" + value); methodB(value - 1); } } public static void methodB(int value) { if (value > 0) { System.out.print(" B" + value); methodA(value - 1); } } Select one: a. methodB(4); b. methodB(5); c. methodA(5); d. methodA(4);

c. methodA(5);

Given an array myArray, which of the following represents an expression to determine the type of the elements of the array? Select one: a. myArray.getComponentType().getClass() b. myArray[0].getClass() c. myArray.getClass().getComponentType() d. myArray.getClass().getElementType()

c. myArray.getClass().getComponentType()

A recursive method without a special terminating case would _________ Select one: a. be more efficient. b. not be recursive. c. never terminate. d. end immediately.

c. never terminate

Consider the method below, which displays the characters from a String in reverse order. Each character appears on a separate line. Select the statement that should be used to complete the method so that it performs a recursive method call correctly. public static void printReverse(String word) { if (word.length() > 0) { ___________________________ System.out.println(word.charAt(0)); } } Select one: a. printReverse(word); b. printReverse(new String(word.charAt(1))); c. printReverse(word.substring(1)); d. printReverse(word.length() - 1);

c. printReverse(word.substring(1));

Insert the missing code in the following code fragment. This fragment is intended to recursively compute xn, where x and n are both non-negative integers: public int power(int x, int n) { if (n == 0) { ____________________ } else { return x * power(x, n - 1); } } Select one: a. return x * power(x, n - 1); b. return x; c. return 1; d. return power(x, n - 1);

c. return 1;

Array lists and linked lists both have the same ____. Select one: a. add/remove efficiency b. random element access efficiency c. concrete implementations d. linear traversal step efficiency

d

Consider the following code snippet, which computes h, the array index for storing x in a hash table. int h = x.hashCode(); if (h < 0) { h = -h; } h = h % size; What does size correspond to? Select one: a. The number of elements to be stored. b. The index of an empty hash table slot. c. The number of collisions. d. The size of the hash table.

d

Using the textbook's implementation of a linked list, why is the LinkedListIterator inner class NOT declared as an inner static class? Select one: a. Because the LinkedList class must have access to the instance variables of the LinkedListIterator class. b. Because the LinkedListIterator class must have access to Node class instance variables. c. Because the Node class must have access to the instance variables of the LinkedListIterator class. d. Because the LinkedListIterator class must have access to LinkedList class instance variables.

d

What feature of the ArrayList class makes it much better for a binary search than the LinkedList class? Select one: a. it is a generic class b. it is an abstract class c. has no link references d. indexing

d

Which Java technique(s) allows generic programming? I type variables II primitive types III inheritance Select one: a. II only b. III only c. I only d. I and III only

d. 1 and III only

Selection sort has O(n2) complexity. If a computer can sort 1,000 elements in 4 seconds, approximately how many seconds will it take the computer to sort 1,000 times that many, or 1,000,000 elements? Select one: a. 16 b. 1,000,000 c. 1,000 d. 4,000,000

d. 4,000,000

Which of the following statements about inserting a node into a red-black tree is correct? Select one: a. If the parent of the new node is red, no other actions are required. b. If it is the first node, it must be red. c. Color the new node black. d. If a double-red situation results, you must correct this.

d. If a double-red situation results, you must correct this.

In Java, generic programming can be achieved with ____. Select one: a. encapsulation b. polymorphism c. arrays d. inheritance

d. Inheritance

Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. What special cases for the simplest values are used by the permutations method to terminate the recursion? public static ArrayList<String> permutations(String word) { ArrayList<String> result = new ArrayList<String>(); if (word.length() == 0) // line #1 { result.add(word); // line #2 return result; // line #3 } else { for (int i = 0; i < word.length(); i++) // line #4 { String shorter = word.substring(0, i) + word(substring(i + 1); // line #5 ArrayList<String> shorterPermutations = permutations(shorter); // line #6 for (String s : shorterPermutations) // line #7 { result.add(word.charAt(i) + s); // line #8 } } return result; // line #9 } } Select one: a. It terminates the recursion when it encounters the empty string or a string with one character. b. It terminates the recursion when it encounters a string with two characters. c. It terminates the recursion when it encounters a string with one character. d. It terminates the recursion when it encounters the empty string.

d. It terminates the recursion when it encounters the empty string.

Adding an element to a balanced binary search tree takes ____ time. Select one: a. O(1) b. O(n) c. O(n2) d. O(log (n))

d. O(log (n))

Which of the following statements about a binary search tree is correct? Select one: a. Adding elements that are already sorted will result in a balanced binary search tree. b. Locating an element in a balanced binary search tree takes O(n) time. c. You can specify an insert position for inserting a node in the tree. d. The speed of inserting or removing a node is dependent on the shape of the tree.

d. The speed of inserting or removing a node is dependent on the shape of the tree.

Consider the following code snippet: public class Box<E> { private E data; public Box() { . . . } public void insert(E value) { . . . } public E getData() { . . . } } What will result from executing the following code? Box<Boolean> box = new Box<>(); Box b = (Box) box.getData(); Select one: a. correct generic assignment b. compile-time warning c. run-time error d. compile-time error

d. compile-time error

In general, the expression ____ means that f grows no faster than g. Select one: a. f(n) = log g2 b. f(n) = log g c. g(n) = O(f(n)) d. f(n) = O(g(n))

d. f(n) = O(g(n))

Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. Which line contains the recursive call in the permutations method? public static ArrayList<String> permutations(String word) { ArrayList<String> result = new ArrayList<String>(); if (word.length() == 0) // line #1 { result.add(word); // line #2 return result; // line #3 } else { for (int i = 0; i < word.length(); i++) // line #4 { String shorter = word.substring(0, i) + word(substring(i + 1); // line #5 ArrayList<String> shorterPermutations = permutations(shorter); // line #6 for (String s : shorterPermutations) // line #7 { result.add(word.charAt(i) + s); // line #8 } } return result; // line #9 } } Select one: a. line #7 b. line #8 c. line #5 d. line #6

d. line #6

A palindrome is a word or phrase that reads the same forward or backward. Consider the methods palindrome and isPal shown below: public boolean palindrome(String string) { return isPal(string, 0, string.length() - 1); } private boolean isPal(String string, int left, int right) { if (left >= right) { return true; } else if (string.charAt(left) == string.charAt(right)) { return isPal(string, left + 1, right - 1); } else { return false; } } The method isPal as shown here would be considered to be a ____ method. Select one: a. static b. terminating c. public d. recursive helper

d. recursive helper


संबंधित स्टडी सेट्स

patho final test bank combined set

View Set

Ch.45 Mgmnt of pts w/ oral esophageal disorders

View Set

CHAPTER 6: Stocks and Stock Valuation

View Set

Ch. 24- 24.3 List the common patient presentation, treatment, standard precautions and postexposure actions for each diseases.

View Set