<?xml version="1.0" encoding="UTF-8"?>
<section version="5.0" xml:id="sd1_exam_2023_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 2023</title>

  <section xml:id="sd1_exam_2023_winter_task1">
    <title>Implementing tasks</title>

    <section xml:id="sd1_exam_2023_winter_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. Your home
          directory is <filename>/home/student</filename>.</para>
        </listitem>
      </orderedlist>
    </section>

    <section xml:id="sd1_exam_2023_winter_task1_task">
      <title>Task</title>

      <qandaset defaultlabel="qanda" xml:id="sd1_exam_2023_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/2023winter/Solve">winter
              2023 exam</link>.</para>
            </answer>
          </qandaentry>
        </qandadiv>
      </qandaset>
    </section>

    <section xml:id="sd1_exam_2023_winter_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_winter_task2">
    <title>Beginner's nightmare</title>

    <qandaset defaultlabel="qanda" xml:id="sd1_exam_2023_winter_task3Qanda">
      <qandadiv>
        <qandaentry>
          <question>
            <para>We consider the following class method
            <methodname>sum</methodname>:</para>

            <programlisting language="none">01 /**
02  * Summing up an int array's values
03  *
04  * @param values An array of int values
05  * @return The sum of all array values
06  */
07 public static int sum(int[] values) {
08
09    int sum;
10
11    for (i = 0; i &lt;= values.length; i++) {
12        sum += values[i];
13    }
14    return sum;
15 }</programlisting>

            <para>This code does contain errors. Explain and correct each of
            those. Line numbers are being provided for your convenience</para>
          </question>

          <answer>
            <para>There are errors:</para>

            <glosslist>
              <glossentry>
                <glossterm>Line 9</glossterm>

                <glossdef>
                  <para>The variable <code language="java">sum</code> lacks
                  initialization e.g. <code language="java">int sum =
                  0</code>.</para>
                </glossdef>
              </glossentry>

              <glossentry>
                <glossterm>Line 16</glossterm>

                <glossdef>
                  <para>There are two errors here:</para>

                  <itemizedlist>
                    <listitem>
                      <para>Variable i has not been declared: <code
                      language="java">for(int i = 0; ...</code></para>
                    </listitem>

                    <listitem>
                      <para>The loop's upper limit is being breached: <code
                      language="java">i &lt; values.length</code> rather than
                      <code language="java">i &lt;= values.length</code> is
                      being required.</para>
                    </listitem>
                  </itemizedlist>
                </glossdef>
              </glossentry>
            </glosslist>
          </answer>
        </qandaentry>
      </qandadiv>
    </qandaset>
  </section>

  <section xml:id="sd1_exam_2023_winter_task3">
    <title>Strange endless loop result</title>

    <qandaset defaultlabel="qanda" xml:id="sd1_exam_2023_winter_task2Qanda">
      <qandadiv>
        <qandaentry>
          <question>
            <para>We consider:</para>

            <programlisting language="java">for (byte b = 0; b &lt;= Byte.MAX_VALUE; b++);</programlisting>

            <para>This snippet causes an endless loop. Give an
            explanation.</para>

            <tip>
              <para>Consider the ++ operator's behavior when reaching variable
              <code>b</code>'s upper limit.</para>
            </tip>
          </question>

          <answer>
            <para>The operator ++ acting on a byte variable will cycle between
            <code language="java">Byte.MIN_VALUE</code> and <code
            language="java">Byte.MAX_VALUE</code>. Consider:</para>

            <informaltable border="1">
              <tr>
                <th>Code</th>

                <th>Output</th>
              </tr>

              <tr>
                <td valign="top"><programlisting language="java">byte b = Byte.MAX_VALUE;
System.out.println(b);
b++;
System.out.println(b);</programlisting></td>

                <td valign="top"><screen>127
-128</screen></td>
              </tr>
            </informaltable>

            <para>Before breaching the loop's upper limit <code
            language="java">Byte.MAX_VALUE</code> our variable <code
            language="java">b will thus transition</code> from Byte.MAX_VALUE
            to <code language="java">Byte.MIN_VALUE</code>. The loop will thus
            continue forever.</para>
          </answer>
        </qandaentry>
      </qandadiv>
    </qandaset>
  </section>
</section>