CIS Exam 4 Coding

24 July 2022
4.7 (114 reviews)
64 test answers

Unlock all answers in this set

Unlock answers (60)
question
Use the variables k and total to write a while loop that computes the sum of the squares of the first 50 counting numbers, and associates that value with total. Thus your code should associate 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 with total. Use no variables other than k and total.
answer
total = 0 k = 50 while k > 0: total += k * k k -= 1
question
Given that n refers to a positive int use a while loop to compute the sum of the cubes of the first n counting numbers, and associate this value with total. Use no variables other than n, k, and total.
answer
total = 0 k = 0 while k <= n: total += k * k * k k += 1
question
In this exercise, use the following variables: i,lo, hi, and result. Assume that lo and hi each are associated with an int and that result refers to 0. Write a while loop that adds the integers from lo up through hi (inclusive), and associates the sum with result. Your code should not change the values associated with lo and hi. Also, just use these variables: i,lo, hi, and result.
answer
i = lo while i <= hi: result += i i += 1
question
Assume there is a variable, h already associated with a positive integer value. Write the code necessary to compute the sum of the perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the sum you compute with the variable q. For example, if h is 19, you would assign 30 to q because the perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16 and 30==1+4+9+16.
answer
q = 0 i = 1 while (i * i < h) : q += i * i i += 1
question
Assume there is a variable, h already associated with a positive integer value. Write the code necessary to count the number of perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Assign the sum you compute to a variable q For example, if h is 19, you would assign 4 to q because there are perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16.
answer
q = 0 i = 1 while i * i < h : q += 1 i += 1
question
Assume there are two variables, k and m, each already associated with a positive integer value and further assume that k's value is smaller than m's. Write the code necessary to compute the number of perfect squares between k and m. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the number you compute with the variable q. For example, if k and m had the values 10 and 40 respectively, you would assign 3 to q because between 10 and 40 there are these perfect squares: 16, 25, and 36,.
answer
i = 1 q = 0 while i * i < k : i += 1 while i * i <= m : q += 1 i += 1
question
Use two variables k and total to write a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus, your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
answer
total = 0 for k in range(51): total += k*k
question
Given a variable n refers to a positive int value, use two additional variables, k and total to write a for loop to compute the sum of the cubes of the first n counting numbers, and store this value in total. Thus your code should put 1*1*1 + 2*2*2 + 3*3*3 +... + n*n*n into total. Use no variables other than n, k, and total.
answer
total = 0 for k in range(n + 1): total += k * k * k
question
Associate the average of the numbers from 1 to n (where n is a positive integer value) with the variable avg.
answer
avg = (1 + n) / 2
question
Assume there is a variable, h already associated with a positive integer value. Write the code necessary to compute the sum of the first h perfect squares, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the sum you compute with the variable q. For example, if h is 4, you would assign 30 to q because the first 4 perfect squares (starting with 1) are: 1, 4, 9, 16 and 30==1+4+9+16.
answer
q = 0 for i in range(1, h + 1) : q += i * i
question
Associate True with the variable is_ascending if the list numbers is in ascending order (that is, if each element of the list is greater than or equal to the previous element in the list). Otherwise, associate False with is_ascending
answer
for i in range(2, len(numbers)) : if numbers[i] < numbers[i-1] : is_ascending = False break else : is_ascending = True
question
Given a positive integer n, assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)
answer
for i in range(2, n) : if n % i == 0 : is_prime = False break else : is_prime = True
question
Given the positive integer distance and the positive integer n, associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance. For example, if distance is 2 and n is 10, then sum would be associated with 25 because 1+3+5+7+9 = 25.
answer
sum = 0 for i in range(1, n+1, distance) : sum += i
question
Write a statement that defines plist to be the empty list.
answer
plist = []
question
Write a statement that defines plist as the list containing exactly these elements (in order): "spam", "eggs", "vikings" .
answer
plist = ["spam", "eggs", "vikings"]
question
Write statement that defines plist to be a list of the following ten elements: 10, 20, 30, ..., 100 in that order.
answer
plist = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
question
Create a list named tax_rates, consisting of the following five elements: 0.10, 0.15, 0.21, 0.28, 0.31, in that order.
answer
tax_rates = [0.10, 0.15, 0.21, 0.28, 0.31]
question
Write a statement that defines the variable denominations, and associates it with a list consisting of the following six elements: 1, 5, 10, 25, 50, 100, in that order.
answer
denominations = [1, 5, 10, 25, 50, 100]
question
Associate True with the variable has_dups if the list list1 has any duplicate elements (that is if any element appears more than once), and False otherwise.
answer
for i in range(len(list1)- 1): if list1[i] in list1[i+1:] : has_dups = True break else : has_dups = False has_dups = len(list1) != len(set(e for e in list1))
question
Given that plist has been defined to be a list of 30 elements, add 5 to its last element.
answer
plist[-1] += 5
question
Assume that a list of integers named salary_steps that contains exactly five elements has been defined. Write a statement that changes the value of the last element in the list to 160000.
answer
salary_steps[-1] = 160000
question
Given a variable plist, that refers to a non-empty list, write an expression that refers to the first element of the list.
answer
plist[0]
question
Given a variable plist, that refers to a list with 34 elements, write an expression that refers to the last element of the list.
answer
plist[-1] plist[33]
question
Assume that the variable plist has been defined and refers to a list. Write a statement that assigns the next to last element of the list to x.
answer
x = plist[-2]
question
Given that a variable named plist has been defined and refers to a non-empty list, write a statement that associates its first element with 3.
answer
plist[0] = 3
question
Assume that salary_steps refers to a non-empty list, write a statement that assigns the value 30000 to the first element of this list.
answer
salary_steps[0] = 30000
question
Assume that plist has been defined and is associated with a non-empty list. Write a statement that increases the first element of this list by 10.
answer
plist[0] += 10
question
Assume that a variable named plist refers to a list with 12 elements, each of which is an int. Assume that the variable k refers to a value between 0 and 6. Write a statement that assigns 15 to the list element whose index is k.
answer
plist[k] = 15
question
Given that plist refers to a non-empty list ,write a statement that assigns the int -1 to the last element of the list.
answer
plist[-1] = -1
question
Assume that plist is associated with a list that has 12 elements. Assume further that k refers to an int between 2 and 8. Assign 9 to the element just after the element in plist whose index is k .
answer
plist[k+1] = 9
question
Assume that a variable named plist has been defined and is associated with a list that consists of 12 elements. Assume further that k refers to an int between 2 and 8. Assign 22 to the element just before the element in plist whose index is k .
answer
plist[k-1] = 22
question
Assume that plist refers to a list containing exactly five elements. Assume, in addition, that j refers to an int with a value that is between 0 and 3. Write a statement that associates a new value with the element of the list indexed by j. This new value should be equal to twice the value of the next element of the list (i.e. the element after the element indexed by j.
answer
plist[j] = plist[j+1] * 2
question
Assume that play_list refers to a non-empty list, and that all its elements refer to values of type int. Write a statement that associates a new value with the first element of the list. The new value should be equal to twice the value of the last element of the list.
answer
play_list[0] = play_list[-1] * 2
question
Given that plist1 and plist2 both refer to lists, write an expression that evaluates to a list that is the concatenation of plist1 and plist2. Do not modify plist1 or plist2.
answer
plist1 + plist2
question
Given that plist1 and plist2 both refer to lists, write a statement that defines plist3 as a new list that is the concatenation of plist1 and plist2. Do not modify plist1 or plist2.
answer
plist3 = plist1 + plist2
question
Given that play_list has been defined to be a list, write an expression that evaluates to a new list containing the elements at index 0 through index 4 play_list. Do not modify play_list.
answer
play_list[0:5]
question
Given that k and j each refer to a non-negative int and that play_list has been defined to be a list with at least j+1 elements, write an expression that evaluates to a new list containing all the elements from the one at index k through the one at index j of list play_list . Do not modify play_list .
answer
play_list[ k : j + 1]
question
Given that k and j each refer to a non-negative int and that play_list has been defined to be a list with at least j elements, write an expression that evaluates to a new list containing all the elements from the one at index k through the one at index j-1 of list play_list . Do not modify play_list .
answer
play_list[ k : j ]
question
Given that play_list has been defined to be a list, write a statement that makes the first 3 elements of play_list be "spam", "eggs" and "vikings" (in that order).
answer
play_list[0:3] = ["spam", "eggs", "vikings"]
question
Given that L1 and L2 both refer to lists, write a statement that replaces the elements in L1 from index 5 through (and including) index 8 with all the elements of L2.
answer
L1[5:9] = L2
question
Given that worst_offenders has been defined as a list with at least 6 elements, write a statement that defines lesser_offenders to be a new list that contains all the elements from index 5 of worst_offenders and beyond. Do not modify worst_offenders.
answer
lesser_offenders = worst_offenders[ 5 : ]
question
Given that plist has been defined to be a list, write an expression that evaluates to True if 3 is an element of plist.
answer
3 in plist
question
Given that k refers to an int and that play_list has been defined to be a list, write a expression that evaluates to True if the value associated with k is an element of play_list.
answer
k in play_list
question
Given: -a variable current_members that refers to a list, and -a variable member_id that has been defined. Write some code that assigns True to a variable is_a_member if the value associated with member_id can be found in the list associated with current_members, but that otherwise assigns False to is_a_member. Use only current_members, member_id, and is_a_member.
answer
is_a_member = member_id in current_members
question
You are given a variable zipcode_list that refers to a list. Write some code that assigns True to a variable duplicates if there are two adjacent elements in the list that have the same value, but that otherwise assigns False to duplicates otherwise. In order to accomplish this, you may, if you wish, use one other variable, k. Use only k, zipcode_list, and duplicates.
answer
duplicates = False k = 0 while k < len(zipcode_list)-1 and not duplicates: duplicates = zipcode_list[k] == zipcode_list[k+1] k += 1
question
You are given a variable named zipcode_list that has been defined and refers to a list of postal codes. Write some code that assigns True to duplicates if any two elements in the list have the same value, but that otherwise assigns False to duplicates. You may, if you wish, use two other variables, j and k. Use only j, k, zipcode_list, and duplicates.
answer
duplicates = False j = 0 while j < len(zipcode_list)-1 and not duplicates: k = j + 1 while k < len(zipcode_list) and not duplicates: if zipcode_list[k] == zipcode_list[j]: duplicates = True k += 1 j += 1
question
Given a variable temps that refers to a list, all of whose elements refer to values of type float, representing temperature data, compute the average temperature and assign it to a variable named avg_temp. Besides temps and avg_temp, you may use two other variables -- k and total.
answer
k = 0 total = 0 for i in range(0, len(temps)): total = total + temps[i] k = k + 1 avg_temp = total / k
question
Given: -a variable named incompletes that refers to a list of student ids, and -a variable student_id Write some code that counts the number of times the value associated with student_id appears in the list associated with incompletes and assigns this value to number_of_incompletes. You may use, if you wish, an additional variable, k. You may use only k, incompletes, student_id, and number_of_incompletes.
answer
number_of_incompletes = 0 for k in incompletes: if student_id == k: number_of_incompletes += 1
question
A list named parking_tickets has been defined to be the number of parking tickets given out by the city police each day since the beginning of the current year. (Thus, the first element of the list contains the number of tickets given on January 1; the last element contains the number of tickets given today.) Write some code that associates most_tickets with the largest value found in parking_tickets. You may, if you wish, use one additional variable, k.
answer
most_tickets = parking_tickets[0] for k in parking_tickets: if k > most_tickets: most_tickets = k
question
Associate the sum of the non-negative values in the list numbers with the variable sum.
answer
sum = 0 for i in numbers : if i >= 0 : sum += i
question
We informally define the term corresponding element as follows: -The first element and the last element of a list are corresponding elements. -Similarly, the second element and the element just before the last element are corresponding elements. -The third element and the element just before the element just before the last element are corresponding elements -- and so on. Given that the variable a is associated with a list, write an expression for the corresponding element of a[i].
answer
a[len(a) - i - 1]
question
Given that a refers to a list, write the necessary code to reverse the elements of the list.
answer
a.reverse()
question
Given a list named play_list, write an expression whose value is the length of play_list.
answer
len(play_list)
question
Given a list named alist, write an expression that removes the last element of alist.
answer
alist.pop()
question
Given a variable alist that refers to a list, remove the list's last element and associates its value with a variable k.
answer
k = alist.pop()
question
Given that play_list has been defined to be a list, write a statement that sorts the list.
answer
play_list.sort()
question
Reverse the list associated with the variable words.
answer
words.reverse()
question
Sort the list, lst (use the list sort method).
answer
lst.sort()
question
Given the lists, lst1 and lst2, create a new sorted list consisting of all the elements of lst1 that also appears in lst2. For example, if lst1 is [4, 3, 2, 6, 2] and lst2 is [1, 2, 4], then the new list would be [2, 2, 4]. Note that duplicate elements in lst1 that appear in lst2 are also duplicated in the new list. Associate the new list with the variable new_list, and don't forget to sort the new list.
answer
new_list = [] for i in lst1 : if i in lst2 : new_list.append(i) new_list.sort()
question
Given a variable named plist that refers to a list, write a statement that adds another element, 5 to the end of the list.
answer
plist.append(5)
question
Given that k refers to a non-negative int and that alist has been defined to be a list with at least k+1 elements, write a statement that removes the element at index k.
answer
del alist[k]
question
Given that alist has been defined to be a non-empty list (that is with at least one element), write a statement that removes its first element.
answer
del alist[0]
question
Given that alist has been defined to be a list with at least 4 elements, write a statement that removes its 4th element.
answer
del alist[3]
question
Given the lists list1 and list2 that are of the same length, create a new list consisting of the last element of list1 followed by the last element of list2, followed by the second to last element of list1, followed by the second to last element of list2, and so on (in other words the new list should consist of alternating elements of the reverse of list1 and list2). For example, if list1 contained [1, 2, 3] and list2 contained [4, 5, 6], then the new list should contain [3, 6, 2, 5, 1, 4]. Associate the new list with the variable list3.
answer
list3 = [] for i in range(len(list1)-1, -1, -1) : list3.append(list1[i]) list3.append(list2[i])