0% found this document useful (0 votes)
59 views

ITCS114-Final - Key

The document is a final exam for a Computer Programming II course consisting of 6 multiple choice questions. The exam tests knowledge of Java programming concepts like classes, methods, arrays, recursion, input/output files. It contains questions that require writing code snippets to find outputs, read from files, search arrays, and handle exceptions. The last question is solely multiple choice to identify correct Java terms and concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

ITCS114-Final - Key

The document is a final exam for a Computer Programming II course consisting of 6 multiple choice questions. The exam tests knowledge of Java programming concepts like classes, methods, arrays, recursion, input/output files. It contains questions that require writing code snippets to find outputs, read from files, search arrays, and handle exceptions. The last question is solely multiple choice to identify correct Java terms and concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

University of Bahrain SERIAL NO:

College of Information Technology


Department of Computer Science
First Semester, 2022-2023
ITCS 114 - Computer Programming II

Final Exam
Date: 10-01-2023 Duration: Two Hours

KEY
STUDENT NAME

STUDENT ID # SECTION #

Question # MARKS COMMENTS

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

(B) Find Output ( 8 Points)


public static int find (int a1, int a2) { Output:
// each line 1 point
System.out.println (a1+"-"+a2);
// last number 2 points
if(a1==0) // Total = 5 points
2-2
return(a1+1); 1-0
else if(a1>0 && a2==2) 0-1
4
return 2 + find(a1-1,0);
else
return 1 + find(a1-1,a1);
}
public static void main(String[] args) {
int R = find(2,2); System.out.println (R);
}

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.

Input.txt Sample input/output


ITCS114;20193456;A- Enter Student ID: 20193456
ITCS113;20193456;F
ITCS254;20181234;D List of Courses Passed by Student 20193456
ITCS114;20201401;F ITCS114;A-
ITCS333; 20193456;B+ ITCS333;B+

import java.io.PrintWriter; import java.io.FileNotFoundException; import java.io.IOException;


import java.io.File; import java.util.Scanner;
public class file{
public static void main(String [] args){
// Total = 18
// open file = 2 pt // try = 2 pts // catch = 2 pt
// read student ID = 1 pt // loop through file = 2 pt // read line = 2 pt // split = 2 pt
// parse = 2 pt // if condition = 2 pt // output = 1 pt

Scanner inputStream = null;


Scanner kb = new Scanner(System.in);
try
{
inputStream = new Scanner(new File("Input.txt"));

System.out.print ("Enter Student ID: ");


long x=kb.nextLong();

System.out.println("List of Courses Passed by Student "+x);

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

catch(FileNotFoundException e) // or IOException { System.out.println(e.getMessage()); }

}// end of main


} // end of class

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

Question (4) Consider the following class

public class laptop {


private double price; private String model;
public laptop ( ) {price=0.0; model="X" ; }
public void setLaptop(double p, String m) {price=p; model=m ; }
public double getPrice ( ) {return price;}
public String getmodel ( ) {return model;}
}

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.

public static void findLaptop (laptop [ ] list, double y){

// 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

// Create array of size 5 integers (2 pt)


// Read array values (2 pts)
// algorithm to find first repeated values (4 pts)
// output final message ( 2 pts)

import java.util.Scanner;
public class arrDemo {
public static void main(String [] args) {

Scanner input = new Scanner (System.in);


int n=5;
int [] list = new int[n];

//read array elements


System.out.println("Enter values:");
for(int i=0; i<n; i++)
list[i] = input.nextInt();

int index = -1;


//check first repeated element
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(list[i]==list[j])
{
index=i;
break;
}
}

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:

1) Which method can change the text in a Label?


a. setText(); b. getText(); c. changeText(); d. all options are correct;

2) ________ can be used to enter or display a string.


a. A text field. b. button c. label d. Both text field and Label
3) The method that can be used to retrieve the text of a text field named myText;
a. myText.text()
b. myText.findText()
c. myText.retrieveText()
d. myText.getText()

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

Consider the Class Hierarchy below for Question 6 and 7

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)

6) The method display in Class B ____________ the method display in Class A


a. Overrides b. Overload c. Implements d. Extends

7) The declaration of the two methods named check in Class C is valid:


a. True b. False
Page 6 of 6

You might also like