kf

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

Which of the following statements about generic methods is correct?

A generic method must have a generic type parameter.

An interface can extend any number of interfaces using the extends keyword.

true

What is the result of the following definition of an array list? ArrayList<Double> points;

The statement defines an array list reference but leaves its value as null.

Finish the while-loop so that Scanner an read until the end of the file. FileInputStream inputFile = new FileInputStream("./numFile.txt"); Scanner scan = new Scanner(inputFile); int fileFum; while (_____________________) { fileNum = scan.nextInt(); System.out.println("num: " + fileNum); } inputFile.close();

scan.hasNextInt()

This method returns a string representing all of the items stored in an ArrayList object.

toString

The numeric wrapper classes are subclasses of Number.

true

You cannot create an instance of an abstract class using the new operator.

true

When typing/writing a UML description, use ________ before a member name to indicate that the member is private.

-

Suppose A is an interface, B is a concrete class with a default constructor that implements A. Which of the following is correct?

A a = new B(); B b = new B();

ArrayList is more efficient than LinkedList for which of the following operations?

Retrieve an element given the index.

What is output? (_ represents a blank space) int age = 50; System.out.printf("She is %4d years old", age);

She is __50 years old

Any Java exception is an instance of ________.

Throwable

A pre-order traversal of a binary search tree visits the nodes in the order: ROOT - LEFT - RIGHT

True

All of the following words are used to describe various properties of trees in CS 108: leaf, node, root, path, height, level, traversal, edge, parent, child

True

The height of a binary tree is calculated as the largest number of edges in a path from the root to a leaf node. Therefore, the height of a binary tree with one node is 0.

True

In the implementation of Java API LinkedList, which of the following are true?

You should use an array list if your application does not require adding and remove an element anywhere in a list. You can use a linked list to improve efficiency for adding and remove an element at the beginning of the list. LinkedList contains all the methods defined in List. Additionally, LinkedList defines several new methods that are appropriate for processing a linked list.

Given the following classes and their objects: class C1 {}; class C2 extends C1 {}; class C3 extends C1 {}; C2 c2 = new C2(); C3 c3 = new C3(); Analyze the following statement: c2 = (C2)((C1)c3);

You will get a runtime error because you cannot cast objects from sibling classes.

A(n) ___ guides the design of subclasses but cannot be instantiated as an object.

abstract class

This occurs when method A calls method B, which in turn calls method A.

indirect recursion

Deriving properties of a base class is called _____.

inheritance

In object-oriented programming, you declare a class that extends another class. This is called ________.

inheritance

Which line(s) immediately flush the output buffer? 1 System.out.print("One for the money"); 2 System.out.print("Two for the show\n"); 3 System.out.println("Three to get ready"); 4 System.out.print("And four to go"); 5 System.out.flush();

lines 2, 3 and 5

A list is a collection that ____.

manages associations between keys and values.

Replace XXX in the following function header for doubly-linked list: ListInsertAfter(currentNode, XXX)

newNode

The most efficient algorithm for sorting integer keys is ________.

radix sort

A method that calls itself is a ________ method.

recursive

Given the list (xlsx, xls, xlr, txt, ods), what is the order of the elements after completing insertion sort's second outer loop iteration?

xlr, xls, xlsx, txt, ods

Using the partition algorithm to partition an array {5, 8, 10, 3, 2, 19, 4} for a quick sort, with 4 as the pivot value, what is the resulting array after the partition?

{2, 3, 4, 8, 5, 19, 10}

Using the partition algorithm to partition an array {5, 8, 10, 3, 4, 19, 2} for a quick sort, with 5 as the pivot value, what is the resulting array after the partition?

{3, 2, 4, 5, 10, 19, 8}

How many additional recursive partitioning levels are required for a list of 64 elements compared to a list of 8 elements?

3

Why does the best recursive method usually run slightly slower than its iterative counterpart?

Each recursive method call takes processor time.

Analyze the following code: Cylinder cy = new Cylinder(1, 1); Circle c = cy;

The code has a compile error.

public class Test { public static void main(String[] args) { Number x = new Integer(3); System.out.println(x.intValue()); System.out.println(x.compareTo(new Integer(4))); } }

The program has a compile error because x does not have the compareTo method.

In the following method, what is the result when printAll("buzz", 0) is called? public static void printAll(String s, int n) { if (s.charAt(n) == 'z') System.out.print(n); else printAll(s, n+1); }

