Question: JAVA 1. Select the derived class constructor definitions that represent actual constructor chaining (never mind the reason or effect, as long as it demonstrates a

JAVA

1.

Select the derived class constructor definitions that represent actual constructor chaining (never mind the reason or effect, as long as it demonstrates a derived class constructor chaining to a base class constructor). There is more than one correct answer.

 A Sub() { setDefaults(); super(); // other statements ... } 
 B Sub() { Base(); // other statements ... } 
 C Sub() { super(); // other statements ... } 
 D Sub(int a) { super(); // other statements ... } 
 E Sub() { super(3); // assumes base constructor takes an int // other statements ... }

2 . If a base class reference, p, points to a derived class instance, and both base and derived classes have a public method void doIt() which takes no parameters, then p.doIt() will ...:

A ... invoke the derived class version, because it is the object pointed to that determines which method is called.
B ... invoke the base class version, because type coercion was not used to first coerce the base class reference to a derived class type.
C

... invoke the base class version, because the base class reference only knows about the base class version of doIt().

3.

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

It will print:

Base Sub 
C

It will print:

Sub Sub 
D

It will print:

Sub Base
E

There will be a compiler warning which is not fatal, and main() will still run.

4.

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.]

A 9
B 255
C 4
D 1
E

256

5.

Consider the following search method.

private static int search (int[] a, int target, int left, int right) { if (left > right) return 1 else { int midpoint = (left + right) / 2 if (a[midpoint] == target) return midpoint else if (a[midpoint] < target) return search (a, target, midpoint + 1, right) else return search (a, target, left, midpoint 1) } }

Which of the following might be described as the escape case(s) for this method to end the recursion?

A

when there are no elements in the specified range of indices

if (left > right)
B

when the value is found in the middle of the range

if (a[midpoint] == target)
C

When the midpoint value is greater than the target

if (a[midpoint] > target)
D

When the midpoint value is less than the target

if (a[midpoint] < target)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!