0% found this document useful (0 votes)
16 views8 pages

Sample Questions

Uploaded by

qasemmohamad227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Sample Questions

Uploaded by

qasemmohamad227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Sample Final Exam Questions

· Duration of test: 85 minutes.

· No questions are allowed during the test.

· This is a closed book test. No aids are permitted.

· Answer each question in the space provided.

· Stay seated; no talking until all tests have been collected.


p. 2 of 8

this page left blank


p. 3 of 8
Question A: Contracts <10 marks>

Suppose the class PigLatinServices contains the following method:

static String translateWord(String s, int prefixSize)}

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:

String result = PigLatinServices.translateWord("Superstar", 2);

In this context, who is the client and who is the implementer?

The client is the main method. The implementer is PigLatinServices.

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:

String result = PigLatinServices.translateWord("Superstar", 15);

Suppose the app crashes as a result of the statement above. Who is responsible:
the client, the implementer, or no one?

The client has satisfied the contract, so the implementer is at fault.

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:

String result = PigLatinServices.translateWord("Superstar", -4);

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?

The client; the client needs to validate the input


p. 5 of 8
Question B: Utility vs Non-Utility Classes <10 marks>

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.

T F 1. PigLatinServices is a non-utility class.


ANS: FALSE
T F 2. PigLatinServices is a utility class and the only reason it is a utility class is because
we cannot instantiate it.
ANS: TRUE
T F 3. PigLatinServices is a utility class and the only reason it is a utility class is because
it has a static method.
ANS: FALSE
T F 4. PigLatinServices is a utility class the reason it is a utility class is because it both has
a static method and cannot be instantiated.
ANS: FALSE
T F 5. It is possible for a class to have both static and non-static methods.
ANS: TRUE (e.g. Fraction)
T F 6. It is possible for a class to have static methods and also for it to be instantiated.
ANS: TRUE (again, Fraction)
T F 7. Utility classes provide the type of delegation that is used in the object-oriented
paradigm.
ANS: FALSE
T F 8. Utility classes provide the type of delegation that is used in the modular paradigm.
ANS: TRUE
T F 9. Utility classes provide the type of delegation that is used in the procedural paradigm.
ANS: TRUE
T F 10. Utility classes are not as versatile as non-utility classes since they can hold only one
state at a time.
ANS: TRUE
p. 6 of 8
Question C: Memory Diagrams <10 marks>

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?

Toolbox, crash(boolean, string)

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.

Input: Scanner, File;


Output: PrintStream, File

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.

No; an assertion is used to express the programmer’s confidence about something;


it should be used for things that are within the programmer’s control; its purpose is
to mitigate risk by early exposure.
p. 8 of 8
Question E: Software Concepts <10 marks>

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.

T F 1. Use of the method getMonthlyPayment(double amount, double rate) is an


example of risk mitigation by early exposure.
ANS: FALSE; see section 2.3.1
T F 1. The wse of the method getMonthlyPayment(double amount, double rate) is
an example of using an obligatory method.
ANS: FALSE; see 149
T F 1. The use of the method getMonthlyPayment(double amount, double rate) is
an example of delegation by abstraction.
ANS: TRUE; see p.57
T F 1. When we examine the body of the method getMonthlyPayment(double amount,
double rate), this is an example of preserving the encapsulation.
ANS: FALSE; see p.60
E–2. [2 mark] What is the rationale behind the strategy of risk mitigation by early exposure?

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?

That variables need to be declared; if the programmer attempts to assign them an


incompatible type, the compiler will issue a warning.
Another possible answer is the use of the programming construct of ”assert” which
is used during development (and not during production).

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

You might also like