100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Exam (elaborations)

COMP 102 Introduction to Computer Science Fall 2020 Midterm Exam Part A Questions

Rating
-
Sold
-
Pages
40
Grade
A+
Uploaded on
05-12-2022
Written in
2022/2023

COMP 102 Introduction to Computer Science Fall 2020 Midterm Exam Part A Questions Part A Question 1 You have a summer job at Subway Sandwiches. You make a program to help you keep track of the jobs you have to do each day. list_of_jobs =["clean floor", "bake bread", "check vegetables", "make cookies", "clean oven", "wipe glass"] while len(list_of_jobs) 0: print() for index in range( len(list_of_jobs) ): print(index, list_of_jobs[ index ]) print("Which job have you done?") this_job_index = input("Enter one of the numbers: ") list_of_e(list_of_jobs[ this_job_index ]) print() print("Finished all jobs!") Part A Question 1 - Answer You have a summer job at Subway Sandwiches. You make a program to help you keep track of the jobs you have to do each day. while len(list_of_jobs) > 0: print() for index in range( len(list_of_jobs) ): this_job_index = int(this_job_index) list_of_e(list_of_jobs[ this_job_index ]) print() Two possible answers: > (is greater than) != (is not equal to) input() produces text, so we need to convert the text to an integer Also OK: print("Finished all jobs!") this_job_index=int(float(this_job_index)) Part A Question 2 When the following program is executed the user enters two numbers (i.e. m and n). Based on the input two values, which one of the following images cannot be generated by the program? import turtle (0) urtle() m = int(input("Enter an integer m:")) n = int(input("Enter an integer n:")) for _ in range(m): for _ in range(n): rd(30) (360 / n) () rd(50) (360 / m) () () Part A Question 2 - Answer When the following program is executed the user enters two numbers (i.e. m and n). Based on the input two values, which one of the following images cannot be generated by the program? import turtle (0) urtle() m = int(input("Enter an integer m:")) n = int(input("Enter an integer n:")) for _ in range(m): for _ in range(n): rd(30) (360 / n) () rd(50) cannot m=3, n=3 m=4, n=4 (360 / m) () rd(50) makes the shapes m=5, n=3 m=5, n=5 m=6, n=4 () not touch at the center Part A Question 3 Fill in the blank so that the number of teachers in the COMP1021 teaching team is correctly counted. Here is a Python program: teaching_team = [ ("Dave", "Teacher"), ("Peter", "TA"), ("Jimmy", "TA"), ("Cecia", "Teacher"), count_teacher = 0 for t in teaching_team: ("Gibson", "Teacher"), if t[ ] == "Teacher": ("Kelvin", "TA"), ("Alex", "Teacher"), ("Dimitris", "Teacher") ] count_teacher = count_teacher + 1 print("count_teacher:", count_teacher) Part A Question 3 - Answer Fill in the blank so that the number of teachers in the COMP1021 teaching team is correctly counted. Here is a Python program: teaching_team = [ ("Dave", "Teacher"), ("Peter", "TA"), ("Jimmy", "TA"), ("Cecia", "Teacher"), ("Gibson", "Teacher"), ("Kelvin", "TA"), ("Alex", "Teacher"), ("Dimitris", "Teacher") The program prints this: count_teacher = 0 for t in teaching_team: if t[ 1 ] == "Teacher": count_teacher = count_teacher + 1 print("count_teacher:", count_teacher) ] Part A Question 4 What is the missing piece of code? There may be more than one answer. However, the code you write must be the simplest (least typing and least complex) code that works. Write the missing piece of code. Don’t write the whole line of code, just write the missing part. Don’t write any spaces in your answer. import turtle (0) or("white") urtle() diameter=400 for value in range(100): ( ) (diameter) diameter= diameter-4 () Part A Question 4 import turtle (0) or("white") urtle() diameter=400 for value in range(100): ( ) (diameter) diameter= diameter-4 () Part A Question 4 - Answer What is the missing piece of code? There may be more than one answer. However, the code you write must be the simplest (least typing and least complex) code that works. Write the missing piece of code. Don’t write the whole line of code, just write the missing part. Don’t write any spaces in your answer. import turtle (0) or("white") urtle() diameter=400 for value in range(100): ( ? Accepted answers: "gray"+str(value) 'gray'+str(value) "grey"+str(value) ) 'grey'+str(value) (diameter) diameter= diameter-4 () Part A Question 5 A student is practicing Python programming by writing a function. When the function is run it randomly chooses and displays one of the 5 days of the week, Monday to Friday. (Saturday and Sunday are not included). You can assume import random has been executed before these functions are executed. Which function is NOT suitable/correct? def ChooseDay(): days=["Monday","Tuesday","Wednesday", "Thursday","Friday"] number=nt(0, 4) print(days[number]) def ChooseDay(): days=["Monday","Tuesday","Wednesday", "Thursday","Friday"] print(e(days)) def ChooseDay(): days=["Monday","Tuesday","Wednesday", "Thursday","Friday"] numbers=[0,1,2,3,4] thisnumber=e(numbers) print(days[thisnumber]) def ChooseDay(): day = nt(1, 5) if day == 1: print("Monday") elif day == 2: print("Tuesday") elif day == 3: print("Wednesday") elif day == 4: print("Thursday") elif day == 5: print("Friday") def ChooseDay(): if nt(1, 5)==1: print("Monday") elif nt(1, 5)==2: print("Tuesday") elif nt(1, 5)==3: print("Wednesday") elif nt(1, 5)==4: print("Thursday") elif nt(1, 5)==5: print("Friday") Part A Question 5 - Answer A student is practicing Python programming by writing a function. When the function is run it randomly chooses and displays one of the 5 days of the week, Monday to Friday. (Saturday and Sunday are not included). You can assume import random has been executed before these functions are executed. Which function is NOT suitable/correct? This function is not correct def ChooseDay(): correct def ChooseDay(): correct def ChooseDay(): days=["Monday","Tuesday","Wednesday", "Thursday","Friday"] number=nt(0, 4) print(days[number]) def ChooseDay(): correct days=["Monday","Tuesday","Wednesday", "Thursday","Friday"] print(e(days)) day = nt(1, 5) if day == 1: print("Monday") elif day == 2: print("Tuesday") elif day == 3: print("Wednesday") elif day == 4: print("Thursday") if nt(1, 5)==1: print("Monday") elif nt(1, 5)==2: print("Tuesday") elif nt(1, 5)==3: print("Wednesday") elif nt(1, 5)==4: print("Thursday") elif nt(1, 5)==5: def ChooseDay(): correct elif day == 5: print("Friday") days=["Monday","Tuesday","Wednesday", "Thursday","Friday"] numbers=[0,1,2,3,4] thisnumber=e(numbers) print(days[thisnumber]) print("Friday") Sometimes this function will print nothing! Part A Question 5 - Incorrect Function def ChooseDay(): if nt(1, 5)==1: print("Monday") elif nt(1, 5)==2: print("Tuesday") elif nt(1, 5)==3: print("Wednesday") elif nt(1, 5)==4: print("Thursday") elif nt(1, 5)==5: print("Friday") The random number is 3 The random number is 5 The random number is 1 The random number is 2 The random number is 4 3 is not equal to 1 5 is not equal to 2 1 is not equal to 3 2 is not equal to 4 4 is not equal to 5 Nothing is printed Nothing is printed Nothing is printed Nothing is printed Nothing is printed So in this situation, nothing is printed Part A Question 6 After the program has finished, what is the letter that you see in the turtle window? You only need to write one capital letter for your answer. import turtle (90) for d in range(2): for i in range(2700): rd(0.1) if d: (0.1) else: (0.1) () Part A Question 6 – Speeding Up The Program After the program has finished, what is the letter that you see in the turtle window? You only need to write one capital letter for your answer. import turtle (90) for d in range(2): for i in range(2700): rd(0.1) if d: This program takes 5 minutes to run! How to speed it up? Possibility 1: add (0) (0.1) else: (0.1) () After doing that, it still takes 4 minutes to run! Part A Question 6 – Speeding Up The Program After the program has finished, what is the letter that you see in the turtle window? You only need to write one capital letter for your answer. import turtle (90) for d in range(2): for i in range(2700): rd(0.1) if d: This program takes 5 minutes to run! How to speed it up? Possibility 2: add r(False) and (0.1) else: (0.1) () r(True) Then the result is immediately displayed! Part A Question 6 – Speeding Up The Program After the program has finished, what is the letter that you see in the turtle window? You only need to write one capital letter for your answer. import turtle (90) for d in range(2): 270 This program takes 5 minutes to run! How to speed it up? for i in range(2700): rd(0.1) Possibility 3: reduce the number of loops by 10 if d: 1 (0.1) and increase the else: 1 distance travelled by 10 (0.1) After doing that, you can see () 1 the result in half a minute! Part A Question 6 - Answer After the program has finished, what is the letter that you see in the turtle window? You only need to write one capital letter for your answer. import turtle (90) for d in range(2): for i in range(2700): rd(0.1) if d: (0.1) else: (0.1) () The result looks like the letter S Part A Question 7 A Python program has been written with an infinite loop (a loop that never ends). If you run the program for a long time, what is the shape that you will see in the turtle window? Write one single English word to describe the shape. import turtle import random (1) while True: x = nt(0, 500) y = nt(0, 500) while x + y > 500: x = nt(0, 500) y = nt(0, 500) (x - 250, y - 250) () Part A Question 7 - Answer A Python program has been written with an infinite loop (a loop that never ends). If you run the program for a long time, what is the shape that you will see in the turtle window? Write one single English word to describe the shape. import turtle import random (1) while True: x = nt(0, 500) y = nt(0, 500) while x + y > 500: x = nt(0, 500) y = nt(0, 500) (x - 250, y - 250)Lots of lines will be drawn, after a while you will see a triangle shape Part A Question 7 - Discussion A Python program has been written with an infinite loop (a loop that never ends). If you run the program for a long time, what is the shape that you will see in the turtle window? Write one single English word to describe the shape. import turtle import random (1) while True: How to answer this question? If you run the code it is slow, but not terribly slow - if you wait 1.5 minutes x = nt(0, 500) y = nt(0, 500) while x + y > 500: x = nt(0, 500) y = nt(0, 500) (x - 250, y - 250) () you see this: So you could just wait a couple of minutes Part A Question 7 - Discussion A Python program has been written with an infinite loop (a loop that never ends). If you run the program for a long time, what is the shape that you will see in the turtle window? Write one single English word to describe the shape. import turtle import random (1) while True: To speed things up, you could change (1) to (0) Then after x = nt(0, 500) y = nt(0, 500) while x + y > 500: x = nt(0, 500) y = nt(0, 500) (x - 250, y - 250) () a few seconds you can see the shape: Part A Question 7 - Discussion A Python program has been written with an infinite loop (a loop that never ends). If you run the program for a long time, what is the shape that you will see in the turtle window? Write one single English word to describe the shape. import turtle import random (1) while True: Another way to get the answer is to look at the code This code means x + y is fixed to a maximum x = nt(0, 500) y = nt(0, 500) while x + y > 500: x = nt(0, 500) y = nt(0, 500) (x - 250, y - 250) () Consequences: If y is big, then x cannot be big If x is big, then y cannot be big COMP1021 Introduction to Computer Science Fall 2020 Midterm Exam Part B Questions Part B Question 1 import turtle rd(1) rd(1) rd(1) rd(1) rd(1) step=6 while step!=11: print("in outer loop") rd(5) rd(5) change=1 while change>3: print("in inner loop") rd(10) change=change+10 step=step+1 What is the x position of the turtle after the program has finished? Part B Question 1 - Answer import turtle rd(1) rd(1) What is the x position The final x rd(1) rd(1) rd(1) step=6 while step!=11: Move forward 5 of the turtle after the program has finished? position is: 5 + (10 * 5) = 55 print("in outer loop") rd(5) rd(5) change=1 while change>3: print("in inner loop") rd(10) change=change+10 step=step+1 The outer loop runs 5 times Each time the turtle moves forward 10 The inner loop runs 0 times, so you can ignore it Part B Question 2 In the appropriate places, select the three missing pieces of code. for week in rangbela(n1k,1 17) : print("Week", week, end=" ") events="" if : events=events + "Exam week (half) " else: events=events + "Exam week " events=events + "- Final Exam date unknown " else: if week!=8: events=events + "Lecture 1 " if week!=4: events=events + "Lecture 2 " if week==6: events=events + "Midterm Practice Exam " if week==7: Here is the result of the program: Week 1 Lecture 1 Lecture 2 Week 2 Lecture 1 Lecture 2 Week 3 Lecture 1 Lecture 2 Week 4 Lecture 1 Week 5 Lecture 1 Lecture 2 Week 6 Lecture 1 Lecture 2 Midterm Practice Exam Week 7 Lecture 1 Lecture 2 Actual Midterm Exam Week 8 Lecture 2 Week 9 Lecture 1 Lecture 2 Week 10 Lecture 1 Lecture 2 Week 11 Lecture 1 Lecture 2 Week 12 Lecture 1 Lecture 2 events=events + "Actual Midterm Exam " print(events) Week 13 Lecture 1 Lecture 2 Week 14 Exam week (half) - Final Exam date unknown Week 15 Exam week - Final Exam date unknown Week 16 Exam week (half) - Final Exam date unknown Part B Question 2 - Answer In the appropriate places, select the three missing pieces of code. for week in range(1, 17) : print("Week", week, end=" ") events="" if week>=14 : if week%2==0 : events=events + "Exam week (half) " else: events=events + "Exam week " events=events + "- Final Exam date unknown " else: if week!=8: events=events + "Lecture 1 " if week!=4: events=events + "Lecture 2 " if week==6: events=events + "Midterm Practice Exam " if week==7: events=events + "Actual Midterm Exam " print(events) Here is the result of the program: Week 1 Lecture 1 Lecture 2 Week 2 Lecture 1 Lecture 2 Week 3 Lecture 1 Lecture 2 Week 4 Lecture 1 Week 5 Lecture 1 Lecture 2 Week 6 Lecture 1 Lecture 2 Midterm Practice Exam Week 7 Lecture 1 Lecture 2 Actual Midterm Exam Week 8 Lecture 2 Week 9 Lecture 1 Lecture 2 Week 10 Lecture 1 Lecture 2 Week 11 Lecture 1 Lecture 2 Week 12 Lecture 1 Lecture 2 Week 13 Lecture 1 Lecture 2 Week 14 Exam week (half) - Final Exam date unknown Week 15 Exam week - Final Exam date unknown Week 16 Exam week (half) - Final Exam date unknown Part B Question 3 Here is a Python program. import turtle import random (0) urtle() The person who made the program wants the program to draw this: colours = [ "red", "orange", "yellow", "green", "blue", "violet" ] num_colours = len(colours) for i in range(num_colours, 0, -1): rd(i*30) (90) olor(colours[num_colours-i]) _fill() e(i*30, 180) _fill() (90) rd(i*30) () Unfortunately, the program shown above does not draw the image shown above. One line of code needs to be edited Which line of code needs to be edited? Part B Question 3 - Answer Here is a Python program. import turtle We need to add "black" to the end import random (0) urtle() of the list in line 5 The person who made the program wants the program to draw this: colours = [ "red", "orange", "yellow", "green", "blue", "violet" ] num_colours = len(colours) for i in range(num_colours, 0, -1): rd(i*30) (90) You can see there is a black semi-circle olor(colours[num_colours-i]) _fill() e(i*30, 180) _fill() (90) rd(i*30) () Unfortunately, the program shown above does not draw the image shown above. One line of code needs to be edited Which line of code needs to be edited? Part B Question 4 When the following program is run, what is printed? # x = "i attended python classes in school!" part1 = x[int(len(x)/3)-1:int(len(x)/2)] part2 = x[0] + x[-7:-9:-1] part3 = x[-6] + x[-4:-1] result = part1+part2+part3 print(result) Part B Question 4 - Answer When the following program is run, what is printed? # x = "i attended python classes in school!" part1 = x[int(len(x)/3)-1:int(len(x)/2)] a space is here "python " part2 = x[0] + x[-7:-9:-1] part3 = x[-6] + x[-4:-1] result = part1+part2+part3 "i" + "s " a space is here "c" + "ool" print(result) The answer is: python is cool Part B Question 5 Two students form a team to answer a set of questions in a competition. They get points by answering questions. No error occurs when the program is run. According to the last message shown by the program, what are the total points they get in the competition? Enter one integer. Part B Question 5 What are the total points they get in the competition? number_of_questions = 30 total_points = 0 next_student_to_answer = "student1” for question in range(number_of_questions): if question >= 15: break elif question % 2: continue elif question == 10: print("Both students answer question", question, "together") total_points += 50 elif next_student_to_answer == "student1": print(next_student_to_answer, "answers question", question) total_points = total_points + 10 next_student_to_answer = "student2" elif next_student_to_answer == "student2": print(next_student_to_answer, "answers question", question) total_points = total_points + 20 next_student_to_answer = "student1" print("The total points they get in the competition is", total_points) Part B Question 5 - Discussion What are the total points they get in the competition? number_of_questions = 30 total_points = 0 next_student_to_answer = "stud What about this code? Remember you can always use rs for question in range(number_o the shell to quickly try things out: if question >= 15: break elif question % 2: continue ": rs elif question == 10: You can see that print("Both students a total_points += 50 question, "toget is the same as total_points = total_points + 50 Part B Question 5 - Discussion Student 1 answered number_of_questions = 30 total_points = 0 question 0, 4, 8, 14; 10 * 4 = 40 points elif next_student_to_answer == "student1": print(next_student_to_answer, "answers next_student_to_answer = "student1” for question in range(number_of_questions): question", question) total_points = total_points + 10 if question >= 15: break elif question % 2: continue elif question == 10: question >= 15 will be skipped odd questions will be skipped next_student_to_answer = "student2" elif next_student_to_answer == "student2": print(next_student_to_answer, "answers question", question) total_points = total_points + 20 print("Both students answer question", question, "together") next_student_to_answer = "student1" print("The total points they get in the total_points += 50 Question 10: 50 points competition is", total_points) Student 2 answered question 2, 6, 12; 20 * 3 = 60 points Part B Question 5 - Answer The answer is 150 Part B Question 6 A program has been written to count some fruits that you receive Give me apple, coconut, cherry, grape, lychee, orange, peach or pear. Enter none when you finish. What is the fruit? grape What is the fruit? peach from a farm. Here is an example of using the program. The user input is shown in bold. What is the fruit? apple What is the fruit? orange What is the fruit? peach What is the fruit? orange What is the fruit? peach What is the fruit? peach What is the fruit? pear What is the fruit? coconut What is the fruit? none Thank you! Here is what I have got: apple 1 coconut 1 cherry 0 grape 1 lychee 0 orange 2 peach 4 pear 1 Part B Question 6 myfruit_names = ["apple", "coconut", "cherry", "grape", "lychee", "orange", "peach", "pear"] myfruit_numbers = [] for _ in myfruit_names: myfruit_d(0) print("Give me apple, coconut, cherry, grape,", end=" ") print("lychee, orange, peach or pear.") print("Enter none when you finish.") received_fruits = [] fruit = "" while fruit != "none": fruit = input("What is the fruit? ") received_d(fruit) for i in range(len(myfruit_names)): = received_( ) print("Thank you!") print() print("Here is what I have got:") for i in range(len(myfruit_names)): print(myfruit_names[i], myfruit_numbers[i]) Part B Question 6 - Answer myfruit_names = ["apple", "coconut", "cherry", "grape", "lychee", "orange", "peach", "pear"] myfruit_numbers = [] for _ in myfruit_names: myfruit_d(0) print("Give me apple, coconut, cherry, grape,", end=" ") print("lychee, orange, peach or pear.") print("Enter none when you finish.") received_fruits = [] fruit = "" while fruit != "none": fruit = input("What is the fruit? ") received_d(fruit) for i in range(len(myfruit_names)): myfruit_numbers[i] = received_( myfruit_names[i] ) print("Thank you!") print() print("Here is what I have got:") for i in range(len(myfruit_names)): print(myfruit_names[i], myfruit_numbers[i])

Show more Read less
Institution
COMP 102
Course
COMP 102











Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
COMP 102
Course
COMP 102

Document information

Uploaded on
December 5, 2022
Number of pages
40
Written in
2022/2023
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
SmartMind Chamberlain College Of Nursing
View profile
Follow You need to be logged in order to follow users or courses
Sold
110
Member since
4 year
Number of followers
112
Documents
1691
Last sold
8 months ago

3.5

22 reviews

5
8
4
6
3
2
2
2
1
4

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions