IS247 exam 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

What is the following data structure? public class GenericStructure<E> { private java.util.ArrayList<E> list = new java.util.ArrayList<>(); public int getSize() { return list.size(); } public E look() { return list.get(getSize() - 1); } public void addorput(E o) { list.add(o); } public E takeitout() { E o = list.get(getSize() - 1); list.remove(getSize() - 1); return o; } public boolean isEmpty() { return list.isEmpty(); } @Override public String toString() { return list.toString(); } }

stack

if recursion does not have a special terminating case what error will occur

stack overflow

which of the following is used for generic programming

templates

java provides two collections classes that can be used to implement stacks. they are ___ and ___

the ArrayStack class, the LinkedList class

in a map

the keys are unique

A systematic method that starts at the beginning of the list and processes every node is called __________________.

traversal

Like a loop, a recursive function must have some method to control the number of times it repeats

true

T/F for recursive code the problem that can be solves now is called the "base case"

true

The contains method of the List ADT (Abstract Data Type) lets you know if a particular element is in a list

true

a linked list is a linear ordering of objects in which an object has a reference to the next object in the list

true

myStack.peek(); returns the element at the top of the stack

true

poll() removes an element from the front of a queue

true

recursive algorithms can also be coded with iterative methods t/f

true

t.f recursion is a process of solving a problem by reducing it to smaller versions of itself

true

t/f recursive algorithms are less efficient than iterative algorithms

true

t/f the binary search algorithm can be coded as a recursive function

true

which kind of parameter do we pass to a generic class

type parameter

a HashMap

uses hash codes to store keys

can a recursive function have more than one base case? Y/S

yes

he programmer must ensure that a recursive function does not become

an endless series of branches to recursive code

a linked list can hold ___ nodes

any number of nodes

stacks are implemented by which one?

array

conversion of a primitive type to its wrapper type when passed a parameter to a generic class

autoboxing

the queue version of add() inserts an element at the ___ of the queue

back

in a recursive solution, ______ is(are) always necessary

base

which real world application uses recursion to calculate the answer

bowling score

a Linked HashMap

can be set up to maintain its keys in insert or access order

To empty a Collection or a Map, you use the __________ method

clear

an ___ is a class structure to gather and organize other objects

collection

Which of the following is correct to sort the elements in a list named "lst?

collections.sort(1st)

the ___ of recursion is the number of times a function calls itself

depth

When recursive methods directly call themselves, it is known as __________.

direct recursion

In a set, the ______ must be unique.

elements

a hashCode method returns an____

int

a hashcode of an object___

is an integer that can be used to characterize an object and help identify

what will be displayed? what do you need to do to fix it? public class Factorial { public static void main(String[] args) { int num = 6; long factorial = multiplyNumbers(num); System.out.println("Factorial of " + num + " = " + factorial); } public static long multiplyNumbers(int num) { if (num >= 1) { return num * multiplyNumbers(num - 1); } else return num; } }

0, return 1 instead of num for second condition

what will be displayed. import java.util.*; public class Test { public static void main(String[] args) { PriorityQueue<Integer> queue = new PriorityQueue<Integer>( Arrays.asList(1, 2, 3, 4, 5)); while (!queue.isEmpty()) System.out.print(queue.poll()); System.out.print(queue.size()); } }

1 2 3 4 5 0

If the following code executed what would the stack contents be from top to bottom? bottom to top? myStack.push(new Integer(1)); myStack.push(new Integer(2)); Integer num1 = myStack.pop(); myStack.push(new Integer(3)); myStack.push(new Integer(4)) myStack.peek(); myStack.pop(); myStack.push(new Integer(5));

1, 3, 5 opposite for bottom to top

after these on an empty stack what is the top down result? push(8);push(7);pop();push(19);

19 8

what will be displayed public class Practice1 { public static void main(String[] args) { System.out.println(fun1(10)); } public static int fun1(int n) { if (n == 3) return 3; else return fun1(n - 1); } }

3

