Computer Science Test # 2

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which of the following is the correct notation for a tuple of only one element?

(10,)

For the following function, def elapsed_time(start_hr, start_mins, end_hr, end_mins) (a) Give an appropriate docstring specification, where function elapsed_time returns the total number of minutes between the provided start and end times. (b) Give a print statement that displays the docstring for this function.

(A) ((end_hr) *60)+end_mins)-((start-hr)*60)+start-mins))) (B) print(elapsedTime_doc_)

Examine the following Python code: def compare(num1, num2): if num1 > num 2: return num1 else: return num2 # ---- main num1 = int(input('Enter a number')) num2 = int(input('Enter a number')) print(compare(num2, num1), 'is the bigger number')) The formal parameters in the function definition are ordered num1, num2, but in the function call, the actual arguments are ordered num2, num1. This is neither a syntactic nor a semantic error. Explain why.

num1 and num2 are both integer inputs, they are interchangeable and the function can still determine the bigger number

Suppose that variable k is assigned to a list object that contains a method called numZeros that returns the number of 0 values in the list. Give the proper syntax for calling method numZeros on variable k.

numZeros(k)

Give a Python Code to add up all the elements in a list of integers named nums and display the results

nums = [1, 2, 3] total = nums[0] + nums[1] + nums [2] total = 6

Give Python code that, for two lists of integers (of the same length) named nums1 and num2, creates a third list named nums3 containing the sum of the corresponding numbers in each list, as demonstrated below. num1 [1, 2, 3, 4] num2[10, 20, 30, 40] num3[11, 22, 33, 44]

