Exam June, answers PDF

Title Exam June, answers
Course Problem Solving and Programming
Institution University of South Australia
Pages 23
File Size 564.2 KB
File Type PDF
Total Downloads 59
Total Views 155

Summary

Download Exam June, answers PDF


Description

SAMPLE Examination

Information Technology and Mathematical Sciences EXAMINATION COMP1039 Problem Solving and Programming This paper is for Mawson Lakes and Mawson Lakes (External) students.

IMPORTANT: THIS IS NOT THE EXAM. There is no guarantee that these questions will appear in the exam. You need to study all the material covered during the study period (i.e. in workshops, lectures, practical and assignment work. This SAMPLE EXAM is provided in order to give you practice in exam style questions only. This is a good indication of the style and type of questions you are likely to get in the actual examination.

Sample Examination SOLUTION

1 of 23

QUESTION 1

[25 marks]

What output does the following code produce? Code

Output

a) k = 0 theList = ['eight', 'seven', 'two'] while k num2): equal = 1 return equal

b) Using the function isEqual written above, write a program that determines whether two numbers are equal. Include the use of a loop that calls function isEqual 100 times. Use the random.randint() function to generate the two random numbers between 1 and 10 (inclusive). Display an appropriate message to the screen to let the user know the outcome of the call to function isEqual. If you didn’t complete part a), you may assume that you did in order to answer this question.

import random for num in range(100): num1 = random.randint(1,10) num2 = random.randint(1,10) equal = isEqual(num1, num2) if (equal == 0): print(num1, "and", num2, "are equal") elif (equal == -1): print(num1, "is less than", num2) elif (equal == 1): print(num1, "is greater than", num2)

12 of 23

QUESTION 4

[18 marks]

a) Write a function called isEven that takes a number as a parameter and determines whether the number is even. If the number is even, the function returns True. If the number is odd, the function returns False. [5 marks] def isEven(number): even = False if number % 2 == 0: even = True return even

b) Write a function called countEven that takes a list as a parameter and determines how many even values are stored in the list. The function returns the number of even values stored in the list, and for each list element displays whether it is odd or even to the screen (as seen below). You must make use of the isEven function by calling it from function countEven. You must call function isEven (written in part a) from within a loop to do this. If you didn’t complete part a), you may assume that you did in order to answer this question. You must use a loop in your solution. [10 marks] nums = [1, 2, 2, 3, 4, 7, 8] is displayed to the screen as follows: 1 2 2 3 4 7 8

is is is is is is is

odd. even. even. odd. even. odd. even.

def countEven(num_list): even_count = 0 for number in num_list: if isEven(number) == True: print(number, "is even.") even_count += 1 else: print(number, "is odd.") return even_count

13 of 23

c) Show how you would call function countEven and display the number of even values returned from the function to the screen. [3 marks] nums = [1, 2, 2, 3, 4, 7, 8] num_even = countEven(nums) print('\nNumber of even values in list are:', num_even)

14 of 23

QUESTION 5

[16 marks]

a) Write a function called displayList that accepts a list (of numbers) as a parameter and displays the list to the screen as seen below. [5 marks] For example: If the list passed to function displayList is [3, 1, 3, 7, 0, 5, 1, 9, 8, 6], the function should display the list to the screen as follows: List is:

3, 1, 3, 7, 0, 5, 1, 9, 8, 6

def displayList(number_list): print("List is:", end=' ') for i in range(len(number_list)): print (number_list[i], end='') if i < len(number_list)-1: print(", ", end='') else: print()

b) Write a function called findLargest that accepts a list (of numbers) as a parameter and finds the largest value stored in the list. The function returns the largest value. [5 marks] For example: If the list passed to function findLargest is [3, 1, 3, 7, 0, 5, 1, 9, 8, 6], the function should return the value 9. def findLargest(marks): result = marks[0] for num in marks: if num > result: result = num return result

15 of 23

QUESTION 5

continued…

c) Given the following list: nums = [3, 1, 3, 7, 0, 5, 1, 9, 8, 6] Write a program that: i. Displays the nums list to the screen. You must call function displayList to do this (written in part a). If you didn’t complete part a), you may assume that you did in order to answer this question. [2 marks] ii. Find the largest value stored in the list. You must call function findLargest to do this (written in part b). If you didn’t complete part b), you may assume that you did in order to answer this question. [2 marks] iii. Display the largest value to the screen as follows (given that we are using the nums list above): [2 mark] Largest value is 9

nums = [3, 1, 3, 7, 0, 5, 1, 9, 8, 6] displayList(nums) largest = findLargest(nums) print("Largest value is", largest)

16 of 23

QUESTION 6 [8 marks] Write a function called findNumber that takes a list of integer numbers (as a list) and a single number. The function returns as a list the position(s) in the list the number was found. The function returns an empty list if the number is not found. For example: If the list that is passed to the function as a parameter contains the following numbers: numList = [2, 4, 1, 2, 3, 2, 7]

and the single number we are looking for in the list is 2, i.e. number = 2

The call to findNumber(numList, number) should return the list [0, 3, 5]. That is, the number 2 was found in positions 0, 3 & 5 in the list.

def findNumber (theList, num): newList = [] index = 0 while index < len(theList): if theList[index] == num: newList.append(index) index += 1 return newList

