Difference between revisions of "Arrays Review Questions and Exercises"

From CompSciWiki
Jump to: navigation, search
m (Chapter 8 Review Questions and Exercises moved to Arrays Review Questions and Exercises: "Chapter 8" is overly ambiguous; moving to more precisely-titled page)
Line 1: Line 1:
 
{{1010Topic|Introduction=This section has some review questions and exercise to help you review this section.|Overview=Learn how to try out your programming skills with these exercises and questions.}}
 
{{1010Topic|Introduction=This section has some review questions and exercise to help you review this section.|Overview=Learn how to try out your programming skills with these exercises and questions.}}
=Question 1=
+
==Review Questions==
(Andrew)
+
===Question 1===
 
+
 
What is it called when you access an element outside of the bounds of an array.
 
What is it called when you access an element outside of the bounds of an array.
  
 
Example
 
Example
<pre style="background-color:green; color:white;">
+
<pre>
 
     int i[] = new int[3];
 
     int i[] = new int[3];
 
     i[4] = 3;
 
     i[4] = 3;
 
</pre>
 
</pre>
[[Chapter 8, question 1 answer|Answer]]
 
  
=Question 2=
 
(Andrew)
 
  
 +
===Question 2===
 
What is it called when a for-loop loops one iteration too many such that it passes the array's bounds.
 
What is it called when a for-loop loops one iteration too many such that it passes the array's bounds.
  
 
Example:
 
Example:
<pre style="background-color:green; color:white;">
+
<pre>
 
     int j;
 
     int j;
 
     int array[] = new int[3];
 
     int array[] = new int[3];
Line 25: Line 22:
 
     }
 
     }
 
</pre>
 
</pre>
[[Chapter 8, question 2 answer|Answer]]
 
  
=Question 3=
 
(ABU)
 
  
 +
===Question 3===
 
Does the second statement of following code copy an array?
 
Does the second statement of following code copy an array?
<pre style="background-color:green; color:white;">
+
<pre>
  
 
int array1[]={2,4}
 
int array1[]={2,4}
 
int array2[]=array1;
 
int array2[]=array1;
 
</pre>
 
</pre>
[[Chapter 8, question 3 answer|Answer]]
 
  
=Question 4=
 
 
(ABU)
 
  
 +
===Question 4===
 
Does the following piece of code compare two arrays?
 
Does the following piece of code compare two arrays?
<pre style="background-color:green; color:white;">
+
<pre>
 
int array1[]={1,3,4}
 
int array1[]={1,3,4}
 
int array2[]={1,3,4}
 
int array2[]={1,3,4}
Line 53: Line 45:
  
 
</pre>
 
</pre>
[[Chapter 8, question 4 answer|Answer]]
 
  
=Question 5=
 
(Graham)
 
  
 +
===Question 5===
 
Does the following piece of code change the value of '''i''' to "Hey"?
 
Does the following piece of code change the value of '''i''' to "Hey"?
<pre style="background-color:green; color:white;">
+
<pre>
 
     String i = "Hello";
 
     String i = "Hello";
 
     String array[] = new String [20];
 
     String array[] = new String [20];
Line 66: Line 56:
 
</pre>
 
</pre>
  
[[Chapter 8, question 5 answer|Answer]]
 
 
=Question 6=
 
(Graham)
 
  
 +
===Question 6===
 
What does the following piece of code print to the screen?
 
What does the following piece of code print to the screen?
  
<pre style="background-color:green; color:white;">
+
<pre>
 
     String s = "Gello";
 
     String s = "Gello";
 
     String h[] = new String [20];
 
     String h[] = new String [20];
Line 82: Line 69:
 
</pre>
 
</pre>
  
[[Chapter 8, question 6 answer|Answer]]
 
 
=Exercise 1=
 
(Andrew)
 
  
 +
==Exercises==
 +
===Exercise 1===
 
Write a program that calculates the product of all the values in an array.
 
Write a program that calculates the product of all the values in an array.
  
[[Chapter 8, exercise 1 answer|Answer]]
 
 
=Exercise 2=
 
(Andrew)
 
  
 +
===Exercise 2===
 
Write a program that finds the position of the smallest value in an array.
 
Write a program that finds the position of the smallest value in an array.
 
[[Chapter 8, exercise 2 answer|Answer]]
 
 
=Exercise 3=
 
(ABU)
 
 
Write an program to create and populate an array?
 
 
[[Chapter 8, exercise 3 answer|Answer]]
 
 
=Exercise 4=
 
(ABU)
 
 
Write a program to copy an array?
 
  
  
[[Chapter 8, exercise 4 answer|Answer]]
+
===Exercise 3===
 +
Write an program to create and populate an array.
  
=Exercise 5=
 
(Graham)
 
  
Create a function to sort an array of integers
+
===Exercise 4===
 +
Write a program to copy the contents of one array into another.
  
[[Chapter 8, exercise 5 answer|Answer]]
 
  
=Exercise 6=
+
===Exercise 5===
(Graham)
+
Create a function to sort an array of integers.
  
Write a program to print all of the values in an array
 
  
[[Chapter 8, exercise 6 answer|Answer]]
+
===Exercise 6===
 +
Write a program to print all of the values in an array.

Revision as of 20:14, 10 September 2007

COMP 1010 Home > Back to Chapter Topics


Introduction

This section has some review questions and exercise to help you review this section.

   

{{{Body}}}

Review Questions

Question 1

What is it called when you access an element outside of the bounds of an array.

Example

    int i[] = new int[3];
    i[4] = 3;


Question 2

What is it called when a for-loop loops one iteration too many such that it passes the array's bounds.

Example:

    int j;
    int array[] = new int[3];
    for (j = 0; j <= 3; j++) {
        array[j] = 0;
    }


Question 3

Does the second statement of following code copy an array?


int array1[]={2,4}
int array2[]=array1;


Question 4

Does the following piece of code compare two arrays?

int array1[]={1,3,4}
int array2[]={1,3,4}

if(array1=array2)
  System.out.println("SAME ARRAY");
else
  System.out.println("NOT THE SAME");


Question 5

Does the following piece of code change the value of i to "Hey"?

    String i = "Hello";
    String array[] = new String [20];
    array[10] = i;
    array[10] = "Hey";


Question 6

What does the following piece of code print to the screen?

    String s = "Gello";
    String h[] = new String [20];

    h[10] = s;
    s = "Jello";
    System.out.println(h[10]);


Exercises

Exercise 1

Write a program that calculates the product of all the values in an array.


Exercise 2

Write a program that finds the position of the smallest value in an array.


Exercise 3

Write an program to create and populate an array.


Exercise 4

Write a program to copy the contents of one array into another.


Exercise 5

Create a function to sort an array of integers.


Exercise 6

Write a program to print all of the values in an array.