Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Practice Question 2: Supposed that the implementation of an ADT changes, but the

ID: 3606598 • Letter: P

Question

Practice Question 2: Supposed that the implementation of an ADT changes, but the interface of the ADT is unchanged. Which statement is true?

a) That can’t happen; if the implementation changes, then the interface must also change.

b) That can only happen if the if the interface has no exceptions.

c) That can only happen if the interface has no public methods.

d) That can happen, but clients will have to be rewritten.

e) That can happen, and clients may continue using the ADT without being rewritten.

Practice Question 3: Which of the following is a reasonable “axiom” about a list that you might test as part of an implementation? (Presented in English to make reading easier.)

a) If you create a new list, then its size is 1.

b) If you remove an item from a non-empty list, then afterward its size decreases by 1.

c) If you remove an item from a non-empty list, then afterward it’s also non-empty.

d) If you add a new item at position k of a list that has more than k elements in it, then the number of items on that list remains the same.

e) If the value that you get for the item at position k of a list is v, then after you add a new item at position k of that list, the value at position k is still v.

I did not understand these two questions, please give me some explanation about the answers

Explanation / Answer

Hello, in the following, I provide detailed explanation to your questions. Hope it helps you to get better understanding of the problems.


Practice Question 2: Supposed that the implementation of an ADT changes, but the interface of the ADT is unchanged. Which statement is true?

Answer: "e) That can happen, and clients may continue using the ADT without being rewritten."


Let us consider the List ADT and the following is its interface
interface List {
add(T element);
remove(T element);
int size();
....
}
All the clients are supposed to use the methods that are declared in the interface definition.

Consider the add() method, at first, you may write the implementation using static Arrays. Note that, the clients are not aware of the specific datastructure that has been used to implement the add() method.

At a later point of time, if you change the datastructure to LinkedList or modify the logic in the add() method, there will be NO impact on the clients. They do NOT need to modify their code and nor will the change be reflected in the interface definition.

In short: Implementation changes are completely transparent to the Clients and Interface definition. This is the advantage of programming to the Interfaces.

Please note: All the other options are incorrect assumptions.



Practice Question 3: Which of the following is a reasonable “axiom” about a list that you might test as part of an implementation?

Answer: "b) If you remove an item from a non-empty list, then afterward its size decreases by 1."

When you remove an item from a non-empty list, the size of the list decreases by one.

Let me explain why other options are incorrect

Option a) If you create a new list, then its size is 1.
- When you create a list, the size will be zero. Only when you add an new element, the size increases

Option c) If you remove an item from a non-empty list, then afterward it’s also non-empty.
- When you remove an item from non-empty list with size 1, then the list size decreases to zero (i.e. empty). So axiom is incorrect

Option d) If you add a new item at position k of a list that has more than k elements in it, then the number of items on that list remains the same.
- When you add a new item at position k of a list, the earlier item at that position shifts to right. Hence the number of items in the list increases and does NOT remain the same.

Option e) If the value that you get for the item at position k of a list is v, then after you add a new item at position k of that list, the value at position k is still v.
- if the new item that is added contains a different value - say v1, then you get the item at position k, the value will NOT be still v but the value will be v1.
The earlier item will be shifted to next index and will be available at k+1.



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote