cpsc111B-Asg1-Fall2023-Chiamaka Amanda Alumonah_10134234

docx

School

Alexander College *

*We aren’t endorsed by this school

Course

111B

Subject

Computer Science

Date

Jan 9, 2024

Type

docx

Pages

7

Uploaded by JudgeField13026

Report
CPSC111B - Introduction to Computation Fall 2023 Assignment #1 – Python Practice: working with functions & numbers Name and student #: __Chiamaka Amanda Alumonah_10134234 Name and student #: _nil_____________________________ ASSIGNMENT MARK: 41 / 50 Due Date: Wednesday, Oct. 4 th (11:59PM) Submission : Submit your assignment to the instructor through Canvas INBOX as one zipped file. For this Assignment, you will pair up with another student to complete it. As always, all your answers should use functions and every function should have a documentation string explaining the purpose of the function. Use the Python template given below at the top of your program you submit (don’t forget to modify it to include your information; your name, your student number, and purpose.) # Program: cpsc111B-F23-asg1.py # Author: Enter your Name and student number here # Class: CPSC 111 # Section: B # Date: 09/27/2023 # Task: Assignment #1 # Purpose: Describe here what assignment 1 is all about # which means what are you trying to solve in this # assignment. # Do not wait until the last minute to do this assignment in case you run into problems For programming questions, you may assume the user’s inputs are well- formatted. The questions are roughly ordered from the easiest to the hardest. This assignment has: 10 multiple choice questions, 2 marks each, 20 in total 2 essay questions, 3 marks each, 6 in total 3 programming questions, 8 marks each, 24 in total am/cpsc111B-Asg1-F23.docx Assigned: Wednesday Sep 27, 2023 Page 1/6 A. Multiple Choice Questions -0
1. What is the output of the following code? def paramTest(a, b): c = a + ' ' + b return c p0 = 'Pokemon' p1 = 'keldeo' c = paramTest(p0, p1) print(c) (a) ab (b) a b (c) Pokemonkeldeo (d) Pokemon keldeo 2. What is the output of the following python code? n,i,s = 6,1,0 while i < n: if i % 2 != 0: s += i i = i + 1 print(s) (a) 6 (b) 9 (c) -9 (d) Error 3. What is the output of the following python code? x = [5, 4, 3, 2, 1] for i in range(len(x) - 1): if x[i] > x[i+1]: x[i],x[i+1] = x[i+1],x[i] print(x) (a) [5, 3, 4, 1, 2] (b) [4, 3, 2, 1, 5] (c) [1, 2, 3, 4, 5] (d) Error 4. What is the output of the following python code? def Fun(a, b, c): if a % b == 0 and a % c == 0: return a elif a % b == 0: return b elif a % c == 0: return c else: return 0 print(Fun(12, 4, 13)) am/cpsc111B-Asg1-F23.docx Assigned: Wednesday Sep 27, 2023 Page 2/6 (a) 4 (b) 12 (c) 13 (d) Error
5. What is the output of the following code? number = 711 def modify(number): number = 7 return (number+4) print('number is', number) (a) number is 7 (b) number is 715 (c) number is 711 (d) None of the above. 6. What is the output of the following code? m = 100 while True: if m < 20: break m -= 19 print(m) (a) 19 (b) 5 (c) 1 (d) None of the above. 7. How many times is the print(“F”) statement executed in the following code? for m in "xyyzttttx": if m in "ztx": continue print("F") (a) 2 (b) 6 (c) 3 (d) None of the above. 8. What is the output of the following code? def checkMe(): x = "Cheers" print(x) print(x, "My Dear") print(x) checkMe() (a) Cheers (b) Cheers (c) Cheers My Dear (d) Error Cheers My Dear Cheers Cheers Cheers Cheers My Dear Cheers am/cpsc111B-Asg1-F23.docx Assigned: Wednesday Sep 27, 2023 Page 3/6 9. What is the output of print('ABcdBGCDGHcdH'.split('cd') )? (a) ['AB', 'BG',’GH’, 'H'] (b) ['AB', 'BGCDGH', 'H']
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
(c) ['ab', 'bgcdgh', 'h'] (d) 'AB' 'BG' ’GH’ 'H' 10.What would be the value of the variable list2 after the execution of the following code? list1 = [7, 8, 9] list2 = list1 list1 = [1, 2, 3] (a) [7, 8, 9, 1, 2, 3] (b) [1, 2, 3] (c) [7, 8, 9] (d) Invalid code B. Essay Questions a) What does this Python program do and what’s the output? def checkMe(arg1, arg2): What does the program do? if arg1<arg2: print(arg1) print(arg2) else: print(arg2) print(arg1) #main/start of program Output: checkMe(7,11) checkMe(11,7) Output: 7 11 7 11
What does the program do? The function checks which of the arguments is smaller, and prints the smaller one before the greater one. am/cpsc111B-Asg1-F23.docx Assigned: Wednesday Sep 27, 2023 Page 4/6 b) Consider the simple function given below. -2 def guess(n): print(3*n) When we compare guess(4) with 12 the Python interpreter would return False after printing 12 as shown below. Explain why it gives False as a result of the comparison. >>> print(guess(4) == 12) 12 False Explain here: The reason why the result is false is because there is nothing being returned from the guess function, hence, nothing to be compared with the integer 12. C. Programming Questions 1. Write a Python function getInfo() that prompts for and reads a person’s name , age , college , and pet’s name . Then print the following paragraph, inserting the appropriate data: -4 Hello, my name is name and I am age years old. I’m enjoying my time at college , though I miss my pet petname very much! def getInfo(): name = input('Please enter your name ') age = input('Please enter your age ') college = input('Please enter your college ')
petname = input('Please enter your pet\'s name ') print('Hello, my name is ', name, 'and i am ', age, 'years old. I\'m enjoying my time at', college, ', though I miss my pet ', petname, 'very much!') getInfo() 2. Convert the following algorithm to a Python function called weatherType() -3 IF (temperature > 90) Write "Hot weather" ELSE IF (temperature > 70) Write "Ideal weather" ELSE IF (temperature > 50) Write "A little chilly" ELSE IF (temperature > 32) Write "Cold weather" ELSE Write "Stay inside" def weatherType(temperature): if temperature > 90: print('Hot weather') elif temperature > 70: print('Ideal weather') elif temperature > 50: print('A little chilly') elif temperature > 32: print('Cold weather') else: print('Stay inside') weatherType( 100) #need general solution am/cpsc111B-Asg1-F23.docx Assigned: Wednesday Sep 27, 2023 Page 5/6 3. Write a Python function that will generate a left triangle shape with a given size; provided by the user. An example of a left triangle shape with a size of 5 is given below.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
N.B : Next, you are going to make sure you can use the submission properly. This will be how you turn in your assignments electronically. The submission procedure only accepts zip files (NOT rar Files) , so you are going to create a “Asg1_YourFirstName_YourStudentID.zip” file containing all the programs you just wrote as well as this document with your name and student number added to it. To create a “.zip” file, find the directory where the files are saved and select all of them. If you right-click on one of the selected files, you should have the option to create an archive by choosing “Send to” then “Compressed “zipped” Folder”. Rename the created “.zip” file and give it the name Asg1_YourFirstName_YourStudentID.zip. 5 marks will be deducted if: 1. the email did not have a full name and a student # in the subject field or 2. the procedure for zipped file is not followed or 3. This document, with your name and student number on it, is not included with the zipped file A score of zero (0) will be given to every empty email. am/cpsc111B-Asg1-F23.docx Assigned: Wednesday Sep 27, 2023 Page 6/6