[go: up one dir, main page]

0% found this document useful (0 votes)
17 views10 pages

Final Exam Spring23

Uploaded by

Imy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

Final Exam Spring23

Uploaded by

Imy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Name: Last Name:

Subject:

Cohort: Classroom:

Grade/100 CS202 FINAL EXAM Supervisors Signature

Advanced Object-Oriented Programming


PROFESSOR NAME: SYRINE BEN MESKINA
Final Exam Spring 2023
Date: May 2nd, 2023
Time: 9 – 11am
Classroom visit: 10am

INSTRUCTIONS

1. Books and notes are not allowed.


2. Cell phones and calculators are not allowed.
3. Answers must be written on the exam paper.
4. Unreadable and not clean answers are not accepted.
5. Do not use pencils.

Statement of Academic Integrity

Statement on Academic Integrity


I pledge to pursue all academic endeavors while at MedTech with honor and integrity. I understand the principles of
the MedTech Honor Code, and I promise to uphold and respect them. I pledge not to ask for or give information
pertaining to any examination before or after I have taken it, in such a way as to gain or give an advantage to one
student over another. I understand to be honor code violation: The giving and/or receiving of unauthorized aid on a
paper, problem, homework, exam, computer program, or other assignment submitted to meet course requirements;
plagiarism on an assigned paper, report, exam, or other submission; failure to report a known or suspected violation
of the Honor Code; any action designed to deceive a member of the faculty, staff, or a fellow student regarding the
principles mentioned in the Code; the submission of work prepared for another course; the use of texts, papers,
computer programs, or other class work prepared by commercial or noncommercial agents and submitted as my own
work; the falsification research results; or the altering of a previously graded exam or test with the objective of having
the original grade reconsidered.
Ps: All classrooms are equipped with video cameras
Statement to be signed by the student:
I confirm that I have read and understood the statement above on academic integrity and certify that I did
not commit and did not attempt to commit academic fraud in this examination.
Signed: ______________________________________
Note: an examination copy or booklet without that signed statement will not be graded and will receive a final exam
grade of zero.

Mediterranean Institute of Technology Final Exam Spring 2023 1/10


ABSTRACT CLASSES, INTERFACES & POLYMORPHISM (25 POINTS)
Question 1 - What is the difference between interfaces and abstract classes in Java? (3pts)
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

Question 2 – Encircle the right option from the following. (3pts)

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)
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

Question 4 – Consider the following interface (4pts)


interface SomethingIsWrong {
void aMethod(int aValue) {
System.out.println("Hi Mom"); }
}

a - What is wrong with the interface above?


………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

b – Fix the interface.


………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

Mediterranean Institute of Technology Final Exam Spring 2023 2/10


Question 5 – Is the following interface valid? If not, what is the reason? (2pts)
public interface Marker {
}

………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

Question 6 – Consider the following two classes. (3pts)

public class ClassA { public class ClassB extends ClassA {


public void methodOne(int i) {...} public static void methodOne(int i) {...}
public void methodTwo(int i) {...} public void methodTwo(int i) {...}
public static void methodThree(int i) {...} public void methodThree(int i) {...}
public static void methodFour(int i) {...} public static void methodFour(int i) {...}
} }

a - Which method overrides a method in the superclass?


………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

b - What do the other methods do?

………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

Exercise - Consider the following three classes. (6pts)


class ProgrammingLanguage {
public void display() {
System.out.println("I am Programming Language.");}
}

class Java extends ProgrammingLanguage {


@Override
public void display() {
System.out.println("I am Object-Oriented Programming Language.");}
}

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 }

Mediterranean Institute of Technology Final Exam Spring 2023 3/10


a – What is the output of the principal program?
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

b – Replace lines 3, 5 and 8 with suitable comments that describe the role of instructions written after them.
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

c – To which class(es) does p1 refer in lines 6 and 9?


………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

EXCEPTIONS HANDLING (20 POINTS)


Question 1 - Is the following code legal? (2pts)
try {

} 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) { }

………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

Mediterranean Institute of Technology Final Exam Spring 2023 4/10


Question 4 – what is the difference between a checked exception and an unchecked one? (2pts)

………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

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;

The JVM starts running your program, but the JVM


b. 2. __checked exception
cannot find the Java platform classes.

A program is reading a stream and reaches the end of


c. 3. __compile error
stream marker.

Before closing the stream and after reaching the end of


d. 4. __no exception
stream marker, a program tries to read the stream again.

Exercise 1 - Modify the following cat method so that it will compile. (4pts)

