Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Name: Last Name:

Subject:

Cohort: Classroom:

Grade/100 Midterm/Final Exam Supervisors Signature

PROFESSOR NAME: OUMAYMA BOUNOUH


Fall 2023
Date: 16/10/2023
Time: 11:30-13:30
Classroom visit:

INSTRUCTIONS

1. Books and notes are not allowed.


2. Cell phones are not allowed.
3. Calculators are not allowed.

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 an exam
grade of zero.

Mediterranean Institute of Technology Midterm Exam Fall 2023 1/10


Understanding questions (30 points)
1. Question 1
Write a method containsAll that takes two ArrayLists of integers as parameters and that returns true if the first
ArrayList contains all of the values of the second ArrayList and that returns false otherwise. For example, if the
two Arraylists are:
s1: [17, 16, 7, 10, 12, 13, 14]
s2: [7, 12, 13]
then the call containsAll(s1, s2) would return true while the call containsAll(s2, s1) would return false.
……………………………………………………………………………………………………………………………………….

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

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

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

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

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

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

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

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

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

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

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

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

2. Question 2
Propose two solutions that allow the creation of non-mutable objects.
……………………………………………………………………………………………………………………………………

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

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

3. Question 3
Write a code snippet to iterate over LinkedList nodes to return a comma-separated, bracketed version of the list.

Mediterranean Institute of Technology Midterm Exam Fall 2023 2/10


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

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

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

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

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

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

Mediterranean Institute of Technology Midterm Exam Fall 2023 3/10


Multiple choice questions (16 Points)
1) Question 1
Which of the following have O(1) complexity at the best case
(A) Linear search with index =0
(B) Binary search with index=mid
(C) Bubble sort with index =n

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

2) Question 2
What is the Big O notation for inserting a new node at the beginning of a singly linked list?
(A) O(1)
(B) O(log n)
(C) O(n)
(D) O(n^2)

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

3) Question 3
Which of the following is the right way to create an ArrayList?
(A) ArrayList <String> Sentences= new ArrayList <String> ()
(B) ArrayList <boolean> tests= new ArrayList <boolean> ()
(C) ArrayList <String> Sentences= new ArrayList <String> (100)

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

4) Question 4
What is the complexity of the following code snippets
static void towerOfHanoi(int numDisks, char source, char destination, char auxiliary)
{
if (numDisks == 1) {
System.out.println("Move disk 1 from " + source + " to " + destination);
return;}
towerOfHanoi(numDisks - 1, source, auxiliary, destination);
System.out.println("Move disk " + numDisks + " from " + source + " to " + destination);

Mediterranean Institute of Technology Midterm Exam Fall 2023 4/10


towerOfHanoi(numDisks - 1, auxiliary, destination, source);
}
(A) O(1)
(B) O(2n)
(C) O(n)
(D) O(n²)
………………………………………………………………………………………………………………………………………….

5) Question 5
What about recursion is true in comparison with iteration?
(A) Very expensive in terms of memory.
(B) Low performance.
(C) Every recursive program can be written with iteration too.
(D) All the above are true!
…………………………………………………………………………………………………………………………………………

6) Question 6
Suppose I have int b = new int[42]. What are the highest and lowest legal array indexes for b?
(A) 0 and 41
(B) 0 and 42
(C) 1 and 41
(D) 1 and 42
…………………………………………………………………………………………………………………………………………

7) Question 7
Suppose cursor refers to a node in a linked list (using the IntNode class with instance variables called data
and link). What statement changes cursor so that it refers to the next node?
(A) cursor++;
(B) cursor = link;
(C) cursor += link;
(D) cursor = cursor.link;
………………………………………………………………………………………………………………………………………….

8) Question 8
Which boolean expression indicates whether the numbers in two nodes (p and q) are the same. Assume that
neither p nor q is null.

Mediterranean Institute of Technology Midterm Exam Fall 2023 5/10


(A) A. p == q
(B) B. p.data == q.data
(C) C. p.link == q.link
(D) D. None of the above.
………………………………………………………………………………………………………………………….

