1: |
|
A: |
|
2: | Consider the following array: private int[] numbers = { 10,5,3,15} ;
|
A: |
|
3: | A class contains the following array declaration: private int[] heights = new int[3];
Is this a valid statement? What will happen when this line is executed? |
A: |
|
4: | Why is it not a good idea to use an array to represent accounts of a real world bank? |
A: | A bank has a constant need to delete and add new accounts. This calls for an array-like structure that dynamically can have new elements added and deleted. Arrays are not suited for this task because they have a fixed length once they are created. |
5: | Consider the following array declaration (same as in question 2): private int[] numbers = { 10,5,3,15} ; A fellow programmer has written the following method for retrieving values from the array: int GetNumber(int index) { return numbers[index]; } Adjust for the zero-based array index so that GetNumber(1) will return the first element value of numbers instead of the second element value, as is the case with the currently shown method. |
A: | int GetNumber(int index) { return numbers[index - 1]; } |
6: | What is wrong with the following array declaration? byte [] ages = new byte [4] { 10, 34, 12, 19, 21, 56} ; |
A: | [4] (positioned after byte) states that the length of the array is 4. However, the same line is attempting to assign six values at the same time; this difference is invalid. |
7: | Write a method called DisplayArray that accepts an array reference of base type int. The method must be able to print the values of the array object referenced by the argument onscreen, regardless of the array length. |
A: | void DisplayArray(int[] tempArray) { for(int i = 0; i < tempArray.Length; i++) { Console.WriteLine(tempArray[i]); } } |
8: | Write a method with the following header: int [] AddNumber(int [] tempArray, int num) that will add num to every element of tempArray and return this array back to the caller. |
A: | int [] AddNumber(int [] tempArray, int num) { for(int i = 0; i < tempArray.Length; i++) { tempArray[i] += num; } return tempArray; } |
9: | Consider the following two declarations: int [] myNumbers = { 2,4,6,8} ; int [] yourNumbers; Suppose that we assign myNumbers to yourNumbers and add 10 to the first element of yourNumbers, as in the following lines yourNumbers = myNumbers; yourNumbers[0] += 10; What is the value of myNumbers[0] after these statements have been executed? Explain what is going on. |
A: | myNumbers and yourNumbers are referencing the same array object, so adding 10 to yourNumbers[0] is the same as adding 10 to myNumbers[0]. As a result, myNumbers[0] is equal to 12. |
10: | What is the fundamental difference between cloning an array with the array's Clone method and simply assigning the array variable value to another array variable? |
A: | The Clone method creates an entire new array object with copies of array element values that are separate from those of the array for which the Clone method is called. In contrast, a simple assignment from one array variable to another only passes a reference along, which will cause the two array variables to reference the same array object. |
11: | Consider the two arrays from question 9, myNumbers and yourNumbers. Suppose each array variable references a different array object. Both these array objects contain exactly the same number of array elements and each pair of corresponding array elements have the same value. Will the following comparison be true or false? Explain why. (MyNumbers == YourNumbers) |
A: | It is false because the comparison operator (==) tests whether the compared array variables are referencing the same object (reference-based comparison). |
12: | Your program contains a class called Planet. You are writing another class called SolarSystem, which in this case must consist of 10 planets. You want to represent the 10 planets in an array of 10 planets. Write the declaration you must insert into the SolarSystem class to enable the representation of the 10 planets. |
A: | Planet [] planets = new Planet[10]; |
1: | Implement a class called ArrayMath, containing the following methods to perform calculations on arrays:
Write the code to test this class and its methods. |
A: | Exercise 1: using System; class ArrayMath { public static double ArrayAverage(double [] tempArray) { double sum = 0; foreach(double temp in tempArray) { sum += temp; } return sum / tempArray.Length; } public static int [] ArraySum(int [] tempArray1, int [] tempArray2) { int [] sumArray = new int [tempArray1.Length]; for(int i = 0; i < tempArray1.Length; i++) { sumArray[i] = tempArray1[i] + tempArray2[i]; } return sumArray; } public static int ArrayMax(int [] tempArray) { int maxValue = -2147483648; foreach(int temp in tempArray) { if(temp > maxValue) maxValue = temp; } return maxValue; } } class Tester { public static void Main() { double [] distances = {100, 200, 300}; int [] agesTeam1 = {10, 20, 30}; int [] agesTeam2 = {34, 38, 31}; int [] sumArray; Console.WriteLine("Average distance of distances array: {0}", ArrayMath.ArrayAverage(distances)); Console.WriteLine("Max age in agesTeam1 array: {0}", ArrayMath.ArrayMax(agesTeam1)); sumArray = ArrayMath.ArraySum(agesTeam1, agesTeam2); Console.WriteLine("sumArray's element values: {0} {1} {2}", sumArray[0], sumArray[1], sumArray[2]); } } |
2: | Write the basic parts of a car game program. The program must include a Car class with the following members:
Furthermore, the program must contain a class called CarGame that (by using an array) contains 5 objects of type Car. It must be possible to move each car (forward and reverse) and to get the position of each of the cars by providing an array index of the corresponding car. Write a small test program to ensure that the two classes function correctly. |
A: | Exercise 2: using System; class Car { private int position = 0; public void MoveForward(int distance) { position += distance; } public void Reverse(int distance) { position -= distance; } public int GetPosition() { return position; } } class CarGame { private Car [] cars; public CarGame() { cars = new Car [5]; for(int i = 0; i < cars.Length; i++) { cars[i] = new Car(); } } public int GetCarPosition(int carIndex) { return cars[carIndex].GetPosition(); } public void MoveCarForward(int carIndex, int distance) { cars[carIndex].MoveForward(distance); } public void ReverseCar(int carIndex, int distance) { cars[carIndex].Reverse(distance); } } class CarGameTester { public static void Main() { CarGame testGame = new CarGame(); testGame.MoveCarForward(0, 100); testGame.MoveCarForward(1, 40); Console.WriteLine("Position of car 0: {0}", testGame.GetCarPosition(0)); Console.WriteLine("Position of car 1: {0}", testGame.GetCarPosition(1)); } } |