ITCS114-Final - Key
ITCS114-Final - Key
Final Exam
Date: 10-01-2023 Duration: Two Hours
KEY
STUDENT NAME
STUDENT ID # SECTION #
1 13
2 18
3 10
4 10
5 10
6 14
TOTAL 75
Notes:
1. Please write only one answer for each question. Only the first answer will be graded.
2. You may use the back pages to complete your answer if needed.
Page 1 of 6
Question (1)
(A) Find Output ( 5 Points)
public class testClass { public class testDemo{ Output:
private int x; public static void main(String [] args) // each 1 point = Total 5 points
private static int y = 0; { 1:0
testClass one = new testClass(1); 2:0
public testClass(int newX) testClass two = new testClass(2); 2:31
{ x=newX; } one.print(); 2:50
4:50
two.print();
public void set(int newX) try{
{ x=newX; y++;} one.set();
one.setY(30);
public void set() two.set(4);
{ x++; } if(two.getY()>10)
{
public void setY(int newY) one.print();
{ y=newY; } throw new Exception("Error");
}
public static int getY ( ) two.print();
{ return y; } one.setY(40);
}
public void print() catch(Exception e){
{ System.out.println(x+":"+y);} two.setY(50);
} // end of class }
one.print();
two.print();
}// end of main
}// end of class
Page 2 of 6
Question (2)
Write a Java program to read the content of a file named “input.txt”, in which each line contains a Course
Code, Student’s ID, and Student’s Grade scored by this student in this course. The different fields are
separated by “;”. Your java program should prompt the user to enter Student ID (X) and then read the
content of the text file to output (on screen) the course code and grade of all courses passed this student
(X). A student will pass the course if his grade is not F.
while(inputStream.hasNextLine())
{
String line = inputStream.nextLine();
String[] ary = line.split(";");
long id = Long.parseLong(ary[1]);
if (id==x)
{
String grade = ary[2];
if (!grade.equalsIgnoreCase("F"))
System.out.println(ary[0] + ";" + grade);
}
}
}// end of try
Page 3 of 6
Question (3)
Write a recursive method that takes an integer (n) as parameter. The method should return the sum of odd
numbers from 1to n. For example: If n =1, the method should return: 1;
If n =3, the method return output: 1 + 3 = 4; If n =7, the method should output: 1 + 3 + 5 + 7 = 16
public static int findSum(int n){
// Total = 10 points
if (n==1) // 2pts
return 1; // 2pts
else if (n%2==1) // 2pts
return n+findSum(n-1); // 2pts
else
return findSum(n-1); // 2pts
Write a static method named findLaptop that takes as parameter the array list and double value y. The
method should output the model of all laptops with price less than y. Note: assume the array list is
declared and populated with data in the main method.
// Total = 9 pts
for (int i=0; i< list.length; i++) // 3 pts
{
if (list[i].getPrice() < y) // 3 pts
System.out.println (list[i].getmodel()); // 3 pts
}
Page 4 of 6
Question (5)
Write Java program that reads five integer values from the user into an array named list. Then search for
the first repeated value in the array and output the value of the first repeated element and its location. In
case no repeated values, output an appropriate message.
Sample Input/output:
Enter values: 1 2 5 2 5 Enter values: 1 2 3 4 5
The first repeated value is 2 Found at index 1 There are no repeated value
import java.util.Scanner;
public class arrDemo {
public static void main(String [] args) {
if(index != -1)
break;
}
if(index!=-1)
System.out.println("The first repeated element is " + list[index] + " Found at index " + index );
else
System.out.println("There are no repeated elements");
}
}
Page 5 of 6
Question (6) Select the correct Answer:
4) Methods in the class hierarchy which have the same name, return type, and parameters overload
corresponding inherited methods
a. True b. False
5) The concept of having more than one method with same name and same signature in super and
sub class but with different implementation.
a. Overriding b. Overloading c. Polymorphism d. Composition
A
int x
int y
int setData ()
int getX()
int getY()
void display()
B C
int b int c
int check (int) int check (int)
void display() void check (int, int)