Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?xml version="1.0" encoding="UTF-8"?>
<section version="5.0" xml:id="sd1_exam_2023_summer" xml:lang="en"
xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:trans="http://docbook.org/ns/transclusion"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<title>SD1 examination summer 2023</title>
<section xml:id="sd1_exam_2023_summer_task1">
<title>Implementing tasks</title>
<section xml:id="sd1_exam_2023_summer_task1_preparation">
<title>Preparation</title>
<orderedlist>
<listitem>
<para>Download and unzip the above file
<filename>exam.zip</filename>. You should see a directory
»<filename>Exam</filename>« containing a
<filename>pom.xml</filename> file.</para>
</listitem>
<listitem>
<para>Open this project in your <productname>IDEA</productname> IDE
by selecting the <filename>Exam/pom.xml</filename> file.</para>
</listitem>
</orderedlist>
</section>
<section xml:id="sd1_exam_2023_summer_task1_task">
<title>Task</title>
<qandaset defaultlabel="qanda" xml:id="sd1_exam_2023_summer_task1Qanda">
<qandadiv>
<qandaentry>
<question>
<para>Open the <filename>Readme.md</filename> file in your
project's root. It contains all necessary instructions for
solving the implementation tasks.</para>
</question>
<answer>
<para>Solution see <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/-/tree/master/Klausuren/Sd1/2023summer/Solve">summer
2023 exam</link>.</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
<section xml:id="sd1_exam_2023_summer_task1Caveats">
<title>Caveats</title>
<itemizedlist>
<listitem>
<para>When approaching end of examination check your input for
completeness prior to being automatically logged out by the system.
Remember: There is 120 minutes for the examination and another 5
minutes to check for completeness.</para>
</listitem>
<listitem>
<para>Projects residing just on your local workstation's file system
cannot be recovered after finishing the exam.</para>
</listitem>
</itemizedlist>
</section>
</section>
<section xml:id="sd1_exam_2023_summer_task2">
<title>switch versus if ... else if ... else</title>
<qandaset defaultlabel="qanda" xml:id="sd1_exam_2023_summer_task3Qanda">
<qandadiv>
<qandaentry>
<question>
<para>Consider a method
<methodname>computeItemCount()</methodname> of following
signature:</para>
<programlisting language="none">static int computeItemCount() {...}</programlisting>
<para>This method will be called by two different implementations
of another method <methodname>getScore()</methodname>:</para>
<informaltable border="1">
<tr>
<th>Implementation by <code>switch</code></th>
<th>Implementation by <code>if ... else if ...
else</code></th>
</tr>
<tr>
<td valign="top"><programlisting language="java">public static int getScore() {
switch (computeItemCount()){
case 1: return 20;
case 3: return 50;
default: return 0;
}
}</programlisting></td>
<td valign="top"><programlisting language="java">public static int getScore() {
if (1 == computeItemCount()) {
return 20;
} else if (3 == computeItemCount()) {
return 50;
} else {
return 0;
}
}</programlisting></td>
</tr>
</informaltable>
<para>An experienced programmer claims these two implementations
possibly return different results when executing
<methodname>getScore()</methodname>. Is she right? <emphasis
role="red">Explain</emphasis> your answer.</para>
<tip>
<para>Consider possible side effects of calling
<methodname>computeItemCount()</methodname> and provide an
example.</para>
</tip>
</question>
<answer>
<para>Consider:</para>
<programlisting language="java">public class X {
private static int itemCount = 2;
static int computeItemCount() {return itemCount++;}
...
}</programlisting>
<para>On a fresh start calling
<methodname>computeItemCount()</methodname> for the first time
will return the current value 2 of our variable <code
language="java">itemCount</code>.</para>
<para>The <code language="java">switch</code> implementation will
match its default and thus return 0.</para>
<para>On contrary the <code>if ... else if ... else</code>
implementation will call
<methodname>computeItemCount()</methodname> two times:</para>
<orderedlist>
<listitem>
<para>The first if <code>(1 == computeItemCount())</code>
fails since 1 != 2. This results in incrementing <code
language="java">itemCount</code> by 1 to the new value
3.</para>
</listitem>
<listitem>
<para>Now the else if <code language="java">(3 ==
computeItemCount())</code> clause succeeds. Thus
<methodname>getScore()</methodname> returns 50.</para>
</listitem>
</orderedlist>
<para>So the <code language="java">switch</code> statement
implementing a jump table differs from <code>if ... else if</code>
in definitely calling <code
language="java">computeItemCount()</code> only once.</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
<section xml:id="sd1_exam_2023_summer_task3">
<title>Exception handling</title>
<qandaset defaultlabel="qanda" xml:id="sd1_exam_2023_summer_task2Qanda">
<qandadiv>
<qandaentry>
<question>
<para>Consider the following snippet:</para>
<programlisting language="java">Object o = ...;
int a = ...;
...
try {
String s = (String) o; // May throw a <link
xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/ClassCastException.html">ClassCastException</link>
int c = 4 / a; // May throw an <link
xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/ArithmeticException.html">ArithmeticException</link>
} catch (<link xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/ClassCastException.html">ClassCastException</link> e) {
System.err.println(e.getMessage());
} catch (<link xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/ArithmeticException.html">ArithmeticException</link> e) {
System.err.println(e.getMessage());
}</programlisting>
<para>Facilitate the above code and <emphasis
role="red">explain</emphasis> why your simpler implementation
always and under all circumstances produces an identical runtime
result.</para>
<tip>
<itemizedlist>
<listitem>
<para>Both <code language="java">catch</code> clauses
contain the same text.</para>
</listitem>
<listitem>
<para>Search the inheritance hierarchy for a common ancestor
class and consider the <methodname>getMessage()</methodname>
method. Where is it actually being defined?</para>
</listitem>
</itemizedlist>
</tip>
</question>
<answer>
<para>Both <link
xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/ClassCastException.html">ClassCastException</link>
and <link
xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/ArithmeticException.html">ArithmeticException</link>
are being derived from their common parent <link
xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/RuntimeException.html">RuntimeException</link>.
All three classes share the common <link
xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/Throwable.html">Throwable</link>#<link
xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/Throwable.html#getMessage()">getMessage()</link>
method by inheritance without redefining it.</para>
<para>Our two <code>catch</code> clauses may thus be combined into
one:</para>
<programlisting language="java">...
try {
String s = (String) o; // May throw a <link
xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/ClassCastException.html">ClassCastException</link>
int c = 4 / a; // May throw an <link
xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/ArithmeticException.html">ArithmeticException</link>
} catch (<link xlink:href="https://freedocs.mi.hdm-stuttgart.de/doc/openjdk-17-doc/api/java.base/java/lang/RuntimeException.html">RuntimeException</link> e) { // Common parent class
System.err.println(e.getMessage());
}</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
</section>