Вы находитесь на странице: 1из 2

Introduction to Computer Science Practice Quiz: Arrays Page 1 of 2

Name _____________________________________ Date __________________ Period ________


PRACTICE QUIZ: ARRAYS

1. Below is an array called qux. Use the array to answer the questions.

a. What is qux.length?
4.9

5.6

84.2 b. What is qux[4]?

1.67

2.1 c. What is qux[qux.length-1]?


111.0

3.5
d. Write a snippet of code that sums all of the elements in the array. Hint: USE A FOR LOOP!
0.0

-65.23

3.14

6.28

1.60

2. Use the table below to predict what the array will look like after each pass with the bubble sort algorithm.
You may or may not use all of the rows. Cross out any rows that are needed. For example, if the algorithm
only requires 3 passes, cross out the rows labeled "after 4 passes" through "after 7 passes".

unsorted 87 42 3 19 71

after 1 pass

after 2 passes

after 3 passes

after 4 passes

after 5 passes

after 6 passes

after 7 passes
Introduction to Computer Science Practice Quiz: Arrays Page 2 of 2

3. The following program is supposed to take two integers from the command line and store those integers
into variables a and b. The program is supposed to print a and b, swap the values of a and b, and then
print a and b again. The program is full of bugs. Find 4 for full credit. Circle the bug and give a one
sentence explanation beside each one.

The following shows example input and output for the program.

% java Swap 7 4
! a = 7, b = 4
! a = 4, b = 7

Fix the program:

public class Swap {

! public static main(String[] args) {

! int a = args[1];
! int b = args[2];
!
! System.out.println("a = a, b = b");

! int temp = b;
! a = b;
! b = a;

! System.out.println("a = a, b = b");

! }
! }