diff --git a/Doc/Sd1/Appendix/Exam/2023/Winter/exam.xml b/Doc/Sd1/Appendix/Exam/2023/Winter/exam.xml index 1e6cbf19bc24bc5712a8ac844049286187f343b7..5b8cfbf8ac13dba7843c336e4adf1bf5e7a5aed2 100644 --- a/Doc/Sd1/Appendix/Exam/2023/Winter/exam.xml +++ b/Doc/Sd1/Appendix/Exam/2023/Winter/exam.xml @@ -107,39 +107,46 @@ <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 < values.length</code> rather than - <code language="java">i <= values.length</code> is - being required.</para> - </listitem> - </itemizedlist> - </glossdef> - </glossentry> - </glosslist> + <programlisting language="java">/** + * Summing up an int array's values + * + * @param values An array of int values + * @return The sum of all array values + */ +public static int sum(int[] values) { + + int sum; <co linkends="sd1_exam_2023_winter_sumErrors-1" + xml:id="sd1_exam_2023_winter_sumErrors-1-co"/> + + for (<co linkends="sd1_exam_2023_winter_sumErrors-2" + xml:id="sd1_exam_2023_winter_sumErrors-2-co"/> i = 0; i <=<co + linkends="sd1_exam_2023_winter_sumErrors-3" + xml:id="sd1_exam_2023_winter_sumErrors-3-co"/> values.length; i++) { + sum += values[i]; + } + return sum; +}</programlisting> + + <calloutlist> + <callout arearefs="sd1_exam_2023_winter_sumErrors-1-co" + xml:id="sd1_exam_2023_winter_sumErrors-1"> + <para>Missing initialization <code language="java">int sum = + 0</code>.</para> + </callout> + + <callout arearefs="sd1_exam_2023_winter_sumErrors-2-co" + xml:id="sd1_exam_2023_winter_sumErrors-2"> + <para>Missing type i.e. <code language="java">int i = + 0</code>;</para> + </callout> + + <callout arearefs="sd1_exam_2023_winter_sumErrors-3-co" + xml:id="sd1_exam_2023_winter_sumErrors-3"> + <para>Off by one error: Last array index being <code + language="java">values.length - 1</code> requires <code + language="java">i < values.length</code>.</para> + </callout> + </calloutlist> </answer> </qandaentry> </qandadiv> @@ -167,9 +174,15 @@ </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> + <para>Every expression of type byte is bounded by <code + language="java">Byte.MAX_VALUE</code>. The expression <code + language="java">b <= Byte.MAX_VALUE</code> is thus always + true.</para> + + <para>A closer look on the operator ++ acting on a byte variable + reveals a cycle passing between <code + language="java">Byte.MIN_VALUE</code> and <code + language="java">Byte.MAX_VALUE</code>:</para> <informaltable border="1"> <tr> @@ -179,26 +192,23 @@ </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> + <td valign="top"><programlisting language="java">byte b = Byte.MAX_VALUE - 1; +System.out.println(b++); +System.out.println(b++); +System.out.println(b++);</programlisting></td> + + <td valign="top"><screen> 126 + 127 +-128 +-127</screen></td> </tr> </informaltable> - <para>Before breaching the loop's upper limit <code + <para>Instead of 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 + language="java">b will transition</code> from Byte.MAX_VALUE to + <code language="java">Byte.MIN_VALUE</code>. The loop will thus continue forever.</para> - - <para>Shorter explanation: The expression <code language="java">b - <= Byte.MAX_VALUE</code> is always true due to <code - language="java">Byte.MAX_VALUE</code>'s definition with respect to - any value of type <code>byte</code>.</para> </answer> </qandaentry> </qandadiv>