Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
057d3a31
Commit
057d3a31
authored
1 year ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Better code error explanation
parent
151c46fd
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/Sd1/Appendix/Exam/2023/Winter/exam.xml
+65
-19
65 additions, 19 deletions
Doc/Sd1/Appendix/Exam/2023/Winter/exam.xml
with
65 additions
and
19 deletions
Doc/Sd1/Appendix/Exam/2023/Winter/exam.xml
+
65
−
19
View file @
057d3a31
...
...
@@ -107,7 +107,15 @@
<answer>
<para>
There are errors:
</para>
<programlisting
language=
"java"
>
/**
<informaltable
border=
"1"
>
<tr>
<th>
Erroneous code
</th>
<th>
Correct code
</th>
</tr>
<tr>
<td
valign=
"top"
><programlisting
language=
"java"
>
/**
* Summing up an int array's values
*
* @param values An array of int values
...
...
@@ -116,35 +124,54 @@
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"
/>
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++) {
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></td>
<td
valign=
"top"
><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 = 0;
for (int i = 0; i
<
values.length; i++) {
sum += values[i];
}
return sum;
}
</programlisting>
}
</programlisting></td>
</tr>
</informaltable>
<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 =
<para>
Missing initialization
<code
language=
"java"
>
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>
<para>
Missing type i.e.
<code
language=
"java"
>
int
i
</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>
language=
"java"
>
i
<
values.length
</code>
avoiding an
<classname>
ArrayIndexOutOfBoundsException
</classname>
.
</para>
</callout>
</calloutlist>
</answer>
...
...
@@ -174,33 +201,52 @@ public static int sum(int[] values) {
</question>
<answer>
<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
<para>
Every
variable
of type byte is bounded by
<code
language=
"java"
>
Byte.MAX_VALUE
</code>
. The
boolean
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 pass
ing between
<code
language=
"java"
>
Byte.MIN_VALUE
</code>
and
<code
language=
"java"
>
Byte.MAX_VALUE
</code>
:
</para>
<para>
A
n even
closer look
reveals
<code
language=
"java"
>
b++
</code>
oscillat
ing between
its lower
<code
language=
"java"
>
Byte.MIN_VALUE
</code>
and
upper
<code
language=
"java"
>
Byte.MAX_VALUE
</code>
bound
:
</para>
<informaltable
border=
"1"
>
<colgroup
width=
"36%"
/>
<colgroup
width=
"12%"
/>
<colgroup
width=
"52%"
/>
<tr>
<th>
Code
</th>
<th>
Output
</th>
<th>
Explanation
</th>
</tr>
<tr>
<td
valign=
"top"
><programlisting
language=
"java"
>
byte b = Byte.MAX_VALUE - 1;
System.out.println(b++);
System.out.println(b++);
System.out.println(b++);
...
System.out.println(b++);
</programlisting></td>
<td
valign=
"top"
><screen>
126
127
<emphasis
role=
"red"
>
127
</emphasis>
-128
-127
</screen></td>
-127
...
<emphasis
role=
"red"
>
127
</emphasis></screen></td>
<td
valign=
"top"
><screen>
Byte.MAX_VALUE
Byte.MIN_VALUE
Byte.MAX_VALUE
</screen></td>
</tr>
</informaltable>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment