Question: Question 1 Consider this method definition: static int someRecMethod( int n ) { if ( n < 0 ) return -1; return someRecMethod( n +
Question 1 Consider this method definition:
static int someRecMethod( int n ) { if ( n < 0 ) return -1; return someRecMethod( n + 1 ); } This method has the following problem -- or none (only one correct choice):
| | A) It will always return the same number no matter what is passed in as an argument from the client. |
| | B) Nothing is wrong; it has both an explicit case and a recursive case. |
| | C) It will sometimes return a -1 without error, and other times produce a runaway recursive call to itself resulting in a run-time error |
| | D) It will always produce a runaway recursive call to itself resulting in a run-time error. |
Question 2 Select the derived class method definitions that represent method or constructor chaining (i.e., calling the base class method or constructor from the (overriding) derived class method or constructor) There may be more than one correct answer.
| | A) class Sub extends Base { Sub() { Base(); } } |
| | B) class Sub extends Base { void methodX() { super.methodX(); // other stuff } } |
| | C) class Sub extends Base { void methodX() { Base.methodX(); // other stuff } } |
| | D) class Sub extends Base { void methodX() { super(); // other stuff } } |
| | E) class Sub extends Base { Sub() { super(); // other statements ... } } |
Question 3 Which of the following statements are true? May be more than one
| | A) The stack is a "Last In, First Out" or LIFO structure.. |
| | B) A stack is a random access container that allows access to any element, regardless of its position in the stack. |
| | C) The stack is a "First In, First Out" or FIFO structure. |
| | D) A stack is a collection that restricts access at one end of the stack.. |
| | E) You can only add new elements to one end of a stack, and you can only remove items from that same end. |
| | F) You can only add new elements to either end of a stack, and you can only remove items from either end. |
Question 5 Searching an array using a binary search (check all that apply):
| | A) ... is usually slower for each search than a simple linear search. |
| | B) ... is usually faster for each search than a simple linear search. |
| | C) ... requires more code and logic than a simple linear search. |
| | D) ... requires a pre-sorted array in order to work. |
Question 6 Consider the following code fragment (assumed to be in an otherwise correct program):
double [] dubArray1 = new double[75]; double [] dubArray2 = new double[100]; // there are no intervening statements. immediately following we have: dubArray1 = dubArray2; dubArray1[99] = 101;
Check the true statements (may be more than one):
| | A) Some allocated memory becomes inaccessible, but that's not a fatal error. |
| | B) It has an array bounds violation, possibly causing a fatal runtime error. |
| | C) It will compile without error but probably crash (i.e., generate a run-time error). |
| | D) It has a compiler error. |
| | E) It has no compiler or runtime errors. |
Question 8 A binary search of a pre-sorted array of 256 elements would take (at most) how many comparisons? (Assume the search key is the key on which the array is sorted).
[I allow for off-by-one errors in this problem, so if your prediction is within one of the posted choice below, choose it.]
Question 9 Check the true statement(s)
(There is at least one correct choice, possibly more.)
| | A) An instance method can access an instance member (of the same class) inside its definition using only the member name, no object or class name prepended (i.e., without ClassName. or someObj. in front). |
| | B) An instance method can access a static member (of the same class) inside its definition using only the member name, no object or class name prepended (i.e., without ClassName. or someObj. in front). |
| | C) A static method can access a static member (of the same class) inside its definition using only the member name, no object or class name prepended (i.e., without ClassName. or someObj. in front). |
| | D) A static method can access an instance member (of the same class) inside its definition using only the member name, no object or class name prepended (i.e., without someObj. in front). |
Question 10 Assume a base, Base has a derived class, Sub.. Also, assume a method called methodX() is defined in Base and overridden in Sub. For simplicity, let's say the only thing this method does is to print out "Base" if Base's methodX() is called and "Sub" if the Sub's methodX() is called.
main() has these lines, in this order, exactly, (and all other lines are correct and unrelated to these) :
Base b = new Base(); Sub s = new Sub(); b = s; s.methodX(); b.methodX();
Check the true statements (there may be one or more):
| | A) There are no compiler errors or warnings. |
| | B) There will be a compiler warning which is not fatal, and main() will still run. |
| | C) It will print: Base Sub |
| | D) It will print: Sub Base |
| | E) It will print: Sub Sub |
Question 11 Which of the following are good candidates for instance variables for a class that you would define?
| | A) Private data whose values could be different for different objects of the class. |
| | B) Variables whose values are shared by all class objects. |
| | C) Variables (like loop counters or temporary variables) used by most or all of your instance methods (in an attempt to avoid redeclaring these variables multiple times as locals for each instance method). |
| | D) Constants used by the class and the client. |
Question 12 We would test whether a parseInt() failed due to a NumberFormatException by:
| | A) placing the parseInt() call in the try portion of a try/catch block. |
| | B) placing the parseInt() call in the catch portion of a try/catch block. |
| | C) using an if statement block that appeared after the parseInt() statement. |
| | D) using a try/catch block that appeared after the parseInt() statement. |