public class Factorial{ static int factorial(int n){ if (n!= 0) //recursive call return n * factorial(n-1); else return 1;} public static void main(String[] args){ int number = 3, result; result = factorial(number); System.out.println(result); } } what is the output

6

what will be displayed import java.util.Comparator; import java.util.PriorityQueue; public class PriorityQueueTest { public static void main(String[] args) { PriorityQueue<Double> queue = new PriorityQueue<Double>(5, new DoubleComparator()); queue.offer(3.2); queue.offer(9.8); queue.offer(5.4); while (queue.size()> 0) { System.out.printf("%.1f ", queue.peek()); queue.poll(); } } private static class DoubleComparator implements Comparator<Double> { public int compare(Double first, Double second) { return -Double.compare(first, second); } } }

9.85.43.2

which of the following is not a valid generic type

<2 extends Number>

to create a generic type bounded by Number use

<E extends Number>

recursion occurs when_____

A method calls itself

which method below is not part of the iterator interface

Add

_______ are generic classes or interfaces

ArrayList Comparable

which of the following is true

Both the Set and List interfaces extend the Collection interface

what exception is thrown if the pop method is called on an empty stack implemented using the ArrayStack

EmptyStackException

a queue is a ___ data structure

FIFO

which one is a queue

FIFO

if the following executed what will be displayed public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Jenny"); list.add("Chhaya"); list.add("Sahara"); list.add("Jose"); list.add("Abdul"); list.add("Sophia"); for (int i = 0; i < list.size(); i=i+2) System.out.print(list.remove(i)); } }

JennyJose

the concrete classes that implement the List interface are

LinkedListand ArrayList

which is a generic

List<String>

what will be displayed import java.util.*; public class Test { public static void main(String[] args) { Map<Integer, String> map = new LinkedHashMap<>(); map.put(1, "Jenny"); map.put(2, "Chhaya"); map.put(3, "Sahara"); map.put(1, "Monsoon"); System.out.print(map); } }

Monsoon, 2=Chhaya, 3=Sahara}

what does it mean when a class is generic

The actual type is decided when you instantiate the class

which one is a java collection that has elements that are ordered by a characteristic of the value of the elements

TreeSet

what will be displayed import java.util.*; public class Test { public static void main(String[] args) { Set<String> set1 = new HashSet<>(); set1.add("Jenny"); set1.add("Chhaya"); set1.add("Sahara"); Set<String> set2 = new HashSet<>(); set2.add("Sahara"); set2.add("Jose"); set2.add("Jenny"); set1.removeAll(set2); System.out.println(set1); } }

[Chhaya]

what will be displayed import java.util.*; public class Test { public static void main(String[] args) { LinkedHashSet<String> set1 = new LinkedHashSet<>(); set1.add("Jenny"); set1.add("Chhaya"); LinkedHashSet<String> set2 = set1; set2.add("Sahara"); set1.add("Monsoon"); set2.add("Jose"); System.out.println(set2); } }

[Jenny, Chhaya, Sahara, Monsoon, Jose]

When the following code executes, what will be displayed? ArrayList<String> list = new ArrayList<>(); list.add("Jenny"); list.add("Chhaya"); list.add("Sahara"); list.remove(1); System.out.println(list);

[Jenny, Sahara]

a stack interface is defined and has an isEmpty()abstract method. What method should this return

a boolean value representing whether the stack is empty or no

a set is

a collection of elements with no duplicate elements

A map is _________________________________

a collection whose elements have two important attributes, keys and values

A list in which each stored element is associated with a reference to its successor is called

a linked list

Which statement about linked lists is false?

a linked list is a fixed data structure

recursion occurs when

a method calls itself

java compiler does this to remove generic notation and sub real type args for formal parameters

erasure

A recursive function cannot call another function.

false

Recursive solutions are always more efficient than iterative solutions

false

The contains operation of the Set interface returns a count of the number of occurrences of an element in the set

false

The head of a linked list is also a linked list node.

false

The index of an element to be added to a list can be the same as the size of the lis

false

The peek operation can retrieve a value from anywhere in the stack.

false

The remove operation returns a boolean value that indicates if the element to be removed was found in the list

false

recursive solutions should be used whenever possible

false

some problems can only be recursively

false

t/f the base case does not stop recursion

false

the pop() function retrieves the element at the of the stack

false

the removeoperation returns a boolean value that indicates if the element to be removed was found in the list

false

the size of a dynamic stack must be known in advance

false

what item is always retrieved from a stack

first

which of the following is not a queue operation

first

what item is always retrieved from a queue

front

Which of these method Map class is used to obtain an element in the map having specified key?

get()

a class is generic if it

has type parameters

Suppose your program frequently tests whether a student is in a soccer team and also need to know the students information such as phone number, address, and age, what is the best data structure to store the students in a soccer team?

hashmap

In the following code that uses recursion to find the greatest common divisor of a number, what is the base case? public static int gcd(int x, int y) { if (x % y == 0) return y; else return gcd(y, x % y); }

if (x % y == 0) return y;

an advantage of using generic types is

increased type-safety w/o the need to typecast at run-time

which type of recursion is more difficult to debug

indirect

what is it called when function A calls function B which in turn calls function A

indirect recursion

____ recursion results from the lack of a base case

infinite

the iterator () method is defined in the ___ interface

iterable

To get an iterator from a set, you may use the __________________ method.

iterator

A circularly linked list makes it easy to __________________

jump from the last node to the first node

to remove a node with index 0 from a linked list

just move the headreference one node forward

Which of these methods can be used to obtain set of all keys in a map?

keyset()

using a generic is intended to

make code reusable

Which of the following data structure stores key and value pairs?

map

which of the following data types does not implement the collection interface

map

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

n * factorial (n-1)

A priority queue orders its elements according to ______________________.

natural order using Comparable

can main be called recursively y/n

no

the objects that form the units of memory allocation in a linked list are called

nodes

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

priority queue

to declare a class named A with two generic types use

public class A<E, F> { ... }

to declare a class named A with a generic type use

public class A<E> { ... }

to declare a class named A with a generic type, use

public class A<E> { ... }

Write code to print linked list elements assuming the list starts at location "head" and uses ListNode and LinkedListNode from our programming assignment, including getData and getNext. How would this be different if it were recursive?

public void display() { LinkedListNode current = head; while(current.getNext() != null){ System.out.println(current.getData()); current = current.getNext(); } System.out.println(current.getData()); }

which of the following methods inserts an element into a stack data structure

push

which of the following is not a valid PriorityQueue() function?

push()

Which data structure is appropriate to store customers in a clinic for taking flu shots?

queue

offer() inserts an element at the ____ of the queue

rear

A _____________________ method is one that calls itself

recursive

ArrayList is more efficient than LinkedList for the following operation:

retrieve an element given the index

public static void countdown(int n) { if (n==0) { System.out.println("Blastoff!"); }else{ System.out.println(n); countdown(n-1); } } what is the output if this method is called when n=-1

segmentation ault

what happens if a recursive never returns

segmentation fault

Which of the following data types does not allow duplication?

set

the ListIterator method that replaces an existing element with a new element is

set

what is one of the benefits of recursion

shorter code

comparable

specifies a single method, compareTo

comparator

specifies two methods, compare and equals


Ensembles d'études connexes

PRE, POST, AND NCLEX QUESTIONS FOR PSYCH EXAM 2

View Set

Chapter 31 - Care of Patients with Noninfectious Upper Respiratory Problems

View Set

How to Read Literature Like a Professor study guide

View Set