Skip to content
Snippets Groups Projects
exam.xml 9.65 KiB
Newer Older
Goik Martin's avatar
Goik Martin committed
<?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>