ELEC0021 Programming and Control Systems
final exam
Questions should be answered using code in Python Java, where appropriate, as the relevant
programming language.
4. (a) Why do we say that random numbers in programming languages are effectively pseudorandom? Write a piece of code that will print a number at random from the following set: 6, 10, 14, 18, 22.
Note: you will need to use the Random class randrange(start, stop, step) or randint(start, stop) method to generate random numbers.
(b) Why making instance variables public is not a good practice in object-oriented software engineering? Which alternative approach does exist and what does it achieve? Give an example of a class that benefits from having its instance variables private or “hidden”.
(c) Implement the relevant methods of a PowerCalculator class to calculate base exponent for a non-negative exponent in both an iterative and in a recursive manner. Write subsequently an excerpt of code to calculate recursively mn for integers m and n and to print out both the result and the time it has taken to calculate it.
5. (a) Write first pseudo-code and then Java Python code for the insertLast method of the List class which has a ListNode self.first instance variable but NOT a self.last one. What would be the key change if a ListNode self.last instance variable was also available? Pseudo-code has not been taught this year, ignore this part
(b) Write an excerpt of code that creates a list which contains integer numbers 1 to 10 and then, based exclusively on the first list, it creates a second one which is a precise copy of the first one but in the reverse order. Note that the content of the first list can change while creating the second one, i.e. the first list is no longer required. You are not allowed to use the Python list reverse() method. The List class is as presented in lecture 6 Data Structures 2
6. (a) Explain the difference between a linked list and a stack data structure. When implementing a stack through a linked list, which linked list instance variable remains unused and why?
(b) We would like to store telephone directory records that include various pieces of information for each person, ordered by their concatenated {surname, name, father’s name}. Comment on the advantages and disadvantages of using either a sorted array or an ordered linked list (note: not an ordered array list) as the candidate data structure for this application. Consider finding a person’s record as the key operation but also consider insertion and removal.
(c) The Java Python code at the end of the question implements the selection sort function which operates on a data list parameter method of a Sort class that includes a data integer array instance variable. Assuming we have an initial array list of the following 5 integers [7, 12, -1, 5, 3], show how the algorithm executes by stating the value of i, the range of j for this value of i, and also the value of smallest and the new content of the data array list at the end of each execution of the inner loop.
咨询 Alpha 小助手,获取更多课业帮助