Computer Science Homework

profilelaram14
quiz_5-cs.pdf

Created by: Shilpa Phanse COSC 1436 4/16/2012

Quiz 5

1. Which of the following array declarations would enable you to store high and low temperatures for each day of one full week:

a. int tempLo1, tempLo2, tempLo3, tempLo4, tempLo5, tempLo6, tempLo7, tempHi1,

tempHi2, tempHi3, tempHi4, tempHi5, tempHi6, tempHi7; b. int temp [7,2] = new int [7,2]; c. temp int [,]= new int[7,2]; d. int temp[,] = new int[7,2];

2. Use the following declaration:

Char [,] n = {{‘a’, ‘b’, ‘c’, ‘d’, ‘e’}, {‘f’, ’g’, ’h’, ’i’, ‘j’}}; What does n {1, 1} refer to:

a. a b. f c. b d. g e. none of the above

3. Which of the following adds 95 to the array element that is currently storing 14?

int [,] x = {{12, 13}, { 14, 15}, { 16, 17}, {18, 19}};

a. x[2] +=95; b. x[1,0] +=95; c. x[1,0 +=95]; d. x= 14 + 95;

4. How many components are allocated by the following statement?

double [,] values = new double[3,2]; a. 32 b. 3 c. 5 d. 6

Created by: Shilpa Phanse COSC 1436 4/16/2012

5. With the following declaration:

int [,] points = {{300, 100, 200, 400, 600}, { 550, 700, 900, 800, 100}}; The statement points [1, 3] = points [1, 3] + 10; will

a. Replace the 300 with 310 and 900 with 910 b. Replace 500 with 510 c. Replace 900 with 910 d. Replace 800 with 810

6. With the following declaration:

int [,] points = {{300, 100, 200, 400, 600}, { 550, 700, 900, 200, 100}}; The statement Console.Write(points[1,2] + points[0,3]); will display

a. 900400 b. 1300 c. Points[1,2] + points[0,3] d. Result in an error

7. Using the following declaration:

string [,] anArray = {{“Hello”, “My”, “ Name” , “Is”, “Winnie”}, {“The”, “Pooh”, “What”, “Is”, “Yours”}};

a. Console.WriteLine(anArray.Length); b. Console.WriteLine(anArray[1,1]); c. Console.WriteLine(0, anArray.GetLength(0) – 2]); d. Console.WriteLine[anArray.Rank);

8. A jagged array is declared in following manner correctly:

a. int [][] aArray = new int[4][]; b. int [,] aArray = new int[,]; c. int [] aArray = new int[];

Created by: Shilpa Phanse COSC 1436 4/16/2012

9. What will this code yield:

int[][] num = new int[4][]; num[0] = new int[] { 100, 200 }; num[1] = new int[] { 100, 345, 300, 400 }; num[2] = new int[] { 500, 600, 11, 22, 37, 45 }; num[3] = new int[] { 888, 789 }; Console.WriteLine("The values in the num[2] array are:"); for (int i = 0; i < num[2].Length; i++) { Console.Write("{0}", num[1][i], "\t"); }

a. 100 200 b. 500 600 11 22 37 45 c. 888 789 d. The values in the num[2] array are:

500 600 11 22 37 45

10. Correct the two separate code snippets:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CorrectTheErrors { class Product { public static void DisplayValues(params double[] dVal) { { Console.Write("{0}", dVal[i] + "\t"); } } static void Main(string[] args) { double[] array1 = new double[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; double[] array2 = new double[10] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; string outputMsg = ""; string caption = "MessageBox Output";

Created by: Shilpa Phanse COSC 1436 4/16/2012

for (int i = 0; i < array1; i++) { array3[i] = array1[i] * array2[i]; DisplayValues(array3[i]); } outputMsg += "Product Array \n\n"; foreach (double val in array3) outputMsg += val + "\n"; MessageBox.Show(outputMsg, caption); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CorrectThis { class program { static void Main() { int [] num = new int [10]; string[] sArray = {"This", "is", "COSC", "1436", "Class"}; for (int i = 0; i < num.Length; i++) { num[i] = sArray[i]; Console.WriteLine("The new array is {0}", num[i]); } Console.Read(); } } }