public static void cat(File file) {

RandomAccessFile input = null;


String line = null;

try {

input = new RandomAccessFile(file, "r");

while ((line = input.readLine()) != null) {

System.out.println(line);
}
return;
}

finally {

if (input != null) {

input.close(); }

}
}

Mediterranean Institute of Technology Final Exam Spring 2023 5/10


Exercise 2 – Implement a custom exception class from your choice and provide an example of instruction(s) that shows how this
exception can be raised. (4pts)

………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

FILE HANDLING & SERIALIZATION (25 POINTS)


Question 1 – The following code is supposed to create a file called "data.txt" in the current working directory and write the
string "Hello, world!" to it. However, there is a mistake in the code that prevents it from working correctly. Your task is to
identify the mistake and correct it. (4pts)

import java.io.*;

public class FileTest {


public static void main(String[] args) {
try {
if (!f.exists())
f.createNewFile();
FileWriter fw = new FileWriter(f);
fw.write("Hello, world!");
fw.close();
System.out.println("File written successfully!");
} catch (Exception e) {
System.out.println(e);
}
}
}

Mediterranean Institute of Technology Final Exam Spring 2023 6/10


Question 2 – What should be added to the code previously corrected (in question 1) in order to make it append the file? (3pts)

………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………

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

public class ReadFile {


public static void main(String[] args) {
try {
File myObj = new File("students.txt");
Scanner myReader = new Scanner(myObj);
String data = myReader.nextLine();
System.out.println(data);
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace(); }
}
}

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

b - Which of the following methods is used to serialize an object in Java?


 Object.write()
 Object.writeToStream()
 ObjectOutputStream.write()
 ObjectOutputStream.writeObject()

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.

Mediterranean Institute of Technology Final Exam Spring 2023 7/10


d - Which of the following is a benefit of object serialization in Java?
 It allows Java objects to be converted into XML format
 It allows Java objects to be shared between different applications running on different machines
 It allows Java objects to be accessed directly in memory
 It allows Java objects to be converted into machine code for faster execution

e - Which of the following is a limitation of object serialization in Java?


 It cannot handle complex object graphs with circular references
 It cannot handle objects with null fields
 It cannot handle objects with primitive data types
 It cannot handle objects with arrays as fields

f - What happens to a transient variable during object serialization in Java?


 It is excluded from the serialization process and not written to the output stream
 It is included in the serialization process and written to the output stream
 It is automatically encrypted for secure storage
 It is converted to a string representation before being written to the output stream

SOCKET PROGRAMMING (15 POINTS)


Question 1 – The following code creates a client-server socket connection. The client sends a message to the server, and the
server responds by echoing the same message back to the client. The program should follow these steps:
• Create a ServerSocket object and bind it to a specific port number. (Instruction 1)
• Create a Socket object and connect it to the server's IP address and port number. (Instruction 2)
• Use the OutputStream object of the Socket object to send a message from the client to the server. (Instruction 3)
• Use the InputStream object of the Socket object to read the response message from the server.
• Print the response message on the client's console. (Instruction 4)
• Close the Socket object and the ServerSocket object. (Instruction 5)

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.*;

public class Server {


public static void main(String[] args) throws IOException {
…………………………………………………………………………… // instruction 1
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.println(inputLine); // Echo the message back to the client
}
out.close();

Mediterranean Institute of Technology Final Exam Spring 2023 8/10


in.close();

…………………………………………………………………………… // instruction 5
…………………………………………………………………………… // instruction 5
}
}

// Client code
import java.net.*;
import java.io.*;

public class Client {


public static void main(String[] args) throws IOException {
…………………………………………………………………………… // instruction 2
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));

String message = "Hello server!";


System.out.println(message);

String response = in.readLine();


…………………………………………………………………………… // instruction 4

out.close();
in.close();
…………………………………………………………………………… // instruction 5
}
}

DATABASE CONNECTION (15 POINTS)


Question 1 - Is the following code legal? (2.5pts each)
a - What is the purpose of the DriverManager class in Java Database Connectivity (JDBC)?
 To connect to the database
 To execute SQL statements
 To retrieve metadata about the database
 To handle exceptions

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()

e - What is the purpose of the try-with-resources statement when using JDBC?


 To ensure that resources are properly closed
 To catch exceptions thrown by JDBC
 To improve the performance of JDBC operations
 To establish a connection to the database

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

Mediterranean Institute of Technology Final Exam Spring 2023 10/10

You might also like