Mediterranean Institute of Technology Midterm Exam Fall 2023 6/10


Problem II (10 points)
Question
What is the worst complexity of the following code snippets?
(A) int MethodExam(int n) {
int sum = 2023;
for(int i=0; i < m; i++) {
for(int k=i; k >= 5; k--) {
sum--;
}
}
return sum;
}

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

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

(B) void smiley (int n) {


for (int i = 0; i < n * n; ++i) {
for (int k = 0; k < i; ++k)
System.out.println(”k = ” + k);
for (int j = n; j > 0; j--)
System.out.println(”j = ” + j);
}
}

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

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

Mediterranean Institute of Technology Midterm Exam Fall 2023 7/10


Problem (44 points)
We want to develop an application for managing resources for an agricultural products store.
The interface 'SeasonCriteria' contains the method 'isFresh(String season)'. The implementation of this
method should return true if the season passed as a parameter is identical to the harvest season of the
product.
1. Class Product:
✓ The different characteristics of an agricultural product are as follows:
• An identifier (integer)
• A label (string)
• A brand (string)
• A price (real)
• Season (string)
✓ The methods of the class are:
• A constructor to initialize the different fields.
• 'toString()' which returns a string.
2. Class Store
The different characteristics of a store are:
• An identifier: an integer representing the store's reference
• An address: string of characters
• Store capacity: the store's capacity is set to 50 products
• A set of products: an array of products
• Number of products: the number of products in the store
The methods of the class are:
• A constructor to initialize the different fields.
• Accessor and modifier for the attribute: identifier.
• Accessor and modifier for the attribute: address.
• addProduct(Product p): allows adding a new product to the store, considering that a store
can contain a maximum of 50 products.
• Boolean findProductById (int productId): searching for a product by its identifier in the
store.
N.B: The 'StoreFullException' exception is triggered when the store is full. The 'getMessage()' method of
'StoreFullException' returns a message indicating that the store is full.
'toString()': returns a string containing the identifier and address of the store in the following format:
Store {identifier=1, address=Downtown}"
So please, fill in the missing blanks:

Mediterranean Institute of Technology Midterm Exam Fall 2023 8/10


interface ………………………………….. {
boolean ………………………..(……………… ……………………);
}

class StoreFullException ……………………….. ………………………………. {


public String ……………………………() {
……………………….. ………………………………………………………;
}
}

class Product …………………… ……………………….{


private int id;
private String ………………………., ………………………., …………………..;
private ………………………… price;

public Product(,………………. ……………..,………………. ……………..,……………….


……………..,………………. ……………..,………………. ……………..) {
this.id = id;
this.label = label;
this.brand = brand;
this.season = season;
……………. ………… ……………………;
}
public String toString() {
return "Product {" + id + "=1021, label=" + label + ", price=" + price + "}";
}

public …………….. IsFresh(…………… ……….) {


……… ……………………………………..;
}
}

Class Store {

private int id, nbP;


private String address;
private …………….. ……………… ……………. = ………;
private ……………. ……………. = new …………………;

public Store(int id, String address) {


this.id = id;
this.address = address;
}

Mediterranean Institute of Technology Midterm Exam Fall 2023 9/10


public int getId() {
………….. …………;
}
public …………. getAdresse() {
return address;
}
public void setId(……… ………) {
this.id = id;
}

public ………….. setAddress(String address) {


………….address = ……………;
}

public void AddProd(Product p) …………. StoreFullException{


if (……………………………..) {
……………. = …………;
nbP++;
} else {
…………… new StoreFullException();
}
}
Public …………………. findProductById(int productId) {
for (int i = 0; i < numProducts; i++) {
if (………………………………………) {
return (products[i]);
}
}
return ………….. ;
}
}

public ………………….. ……………….() {


return "Store {identifier=" + id + ", address=" + address + "}";
}

END
Good Luck

Mediterranean Institute of Technology Midterm Exam Fall 2023 10/10

You might also like