Final Exam Spring23
Final Exam Spring23
Subject:
Cohort: Classroom:
INSTRUCTIONS
If you need to define a set of behaviors that can be implemented across unrelated classes, then an
[abstract class / interface] is the best choice. If you need to define a set of behaviors that can be
extended by subclasses, then [abstract class / interface] is the best choice.
Question 3 – Can an interface extend to another interface? If so, how do you declare it (give an example of declaration)
and what is the purpose? (4pts)
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
1 class Main {
2 static void main(String[] args) {
3 // instruction 1
4 ProgrammingLanguage pl;
5 // instructions 2
6 pl = new ProgrammingLanguage();
7 pl.display();
8 // instructions 3
9 pl = new Java();
10 pl.display(); }
11 }
b – Replace lines 3, 5 and 8 with suitable comments that describe the role of instructions written after them.
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
} finally {
}
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
Question 2 - What exception types can be caught by the following handler? What is not good with using this type of exception
handler? (2pts)
catch (Exception e) {
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
Question 3 - Is there anything wrong with the following exception handler as written? Will the following code be compiled?
(2pts)
try {
} catch (Exception e) { }
catch (ArithmeticException a) { }
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
Question 5 - Match each situation in the first list with an item in the second list. (4pts)
int[] A;
a. 1. __error
A[0] = 0;
Exercise 1 - Modify the following cat method so that it will compile. (4pts)
try {
System.out.println(line);
}
return;
}
finally {
if (input != null) {
input.close(); }
}
}
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
import java.io.*;
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
Question 3 – a - What is the output of the following code? (See part b of this question) (2pts)
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
b – What is missing in the following code to read the data line by line from the text file named “students.txt”? (4pts)
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
Question 4 – Tick the right answer(s) in the following statements. (2pts each)
a - What is object serialization in Java?
A process of converting a Java object into a sequence of bytes
A process of converting a sequence of bytes into a Java object
A process of encrypting a Java object for secure storage
A process of compressing a Java object to save disk space
c - What happens if a class does not implement the Serializable interface and an attempt is made to serialize an object of
that class?
The object will be serialized successfully without any issues.
The JVM will throw a NullPointerException at runtime.
The JVM will throw a NotSerializableException at runtime.
The JVM will throw a ClassNotFoundException at runtime.
Complete the following code snippet to implement this program based on the instructions described above and find the lines
implementing instructions 3.
// Server code
import java.net.*;
import java.io.*;
…………………………………………………………………………… // instruction 5
…………………………………………………………………………… // instruction 5
}
}
// Client code
import java.net.*;
import java.io.*;
out.close();
in.close();
…………………………………………………………………………… // instruction 5
}
}
b - Which of the following is not required when establishing a connection to a database using JDBC?
Database URL
Database driver
Database schema
Database username and password
c - Which method of the Connection interface is used to execute SQL statements and retrieve results?
prepareStatement()
executeQuery()
executeUpdate()
setAutoCommit()
Mediterranean Institute of Technology Final Exam Spring 2023 9/10
d - Which method of the ResultSet interface is used to move the cursor to the next row of the result set?
next()
previous()
first()
last()
f - Which of the following is not a valid way to retrieve data from a ResultSet object?
Using the getString() method to retrieve a string value
Using the getInt() method to retrieve an integer value
Using the getDate() method to retrieve a date value
Using the executeQuery() method to retrieve a result set
END
Good Luck