Sample Questions
Sample Questions
Returns a new string that is the Pig Latin version of the String s.
In this method, Pig Latin is formed by taking the prefix of the word (where
the size of the prefix is specified by the value of prefixSize), moving the
prefix to the end of the word and adding the suffix "ay". For example,
"hello" with a prefix size of 1 becomes "ellohay" and with a prefix size
of 2 becomes "lloheay".
Parameters:
s - a string.
prefixSize - the size of the prefix
Precondition:
prefixSize > 0
Returns:
the string as specified above
Throws:
A–1. [2 marks] The main method of an app contains the following statement:
A–2. [2 marks] Suppose the app crashes as a result of the statement above. Who is re-
sponsible: the client, the implementer, or no one? Explain your answer.
The implementer. The precondition is satisfied and the ”throws condition” is not
satisfied.
p. 4 of 8
A–3. [2 marks] Suppose the main method of an app contains the following statement:
Suppose the app crashes as a result of the statement above. Who is responsible:
the client, the implementer, or no one?
A–4. [2 marks] In the case above, what, if anything, should the responsible party do in
order to prevent the crash?
The implementer must have a way to deal with this situation. The implementer
could specify an additional pre condition or could specify in the throws section
A–5. [2 marks] In the case above, what, if anything, should the non-responsible party do
in order to prevent the crash?
Nothing; it is inefficient to do so
A–6. [2 marks] Suppose the main method of an app contains the following statement:
Suppose the method returns the string ”123123123” and the app doesn’t crash. In-
stead, the app continues to run, but this value later causes a big problem because
the Pig Latin translation is incorrect. Who is to blame for this situation and what
should the responsible party have done?
For the class PigLatinServices described in the previous question, we saw that it has a
static method called translateWord(String, int). In addition, this class does not have
a constructor listed in its API.
Read the following statements carefully. For each one, circle T if the statement is True and
F if it is False.
Consider the following application, which compiles and runs without error.
import type.lib.Rectangle3;
public class D1
{
public static void main(String[] args)
{
int width = 10;
Rectangle3 r = new Rectangle3(width, 200);
}
}
In the space below, draw the symbol table and a memory diagram to reflect what has
been accomplished up to including the invocation of the two lines of the main methods
(but before the main method has finished).
For aspects of the diagrams that you cannot know precisely (like memory block addresses and the binary
representations for various numerical values) you can use reasonable alternatives (like invented addresses
and decimal representations instead of binary representations).
p. 7 of 8
Question D: Validation and User Input <10 marks>
In class, we discussed three ways to handle input from the user that is invalid: (1) Ter-
minate the program and display a message; (2) explain the problem and ask the user to
re-enter, and (3) to trigger an error and stop the program.
D–1. [1 mark] Which class have we used that provides services that can be used by an
app in order to stop a program? And which method in this class specifically?
D–2. [4 marks] Why doesn’t the list above also include a fourth alternative, which is to
stipulate that input validity is a precondition? Answer in one or two sentences.
In a perfect world, users would not mistype or misunderstand prompts, but in fact
the chances of the user complying are slim. Since the consequences of violating the
precondition are serious, this strategy is never adopted. See IMD 3.2
D–3. [2 marks] Name at least one class in the Java standard library that provides a ready-
made input component and at least one that provides a ready-made output compo-
nent.
D–4. [3 marks] The assertion programming construct will trigger a runtime error if its
condition is not met. Should it be used for input validation? Explain why or why
not.
E–1. [4 marks] Recall in lecture when we worked on the Mortgage application. This
application required the calculation of the monthly payment for a given amount,
interest rate and amortization period. In lecture, we made use of the static method
getMonthlyPayment(double amount, double rate) which was provided in the class
MortgageServices. In the textbook, the Mortgage application was implemented
without the use of the class MortgageServices (and instead the monthly payment
was calculated within the main method).
Read the following statements carefully. For each one, circle T if the statement is
True and F if it is False.
To expose errors as soon as possible, so that they can be dealt with before they turn
into bigger problems.
E–3. [2 mark] What is an example in Java that demonstrates the strategy of risk mitigation
by early exposure?
E–4. [2 marks] Is Java bytecode platform independent? Is the Java virtual machine plat-
form independent? What is meant by platform independence?
Yes, No, means that it runs on any machine/processor