2

What is output? public class BaggageWeight { static void baggageWeight(int weight) { boolean value = false; try { while (!value) { if (weight > 30) { throw new Exception("Double Max Weight"); } if (weight > 15) { throw new Exception("Excess Weight"); } value = true; System.out.println("Accepted"); } } catch (Exception excpt) { System.out.println(excpt.getMessage()); } } public static void main(String[] args) { baggageWeight(42); } }

Double Max Weight

Identify the correct algorithm for reverse traversal in the doubly-linked list studentList.w

ListTraverseReverse(students) { curNode = students⇢tail while (curNode is not null) { Print curNode's data curNode = curNode⇢prev } }

When using an insertion sort, each list element is examined one at a time and moved down if the tested element should be inserted before them.

True

Which of the following statements about generic programming is NOT correct?

Type parameters can be instantiated with primitive data types.

Which of the following completes the selection sort method minimumPosition()? private static int minimumPosition(int[] a, int from) { int minPos = from; for (int i = from + 1; i < a.length; i++) { _______ } return minPos; } }

if (a[i] < a[minPos]) { minPos = i; }

Suppose list1 is an ArrayList and list2 is a LinkedList. Both contains 1 million double values. Analyze the following code: A: for (int i = 0; i < list1.size(); i++) sum += list1.get(i); B: for (int i = 0; i < list2.size(); i++) sum += list2.get(i);

Code fragment A is more efficient that code fragment B.

Which of the following are interfaces implemented by java.util.ArrayList?

List, Iterable

The ________ is at least one case in which a problem can be solved without recursion.

base case

Given the partial LinkedList class declaration below, select a statement to complete the clear method, which removes every element from the list. class LinkedList { public void clear() { // Your code here } }

first = null;

Identify the correct way to insert 52 at the end of the following list: 12, 24, 36, 48.

ListAppend(list, node 52)

Suppose that on a particular computer, it takes the merge sort algorithm a total of 60 seconds to sort an array with 60,000 values. Approximately how long will it take the algorithm to sort an array with 120,000 values?

130 seconds

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)?

2

The array (67, 23, 32, 80, 53, 60) is to be sorted using selection sort. What is the order of the elements after the third swap?

23, 32, 53, 80, 67, 60

Consider the following recursive code snippet: public int mystery(int n, int m) { if (n==0) return 0; if (n==1) return m; return m + mystery(n-1, m); What value is returned from a call to mystery(1,5)?

5

Consider the recursive method myPrint: 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 is printed for the call myPrint(8)?

8

Which of the following lists is sorted?

89, 79, 63, 22

In the implementation of SinglyLinkedClient in class, which of the following are true?

A new Node initially sets its next property to null. Node has a property named next that links to the node after this node. Node has a property named data that stores an element. Node is defined as an inner class inside MyLinkedList.

Which of the following statements are true? It is a compilation error if two methods differ only in return type in the same class. A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass.

All of the above.

Which of the following problems can be solved recursively?

All of these

What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bianca"); names.add(0, "Anthony"); names.remove(1); names.add("Cesar"); names.set(1, "Teresa"); for (String s : names) { System.out.print(s + ", "); }

Anthony, Teresa,

public class Summation{ public static int sum(int arr[]) { int result = 0; int i; for (i = 0; i <= arr.length; i++) { result = result + arr[i]; } return result; } public static void main(String[] args) { int list[] = {1,3,4,5}; try { System.out.println("Sum of given array is " + sum(list)); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Array Index is Out Of Bounds"); } catch (IndexOutOfBoundsException except) { System.out.println("Index is Out Of Bounds"); } } }

Array Index is Out Of Bounds

Analyze the following code where Number is the abstract superclass of Integer and Double. Number[] numberArray = new Integer[2]; numberArray[0] = new Double(1.5);

At runtime, new Integer[2] is assigned to numberArray. This makes each element of numberArray an Integer object. So you cannot assign a Double object to it.

Which of the following statements are true? If a method overrides another method, these two methods must have the same signature. If a method overloads another method, these two methods must have the same signature. A method in a subclass can overload a method in the superclass. A method can be overridden in the same class. A method can be overloaded in the same class.

If a method overrides another method, these two methods must have the same signature. A method in a subclass can overload a method in the superclass.

If a recursive method does not simplify the computation within the method and the base case is not called, what will be the result?

Infinite recursion will occur.

Given the singly-linked list (10, 20, 30, 40, 50, 60), what commands remove 10, 20, and 60, such that the final list is (30, 40, 50)?

ListRemoveAfter(list, null) ListRemoveAfter(list, null) ListRemoveAfter(list, node 50)

Given classes Dog and Mammal. Which is true?

Mammal is the base class

Which of the following is the fastest algorithm to sort a string?

Merge sort

Given the singly-linked list (80, 81, 82, 83), what is returned from ListSearch(list, 84)?

Null

The worst-time complexity for quick sort is ________.

O(n*n)

The average-time complexity for merge sort is ________.

O(nlogn)

Programmers often use a powerful programming paradigm that consists of three key features — classes, inheritance, and abstract classes. What is the paradigm called?

Object-oriented programming

Based on ZyBooks homework, given the list (24, 36, 48), which is the correct way to add 12 at the start of the list?

Prepend(list, 12)

Which data structure is appropriate to store patients in an emergency room?

Priority Queue

Suppose the rule of the party is that the participants who arrive later will leave earlier. Which data structure is appropriate to store the participants?

Stack

In each iteration, selection sort places which element in the correct location?

The smallest element not yet placed in prior iterations.

Which type of relationship is depicted between Building and School? public class Building { private int squareFeet; } public class School extends Building{ private String schoolDistrict; }

There is no relationship between the two classes

If the following nodes are inserted in the order given into a binary search tree, the result will be a tree where all nodes except the leaf node have a right child node, and none have a left child node. 2, 4, 6, 8, 10, 12

True

Complete the code at XXX 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) { XXX } else { answer = baseNum * calcPower(baseNum, exponent-1); } return answer; }

answer = 1;

When removing curNode from a doubly-linked list with at least 2 elements, the list's tail may be assigned with _____.

curNode's predecessor

At least one method in an abstract class must be abstract.

false

On an average, linear search searches ________.

half of the list

Which of the following is a correct interface?

interface B { void print(); }

What is output given the user input in bold? 3 blind mice System.out.print("Enter info: "); Scanner keyboard = new Scanner(System.in); String info = keyboard.nextLine(); Scanner inputScnr = new Scanner(info); int item1 = inputScnr.nextInt(); String item2 = inputScnr.next(); String item3 = inputScnr.next(); System.out.println(item3 + " " + item1);

mice 3

Which XXX would replace the missing statements in the following code to prepend a node in a doubly-linked list? ListPrepend(students, newNode) { if (head == null) { head = newNode tail = newNode } else { XXX head = newNode } }

newNode⇢next = list⇢head list⇢head⇢prev = newNode

Select the correct header for this generic print method. public class GenDemo { public static void main(String[] args) { Integer[] nums = {1, 2, 3, 4, 5}; String[] cities = {"Portland", "Seattle", "Napa", "Chicago"}; print(nums); print(cities); } _______________ { for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); } }

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

Which syntax defines a throws clause for exceptions that are not handled within a method?

public static void myMethod() throws Exception {...}

Complete the code at XXX for the recursive method printSum shown in this code snippet, which is intended to return the sum of digits from 1 to n: public static int printSum(int n) { if (n == 0) return 0; else XXX }

return (n + printSum(n - 1));

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

return 1;

Fill in the code at XXX to complete the following function for computing a Fibonacci number. public static int fib( int index ) { if (index == 0 || index == 1) // Base case XXX; else // Reduction and recursive calls return fib(index - 1) + fib(index-2); }

return index

Complete the code at XXX for the myFactorial recursive method shown below, which is intended to compute the factorial of the value passed to the method: public int myFactorial(int anInteger) { if (anInteger == 1) return 1; else XXX; }

return(anInteger * (myFactorial(anInteger - 1)));


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

To Kill a Mockingbird Vocabulary Chapters 15-17

View Set

Regla de 3 simple directa e inversa

View Set

Unit 02 - Determining Filing Status and Residency

View Set

Chapter 24: Asepsis and Infection control

View Set

Cognition Ch 7: Long Term Memory: Encoding, Retrieval, and Consolidation

View Set

Activity: Genetics of ABO Blood Types

View Set

Chapter 12 Physical Geography: River Systems

View Set