Lab Quiz, Data Structures and Algorithms, Week 2 -- Thurs Oct 4 2001


Two files contain the following Java code (exactly as it appears here, there is nothing else in the files). The files are compiled by the Java compiler and the resulting Java program is run.

Here is the first file:

public class PosArraySum {

    public static int pos_array_sum(int a[]) {

	int i, sum = 0;

	for (i = 0; i < a.length; i++) {

	    if (a[i] > 0) sum = sum + a[i];


	return sum;



Here is the second file:

public class PosArraySumDemo {

    static int[] half_neg = {1,1,1,-1,-1,-1};

    public static void main(String argv[]) {

	System.out.println("The result for half_neg is "

			   + PosArraySum.pos_array_sum(half_neg));



  1. What is the name of the first file?

  2. What is the command to compile the first file with the Java compiler?

  3. What is the command to run the program defined by these two files?

  4. What output does the program produce? (just write the output reasonably neatly)

  5. In the first file, the first expression in the for statement is changed from i = 0 to i = 3
    What output does the program produce now?