Four short programs about arrays — Activities 1.23 and 1.24.
These four fail in four different ways. One will not compile. One crashes. One runs and gives a wrong answer with no warning at all. And one passes the test you would try first and crashes on a different one.
Part of each answer is saying which kind of failure it is— because the kind tells you where to look.
This should say how many scores there are. It does not compile.
int[] scores = { 88, 92, 79, 95 };
Console.WriteLine($"There are {scores.length} scores.");This should print the last name. It crashes.
string[] names = { "Avery", "Blake", "Casey" };
Console.WriteLine($"Last: {names[names.Length]}");Also: the same mistake in exam notation, names[LENGTH(names)], is not a mistake at all. Say why.
This should make a backup, change the working copy, and leave the backup alone. Instead, this is what it prints:
int[] original = { 10, 20, 30 };int[] backup = original;
original[0] = 999;
Console.WriteLine($"Working: {original[0]}, {original[1]}, {original[2]}");Console.WriteLine($"Backup: {backup[0]}, {backup[1]}, {backup[2]}");Working: 999, 20, 30Backup: 999, 20, 30Also: written in exam notation as backup ← original, this program would work. Say what is different.
This should look up a seat and turn away seats that do not exist. Seats 1, 5, and 6 all behave correctly.
string[] seats = { "Avery", "Blake", "Casey", "Dakota", "Emery" };
Console.Write("Which seat? ");int seat = int.Parse(Console.ReadLine());
if (seat <= seats.Length){ Console.WriteLine($"Seat {seat}: {seats[seat - 1]}");}else{ Console.WriteLine($"There is no seat {seat}.");}Also: name the seat number that crashes it, and say why testing 1, 5, and 6 was not enough.
For each program, give:
Plus the three written answers, for Bugs 2, 3, and 4.
Work them out by reading first. Running the code tells you that something is wrong; it rarely tells you why, and on the exam there is no computer.