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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?xml version="1.0" encoding="UTF-8"?>
<section version="5.0" xml:id="sd1_exam_2024_winter" 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 winter 2024</title>
<section xml:id="sd1_exam_2024_winter_task1">
<title>Implementing tasks</title>
<section xml:id="sd1_exam_2024_winter_task1_preparation">
<title>Preparation</title>
<orderedlist>
<listitem>
<para>Download and unzip above <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 as a
project.</para>
</listitem>
</orderedlist>
</section>
<section xml:id="sd1_exam_2024_winter_task1_task">
<title>Task</title>
<qandaset defaultlabel="qanda" xml:id="sd1_exam_2024_winter_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/2024winter/Solve">winter
2024 exam</link>.</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
<section xml:id="sd1_exam_2024_winter_task1Warning">
<title>Warning</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_2024_winter_task2">
<title>Swapping two variables</title>
<qandaset defaultlabel="qanda" xml:id="sd1_exam_2024_winter_task3Qanda">
<qandadiv>
<qandaentry>
<question>
<para>We consider:</para>
<informaltable border="1">
<colgroup width="9%"/>
<colgroup width="91%"/>
<tr>
<th>Code</th>
<td valign="top"><programlisting language="java">public static void main(String[] args) {
int a = 1, b = 2;
System.out.println("a = " + a + ", b = " + b);
{
// Swapping values of a and b
final int bCopy = b;
b = a;
a = bCopy;
}
System.out.println("a = " + a + ", b = " + b);
}</programlisting></td>
</tr>
<tr>
<th>Result</th>
<td valign="top"><screen>a = 1, b = 2
a = 2, b = 1</screen></td>
</tr>
</informaltable>
<para>The values of <code>a</code> and <code>b</code> are being
swapped within the block as expected. We now replace this block by
a class method <methodname>swap(int a, int
b){...}</methodname>:</para>
<informaltable border="1">
<colgroup width="9%"/>
<colgroup width="91%"/>
<tr>
<th>Code</th>
<td valign="top"><programlisting language="java">public static void main(String[] args) {
int a = 1, b = 2;
System.out.println("a = " + a + ", b = " + b);
swap(a, b);
System.out.println("a = " + a + ", b = " + b);
}
/**
* Swapping values of two variables
* @param a First variable
* @param b Second variable
*/
static void swap(int a, int b) {
final int bCopy = b;
b = a;
a = bCopy;
}</programlisting></td>
</tr>
<tr>
<th>Result</th>
<td valign="top"><screen>a = 1, b = 2
a = 1, b = 2</screen></td>
</tr>
</informaltable>
<para>This time the values of a and b in
<methodname>main(...)</methodname> remain unchanged. Why is this
not at all surprising?</para>
</question>
<answer>
<para>For the sake of easy variable identification by name we
re-factor all variables in
<methodname>swap(...)</methodname>:</para>
<programlisting language="java">public static void main(String[] args) {
int a = 1, b = 2;
System.out.println("a = " + a + ", b = " + b);
swap(a, b);
System.out.println("a = " + a + ", b = " + b);
}
/**
* Swapping values of two variables
* @param x First variable
* @param y Second variable
*/
static void swap(int x, int y) {
int yCopy = y;
y = x;
x = yCopy;
}</programlisting>
<para>This re-factoring has no effect on execution since both sets
of variables {<varname>a</varname>, <varname>b</varname>,
<varname>bcopy</varname>} or {<varname>x</varname>,
<varname>y</varname>, <varname>yCopy</varname>} are local to our
<methodname>swap(...)</methodname> method.</para>
<para>Calling <methodname>swap(a, b)</methodname> in
<methodname>main(...)</methodname> <emphasis>copies</emphasis> the
values of <varname>a</varname> and <varname>b</varname> into
<varname>x</varname> and <varname>y</varname> respectively. This
the usual <acronym>call-by-value</acronym> mechanism being the
only way calling a method in Java. Swapping these copied values of
<varname>x</varname> and <varname>y</varname> in
<methodname>swap(...)</methodname>'s stack frame has no effect on
<methodname>main(...)</methodname> thus leaving its values
<varname>a</varname> and <varname>b</varname> unchanged.</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
<section xml:id="sd1_exam_2024_winter_task3">
<title>0 and null</title>
<qandaset defaultlabel="qanda" xml:id="sd1_exam_2024_winter_task2Qanda">
<qandadiv>
<qandaentry>
<question>
<para>Consider the following two code snippets:</para>
<itemizedlist>
<listitem>
<para><code language="java">if (0 == someVariable) {/* Code
omitted for brevity */}</code></para>
</listitem>
<listitem>
<para><code language="java">if (null == someOtherVariable) {/*
Code omitted for brevity */}</code></para>
</listitem>
</itemizedlist>
<para>Answer the following two questions:</para>
<orderedlist>
<listitem>
<para>What is the difference between <code>0</code> and
<code>null</code>?</para>
</listitem>
<listitem>
<para>Assuming the above snippets are part of well compiling
Java code: What do we know about the possible data types of
<code>someVariable</code> and <code>someOtherVariable</code>
?</para>
</listitem>
</orderedlist>
</question>
<answer>
<orderedlist>
<listitem>
<itemizedlist>
<listitem>
<para>0 is a literal denoting the value zero of type
<code>int</code>.</para>
</listitem>
<listitem>
<para>A variable of class or array type of value <code
language="java">null</code> does not reference an
object.</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<itemizedlist>
<listitem>
<para><code language="java">someVariable</code> must be of
type <code language="java">byte</code>, <code
language="java">short</code>, <code
language="java">int</code>, <code
language="java">long</code>, <code
language="java">char</code>, <code
language="java">float</code> or <code
language="java">double</code>. In other words: Any
primitive type except <code
language="java">boolean</code>.</para>
</listitem>
<listitem>
<para><code language="java">someOtherVariable</code> must
be of either class / <code>enum</code> or array
type.</para>
</listitem>
</itemizedlist>
</listitem>
</orderedlist>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
</section>