Skip to content

Commit cccb39d

Browse files
committed
Starting to integrate Spring 2026 exam into solutions.
1 parent 05c406d commit cccb39d

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

source/code/projects/ReferenceMethods/ReferenceMethods/Program.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,41 @@ class Program
44
{
55
static void Main(string[] args)
66
{
7+
// Example for NameChange
8+
static void NameChange(ref string name, string newname, out string oldname){
9+
oldname = name;
10+
name = newname;
11+
}
12+
13+
string name = "Smith", oldname;
14+
Console.WriteLine("Enter new name.");
15+
NameChange(ref name, Console.ReadLine(), out oldname);
16+
Console.WriteLine($"New name: {name}\nOld name: {oldname}");
17+
18+
/*
19+
// Example for Test method
20+
21+
static void Test(int a, out int b){
22+
if (b < 0) { b = a; }
23+
else { b++;}
24+
}
25+
*/
26+
27+
// SafeLength
28+
static int SafeLength<T>(T[] aP){
29+
int ans;
30+
if (aP == null) ans = -1;
31+
else ans = aP.Length;
32+
return ans;
33+
}
34+
35+
int[] test1 = null;
36+
Console.WriteLine(SafeLength(test1));
37+
char[] test2 = {'a', 'b'};
38+
Console.WriteLine(SafeLength(test2));
39+
40+
41+
742
// Example for AddRev
843
int x0 = 4,
944
y0 = 3;

source/solutions/data/1darrays.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ Those two problems are related to [binary search](./lectures/collections/search#
897897
</details>
898898
#. Explain why this implementation is incorrect, and how it can be fixed.
899899
<details><Summary>Solution</summary>
900-
The problem is that `mid` will never reach `arrayP.Length -1`.
900+
The problem is that what we use for `mid` (`(sta + end) / 2`) will never reach `arrayP.Length -1`.
901901
Indeed, we will have an expression equivalent to `((arrayP.Length -2) - arrayP.Length-1) / 2` (for example, as above, `(10 + 11) / 2`) that will never be rounded up to `arrayP.Length-1` (in our example, `11`), but always truncated.
902902

903903
To fix this implementation, instead of `end = (sta + end) / 2;`, we should have `end = ((sta + end) / 2) + 1;`.

source/solutions/oop/references.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ tags:
7979
`c` holds `'*'`, `d` holds `%`.
8080
</details>
8181

82+
#. Consider the following method:
83+
84+
```
85+
static void NameChange(ref string nameP, string newnameP, out string oldnameP){
86+
oldnameP = nameP;
87+
nameP = newnameP;
88+
}
89+
```
90+
91+
<details><summary>Solution</summary>
92+
93+
</details>
94+
95+
96+
Assume given a `string` `name` variable containing a value. Write a short program (possibly declaring additional variables) that
97+
#. Asks the user their new name,
98+
#. Calls the `NameChange` method with appropriate arguments,
99+
#. Displays the new name and the old name.
82100

83101
## Problems
84102

0 commit comments

Comments
 (0)