EECS 138-Python PRACTICE QUESTIONS Michael S. Branicky, 2020 Problem PQ1. Evaluating Python Expressions. Evaluate each of...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
EECS 138-Python PRACTICE QUESTIONS Michael S. Branicky, 2020 Problem PQ1. Evaluating Python Expressions. Evaluate each of the following Python expres- sions, entering their values in the boxes provided (2 points each). You may use the area outside the boxes for calculations. Example: 3 = 2 Throughout, assume the following variable declarations/statements: filename= "Midterm.2.PDF" s="Python is fun!" prime [2, 3, 5, 7, 11, 13, 17, 19 ] mat= [[1, 2, 3], [4, 5, 6], [7, 8, 9] ] dotloc filename.index(".") (a) len(prime) (b) prime [5] (c) mat [1] [1] (d) mat [2] [1] (e) len(filename) (f) filename [3] (g) dotloc (h) filename [:dotloc] (i) filename.lower ().endswith(".pdf") False (j) "138"+s [5:] 2 EECS 138-Python Michael S. Branicky, 2020 Problem PQ2. Writing Python Statements. Produce the requested Python statements. Example: Write a one-line Python statement declaring a string storing a campaign slogan. slogan "Guido in 2024! " (a) [3 points] Write a one-line Python statement creating and initializing a list of exam grades containing the numbers 94, 77, 83, and 91. PRACTICE QUESTIONS 3 (b) [3 points] Use list comprehension to write a one-line Python statement creating and initializing to all zeros a 1-d array (list) to store daily temperatures for one (non-leap) year. (c) [3 points] Write a one-line Python statement creating a 2-d array roll (list of lists) that will be used in a game program to store the results of rolling two dice on each turn and initializing it to store these three rolls: 1, 2 then 3, 4 then 5, 6. (d) [3 points] Write one or more Python statements to print out all the values of the temper- ature list defined in part (b), assuming that it has been populated with non-zero data since its initialization. (e) [4 points] Write one or more Python statements to grab the "extension" (i.e., all char- acters after the last .') from str variable filename and store the result in the str named extension. Assume filename contains a '.' and both strings have been created and assigned. (f) [4 points] Write one or more Python statements to go through a slogan (stored in slogan, like the one in the Example above) and print "digit" for each character between 0 and 9. EECS 138-Python PRACTICE QUESTIONS (a) [5 points] Problem PQ3. Code Analysis. Fill in the values for the lists/variables indicated after the corresponding code has run. You must enter your final answers into the boxes provided below to receive credit. Feel free to use the area outside the boxes for preliminary calculations. Be careful! SIZE = 5 sums [0 for i in range (SIZE)] for i in range (SIZE): sums [i]=i*(1+1)/2 (b) [5 points] arr [7, 12, 24, 31, 49] 10 = 0 hi len (arr) - 1 while lo < hi: temp = arr [10] arr [lo]= arr [hi] arr [hi] = temp lo + 1 hi -= 1 (c) [10 points] The following code might appear in a game program that uses a grid world. for i in range (XMAX): for j in range (YMAX): Michael S. Branicky, 2020 sums idist = abs (i-goali) jdist = abs(j-goalj) world [i] [j] = idist jdist if world [i][j] > m: m = world [i][j] arr XMAX = 3 YMAX = 3, 3 world = [[ 0 for j in range (YMAX)] for i in range (XMAX)] goali = 1 goalj = 2 m = -1 world 4 m: EECS 138-Python PRACTICE QUESTIONS Problem PQ4. Modifying and Filling In Python Code. (a) [6 points] Here are the formulas for computing the mean, u, and standard deviation, o, of a set of n values: l = =1 Fi n x + x + + In n def mean (x): xsum = 0 for i in range (len(x)): 0 = xsum + x[i] return xsum/len(x) Michael S. Branicky, 2020 |=1(*; - )2 n =1 n 4 The following Python functions returns the mean of the values of a passed list, x, using the formula for the mean, u, given above. 5 Write another Python function (which could call the function above) to return the standard deviation of the values of a passed list, x. Also (for purposes of assigning partial credit), circle the formula for the standard deviation, o, that you used above. Problem PQ4 continued (b) [6 points] The text file autompg.dat contains 398 lines of automobile mileage data of the following, tab-separated data (where the character 'l' is used only to denote the tab locations): car name model year | number of cylinders | miles per gallon For example, the first two lines of the file might look like: Prius Escalade 2005 4 55.0 2005 8 16.5 Add Python expressions/statements to complete the program below, which should read the data from the file and display it to the screen. Assume car name contains no spaces. #Open the file input file.open( = # name is of type str # year and cyls are of type int #mpg is of type float print("Name \t Year \t # Cyls. \t MPG") # Get input from file for ) # Close the file input.close() : # within for loop body, display that line of data print (f"{name} \t {year} \t {cyls} \t {mpg}") # end for loop body EECS 138-Python PRACTICE QUESTIONS Problem PQ4 continued (c) [8 points] Assume that values is a 101-element list containing measurements from a scientific experiment, as declared in the Python code below. Add Python statements that would complete the program below to count the number of positive values, negative values, and zero values in the list, and (complete the code to) write out a message summarizing how many values of each type were found. values = [0 for i in range (101)] # LIST DECLARATION # OTHER CODE TO INITIALIZE values WOULD BE HERE (DO NOT WRITE) for i in Michael S. Branicky, 2020 # DECLARE/INITIALIZE YOUR OTHER VARIABLES HERE # end for loop body : # Print statements: COMPLETE AS NECESSARY print (f"# of positive entries: print (f"# of negative entries: print (f"# of zero entries: 7 ") ") ") EECS 138-Python PRACTICE QUESTIONS Problem PQ5. Writing Object-Oriented Python Code. (a) [15 points] Complete the code below for an Element class. It will have attributes for symbol, atomic number, and atomic weight. For example, helium has symbol He, atomic number 2, and atomic weight 4.003. Write a 3-parameter constructor __init__ that can be used to set all three attributes, with default attribute values of "", 0, and 0.0, respectively. Write getter and setter instance methods for the atomic number only. class Element: Michael S. Branicky, 2020 # test the Element class. 8 (b) [5 points] Add Python statements to create an application program below that (i) creates and initializes the Element object helium using your constructor and all three of its actual attribute values (listed above); (ii) creates an list of objects that could be used for all 118 elements of the periodic table, but with only the atomic number initialized to a non-default value. EECS 138-Python PRACTICE QUESTIONS Michael S. Branicky, 2020 Problem PQ1. Evaluating Python Expressions. Evaluate each of the following Python expres- sions, entering their values in the boxes provided (2 points each). You may use the area outside the boxes for calculations. Example: 3 = 2 Throughout, assume the following variable declarations/statements: filename= "Midterm.2.PDF" s="Python is fun!" prime [2, 3, 5, 7, 11, 13, 17, 19 ] mat= [[1, 2, 3], [4, 5, 6], [7, 8, 9] ] dotloc filename.index(".") (a) len(prime) (b) prime [5] (c) mat [1] [1] (d) mat [2] [1] (e) len(filename) (f) filename [3] (g) dotloc (h) filename [:dotloc] (i) filename.lower ().endswith(".pdf") False (j) "138"+s [5:] 2 EECS 138-Python Michael S. Branicky, 2020 Problem PQ2. Writing Python Statements. Produce the requested Python statements. Example: Write a one-line Python statement declaring a string storing a campaign slogan. slogan "Guido in 2024! " (a) [3 points] Write a one-line Python statement creating and initializing a list of exam grades containing the numbers 94, 77, 83, and 91. PRACTICE QUESTIONS 3 (b) [3 points] Use list comprehension to write a one-line Python statement creating and initializing to all zeros a 1-d array (list) to store daily temperatures for one (non-leap) year. (c) [3 points] Write a one-line Python statement creating a 2-d array roll (list of lists) that will be used in a game program to store the results of rolling two dice on each turn and initializing it to store these three rolls: 1, 2 then 3, 4 then 5, 6. (d) [3 points] Write one or more Python statements to print out all the values of the temper- ature list defined in part (b), assuming that it has been populated with non-zero data since its initialization. (e) [4 points] Write one or more Python statements to grab the "extension" (i.e., all char- acters after the last .') from str variable filename and store the result in the str named extension. Assume filename contains a '.' and both strings have been created and assigned. (f) [4 points] Write one or more Python statements to go through a slogan (stored in slogan, like the one in the Example above) and print "digit" for each character between 0 and 9. EECS 138-Python PRACTICE QUESTIONS (a) [5 points] Problem PQ3. Code Analysis. Fill in the values for the lists/variables indicated after the corresponding code has run. You must enter your final answers into the boxes provided below to receive credit. Feel free to use the area outside the boxes for preliminary calculations. Be careful! SIZE = 5 sums [0 for i in range (SIZE)] for i in range (SIZE): sums [i]=i*(1+1)/2 (b) [5 points] arr [7, 12, 24, 31, 49] 10 = 0 hi len (arr) - 1 while lo < hi: temp = arr [10] arr [lo]= arr [hi] arr [hi] = temp lo + 1 hi -= 1 (c) [10 points] The following code might appear in a game program that uses a grid world. for i in range (XMAX): for j in range (YMAX): Michael S. Branicky, 2020 sums idist = abs (i-goali) jdist = abs(j-goalj) world [i] [j] = idist jdist if world [i][j] > m: m = world [i][j] arr XMAX = 3 YMAX = 3, 3 world = [[ 0 for j in range (YMAX)] for i in range (XMAX)] goali = 1 goalj = 2 m = -1 world 4 m: EECS 138-Python PRACTICE QUESTIONS Problem PQ4. Modifying and Filling In Python Code. (a) [6 points] Here are the formulas for computing the mean, u, and standard deviation, o, of a set of n values: l = =1 Fi n x + x + + In n def mean (x): xsum = 0 for i in range (len(x)): 0 = xsum + x[i] return xsum/len(x) Michael S. Branicky, 2020 |=1(*; - )2 n =1 n 4 The following Python functions returns the mean of the values of a passed list, x, using the formula for the mean, u, given above. 5 Write another Python function (which could call the function above) to return the standard deviation of the values of a passed list, x. Also (for purposes of assigning partial credit), circle the formula for the standard deviation, o, that you used above. Problem PQ4 continued (b) [6 points] The text file autompg.dat contains 398 lines of automobile mileage data of the following, tab-separated data (where the character 'l' is used only to denote the tab locations): car name model year | number of cylinders | miles per gallon For example, the first two lines of the file might look like: Prius Escalade 2005 4 55.0 2005 8 16.5 Add Python expressions/statements to complete the program below, which should read the data from the file and display it to the screen. Assume car name contains no spaces. #Open the file input file.open( = # name is of type str # year and cyls are of type int #mpg is of type float print("Name \t Year \t # Cyls. \t MPG") # Get input from file for ) # Close the file input.close() : # within for loop body, display that line of data print (f"{name} \t {year} \t {cyls} \t {mpg}") # end for loop body EECS 138-Python PRACTICE QUESTIONS Problem PQ4 continued (c) [8 points] Assume that values is a 101-element list containing measurements from a scientific experiment, as declared in the Python code below. Add Python statements that would complete the program below to count the number of positive values, negative values, and zero values in the list, and (complete the code to) write out a message summarizing how many values of each type were found. values = [0 for i in range (101)] # LIST DECLARATION # OTHER CODE TO INITIALIZE values WOULD BE HERE (DO NOT WRITE) for i in Michael S. Branicky, 2020 # DECLARE/INITIALIZE YOUR OTHER VARIABLES HERE # end for loop body : # Print statements: COMPLETE AS NECESSARY print (f"# of positive entries: print (f"# of negative entries: print (f"# of zero entries: 7 ") ") ") EECS 138-Python PRACTICE QUESTIONS Problem PQ5. Writing Object-Oriented Python Code. (a) [15 points] Complete the code below for an Element class. It will have attributes for symbol, atomic number, and atomic weight. For example, helium has symbol He, atomic number 2, and atomic weight 4.003. Write a 3-parameter constructor __init__ that can be used to set all three attributes, with default attribute values of "", 0, and 0.0, respectively. Write getter and setter instance methods for the atomic number only. class Element: Michael S. Branicky, 2020 # test the Element class. 8 (b) [5 points] Add Python statements to create an application program below that (i) creates and initializes the Element object helium using your constructor and all three of its actual attribute values (listed above); (ii) creates an list of objects that could be used for all 118 elements of the periodic table, but with only the atomic number initialized to a non-default value.
Expert Answer:
Answer rating: 100% (QA)
Copy code PQ 3 PQ 1 a len prime is 8 b prime5 refers to the 6th element of the prime list which is 13 c mat1 1 is 5 This refers to the second element of the second list within mat d mat 2 1 gives an e... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
Can you provide an example of when you led a previous organization through a major operational change? Did you encounter any obstacles and how did you motivate and guide your team through these...
-
The following selected information is for Xtreme Corporation: Instructions (a) Calculate the gross profit margin, profit margin, asset turnover, return on assets, and return on equity ratios for 2014...
-
Solve Problem 4.4.4 using the Hazen-Williams equation instead of the Darcy-Weisbach for friction losses. Let CHw = 120 for the concrete pipes. Problem 4.4.4 An industrial water-distribution system is...
-
What is the importance of vibration measurement?
-
Fill in the missing amounts in each of the eight case situations below. Each case is independent of the others. a. Assume that only one product is being sold in each of the four following case...
-
Land improvements As of December 31 2022 : Beginning balance - P5,000,000 Estimated useful life - 10 years Depreciation method - 150% declining balance method Accumulated Depreciation - P2,781,473 a....
-
Given the active filter shown below, answer the following questions if the capa replaced by an inductor L: a) Find the transfer function of this filter: V. (s) Vi(s) H(s) = b) What is the type of the...
-
In the track meet, I competed in the high jump and the pole vault. In Exercises 2532, determine whether the data described are nominal or ordinal.
-
How do the various types of countertrade arrangements differ from one another?
-
All the customers who entered a store on a particular day were given a survey to fill out concerning their opinions of the service at the store. In Exercises 2536, identify the kind of sample that is...
-
I finished first in the high jump and third in the pole vault. In Exercises 2532, determine whether the data described are nominal or ordinal.
-
How do a trade acceptance and a bankers acceptance differ?
-
Calculate the volume of concentrated HCl with 36% purity and density of 1.19 g / mL needed to prepare 100mL of 0.1 M solution.
-
An investor sells a European call on a share for $4. The stock price is $47 and the strike price is $50. Under what circumstances does the investor make a profit? Under what circumstances will the...
-
A life insurance company quoted an annual premium of $387.50 (payable at the beginning of the year) for a $250,000 term insurance policy on a 35-year-old male nonsmoker. Alternatively, the insured...
-
A $100,000 mortgage loan at 7.2% compounded semiannually requires monthly payments based on a 25-year amortization. Assuming that the interest rate does not change for the entire 25 years, complete...
-
What is meant by the effective rate of interest?
-
Trust Management System: A university would like to develop a system for students to evaluate their peers performance in some group projects. Students ratings in various categories will affect a...
-
Building trust in potential students to join music classes by a music teacher a. Explain the above scenario. b. Draw a class diagram for it. c. Document a detailed and significant use case. d. Create...
-
Building trust in parents by hostel authorities to state that hostel is safe for students to live a. Explain the above scenario. b. Draw a class diagram for it. c. Document a detailed and significant...
Study smarter with the SolutionInn App