exam 2 draft 3_PRACTICE

pdf

School

Texas A&M University *

*We aren’t endorsed by this school

Course

102

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

12

Uploaded by GrandTitaniumMink11

Report
-1- 1. (20 points). The function 𝑃(𝑥) = 1 √2 𝜋𝑠 2 exp (− (𝑥−𝑢) 2 2𝑠 2 ) is very important in probability and statistics. Write a user-defined function that takes as input parameters 𝑥 , 𝑢 , and 𝑠 , returns the value of the function evaluated at these values. You can use the numpy function numpy.exp() to calculate the exponential. Then, write the programming commands which call this function and plot it for 𝑥 between -3 and 3 with 𝑢 = 0 and 𝑠 = 1 (for all values of x). Be sure to label your axes and show the plot.
-2- 2. (4 points) Other than answering that the Top-Down process starts at the top and the Bottom- Up process starts at the bottom, describe the differences in the approach to programming, using the Top-Down Design Process and the Bottom-Up Design process. List 2 advantages of each design process.
-3- 3. (12 points) Assume a function called isprime is available for you to use in a module called ENGR102 which determines if a number is a prime number or not. The input parameter for isprime is a single integer and the output is either True or False. Write the Python program that would input two integers and then test only the odd numbers between and including these two numbers, to see if they are prime numbers or not using the isprime function. The output of your program should be a list of prime numbers found. If no prime numbers were found, then output should state that no prime numbers were found. Start your code with: from ENGR102 import isprime .
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
-4- 4. (12 points) Write a Python program that can be used to find the x,y coordinates of the local maxima and the minima points between x = -3 and x=5 for the polynomial y = x 3 -3 x 2 5.5x + 25. The output should be the coordinates for both the maxima and the minima.
-5- 5. (12 points ) A text file named ‘data.dat’ is stored on a computer's hard drive. In a text editor the file displays: # Created on November 15, 2018 # Time, Temperature, Windspeed # (min), (deg F), (knots) 0 , 33.47 , 1.27 5 , 32.59 , 1.95 10 , 33.62 , 0.76 15 , 33.79 , 1.12 Write a program that reads the contents of this file, assigns the header lines to a variable that is a list of strings, and assigns the data to a variable that is a floating point np.array that contains all of the data in the file. (The data should be in a 4 x 3 array. ) Write this code using standard file I/O commands; do not use any csv reader that you may know of in some Python package.
-6- 6. (12 points) Write a function that will take as input a set of numbers, and which returns the mean and variance of the set of numbers. The input can be a list, tuple or numpy.array. For a set of numbers 𝑥 1 , 𝑥 2 , … , 𝑥 𝑛 , the mean is defined as 𝑚 = 1 𝑛 𝑥 𝑖 𝑛 𝑖=1 and the variance is defined as 𝑆 2 = 1 𝑛 (𝑥 𝑖 − 𝑚) 2 𝑛 𝑖=1 . DO NOT use the numpy functions numpy.mean and numpy.var ; you must use a loop in your function.
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
-7- 7. Pick the right choice for the following 14 questions. Each is worth 2 points. a) The output of the following python program is: i = 1 while True: if i % 3 == 0: break print(i) i+=1 i. 1 2 ii. 1 2 3 iii. error b) What does the following python code output? def myfunc( x ): a = 1 print (x) a = 5 myfunc(a) print (a) I. Error II. 5 1 III. 5 5 IV. 1 5 V. 1 1
-8- c) The output of the following program is: name = "I enjoy programing in Python" print(name[4:9]) i. njoy ii. joy pr iii. joy p iv. njoy p d) What is the output of the following program? x = "abcdef" i = "i" while i in x: print(i, end=" ") i. no output ii. i i i i i i … iii. a b c d e f iv. abcdef e) What is the output of the following python program? x = 'abcd' for i in range(x): print(i) i. a b c d ii. 0 1 2 3 iii. error iv. none of the answers given
-9- f) What is the output of the following python program? matrix = [[1, 2, 3, 4], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] for i in range(0, 4): print(matrix[i][1], end=" ") i. 1 2 3 4 ii. 4 5 6 7 iii. 1 3 8 12 iv. 2 5 9 13 g) What is the output of the following python program? data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] print(data[1][0][0]) i. 1 ii. 2 iii. 4 iv. 5 h) Which of the following statements create a dictionary? i. Dictionary = {} ii. Dictionary = {“john”:40, “peter”:45} iii. Dictionary = {40:”john”, 45:”peter”} iv. All of the mentioned i) What will be the output of the following program? d = {"john":40, "peter":45} print(list(d.keys())) i. [“john”, “peter”] ii. [“john”:40, “peter”:45] iii. (“john”, “peter”) iv. (“john”:40, “peter”:45)
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
-10- j) What is printed by the Python code? def f1(): print ( 'Hi' ) def f2(): print ( 'Lo' ) f2() f1() f1() I. Hi Lo Hi II. Hi Hi Lo III. Lo Hi Hi IV. Hi Lo Hi k) What is printed by the Python code? def func(): print ( 'Yes' ) print ( 'No' ) func() I. No Yes II. Yes No III. No IV. Yes
-11- l) What is printed by the Python code? def func(x): return x - 1 print (func( 3 ) * func( 5 )) I. 15 II. 10 III. 6 IV. 8 m) What is printed by the Python code? def func(x): print ( 2 *x) func( 5 ) func( 4 ) I)10 8 II)5 4 III) 10 10 IV)10 4
-12- n) Imagine you want to compute c=a/b, but if b is 0, you instead want to compute c=a. Which of the following methods would accomplish this? OPTION A: try : c=a/b except : c=a OPTION B: if b== 0 : c=a else : c=a/b OPTION C: if b: c=a/b else : c=a OPTION D: c=a/ max (b, 1 ) I. Options A and B II. Options B and C III. Options A, B, and C IV. Options A, C, and D V. All of them accomplish the task
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