Final CSC 2
Which of the following is true? Question 27 options: A) A map allows duplicate keys to be stored B) A list allows duplicate values to be stored C) A set allows duplicate values to be stored D) No Java collection allows duplicate values to be stored
A list allows duplicate values to be stored
Linear time is the class of all complexity functions that are in ________. Question 8 options: A) O(n log n) B) O(n) C) O(log n) D) O(1)
B) O(n) Linear time complexity, represented as O(n), is the class of all complexity functions where the growth rate of the algorithm is directly proportional to the size of the input. In other words, the time taken by the algorithm increases linearly with the size of the input.
An object that is used to retrieve objects from a collection is called ________. A) a collector B) an accessor C) an iterator D) All of the above
C) an iterator
When a generic class with an unconstrained type parameter is instantiated without specifying an actual type argument ________. A) the type Object is used for the unspecified type B) the computer throws a ClassCastException C) the compiler generates an error D) None of the above
the type Object is used for the unspecified type
Which of the following is true? Question 24 options: A) Both the Set and List interfaces extend the Collection interface B) Both the List and Map interfaces extend the Collection interface C) Both the Set and Map interfaces extend the Collection interface D) None of the above
A) Both the Set and List interfaces extend the Collection interface
An object that can compare two other objects and determine whether one is greater than, less than, or equal to the other is a ________. Question 16 options: A) Comparator B) Comparable C) Sequencer D) Sorter
A) Comparator An object that can compare two other objects and determine their order is typically referred to as a Comparator in Java. The Comparator interface defines a method, compare(), that is used to compare two objects. Objects that implement the Comparator interface can be used to provide custom comparison logic for sorting operations or other ordering-related tasks.
The declarationclass Point<T extends Number>{}is an example of ________. A) a generic class extending a non-generic class B) a generic class extending another generic class C) a generic class extending the class Number D) a non-generic class extending a generic class
A) a generic class extending a non-generic class
A stream operation that converts the stream it is applied to into another stream is called ________. A) an intermediate operation B) a final operation C) a terminal operation D) a reduction
A) an intermediate operation
A static method of a generic class ________. Question 12 options: A) cannot refer to any of the type parameters of its class B) must specify an upper bound for its type parameters C) can refer to any of the type parameters of its class D) None of the above
A) cannot refer to any of the type parameters of its class In Java, when defining a static method in a generic class, the static method cannot directly refer to the type parameters of the class. Static methods operate at the class level, and they do not have access to the type parameters of an instance of the class. They are shared among all instances of the class and are not associated with any specific instance's type parameters. So, option A is correct: a static method of a generic class cannot refer to any of the type parameters of its class.
The stack pull operation ________. Question 46 options: A) does not exist: There is no such stack operation B) brings two stacks together and combines their elements C) increases the capacity of a stack that is about to fill up D) extracts one element from the stack and returns it
A) does not exist: There is no such stack operation
Consider a class that uses the following variables to implement an array-based stack:String [] s = new String[100];int top = 0;a method that implements the String peek() operation can be written as ________. A) if (top == 0)throw new RuntimeException("Underflow");elsereturn s[top-1]; B) return s[top-1]; C) if (top == 0)throw new RuntimeException("Underflow");elsereturn s[top]; D) return s[top];
A) if (top == 0)throw new RuntimeException("Underflow");elsereturn s[top-1]
A static field of a generic class ________. Question 15 options: A) may not have as its type one of the type parameters of the class B) may have any type that is legal in Java C) can only be referenced by a generic static method D) None of the above
A) may not have as its type one of the type parameters of the class In Java, when defining a static field in a generic class, the static field may not have its type as one of the type parameters of the class. Static fields are associated with the class itself rather than with a particular instance of the class, and they are not influenced by the type parameters of instances. Therefore, the type of a static field in a generic class cannot directly depend on the generic type parameters.
The stack peek operation ________. Question 26 options: A) returns the item at the top of the stack, but does not remove it B) adds a single item to the stack C) removes and returns an item from the stack D) checks a stack to see if there are any elements in it
A) returns the item at the top of the stack, but does not remove it The stack peek operation retrieves the item at the top of the stack without removing it. It allows you to examine the element at the top of the stack without modifying the stack's structure.
A TreeSet ________. Question 33 options: A) is like a Set that allows elements to be retrieved according to their natural order, or according to an order specified by a Comparator B) is like a Set, only it is faster C) is a Set that allows elements to be retrieved in the order added D) is a Set that organizes its elements in a tree according to the inheritance hierarchy of the elements
A) is like a Set that allows elements to be retrieved according to their natural order, or according to an order specified by a Comparator A TreeSet in Java is a Set implementation that uses a Red-Black tree to organize its elements. It provides the ability to retrieve elements in their natural order or according to a specified Comparator. By default, it sorts elements in their natural order, but you can also provide a custom Comparator during construction to define a different order.
A doubly linked list makes it easy to ________. Question 41 options: A) move from any node to its successor, and from any node to its predecessor B) skip two nodes at a time when moving backward through the list C) skip two nodes at a time when moving forward through the list D) to create a second copy of the linked list
A) move from any node to its successor, and from any node to its predecessor In a doubly linked list, each node has references (pointers) to both its successor (next node) and its predecessor (previous node). This structure allows for easy navigation in both directions - moving from any node to its successor or moving from any node to its predecessor. This bidirectional linkage makes operations like insertion, deletion, and traversal more flexible compared to a singly linked list.
One of the advantages of using generics is ________. A) that more type problems can be uncovered at compile-time rather than at run time B) that programs that use generic code require less effort in design and development C) that program that use generics execute faster than programs that do not D) that programs that use generics are smaller when translated to byte code
A) that more type problems can be uncovered at compile-time rather than at run time One of the primary advantages of using generics in Java is that they provide stronger type checking at compile-time. This helps catch type-related errors early in the development process, reducing the likelihood of runtime errors and improving the overall robustness and reliability of the code. Therefore, option A is the correct answer.
LinkedHashSet differs from HashSet because ________. Question 34 options: A) the LinkedHashSet allows elements to be retrieved in the same order as they were added B) the LinkedHashSet is a subclass of LinkedList C) the LinkedHashSet stores objects that have the same hash code in the same bucket D) the LinkedHashSet is a subclass of TreeSet
A) the LinkedHashSet allows elements to be retrieved in the same order as they were added The primary difference between LinkedHashSet and HashSet is that LinkedHashSet maintains the order in which elements are inserted into the set. It uses a linked list to keep track of the order, so when you iterate over a LinkedHashSet, the elements are returned in the order they were added. In contrast, HashSet does not guarantee any specific order during iteration.
The code ________.public class MyClass<T>{public MyClass(){T myObject = new T();}} Question 36 options: A) will not compile B) compiles correctly, but causes a runtime exception when the program is executed C) compiles and runs correctly and efficiently D) compiles and runs correctly, but is inefficient
A) will not compile The code will not compile because you cannot instantiate a generic type parameter (T) using new T() directly in Java. The reason is that the type information for T is erased at runtime, and the compiler cannot create an instance without knowing the actual type. If you need to create an instance of a generic type, you typically need to pass a Class object or use other mechanisms such as providing an instance of the class through a constructor or a method.
The automatic conversion of a primitive type to the corresponding wrapper type when being passed as parameter to a generic class is called ________. Question 2 options: A) autoboxing B) type wrapping C) type promotion D) autoconversion
A)autoboxing The process of automatically converting a primitive type to its corresponding wrapper class is called autoboxing. Autoboxing is a feature in Java that allows you to use primitive data types and their corresponding wrapper classes interchangeably in certain situations, such as when working with generics. For example, converting an int to an Integer or a double to a Double is known as autoboxing A corresponding wrapper class is a class in Java that represents a primitive data type and provides utility methods for working with that primitive type. In Java, primitive types (like int, double, etc.) are not objects, but sometimes you need to use them in a context where objects are expected.
What is an array
An array is a data structure that stores a collection of elements, all of the same type, in contiguous memory locations. The elements within an array are identified by index or subscript, starting from 0 for the first element. Arrays provide a convenient way to organize and access a fixed-size sequential collection of elements. Key characteristics of arrays include: Homogeneity: All elements in an array must be of the same data type. For example, an array of integers, an array of characters, etc. Contiguous Memory Allocation: The elements of an array are stored in adjacent memory locations, making it efficient to access elements using indexing. Fixed Size: The size of an array is determined at the time of declaration and cannot be changed during runtime. If a dynamic size is required, other data structures like dynamic arrays or lists may be more appropriate. Here is a simple example of declaring and initializing an array in various programming languages: java example - int[] numbers = {1, 2, 3, 4, 5}; In these examples, numbers is an array containing five integers. You can access individual elements using their indices. For instance, numbers[0] refers to the first element, numbers[1] to the second element, and so on. Arrays are widely used in programming for various purposes, such as storing and manipulating collections of data, implementing algorithms, and representing matrices or grids.
What is an array based stack
An array-based stack is a data structure that implements the stack abstract data type using an array to store elements. A stack is a Last-In, First-Out (LIFO) data structure, meaning that the last element added is the first one to be removed. The array-based implementation provides a straightforward and efficient way to manage a stack. An array-based stack is a data structure that implements the stack abstract data type using an array to store elements. A stack is a Last-In, First-Out (LIFO) data structure, meaning that the last element added is the first one to be removed. The array-based implementation provides a straightforward and efficient way to manage a stack. Here are the basic operations typically supported by an array-based stack: Push: Adds an element to the top of the stack. Pop: Removes the element from the top of the stack. Peek (or Top): Retrieves the element from the top of the stack without removing it. isEmpty: Checks if the stack is empty. An array-based stack can be implemented in various programming languages. Below is a simple example in Java:
Which of the following is true of a HashSet object? A) Increasing the capacity of the HashSet usually has no effect on the number of collisions B) The time required to add an element, or search for an element, increases as the number of collisions increases C) The time required to add an element, or search for an element, is unaffected by the number of collisions D) The time required to add an element, or search for an element, decreases as the number of collisions increases
B) The time required to add an element, or search for an element, increases as the number of collisions increases
n operation on a stream of type to each element of the first stream, is called ________. A) a reduction B) a mapping stream operator C) an iterating stream operator D) None of the above
B) a mapping stream operator
A pipeline of operations is ________. A) a sequence of operations where each operation is a subclass of the previous B) a sequence of operations where the output of each operation becomes the input into the next C) a concatenation of I/O streams that lets data from one stream to the next D) None of the above
B) a sequence of operations where the output of each operation becomes the input into the next
To remove a node with a positive index k from a linked list ________. A) decrement k by 1, and set the reference to the node to be removed to null B) assign the successor reference in the node with index k to the successor reference in the node with index k-1 C) decrement k by 1, and then use recursion D) start a reference r at the head of the list, walk r forward k steps, and then set r to null
B) assign the successor reference in the node with index k to the successor reference in the node with index k-1
A search for an item X in a portion of a sorted array works by repeatedly selecting the middle item and comparing it to X. If X is not found there, the search method selects either the portion of the array to the left of the middle item, or the portion of the array to the right of the middle item and continues the search. This method is called ________. A) selection search B) binary search C) sequential search D) None of the above
B) binary search
A linked list class keeps its elements in the order in which they are added, with the index of an element X being greater than the index of any element added to the list before X. Addition of new elements to such a list can be made more efficient ________. A) by keeping a reference to the element with the least index B) by keeping a reference to the last element added C) by starting at the head, and quickly traversing the list to locate the place where a new element should be added D) None of the above
B) by keeping a reference to the last element added
Collections ________. A) is the interface from which all collection interfaces are derived B) is a class that contains static methods for working with collections C) is the superclass of all abstract and concrete collection classes D) None of the above
B) is a class that contains static methods for working with collections
The load factor of a HashSet ________. A) increases as elements are added, and decreases as elements are removed B) is the fraction of the capacity that must be filled before the capacity is increased C) needs to be kept low to ensure good performance D) is the fraction of the capacity that is available to be filled with new elements
B) is the fraction of the capacity that must be filled before the capacity is increased
The worst-case complexity function f(n) of an algorithm ________. A) is always harder to compute than the average case complexity B) is the maximum number of basic steps performed in solving a problem instance with input size n C) occurs when the load on the system is heaviest D) is the maximum execution time measured when a program with n inputs is executed
B) is the maximum number of basic steps performed in solving a problem instance with input size n
A queue based on a linked list uses the following codeclass Node{String element;Node next;Node (String el, Node n){element = el;next = n;}}Node front = null, rear = null; What is the right code for the boolean empty() method?
B) return front == null; This option checks whether the front pointer is pointing to null, which indicates that the queue is empty. If front is null, then the queue is empty, and the method returns true; otherwise, it returns false.
When using recursion on linked lists ________. Question 19 options: A) the recursive method should be one of the methods specified in the List interface B) the recursive method should be made private, and should be called by a public non-recursive method C) the recursive method should not call itself outside of its base case D) the linked list class is subclassed, and then the recursive method overrides a method of the same name in the list class
B) the recursive method should be made private, and should be called by a public non-recursive method When using recursion on linked lists, it is a common practice to have a private recursive method that performs the actual recursive logic, and a public non-recursive method that serves as an entry point for users of the class. The private recursive method typically includes the base case(s) and the recursive calls. Option B is a good practice because it encapsulates the details of the recursion within the class and exposes a clean interface for external use. It also allows for additional parameters to be passed to the private recursive method as needed for the recursion process.
In Java, the first node in a list has index ________. Question 30 options: A) 1 B) 0 C) 2 D) -1
B) 0 In Java, the indexing of elements in a list (such as an ArrayList or LinkedList) starts from 0. Therefore, the first node in a list has an index of 0.
Which of the following is true? Question 21 options: A) A TreeSet created with the no-arg constructor expects its elements to implement the Comparator interface B) A TreeSet created with the no-arg constructor expects its elements to implement the Comparable interface C) The load factor of TreeSet should never exceed 75% to ensure maximum performance D) The load factor of TreeSet should never exceed 50% to ensure maximum performance
B) A TreeSet created with the no-arg constructor expects its elements to implement the Comparable interface When you create a TreeSet using the no-arg constructor, it expects its elements to implement the Comparable interface. The Comparable interface provides a natural ordering for the elements, and the TreeSet uses this ordering to maintain a sorted set. Option A is not correct because when the no-arg constructor is used, it doesn't expect elements to implement the Comparator interface explicitly. Options C and D are not applicable to TreeSet. Load factors are concepts associated with hash-based data structures like HashMap, and they are not relevant to TreeSet, which is based on a Red-Black Tree data structure.
A Java collection ________. Question 35 options: A) is a class that is defined in the java.awt package B) is an object that is used as a container for other objects C) is an abstract class in the java.collect package D) None of the above
B) is an object that is used as a container for other objects In Java, a collection refers to an object that is used to store and organize other objects. It provides a way to group multiple elements into a single unit. Collections in Java can take various forms, such as lists, sets, queues, and maps, each serving different purposes and providing different behaviors. They are not limited to a specific package like java.awt but are part of the broader Java Collections Framework, often found in the java.util package.
The enhanced for loop, when used on a collection ________. Question 28 options: A) should be avoided because it incurs runtime overhead B) is converted by the compiler to a traditional loop that uses an iterator C) throws an runtime exception D) None of the above
B) is converted by the compiler to a traditional loop that uses an iterator When you use the enhanced for loop (also known as the "for-each" loop) on a collection, the Java compiler converts it into a traditional loop that uses an iterator. This process is done automatically, and it simplifies the syntax for iterating over elements in a collection. Under the hood, the iterator is used to iterate through the elements of the collection without exposing the details of indices or explicit iterators to the programmer.
The best method for searching an array that is not sorted is ________. Question 37 options: A) selection search B) sequential search C) binary search D) None of the above
B) sequential search The best method for searching an array that is not sorted is a sequential search (also known as a linear search). In a sequential search, each element in the array is compared with the target value until a match is found or the end of the array is reached. This search method is straightforward and works efficiently on unsorted arrays but may not be as efficient as binary search on sorted arrays.
In the notation <T extends Number>, the Number class ________. Question 40 options: A) specifies a lower bound for the parameter type T B) specifies an upper bound for the parameter type T C) specifies both an upper and lower bound for the type T D) None of the above
B) specifies an upper bound for the parameter type T In the notation <T extends Number>, the keyword extends is used to specify an upper bound for the parameter type T. It means that T can be any type that is a subclass of Number or Number itself. The upper bound restricts the types that can be used for T to those that extend (are subclasses of) the specified class (Number in this case).
When applied to an array a[] of integers, the pseudo code ________.Boolean sort = trueint k = 0While sort == true and k < a.length-1 If a[k] > a[k+1] Then sort = false End If k = k +1End While Question 32 options: A) will sort the array a[ ] in descending (nonincreasing) order B) will determine if the array is arranged in ascending order C) will determine if the array is arranged in descending order D) will sort the array a[ ] in ascending (nondecreasing) order
B) will determine if the array is arranged in ascending order The given pseudo code checks whether the array a[] is already sorted in ascending order. It sets the boolean variable sort to true initially and then iterates through the array elements, comparing each element with the next one. If it finds any pair of elements out of order (where a[k] > a[k+1]), it sets sort to false. The loop continues until either the entire array is checked or an out-of-order pair is found. So, the primary purpose of this pseudo code is to determine if the array is arranged in ascending order.
Let Point<T> be a generic type. We want to write a method that takes as parameter Point objects whose type parameter is the Number class, or any subclass of Number. We can do this by declaring the type of the method parameter as ________. Question 25 options: A) Point<? super Number> B) Point<Number> C) Point<? extends Number> D) Point<? sub Number>
C) Point<? extends Number> To allow a method to accept Point objects with a type parameter that is the Number class or any of its subclasses, you can use the upper-bounded wildcard <? extends Number>. This allows the method to accept Point objects with any type that is a subclass of Number, providing the desired flexibility for the type parameter.
The JCF Stack class is used to instantiate a stack:Stack<Integer> intStack = new Stack<Integer>();The statementsint k = 77;intStack.push(k*k);use the primitive type int instead of the wrapper type Integer. These statements ________. A) cause a compile time error B) cause a ClassCastException C) compile and execute correctly D) None of the above
C) compile and execute correctly
The process used by the Java compiler to remove generic notation and substitute actual type arguments for formal type parameters is called ________. Question 20 options: A) substitution B) masking C) erasure D) removal
C) erasure The process used by the Java compiler to remove generic notation and substitute actual type arguments for formal type parameters is called "erasure." Java uses type erasure to ensure compatibility with pre-existing Java code that was written without generics. At runtime, the information about generic types is erased, and the compiled code uses the raw types or casts where necessary. This allows generic code to be used seamlessly with non-generic code and helps maintain backward compatibility.
A list can be considered a recursive data structure because ________. A) list objects are instances of list classes B) list classes implement list interfaces C) if you remove the head of the list, what remains is also a list D) None of the above: only methods can be considered recursive.
C) if you remove the head of the list, what remains is also a list A list can be considered a recursive data structure because it can be broken down into smaller subproblems, where each subproblem is essentially a smaller list. Removing the head of the list leaves behind a smaller list, and this recursive structure is a characteristic feature of many recursive data structures.
The selection sort algorithm works by ________. Question 6 options: A) partitioning the unsorted portion of the array into two sublists and a pivot and recursively sorting the two sublists B) repeatedly taking the first value in the unsorted portion of the array and placing it at its proper place in the part of the array that is already sorted C) repeatedly locating the smallest value in the unsorted portion of the array and moving it toward the lower end of the array D) repeatedly comparing adjacent items and swapping them so smaller values come before larger values
C) repeatedly locating the smallest value in the unsorted portion of the array and moving it toward the lower end of the array
Let F be an algorithm with complexity function f(n), and let G be an algorithm with complexity function g(n). If the ratio f(n)/g(n) converges to infinity as n increases to infinity, then ________. A) the algorithm F is asymptotically faster than G B) the two algorithms are asymptotically equivalent in efficiency C) the algorithm G is asymptotically faster than F D) None of the above
C) the algorithm G is asymptotically faster than F
Consider the classclass Value <T extends Number>{private T v;public Value(T v1){v = v1;}public void output(){System.out.println(v);}}The code ________. Value<Number> nV = new Value<Number>(12); A) will compile correctly, but cause an exception at run time B) will cause a compiler error C) will compile and run correctly D) None of the above
C) will compile and run correctly
If lower is the first subscript in a contiguous portion of an array, and upper is the last subscript, then the array item in the middle of that array portion is at subscript ________. Question 31 options: A) (lower - upper)/2 B) lower + upper/2 C) (lower + upper)/2 D) (upper - lower)/2
C) (lower + upper)/2 To find the middle subscript in a contiguous portion of an array, you take the average of the lower and upper subscripts. Therefore, the correct formula is (lower + upper)/2. Option C is the accurate representation of this calculation.
A Node class for a linked list that can hold elements of type Object can be declared to have fields ________. Question 17 options: A) Object element; Node *next; B) Object element; next element; C) Object element; Node next; D) Object element;
C) Object element; Node next; This option correctly declares a Node class for a linked list with fields for storing an element of type Object (Object element) and a reference to the next Node (Node next).
Which of the following is true? Question 14 options: A) The ListIterator interface extends the List interface B) The ListIterator interface is a superinterface of the Iterator interface C) The ListIterator interface is a subinterface of the Iterator interface D) None of the above
C) The ListIterator interface is a subinterface of the Iterator interface
A circularly linked list makes it easy to ________. Question 43 options: A) move from any node to its successor B) move from any node to its predecessor C) jump from the last node to the first D) jump from node to node
C) jump from the last node to the first In a circularly linked list, each node has a reference to its successor, and the last node points back to the first node, forming a circular structure. This allows for easy navigation from the last node to the first node, creating a circular connection. Therefore, option C, "jump from the last node to the first," is a characteristic of a circularly linked list.
In an array-based implementation of a stack, an operation that needs to add a new element to the stack may not be able to complete because the array is full. In this case, the failed operation should ________.
C) throw some appropriately defined exception Explanation: In an array-based implementation of a stack, if an operation that needs to add a new element to the stack cannot be completed because the array is full, it is a situation commonly known as "stack overflow." In this case, throwing an exception is a suitable way to handle the error. By throwing an exception, you provide a mechanism for the calling code to catch and handle the exceptional condition. Option A (print an error message and return from the method) could leave the stack in an inconsistent state and might not inform the calling code adequately about the failure. Option B (print an error message and exit) is generally not recommended in library or application code because it terminates the entire program abruptly, and it doesn't allow for graceful handling of the error. Option C (throw some appropriately defined exception) is a common practice. By throwing an exception, you signal to the calling code that an exceptional condition has occurred, and the calling code can choose to catch and handle the exception or let it propagate up the call stack. This allows for more flexible error handling and provides information about the nature of the error. Option D (alert the user before continuing execution) is less common in low-level implementations like a stack because it introduces a user interface concern, which may not be appropriate at this level. Exception handling is a more standard and flexible way to deal with errors in program logic.
Which of the following statements are true? A) You can declare references to arrays whose elements are of a generic type B) You cannot instantiate an object of a generic type C) You cannot create arrays whose elements are instances of a generic type D) All of the above
D) All of the above
An entry in a map ________. Question 22 options: A) associates a key with a value B) may not share a key value with any other entry in the map C) is called a mapping D) All of the above
D) All of the above An entry in a map typically: A) Associates a key with a value. B) May not share a key value with any other entry in the map. C) Is often referred to as a mapping. So, all of the given options (A, B, C) are correct.
Look at the following method header:void displayPoint(Point<? extends Number> myPoint)Which of the following objects can be passed as an argument to the displayPoint method? Question 11 options: A) Point<Number> p; B) Point <Double> p; C) Point<Integer> p; D) All of the above
D) All of the above The method displayPoint is declared to accept an argument of type Point with a type parameter that extends Number. Therefore, any Point object with a type parameter that is a subclass of Number can be passed as an argument to this method. Options A, B, and C are all valid choices because: A) Point<Number> is valid because Number is the base class. B) Point<Double> is valid because Double extends Number. C) Point<Integer> is valid because Integer extends Number.
Suppose that Point<T> is a generic type, and that we have a method of the formvoid printPoint(Point<?> p){// Code not shown} Question 1 options: A) We may pass any object as an actual parameter for p. B) We may only only pass Number objects as parameters for p. C) We will get a compiler error unless we declare the method abstract. D) None of the above
D) None of the above
The role of the partition(array, start, end) method in Quicksort ________. A) is to split the array into two sorted sublists on either side of the pivot element B) is to sort the segment of the array between start and end C) is to identify the position of the largest value in the part of the array that lies between start and end D) None of the above
D) None of the above
Exceptions of a generic type ________. Question 7 options: A) have very high execution overhead B) may have at most a single type parameter C) may have an unlimited number of type parameters D) are not permitted in Java
D) are not permitted in Java
When you create an instance of a generic class, what types can you pass as arguments for the class type parameters? Question 10 options: A) primitive types only B) interface types only C) primitive, reference, and interface types D) reference types only
D) reference types only When you create an instance of a generic class in Java, you can pass reference types as arguments for the class type parameters. Generics are primarily designed to work with reference types, and you cannot use primitive types directly as type arguments. If you need to use primitive types, you can use their corresponding wrapper classes (e.g., Integer for int, Double for double).
A search for an item X in an array starts at the lower end of the array, and then looks for X by comparing array items to X in order of increasing subscript. Such a method is called ________. A) lower to upper search B) binary search C) selection search D) sequential search
D) sequential search In a sequential search, each element is checked one by one until either the desired element is found or the end of the array is reached.
The best way to measure the goodness of an algorithm is ________. A) to write a computer program that uses the algorithm and time its execution B) to look at the sum of its execution time and the space it uses C) to write a computer program that uses the algorithm and run it on the hardest and largest inputs D) to look at its worst case and average case complexity functions
D) to look at its worst case and average case complexity functions
Which of the following statements is true? Question 29 options: A) A generic class cannot be abstract. B) A raw type is a class that must be extended to be used. C) A raw type is a class that cannot be extended. D) Generic types do not exist at the byte code level.
D) Generic types do not exist at the byte code level. At the bytecode level in Java, generic types are subject to type erasure. This means that the type information is removed during the compilation process, and the resulting bytecode uses raw types. This is done for backward compatibility with pre-generic code and to ensure that generic code can be used seamlessly with non-generic code. Option A is incorrect because a generic class can be abstract. Option B and Option C are incorrect as they don't accurately describe raw types.
In a typical circular doubly linked list, a node has ________. Question 38 options: A) either one of A or C B) a field to store the element, and two references to keep track of two predecessor nodes, and a reference to keep track of the end of the list C) a field to store the element, and two references to keep track of two successor nodes, and a reference to keep track of the start of the list D) a field to store the element, and two references to keep track of successor and predecessor nodes
D) a field to store the element, and two references to keep track of successor and predecessor nodes In a typical circular doubly linked list, each node has a field to store the element, a reference to the next (successor) node, and a reference to the previous (predecessor) node. This allows for efficient traversal in both directions within the circular linked list.
You The operation for adding an item to a queue is called ________. Question 45 options: A) dequeue B) requeue C) proqueue D) enqueue
D) enqueue The operation for adding an item to a queue is called "enqueue."
The maximum number of comparisons that binary search will ever need to make on an array of N elements is ________. Question 23 options: A) N B) N/2 C) N-1 D) the smallest integer k such that 2k is larger or equal to N
D) the smallest integer k such that 2k is larger or equal to N Binary search repeatedly divides the search interval in half. In each step, it compares the middle element of the current interval with the target value. Therefore, the maximum number of comparisons is related to the number of times the array can be divided by 2 until the remaining interval becomes 1 element or less. This is given by the smallest integer k such that 2^k is larger or equal to N.
A hashcode of an object ________. Question 4 options: A) is useful when storing objects in HashMap, HashSet, and List collections B) is an integer that can be used to characterize an object and help identify it C) is the value returned when you store the object into a HashSet collection D) None of the above
In Java, the hashCode() method is a method of the Object class. It returns a 32-bit signed integer that represents the hash code value of an object. The primary purpose of the hash code is to provide a numeric representation of an object that can be used for various purposes, such as optimizing data storage and retrieval. Options A and C are related to the usage of hash codes in collections: Option A: Hash codes are indeed useful when storing objects in HashMap, HashSet, and List collections. Hash-based collections use the hash code to determine the bucket or index where an object should be stored. Option C: The hash code is not directly the value returned when you store an object in a HashSet collection, but it is used internally to determine the bucket in which to place the object. Option D is incorrect because hash codes are used in the context of collections, and they serve the purpose of helping identify objects efficiently. B) is an integer that can be used to characterize an object and help identify it
In a list implementation of a queue, the end of the list at which elements are added is called ________. Question 3 options: A) the bottom of the queue B) the head of the queue C) the rear of the queue D) the front of the queue
In a list implementation of a queue, elements are typically added at one end and removed from the other. The end of the list at which elements are added is referred to as the "rear" of the queue. This is because, in a queue, the order of elements follows the First-In-First-Out (FIFO) principle, where the element that has been in the queue the longest (added first) is the first one to be removed. Therefore, the "rear" of the queue corresponds to the end where new elements are enqueued, and the opposite end, from which elements are dequeued, is often referred to as the "front" or "head" of the queue. So, in the context of the options provided: C) the rear of the queue
What are recursive operations
Recursive operations refer to operations in computer science and programming that involve a function or method calling itself, either directly or indirectly, to solve a smaller instance of the same problem. Recursive operations often have two main components: Base Case: A condition that specifies when the recursion should stop. The base case provides the solution for the smallest, simplest instance of the problem. Recursive Step: A step where the function calls itself to solve a smaller or simpler version of the problem. The recursive step works towards reaching the base case.
When the next() method of Iterator is called at the end of a collection, it ________. Question 2 options: A) throws NoSuchElementException B) wraps around to the beginning of the collection and returns the first element C) returns null D) returns the a copy of the last element returned
The correct option is: A) throws NoSuchElementException When the next() method of an Iterator is called at the end of a collection and there are no more elements to return, it throws a NoSuchElementException to indicate that there are no more elements in the iteration.
The Iterator interface ________. Question 18 options: A) is defined in the javax.swing package B) has next(), hasNext(), and remove() methods C) has next(), isEmpty(), and retrieve() methods D) is defined in the java.awt package
The correct option is: B) has next(), hasNext(), and remove() methods The Iterator interface in Java, which is part of the Java Collections Framework, defines the next(), hasNext(), and remove() methods. These methods are used for iterating over a collection of elements. The next() method returns the next element in the iteration, hasNext() returns true if there are more elements, and remove() removes the last element returned by the iterator.
The stack empty operation ________. Question 4 options: A) destroys the stack and creates a new empty one in its place B) removes all elements from the stack C) checks to see if there is at least one item on the stack D) None of the above
The stack empty operation typically involves checking whether the stack is empty, meaning it contains no elements. This is commonly done by checking if there is at least one item on the stack. Therefore, option C is the correct description of the stack empty operation. C) checks to see if there is at least one item on the stack
The two criteria most often used to measure the efficiency of an algorithm are ________. Question 5 options: A) style and time B) efficiency and style C) space and time D) execution and speed
The two criteria most often used to measure the efficiency of an algorithm are space complexity (how much memory or space the algorithm uses) and time complexity (how much time the algorithm takes to complete its task). C) space and time
Consider a class that uses the following variables to implement an array-based stack:String[] s = new String[100];int top = 0;
a method for adding an item x to the stack can be written as ________. Question 1 options: A) if (top < s.length) {s[top] = x; top++; } else throw new RuntimeException("Overflow");
If a[] is an array of integers, the pseudo codeint k = 0int m = 1While m < a.lengthIf a[m] < a[k] Thenk = mEnd Ifm = m+1End Whiledescribes a strategy for ________. A) sorting an array in descending order B) determining the location of the smallest value in the array C) implementing a part of the logic for bubble sort D) sorting an array in ascending order
determining the location of the smallest value in the array
A stack based on a linked list is based on the following codeclass Node{String element;Node next;Node(String el, Node n){element = el;next = n;}}Node top = null;The code for implementing the String peek() operation is
if (top != null)return top.elementelsethrow new RuntimeException("Empty Stack"); This code checks if the stack is not empty (top != null). If it's not empty, it returns the element at the top of the stack (top.element). Otherwise, it throws a runtime exception indicating that the stack is empty. This is the typical implementation of the peek() operation in a stack, which retrieves the top element without removing it.
Consider a class that uses the following variables to implement an array-based stack:String [] s = new String[100]; int top = -1;// Note top == -1 indicates stack is empty a method that implements a void push(String x) operation can be written as ________.
if (top == s.length-1) throw new RuntimeException("Overflow"); top++; s[top] = x;
Consider a class that uses the following variables to implement an array-based stack: String [] s = new String[100];int top = -1;// Note top == -1 indicates stack is empty a method that implements a String peek() operation can be written as
if (top > -1) return s[top]; else throw new RuntimeException("Empty Stack") The peek() operation is used to retrieve the element at the top of the stack without removing it. In this case, the condition if (top > -1) checks whether the stack is not empty (top index is greater than or equal to 0). If the stack is not empty, it returns the element at the top (s[top]). If the stack is empty (top is -1), it throws a RuntimeException with the message "Empty Stack."
Constraining a type parameter in a generic class ________. A) was added to Java in version 1.3 of the language B) restricts the types that can be used as type arguments C) can only be used when the generic class will be used as a superclass for other classes D) causes programs to compile faster
restricts the types that can be used as type arguments
In many recursive operations on lists ________. Question 3 options: A) some operation that does not require recursion is performed on the head element B) a recursive call is made on the tail of the list C) the base case of the recursion corresponds to the empty list D) All of the above
you often perform some operation on the head element, make a recursive call on the tail of the list, and have a base case that corresponds to the empty list. This is a common pattern in recursive algorithms for processing lists. Option A: Performing some operation on the head element is a common step in recursive list processing. Option B: Making a recursive call on the tail of the list is a typical approach to handle the rest of the list after processing the current element. Option C: Having a base case that corresponds to the empty list is crucial to stop the recursion and define the termination condition. So, all three options (A, B, and C) are commonly observed in recursive operations on lists. D) All of the above