Assume MyClass contains a public method, doSomething() , which throw s a variety
ID: 3739777 • Letter: A
Question
Assume MyClass contains a public method, doSomething(), which throws a variety of exceptions, like ArithmeticException, IndexOutOfBoundsException, etc., each under certain distinct error conditions, and returns without throwing anything when it decides no error has occurred. We call doSomething() four times in a try/catch block:
MyClass a = new MyClass();
try
{
a.doSomething();
a.doSomething();
a.doSomething();
a.doSomething();
}
catch (ArithmeticException e)
{
System.out.println(" bad math ");
}
catch (IndexOutOfBoundsException e)
{
System.out.println(" bad bounds ");
}
catch (NumberFormatException e)
{
System.out.println(" bad format ");
}
catch (Exception e)
{
System.out.println(" just bad ");
}
}
Check the true statements (there will be more than one correct answer).
[Ignore whether spaces or ' ' are displayed - it is the words of println()statements that we are interested in.]
After this block is executed, regardless of the conditions that prevail whendoSomething() is called, at least one of the "bad" messages will be displayed on the screen.
It is possible that doSomething() will return without error in one or more of these invocations, yet we still have one or more of "bad" messages displayed on the screen after this block.
It is possible neither "bad math" nor "bad bounds" is displayed on the screen after this block.
It is possible that both "bad math" and "bad bounds" are displayed on the screen after this block.
It is possible none of "bad math", "bad bounds", "bad format", or "just bad" is displayed on the screen after this block.
------------------------------------------------
Assume that a class, Deep, has an array reference member
private int someArrayRef[];
which will be assigned some dynamically-allocated array using new during object construction (in the constructor).
Assume, further, that the clone() for class Deep is very short:
public Object clone() throws CloneNotSupportedException
{
Deep newDeepObj = (Deep)super.clone();
return newDeepObj;
}
Two Deep objects, deepA and deepB are involved in an assignment statement in the client:
deepB = (Deep)deepA.clone();
Check the true statements:
(There may be more than one.)
deepB's someArrayRef will have its own copy of int array associated with its someArrayRef member after the assignment statement (distinct from deepA's)
After this assignment, both deepA and deepB will have their respective someArrayRef pointing to the same exact memory.
deepB's someArrayRef will be undefined (unaffected) by this assignment statement.
This is a shallow copy; deep memory controlled by deepA is not copied and that copy pointed to by deepB.
==========================================
MyClass has an internal 2-D array of dynamically allocated doubles, pointed to by the member named data:
class MyClass
{
private double data[][];
private int width, height;
// other stuff
}
Assume width and height are set by constructors and mutators, and the class deals with them and all other data management correctly. Here is the instance of MyClass method we are interested in analyzing:
// re-allocate an array using the parameters passed as the new size
void allocateArray(int newHeight, int newWidth)
{
// make sure the arguments supplied are in legal bounds
if (!valid(newHeight, newWidth))
return;
data = new double[height][width]; // allocate with new sizes
height = newHeight;
width = newWidth;
setArrayToAllOnes(); // place 1.0 into all locations of the array
}
Assume the methods invoked have been correctly defined, elsewhere. Check the true statements (there will be one and possible more):
setArrayToAllOnes() needs parameters to know what bounds to use for its (likely) internal loops.
It's fine as is.
The statement
data = new double[height][width];
needs to be repositioned to a different place in the code because it is using the old value rather than the new values for allocation.
It could be fine that we are returning without doing anything if valid() returns false. It may be, e.g., that the class designer does not want to disturb the existing array in that case.
After this block is executed, regardless of the conditions that prevail whendoSomething() is called, at least one of the "bad" messages will be displayed on the screen.
Explanation / Answer
1)
True: It is possible none of "bad math", "bad bounds", "bad format", or "just bad" is displayed on the screen after this block.
True: It is possible neither "bad math" nor "bad bounds" is displayed on the screen after this block.
2)
true: deepB's someArrayRef will have its own copy of int array associated with its someArrayRefmember after the assignment statement (distinct from deepA's)
3)
true:
The statement
data = new double[height][width];
needs to be repositioned to a different place in the code because it is using the old value rather than the new values for allocation.
true:It could be fine that we are returning without doing anything if valid() returns false. It may be, e.g., that the class designer does not want to disturb the existing array in that case.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.