






























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!































































What's the output of the following code? m = [[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3]] def mystery(mat): prod = 1 for i in range(len(mat)): j = 0 sum = 0 while j < len(mat[i]): sum += mat[i][j] j += 1 prod *= sum return prod print(mystery(m)) -- ANSWER-- 750
Given m = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]] What's the value of len(m) -- ANSWER-- 3 Given m = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]] What's the value of len(m[1]) -- ANSWER-- 5 Which of the following is correct about the following statement? x = 1, 2, 3, 4 -- ANSWER--x is a tuple Given a tuple t = (3, 6, 2, 4), which of the following will result in errors? -- ANSWER--t[0] = 5 print(t[4]) Given a tuple t = (1, 2, 3, 4), which of the following is the result of t[2:-1]? -- ANSWER--(3) Given a tuple t = (1, 2, 3), which of the following is the result of t * 2? -- ANSWER--(1, 2, 3, 1, 2, 3)
A list may have duplicates. -- ANSWER--TRUE Given list1 = [1, 5, 9], what is sum(list1)? -- ANSWER-- 15 Which of the following statements create empty lists? -- ANSWER--li = [] li = list() Every element in a list must be of the same type? -- ANSWER--FALSE What is the output of the following code? list1 = [3, 5, 25, 1, 3]print(min(list1)) -- ANSWER-- 1 What's the output of the following code segment? aList = [14, 55, 4, 34, 2, - 3, 76] index = 0 for i in range(1, len(aList)): if ( aList[i] < aList[index] ): index = i print(index) -- ANSWER-- 5 What is the output when following code is executed? listA = [-8, 3, 45, 22, 12, 85] for i in range (1, 6): listA[i - 1] = listA[i] *2 for i in range(0, 6): print(listA[i], end = " ") -- ANSWER--6 90 44 24 170 85
What is the output when the following code is executed? myList = [-85, 34, 23, 90, 123]maximumValue = myList[0]indexOfMax = 0for i in range(1, len(myList)): if (myList[i] > maximumValue): maximumValue = myList[i] indexOfMax = i print(indexOfMax) -- ANSWER-- 4 Given list1 is [1, 3, 2], what is the result of list1 * 2? -- ANSWER--[1, 3, 2, 1, 3, 2] What is the output of the following code? list1 = [1, 2, 3, 4]list2 = [5, 6, 7, 8] print(len(list1 + list2)) -- ANSWER-- 8 What is printed by the following statements? a = [4, 2, 8, 6, 5]b = a * 2b[3] = 999print(a) -- ANSWER--[4, 2, 8, 6, 5] What's the output of the following code? a = [1, 2, 3, 4]b = [10, 20, 30, 40]c = a + bprint(c) -- ANSWER--[1, 2, 3, 4, 10, 20, 30, 40] What is printed by the following statements? a = [4, 2, 8, 6, 5] b = [a] * 2 c = a * 2 a[3] = 999
result = [] while (i < len (L)) : result.append(L[i]) i = i + 3 return result
x = example ( [2,3,4.5,6,7,8,9,10,11,12] ) print (x) -- ANSWER--Returns a list containing every third item from L starting at index 0. What is the output when the following function is called as below? def function1 (values): values[0] = 89
list1 = [11, 12, 30] function1(list1) print (list1) -- ANSWER--[89, 12, 30] What's the output from the following code segment: def mid(vals): vals[1] = 10 nums = [1, 2, 3] mid(nums) print(v) -- ANSWER--[1, 10, 3]
What will be the output of calling the function addItem from the main function? def addNewItem (list1): list1 += [1] def main(): anyList = [1, 2, 3, 4] addNewItem ( anyList ) print ( len ( anyList ) ) print ( anyList [4] ) main() -- ANSWER-- 5 1 What is the output of the following code? def changeValues (intValue, listValue): intValue = 1 listValue [0] = 44 value1 = 3 list1 = [89, 15, 13] changeValues (value1, list1) print (value1, list1) -- ANSWER--3 [44, 15, 13] Given m = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
return mid elif all[mid] > target: right = mid - 1 else: left = mid + 1 return - 1 def main(): all = [1, 2, 5, 8, 11, 15, 19] print(find(all, 6)) main() -- ANSWER--3 8 1 2 2 5
right = mid - 1 else: left = mid + 1 return - 1 def main(): all = [1, 2, 5, 8, 11, 15, 19] print(find(all, 5)) main() -- ANSWER--3 8 1 2 2 5 2 Given the following statement in a code from random import * which of the following is a correct statement to shuffle list list1? -- ANSWER--shuffle(list1) What is the output of the following code? aList = [1, 2, 3, 4] aList.reverse() print(aList) -- ANSWER--[4, 3, 2, 1] What is the output when the following code segment is executed? firstList = [10, 31, 85, 96] secondList = firstList
print(max(list1)) -- ANSWER--abc What's the output of the following code? sList = [1, 2, 3, 4, 5] print(sList[-2]) print(sList[-4:-1]) -- ANSWER-- 4 [2, 3, 4] Consider the following code segment aList = ['a', 'b', 'c', 'd'] aList[0] = 'z' print(aList == bList) which of the following statement may appear on line 2, such that the program displays False -- ANSWER--bList = aList.copy() bList = list(aList) Consider the following code listOne = ['a', 'b', 'c'] listTwo = ['d', 'e', 'f'] print(listThree) Choose all the correct statement that can be placed between line 2 and 5, such that the above program displays ['a', 'b', 'c', 'd', 'e', 'f'] -- ANSWER--listThree = listOne + listTwo listOne.extend(listTwo)listThree = listOne
What's the output of the following code? aList = [1, 2, 3, 4, 5, 6] for i in range(1, len(aList)): aList[i - 1] = aList[i] print(aList) -- ANSWER--[2, 3, 4, 5, 6, 6] What's the output of the following code? aList = [1, 2, 3, 4, 5, 6] for i in range(1, len(aList) - 1): aList[i] = aList[i - 1] print(aList) -- ANSWER--[1, 1, 1, 1, 1, 6] What's the output of the following code? aList = [1, 2, 3, 4] * 2print(aList) -- ANSWER--[1, 2, 3, 4, 1, 2, 3, 4] What's the output of the following code? def myadd(element, lis = []): lis.append(element) return lis myadd(1) lis = myadd(2) print(lis) -- ANSWER--[1, 2] What is the output of the following code?
What's the output of the following program? t1 = (1, 2) t2 = (3, 4) print(t1 + t2) -- ANSWER--(1, 2, 3, 4) Which of the following best describe the following code? ta = (1, 2, 3, 4) ta.append(5) print(ta) -- ANSWER--There is an error on line 2. Which of the following is not a valid escape sequence? -- ANSWER--% Which of the following represents a newline character? -- ANSWER--\n Which of the following statement produces the following output? You can't beat them all. -- ANSWER--print('You can't beat them all.') print("You can't beat them all.") What is the value of len('It's')? -- ANSWER-- 4 What is the output of the following code? str1 = "Python is fun" res = str1 [-3:-1]
print (res) -- ANSWER--fu What is the output of the following code? str1 = "Python is fun" res = str1[-1] print(res) -- ANSWER--n What's the value of res after the following code? str1 = "What is fun?" res = str1[1:1] -- ANSWER--"" Strings are immutable. This means that once defined, they cannot be modified. -- ANSWER-- true Given the following code: string1 = "hi" string2 = "hi" which of the following statements about string1 and string2 are true? -- ANSWER--The values of both strings are the same. The values of the id function of both strings are the same. How many lines of output does the following program produce? s = "Coding in Python is fun." for ch in s[3:8]:
What is the value stored in res when the following code is executed? x= "Welcome to Python" res = x.split() -- ANSWER--["Welcome", "to", "Python"] Suppose that the string x is as follows: x = "This is a test" what is the value of x.isupper()? -- ANSWER--FALSE What is the value of len("Python")? -- ANSWER-- 6 What is the value of "Python is fun"[:2]? -- ANSWER--Py To check whether or not string s1 contains string s2, use _________. -- ANSWER-- s1.find(s2) >= 0 s2 in s To retrieve the third character from string s, use _________. -- ANSWER--s[2] Suppose s is "\t\tPython Programming\n", what is the value of s.strip()? -- ANSWER--Python Programming Suppose s is "python", what is the value of s.upper()? -- ANSWER--PYTHON
Which operations may be performed using the % operator? Check all that apply. -- ANSWER--format modulus (remainder) Which of the following is used to format a floating point number? -- ANSWER--%g We may use _______ delimiters for string literals. -- ANSWER--double quote single quote Given a string variable s, which of the function returns the length of a string? -- ANSWER-- len(s) Which of the following is the result of the following split operation "Introduction to Python Programming".split() -- ANSWER--['Introduction', 'to', 'Python', 'Programming'] What is the output when following code is executed? studentsNames = ['Sam', 'Alex', 'Frank', 'David'] print(studentsNames[-1][-1]) -- ANSWER--d How many times is the word HELLO printed by the following statements? s = "Hello." for ch in s: print("HELLO") -- ANSWER-- 6