ITCS114 Final Key
ITCS114 Final Key
Final Exam
KEY
STUDENT NAME
STUDENT ID # 2 0 SECTION #
1 10
2 – Part (A) 14
2- Part (B) 18
3 20
4 – Part (A) 10
4 – Part (B) 8
TOTAL 80
Page 1 of 7
Question 1:
OUTPUT
// each line 2 pt
3-10
0-10
1-10
10,20
R=30
Page 2 of 7
Question 2 – Part(A):
Write a Java program that defines a two dimensional array named numbers of size N X M of type integer.
N and M values should be entered by the user. The program should read the data from the keyboard into the
array. The program should then find and display the rows containing two consecutive zeros (i.e. two zeros
coming after each other in the same row).
Sample Input/output
Enter # Rows , # Cols: 4 5
Enter Values for Row 0: 1 2 3 0 5
Enter Values for Row 1: 1 0 0 3 8
Enter Values for Row 2: 0 0 0 0 4
Enter Values for Row 3: 1 0 1 0 5
Rows with two consecutive zeros:
Row = 1
Row = 2
import java.util.Scanner;
public class arrayTwo {
public static void main(String [] args){
Page 3 of 7
Question 2 – Part(B):
Write the definition of a class named shoppingList that include the following private members:
- list: an array of type String to hold the names of items in the list.
- numItems: an integer to represent the number of items in the list.
Additionally, the class should include the following public methods:
1. A default constructor to initialize numItems to zero and create list as an array of size 10.
2. A constructor with an integer parameter (m) to initialize numItems to zero and create list as an array
of size (m).
3. A method named addItem that takes a string (y). The method should add (y) to the end of the list array
and increment numItems if it is not full.
4. A method named replace that takes two strings (M, N). The method should replace all of occurrences
of (M) in the list array by (N).
5. A method named print to print the items in the list array.
import java.util.Scanner;
public class shoppingList {
Page 4 of 7
Question 3 :
Write a Java program to read from a file named “input.txt”, which includes the following:
- The first line shows the average price of all items in the file
- The file then contains a unknown number of items. Each item has a name and price separated by
comma, as the sample below.
The program should write into another file named “output.txt” the following:
- Items (name, price) with price above average.
- Total number of items in the input file and number of items with price above average.
Format your output file as the sample below.
input.txt output.txt
Average=8.275 Items above average price
Chair, 12.500 Chair, 12.500
Table, 10.00 Table, 10.00
Desk, 55.750 Desk, 55.750
Lamp, 4.00 …..
Glass 0.500 ……
.... # Items in the input file = 100
.... # Items above average = 13
Page 5 of 7
output.close();
} // end of main
}// end of class
Page 6 of 7
Question 4
Part (A) :
Find the output of the following program
public static int test (int x, int y)
{ Output
System.out.println(x+" "+y);
if ( y == 1){ // each line 2 pts
System.out.println("*"); 3 2
return y; 1 1
} *
else { 3 1
y--; 2
int r = test(x-2, y);
System.out.println(x+" "+y);
return (r+1);
}
}
Part (B):
Write a recursive function int power (int base, int exponent) to return baseexponent, i.e. to return the result
of raising the base number to the exponent power.
For example, power (3, 4) = (3)4 = 3 * 3 * 3 * 3 = 81
Assume that exponent is any integer number greater than or equal to Zero.
Hints: (base)0 = 1, (0)exponent = 0
Page 7 of 7