Java Object Oriented Programming Final Exam Review Mesa Community College
A dynamic data structure () almost always is implemented using references (pointers) to objects () almost always is implemented using lists of one sort or another () can have a fixed size () is just a collection () None of these
almost always is implemented using references (pointers) to objects
The IllegalArgumentException class extends the RuntimeException class and is, therefore, ________. () never used directly () an unchecked exception class () None of these () a checked exception class
an unchecked exception class
All fields declared in an interface ________. () have protected access () must be initialized in the class implementing the interface () have private access () are treated as final and static
are treated as final and static
Given the following code: Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(int a){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB(){} Line 9 public void method1(){} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC(){} Line 14 public void method1(){} Line 15 } Which method1 will be executed when the following statements are executed? ClassA item1 = new ClassB(); item1.method1(); () method1 on Line 9 () method1 on Line 4 () method1 on Line 14 () This is an error and will cause the program to crash.
method1 on Line 9
In Java there are two categories of exceptions which are ________. () runtime and compile-time () unchecked and checked () critical and nominal () static and dynamic
unchecked and checked
The bubble sort algorithm works by ________. () partitioning the unsorted portion of the array into two sublists and a pivot and recursively sorting the two sublists () repeatedly comparing adjacent items and swapping them so smaller values come before larger values () repeatedly locating the smallest value in the unsorted portion of the array and moving it toward the lower end of the array () 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
repeatedly comparing adjacent items and swapping them so smaller values come before larger values
It always is possible to replace a recursion by an iteration and vice versa, True or False?
True
Which method below is not part of the iterator interface? () add () remove () next () hasNext () all of these are methods in the Iterator interface
add
Exceptions of a generic type ________. () have very high execution overhead () are not permitted in Java () may have at most a single type parameter () may have an unlimited number of type parameters
are not permitted in Java
When writing a class for an array-based or linked implementation of a list, the class that defines an iterator for the list should be () an inner class () public () an interface () none of the above
an inner class
Which of the following is NOT a method of the Object class? () equals () All of the above are methods of the Object class. () clone () toString () compareTo
compareTo
Copying an array is a (n)_______ time operation. () exponential () constant () linear () logarithmic
linear
Having multiple class methods of the same name where each method has a different number of or type of parameters is known as () encapsulation () tokenizing () importing () information hiding () method overloading
method overloading
What is required for an interface method that has a body? () A class that implements the interface must override the method. () The method header must begin with the key word default. () The @Default annotation must precede the method header. () All of these are true.
The method header must begin with the key word default.
The Abstract Data Type (ADT) is thought of as abstract because the operations that are to be implemented are separated from the actual implementation; that is, an ADT can be implemented in more than one way and that implementation is separate from how we might use the ADT, True or False?
True
The key word this is the name of a reference variable that an object can use to refer to itself, True or False?
True
The type of an object is tested with the instanceof operator, True or False?
True
ArrayList<int> aL = new ArrayList<int>(); The declaration ________. () compiles correctly, but causes an exception to be thrown at run time () compiles and runs correctly, but is not recommended () allows the programmer to create an ArrayList that holds integer types () causes a compile-time error
causes a compile-time error
In order to implement Comparable in a class, what method(s) must be defined in that class? () equals () both lessThan and greaterThan () both compares and equals () compareTo () compares
compareTo
When you write a method that throws a checked exception, you must ________. () use each class only once in a method () ensure that the error will occur at least once each time the program is executed () have a throws clause in the method header () override the default error method
have a throws clause in the method header
Consider a class that uses the following variables to implement an array-based stack: Assuming, this implementation throws an exception if the array is full. String[] s = new String[100]; int top = 0; a method for adding an item x to the stack can be written as ________. () if (top < 0) throw new IndexOutBoundsException() s[top] = x; top++; () if (top < s.length) { s[top] = x; top++; } else throw new RuntimeException("Overflow"); () s.add(top, x); () if (top == s.length) throw new RuntimeException("Overflow"); else { top++; s[top] = x; }
if (top < s.length) { s[top] = x; top++; } else throw new RuntimeException("Overflow");
The three major categories of Java collections are ________. () tree sets, list sets, and hash maps () tree sets, list sets, and hash maps () lists, sets, and maps () sets, collections, and maps
lists, sets, and maps
Consider the class class Value < T extends Number> { private T v; public Value(T v1) { v = v1; } public void output() { System.out.println(v); } } The code ________. Value <Number> nV1 = new Value <Number>(34.5); () will compile correctly, but cause an exception at run time () will cause a compiler error () will compile and run correctly () None of the above
will compile and run correctly
ArrayList <String> [] a = new ArrayList<String>[100]; The code _________. () compiles and runs correctly, but is inefficient () compiles correctly, but causes a runtime exception when the program is executed () will not compile () compiles and runs correctly and efficiently
will not compile
________ is the term for the relationship created by object aggregation. () "Is a" () One-to-many () "Has a" () Inner class
"Has a"
Assume that a linked list is implemented using the Node class where a Node contains instance data of int info; and Node next; where next references the next Node in the linked list. Also assume that head references the first Node in the list. Assume Node temp references the last element of the linked list. Which of the following conditions is TRUE about temp? () (temp.next == null) () (temp == head) () (temp.next == null && temp.info == null) () (temp == null) () (temp.info == 0)
(temp.next == null)
What will the following code display? String input = "99#7"; int number; try { number = Integer.parseInt(input); } catch(NumberFormatException ex) { number = 0; } catch(RuntimeException ex) { number = 1; } catch(Exception ex) { number = -1; } System.out.println(number); () 1 () 0 () 99 () -1
0
Assume a stack class stores int values. Consider the following sequence of instructions. Stack <Integer> s = new Stack<>(); s.push(16); s.push(12); s.push(19); int x = s.pop(); s.push(5); s.push(9); s.push(4); int y = s.pop(); int z = s.pop(); After the instructions execute, z has the value () 16 () 12 () 9 () 5 () 4
9
Given the following partial class definition: public class A1 { public int x; private int y; protected int z; ... } public class A2 extends A1 { protected int a; private int b; ... } public class A3 extends A2 { private int q; ... } Which of the following is TRUE with respect to A1, A2, and A3? () A3 is a subclass of A2 and A2 is a subclass of A1. () A1 and A2 are both subclasses of A3. () A2 and A3 are both subclasses of A1. () A1 is a subclass of A2 and A2 is a subclass of A3. () A1, A2 and A3 are all subclasses of the class A.
A3 is a subclass of A2 and A2 is a subclass of A1.
In a UML diagram for a class () there may be a section containing the methods of the class () All of these () there may be a section containing the name of the class () classes are represented as rectangles () there may be a section containing the attributes of the class
All of these
The ____________ class implements many static utility methods that can be used with various types of collection objects. () Collection () Collectors () Collections () Collector
Collections
The parent class of Error is () Object () RunTimeError () RunTimeException () Exception
Exception
The instruction super(); does which of the following? () It calls the constructor as defined in the current class.() () It calls the method super as defined in the current class. () It calls the method super as defined in java.lang. () It calls the constructor as defined in the current class's parent class. () It calls the method super as defined in the current class's parent class.
It calls the constructor as defined in the current class's parent class.
Binary Search has a worst case of ________. () O(n log n) () O(n) () O(log n) () O(1)
O(log n)
A method's parameter can be polymorphic, giving the method flexible control of its arguments, True or False?
True
A recursive method without a base case leads to infinite recursion, True or False?
True
One of the advantages of using generics is ________. There are more than one correct answers. [] that programs that use generic code require less effort in design and development [] that programs that use generics are smaller when translated to byte code [] that more type problems can be uncovered at compile-time rather than at run time [] that program that use generics execute faster than programs that do not [] increased type-safety without the need to do typecasts at run time
[] that more type problems can be uncovered at compile-time rather than at run time [] increased type-safety without the need to do typecasts at run time
The notation < E extends Comparable<E> > () will be rejected by the Compiler as an error () is used when there is a recursive definition of the generic type E () declares a generic type that implements the Comparable interface () None of the above
declares a generic type that implements the Comparable interface
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 pop() operation can be written as ________. () if (top == 0) throw new EmptyCollectionException("Stack"); return s[top-1]; top--; s[top] = null; () top--; return s[top]; () if (top == 0) throw new EmptyCollectionException("Stack"); top--; String temp = s[top]; s[top] = null; return temp; () if (top == 0) throw new EmptyCollectionException("Stack"); String temp = s[top]; top--; s[top] = null; return temp;
if (top == 0) throw new EmptyCollectionException("Stack"); top--; String temp = s[top]; s[top] = null; return temp;
Java does not support multiple inheritance but some of the abilities of multiple inheritance are available by () importing classes () implementing interfaces () creating aliases () using public rather than protected or private modifiers () overriding parent class methods
implementing interfaces
The insertion sort algorithm works by ________. () repeatedly locating the smallest value in the unsorted portion of the array and moving it toward the lower end of the array () 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 () repeatedly comparing adjacent items and swapping them so smaller values come before larger values () partitioning the unsorted portion of the array into two sublists and a pivot and recursively sorting the two sublists
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
Given the following method header, what will be returned from the method? public Rectangle getRectangle() () the address of an object of the Rectangle class () the values stored in the data members of the Rectangle object () an object of the class Rectangle () a graph of a rectangle
the address of an object of the Rectangle class
The only limitation that static methods have is ________. () they cannot refer to nonstatic members of the class () they can only be called from static members of the class () they must be declared outside of the class () they can refer only to nonstatic members of the class
they cannot refer to nonstatic members of the class
What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0. for (j = 0; j < list.length; j++) if (list[j] < temp) c++; () It finds the largest value and stores it in temp. () It counts the number of elements in list that are less than temp. () It finds the smallest value and stores it in temp. () It counts the number of elements equal to the smallest value in list. () It sorts the values in list to be in ascending order.
It counts the number of elements in list that are less than temp.
The following statement creates an ArrayList object. What is the purpose of the notation? ArrayList <String > arr = new ArrayList<>(); () It specifies that only String objects may be stored in the ArrayList object. () It specifies that the get method will return only String objects. () It specifies that String objects may not be stored in the ArrayList object. () It specifies that everything stored in the ArrayList object will be converted to a String.
It specifies that only String objects may be stored in the ArrayList object.
If a subclass constructor does not explicitly call a superclass constructor, ________. () it must include the code necessary to initialize the superclass fields () Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes () the superclass's fields will be set to the default values for their data types () Java will automatically call the superclass's default or no-arg constructor immediately after the code in the subclass's constructor executes
Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes
In an ideal implementations of a stack and a Queue, all operations are ______________________ () O(1) () O(n) () O(n log n) () It depends on the operations
O(1)
Sequential Search has a worts case of ________. () O(1) () O(n) () O(n log n) () O(log n)
O(n)
Consider a method of the form: void printPoint(Point <Number> p) { //Code not shown } where Point is a generic type. When the method is called, the only objects that may be passed for the parameter p are ________. () Point<Number> objects only () Point<T> objects, where T is the Number class, or is any superclass of Number () Point<T> objects, where T is the Number class, or is any subclass of Number () All of the above
Point<Number> objects only
In order for an object to be serialized, its class must implement the ________ interface. () Serializable () Serial () ObjectOutputStream () Writeable
Serializable
An abstract class contains both abstract and non abstract method, True or False?
True
An interface reference can refer to any object of any class that implements the interface, True or False?
True
Which of the statements is true about the following code snippet? int[] array = new int[25]; array[25] = 2; () This code will result in a compile-time error. () The integer value 25 will be assigned to the third value in the array. () TrueThis code will result in a run-time error. () The integer value 2 will be assigned to the last index in the array.
TrueThis code will result in a run-time error.
The Quicksort algorithm ________. () is in O(log n) in the average case () is in O(n log n) in the worst case () is in O(log n) in the average case () is in O(n log n) in the average case
is in O(n log n) in the average case
Which of the following statements correctly specifies two interfaces? () public class ClassA implements (Interface1, Interface2) () public class ClassA implements Interface1 | Interface2 () public class ClassA implements [Interface1, Interface2] () public class ClassA implements Interface1, Interface2
public class ClassA implements Interface1, Interface2
In a class hierarchy ________. () the more general classes are toward the top of the tree and the more specialized classes are toward the bottom () the more general classes are toward the left of the tree and the more specialized classes are toward the right () the more general classes are toward the right of the tree and the more specialized classes are toward the left () the more general classes are toward the bottom of the tree and the more specialized classes are toward the top
the more general classes are toward the top of the tree and the more specialized classes are toward the bottom
In a try/catch construct, after the catch statement is executed ________. () the program terminates () the program resumes at the first statement of the try statement () the program resumes at the statement that immediately follows the try/catch construct () the program returns to the statement following the statement in which the exception occurred
the program resumes at the statement that immediately follows the try/catch construct
To compare String objects for the purpose of sorting, a programmer should ________. () use the compareTo method of the Comparable interface () use the comparison operator < () use the comparison operator <= () use the relational operator < to compare references to the String objects
use the compareTo method of the Comparable interface
Consider the following operations on a queue data structure that stores int values: Queue q = new Queue(); q.enqueue(3); q.enqueue(5); q.enqueue(9); System.out.println(q.dequeue()); // d1 q.enqueue(2); q.enqueue(4); System.out.println(q.dequeue()); // d2 System.out.println(q.dequeue()); // d3 q.enqueue(1); q.enqueue(8); What value is returned by the last dequeue operation, denoted with a d3 as a comment? () 2 () 4 () 3 () 9 () 5
9
Java allows one to create polymorphic references using inheritance and using interfaces, True or False?
True
The following method lacks a base case, True or False? public int noBaseCase(int x) { if (x > 0) return noBaseCase(x - 1) + 1; else return noBaseCase(x - 2) + 2; }
True
A HashMap ________. () is a subclass of HashSet that implements the Map interface () is a subclass of Map that implements the HashCode interface () uses hash codes to store keys () None of the above
uses hash codes to store keys