nums3 = [num1[0] + num2[0], (num1[1]+num2[1], (num1[2]+num2[2], (num1[3]+num2[3])]

Examine the following Python code: def avg(n1, n2, n3): #Line 1 sum = n1 + n2 + n3 #Line 2 return sum / 3.0 #Line 3 In Line 1, what is (n1, n2, n3) referred to as?

parameter list

The _____________ is used to access a member of an object.

period

There are two values associated with an object, its ___________________ value and its ______________________ value.

referenced, dereferenced

The interface of a module is...?

specification of the module providing information to any client

Examine the following Python code: def sumPos(nums): for k in range(0, len(nums)): if nums[k] < 0: nums[k] = 0 return sum(nums) # ---- main nums_1 = [5, -2, 9, 4, -6, 1] total = sumPos(nums_1) print('total = ', total) print(nums_1) What will the last line print out?

[5, 0, 9, 4, 0, 1]

A docstring is...?

any Python string delimited by triple quotes providing the specification of a program element.

An object contains a set of __________________ and a set of ____________.

attributes, functions

For Fruit = 'banana' , what is displayed by the following python code? For k in range(0,len(Fruit),2): print(Fruit[k],end=' ')

bnn

The modules of the Python Standard Library are referred to as the Standard ____________ modules.

built-in

Build in function _______ can be used in Python to determine if two variables reference the same object in memory.

id()

Give Python code to determine if two lists, list1 and list2, reference two completely different list objects or not.

id(list1) == id(list2)

For some lst, what will be the result of the execution of the instruction lst.insert(1,3)?

insert the value 3 in the location of the 1st element

Examine the following Python code: def avg(n1, n2, n3): #Line 1 sum = n1 + n2 + n3 #Line 2 return sum / 3.0 #Line 3 Which line/lines of code make up the suite (body) of the function?

lines 2 and 3

After the following series of assignments are performed, list1 = [1, 2, 3, 4] list2 = list1 list1 = [5, 6, 7, 8]

list1 will reference the list [5, 6, 7, 8] and list2 will reference the list [1, 2, 3, 4]

Suppose that list1 is assigned to a list of tuples. Given the proper code so that a complete copy of list1 is assigned to variable list2.

list2 = list(list1)

Suppose that list1 is assigned to a list of integers. Given the proper code so that a complete copy of list1 is assigned to variable list2.

list2 = list1

Give a Python code that inserts the value 35 in lst = [10, 20, 25, 40] so that the values remain in numerical order.

lst = [10, 20, 25, 40] lst.insert(3,35)

Give a Python code that replaces the value 25 in the list lst = [10, 20, 25, 40] with the value 40.

lst = [10, 20, 25, 40] lst[2] = 40

For nested list lst = [ [10, 20, 30] , [40, 50, 60] ] , which of the following is the correct means of assessing the element with the value 40?

lst[1][0]

Which of the following is the correct way to access the fourth element of a list named lst?

lst[3]

Top-down design is an approach for developing a _______________ design.

modular

A ___________________ consists of the design and/or implementation of specific functionality.

module

Examine the following function header: def mortgagePayment(amount, rate, term): Which of the following calls to function mortgagePayment is invalid in Python?

mortgagePayment(rate=5.5, term=30, 350,000)

For variable n with the value 10..?

n provides the value 10 and id(n) provides the location of the value 10 in memory

A _________________ is when two otherwise distinct entities with the same name become part of the same namespace.

name clash

The main module in Python is given the special name ______________.

top-level

For lst = [10, 20, 30, 40, 50], what is the value of the lst after lst.insert(3,0) is performed?

[10, 20, 30, 0, 40, 50]

Which of the following is NOT a valid list in Python?

[10; 20; 30]

Examine the following code: def myMoney(cashOnHand=0, debt=0): #Line 1 return cashOnHand - debt #Line 2 # ---- main print(myMoney()) #Line 3 print(myMoney(50)) #Line 4 print(myMoney(120, 50)) #Line 5 print(myMoney(debt=250, cashOnHand=25)) #Line 6 What will the output of this code be?

0 50 70 -225

Examine the following Python code: list1 = [1, 2, 3, 4, 5] list2 = nList list1.append(6) list2.append(7) for i in list1: print(i, end = '') print() for i in list2: print(i, end = '') What will this code output? Explain.

1234567 1234567 list1 = list2

Examine the following Python code: def avg(n1, n2, n3): #Line 1 sum = n1 + n2 + n3 #Line 2 return sum / 3.0 #Line 3 When calling this function, how many arguments would be passed to it?

3

Examine the following Python Code: numList = [1, 4, 6, 8, 10, 3, 12] For k in range(0,len(numList)): print(numList[k]) How many times does the above loop iterate?

7

What should the value K be for lst[K] to access the tenth element of the list?

9

Which of the following IS NOT a characteristic of lists in Python?

Nonlinear

Any program code that makes use of a given module is called a ____________ of the module.

client

In Python, user-defined functions begin with a function header that starts with the keyword?

def

Write the function header only of a function named largest that takes 3 integer parameters and returns the largest of the 3

def largest(num_list):

Using function largest in the previous problem, write a python code that gives an example of its use.

def largest(num_list): return (max(num_list)) num_list = [10 ,20, 30] biggest = largest(num_list)

Which of these is not a type of namespace in Python?

foreign namespace

To import a module name into the namespace of another module, the ____________________ form of import is used.

from-import

Examine the following Python code: def displayFunction(): print('This program will convert between Fahrenheit and Celsius') print('Enter (C) to convert to Celsius from Fahrenheit') print('Enter (F) to convert to Fahrenheit to Celsius') Write a line of Python code that properly calls function displayFunction.

displayFunction()

Write a function named displayGrades that is passed a list of grades in the range 0-100, and displays the grades from highest to lowest, each grade on a separate line. Note that the original list argument passed should be not be altered. You may use the built-in sort method in the function.

displayGrades = [33, 88, 66, 77] view = reversed(sorted(displayGrades)) for displayGrades in view: print(displayGrades)


संबंधित स्टडी सेट्स

P300 - Operations Management Exam 2

View Set

Hist 102 - Midterm 1 Ch. 15 & 16

View Set

231 Lec 18: Multiple Inheritance, Interfaces

View Set

Garret Hardin: Tragedy of the Commons Assignment

View Set

3.06 Unit Test: Critical Skills Practice 1

View Set