17 of 23

QUESTION 7

[18 marks]

a) Write a function called swap(my_list, pos1, pos2) that swaps the positions of the items with indices pos1 and pos2 in the list named my_list. The function should not return anything. [4 marks] For example: Given the following list, my_list = ['a', 'b', 'c', 'd', 'e', 'f'] if function swap() were called as follows: swap(my_list, 1, 4) the resulting list would be: my_list = ['a', 'e', 'c', 'd', 'b', 'f'] Note that the list elements at positions 1 and 4 have been swapped.

def swap(my_list, pos1, pos2): temp = my_list[pos1] my_list[pos1] = my_list[pos2] my_list[pos2] = temp

18 of 23

QUESTION 7

continued…

b) Write a function called reverse_list(my_list) that takes a list as a parameter and reverses the list. The function should not return anything. Your solution MUST make use of the swap function you wrote in part a). You MUST use a loop in your solution. You must not use built-in functions (other than the len() and range() functions if necessary), slice expressions, list methods or string methods in your solution. [6 marks] If you didn’t complete part a), you may assume that you did in order to answer this question. For example: Given the following list, my_list = ['a', 'b', 'c', 'd', 'e', 'f'] if function reverse_list() were called, the resulting list would be: my_list = ['f', 'e', 'd', 'c', 'b', 'a'] Note that the entire list has been reversed.

def reverse_list(my_list): top = len(my_list)-1 bottom = 0 while bottom < top: swap(my_list, bottom, top) bottom = bottom + 1 top = top - 1

19 of 23

QUESTION 7

continued…

c) Given the following list: my_list = ['a', 'b', 'c', 'd', 'e', 'f'] Show how you would call function reverse_list(). Display the list (resulting from the call to function reverse_list()) to the screen. If you didn’t complete part a) or part b), you may assume that you did in order to answer this question. [2 marks]

my_list = ['a', 'b', 'c', 'd', 'e', 'f'] print(my_list) reverse_list(my_list) print(my_list)

20 of 23

QUESTION 8

[12 marks]

a) Given the following class Movie, what output does the following code produce? class Movie: def __init__(self, title, actor_list, time, rating): self.__title = title self.__actor_list = actor_list self.__running_time = time self.__rating = rating def get_title(self): return self.__title def set_title(self, title): self.__title = title def add_actor(self, actor): self.__actor_list.append(actor) def get_actors(self): return self.__actor_list def get_rating(self): return self.__rating def set_rating(self, rating): self.__rating = rating def __str__(self): movie_str = self.__title + ": " for actor in self.__actor_list: movie_str += actor + ", " movie_str += self.__rating return movie_str movie_list = [] movie1 = Movie("Captain America: Civil War", ["Chris Evans", "Robert Downey Jr."], 147, "M")

movie1.add_actor("Scarlett Johansson") print(movie1) movie_list.append(movie1) movie2 = Movie("The Heat", ["Sandra Bullock", "Melissa McCarthy"], 117, "MA15+") print(movie2) movie_list.append(movie2) print("\n\nMy movie list:") for movie in movie_list: print(movie.get_title(), "-", movie.get_rating())

[4 marks] Write your answer below. Captain America: Civil War: Chris Evans, Robert Scarlett Johansson, M The Heat: Sandra Bullock, Melissa McCarthy, MA15+ My movie list: Captain America: Civil War - M The Heat - MA15+ 21 of 23

Downey

Jr.,

QUESTION 8

continued…

b) Complete the set_running_time and get_running_time methods of the following Movie class. Add your code to the following class (you must also include the appropriate parameters). [4 marks] class Movie: def __init__(self, title, actor_list, time, rating): self.__title = title self.__actor_list = actor_list self.__running_time = time self.__rating = rating def get_running_time(self): return self.__running_time def set_running_time(self, running_time): self.__running_time = running_time def get_title(self): return self.__title def set_title(self, title): self.__title = title def add_actor(self, actor): self.__actor_list.append(actor) def get_actors(self): return self.__actor_list def get_rating(self): return self.__rating def set_rating(self, rating): self.__rating = rating def __str__(self): movie_str = self.__title + ": " for actor in self.__actor_list: movie_str += actor + ", " movie_str += self.__rating return movie_str

22 of 23

QUESTION 8

continued…

c) Create an object of the Movie class (e.g. The Big Short, Christian Bale, Steve Carell etc.) and show how you would call the get_running_time() method. Display the movie running time returned from the call to method get_running_time() to the screen. If you didn’t complete part b), you may assume that you did in order to answer this question. [4 marks]

another_movie = Movie("The Big Short", ["Christian Bale", "Steve Carell"], 130, "MA15+") running_time = another_movie.get_running_time() print("The running time is:", running_time)

or…

another_movie = Movie("The Big Short", ["Christian Bale", "Steve Carell"], 130, "MA15+") print("The running time is:", another_movie.get_running_time())

THIS IS NOT THE EXAM. There is no guarantee that these questions will appear in the exam. You need to study all the material covered during the study period (i.e. in workshops, lectures, practical and assignment work. This SAMPLE EXAM is provided in order to give you practice in exam style questions only. This is a good indication of the style and type of questions you are likely to get in the actual examination.

-- END OF SAMPLE EXAM -23 of 23...


Similar Free PDFs