1. (10 points)


i. How many times will the following for loop execute?

for(index = -1; index < 7; index *= 2);

ii. Given the folling declaration:

int [] myArray = new int [99];

What are the valid subscripts for array myArray?

iii. Explain the difference between call-by-value and call-by-reference.

iv. Given: int a = 4, b = 6;

What is the value of the following expression (true/false) ?

(((t == 5) || (b == 6)) && (a < b))

v. Given: int f = 43, g = 5;

What will be the output of the following statement ?

System.out.println((f % g) + (f / g));


2. (5 points)


What does the following code produce?


public class WhatDoIDO


public static void main (String [] args)


int i = 0, j = 0;

for ( i = 2; i >= -1; i-- )


for ( j = 0; j <= 3; j++ )

System.out.print(i + j );






3. (15 points)

Write a program that reads an integer N from the keyboard and then computes and displays the sum of the numbers from N to (2 * N) if N is nonnegative, or the sum of the numbers from (2 * N) to N if N is negative. The starting and ending points are included in the sum.

Sample run:

 Enter number : 5
 Sum : 45


Sample run:

 Enter number : -4
 Sum : -30

Notes and hints:

4. (15 points)


Write a program that reads a date as an integer in the format MMDDYYYY. The program will then print out the date in the format <Month Name> <day>, <year>.

Sample run:

   Enter date: 12252003
   December 25, 2003.


In the input the month and the day will always be given as two digits, including leading zero. So the 20th of May, 2000 will be entered as 05202000 and the 3rd of December, 2000 will be entered as 12032000.


Don't worry about writing down all of the months, write code that can handle just the first couple and put a comment like: /* Remaining months the same way */.




5. (15 points)


Write a program that reads a positive integer N from the keyboard and outputs all of the prime numbers from 1 up to N. Write and use a method:
    public static boolean isPrime (int num);
which returns true if num is prime, false if it is not.

The main method will prompt for the number N and then it will test all of the numbers from 1 to N inclusive calling isPrime for each number. If the number is prime it will print it.

Note: A prime number is a number divisible only by itself and 1.

Sample run:

   Enter number : 9
   Prime numbers: 1 2 3 5 7





6. (20 points)


Write a method called CellFiller that takes an array of integers (call it cells) as it's parameter and then:

1.      Reads in a positive integer, k.

2.      If the element of cells with index k is set to -1, you will store k in that location.

3.      If the element of cells with index k is not set to -1, you will store k in the next available location of cells (available indicated by the value 1)

So, if cells contains the following values: cells[3] = 3, cells[4] = 4, cells[5] = -1, cells[6] = 6 and cells[7] = -1. If the value read is 7 it will be stored in cells[7]; if the value read is 3, it will be stored in cells[5].

You can assume that cells is infinitely large.



7. (20 points)

A palindrome is a word, verse, or sentence that reads the same backward or forward. For example, "able was I ere I saw elba" is a palindrome, as are "abba", and "a".

Write a method called isPalindrome that takes one String parameter called str and returns true if str is a palindrome or false if it's not a palindrome.