From d2c71a535080a8c757458c849703ef632f217961 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Mon, 3 Jul 2023 12:47:27 +0200 Subject: [PATCH] Summer 2023 Exam --- Doc/Sd1/Appendix/Exam/2023/Summer/exam.xml | 255 +++++++++++++++++++++ Doc/Sd1/Appendix/appendix.xml | 3 + 2 files changed, 258 insertions(+) create mode 100644 Doc/Sd1/Appendix/Exam/2023/Summer/exam.xml diff --git a/Doc/Sd1/Appendix/Exam/2023/Summer/exam.xml b/Doc/Sd1/Appendix/Exam/2023/Summer/exam.xml new file mode 100644 index 000000000..f8e04de04 --- /dev/null +++ b/Doc/Sd1/Appendix/Exam/2023/Summer/exam.xml @@ -0,0 +1,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> diff --git a/Doc/Sd1/Appendix/appendix.xml b/Doc/Sd1/Appendix/appendix.xml index 1d330794b..dfb566eae 100644 --- a/Doc/Sd1/Appendix/appendix.xml +++ b/Doc/Sd1/Appendix/appendix.xml @@ -2353,6 +2353,9 @@ Value 2147483645 is not prime. </listitem> </orderedlist> + <xi:include href="Exam/2023/Summer/exam.xml" xpointer="element(/1)" + ns:idfixup="auto"/> + <xi:include href="Exam/2022/Winter/exam.xml" xpointer="element(/1)" ns:idfixup="auto"/> -- GitLab