Review Questions


  1. Declare an array variable called distances with elements of type double.

  2. Assign an array object with 5 elements to distances.

  3. Assign an array object to distances with the values 20.1, 30.7, 45.8, 19.1, 12.4, and 34.5.


Consider the following array:

 private int[] numbers = { 10,5,3,15} ; 
  1. What is the value of the following array element? numbers[1]

  2. What is the output from the following loop construct:

     for(int i = 3; i >= 0; i--) {     Console.Write(" { 0} ", numbers[i]) } 
  3. Write a loop using the foreach construct to traverse and print onscreen each element in numbers. This should result in the following output: 10 5 3 15.

  4. What is wrong with the following code that attempts to traverse the numbers array? What will happen when it is executed?

     for(int i = 0; i <= 4; i++) {     Console.WriteLine(numbers[i]); } 

A class contains the following array declaration:

 private int[] heights = new int[3]; 
  1. What is the value of each array element just after this array has been implicitly initialized?

  2. A line in your class looks like the following

     heights[4] = 10; 

Is this a valid statement? What will happen when this line is executed?


Why is it not a good idea to use an array to represent accounts of a real world bank?


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.


What is wrong with the following array declaration?

 byte [] ages = new byte [4] { 10, 34, 12, 19, 21, 56} ; 

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.


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.


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.


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?


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) 

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.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: