CSCI 256 - Final Exam (Sample Multiple Choice Questions)

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

Consider the following class: class ClassAct: def __init__(self, number): self.__item = number (a) What is the name of the class? What is the data attribute in the class? (b) Write a statement that creates an object from the class and passes the value 25 as an argument to the initializer method. (c)How to write mutator and accessor methods for the data attribute.

(a) Name of class is : ClassAct. and data attribute item (b) creating an object : object = ClassAct(25) (c) creating a mutator : def set_number(self, number): self.__item = number creating an accessor : def get_number(self): return self.__item

Consider the program below: a = [] result = 0 for i in range(10): a.append(i+1) result += a[i] print ('Result is: %d' % result) The output of this program will be: a. Result is: 55 b. Result is: 75 c. Result is: 65 d. Result is: 67

a. (55)

When displaying output within the quotes, how do you insert a new line? a. \n b. /n c. %n d. ''n

a. (\n)

825 in hexadecimal is what binary number? a. 0000 1101 0111 b. 1000 0010 0101 c. 0010 0001 0101 d. 0101 0001 0010

b. (1000 0010 0101)

Which of the following is NOT a valid variable name? a. firstHalf b. 1stHalf c. half_one d. _half1

b. (1stHalf)

What is the output of the following code? How do you rewrite print statement using the format function or % specifier? a = 10 b = 3 print ('a/b is', (a // b)) a. a/b is 3.3333 b. a/b is 3 c. 3 is 3 d. 3.3333 is 3.3333

b. (a/b is 3)

Analyze the following Python code: x = 8 y = 4 z = 2 answer = x + y / z ** z What is the answer? a. 12.0 b. 9.0 c. 3.0 d. 9

b. 9.0

Consider the following code that assigns a letter grade to a student's score: if score >= 90: grade = 'A' if score >= 80: grade = 'B' if score >= 70: grade = 'C' if score >= 60: grade = 'D' else: grade = 'F' a. This code will work correctly in all cases b. This code will work correctly only if grade >= 60 c. This code will work correctly only if grade < 60 d. This code never works correctly

c. (This code will work correctly only if grade < 60)

Analyze the following Python code: str = 'hello world' ch = str[len(str)-2] What is ch? a. e b. d c. l d. r

c. (l)

What is the output of the following fragment? for i in range(0, 12): if (i % 4) == 0: print ('%d' % i) a. 0 2 4 6 8 10 12 b. 0 4 8 12 c. 4 8 12 d. 0 4 8

d. (0 4 8)

What is the output of the following code: a = 6.5 a += a + 1 print (a) a. 7.5 b. 6.5 c. 14 d. 14.0

d. (14.0)

What is the output from the code segment? Which of the following for-loops produces the equivalent result to this code segment? total = 0 for i in range(2, 20, 2): t.otal += i print(total) a. for i in range(20, 1, -1) b. for i in range(19, 1, -1) c. for i in range(20, 2, -2) d. for i in range(18, 0, -2)

d. (for i in range(18, 0, -2)

Consider the following code: s = [] for i in range(1, 10, 2): s.append(i) The value of s [s[3] - s[1]] is: a. 3 b. 5 c. 7 d. 9 e. IndexError

e. (IndexError)

Analyze the following, is there anything wrong with the code? How to fix it and then what is the output? outputFile = open('Numbers.txt', 'w') for i in range(1, 11): outputFile.write(i + '\n') outputFile.close() inputFile = open('Numbers.txt', 'r') s = 0 c = 0 for num in inputFile: s += num c += 1 print(s/c) inputFile.close()

error: unsupported operand type because we are here trying to print integer and string together. So in order to fix it: - we have to convert the integer value to string and then print it : by using str(i) - secondly, we need to convert back the string value to int while we are adding it to variable 's' - by using int(num) Fixed code: outputFile = open('Numbers.txt', 'w') for i in range(1,11): # fixed below line outputFile.write(str(i)+'\n') outputFile.close() inputFile = open('Numbers.txt', 'r') s = 0 c = 0 for num in inputFile: # fixed below line s+=int(num) c+=1 print(s/c) inputFile.close()

Rewrite the following code fragment using a for loop instead of a while loop. i = 0 while i <= 50: print('%d ' % i) i += 2

for i in range(0, 51, 2): print('%d' %i)

Analyze the following code, what is the output from the program? import math def main(): rad1 = 3 rad2 = 1 area1 = math.pi * rad1 ** 2 area2 = math.pi * rad2 ** 2 twoCircles(rad1, rad2) print('main function: The area of circle 1 with radius %d is %.2f' % (rad1, area1)) print('main function: The area of circle 2 with radius %d is %.2f' % (rad2, area2)) # user-defined function def twoCircles(rad2, rad1): r1 = rad1 + 1 r2 = rad2 + 1 res1 = math.pi * r1 ** 2 res2 = math.pi * r2 ** 2 print('function: The area of circle 1 with radius %d is %.2f' % (r1, res1)) print('function: The area of circle 2 with radius %d is %.2f' % (r2, res2)) # call the main function main() What rad1 is output in the main function? What rad2 is output in the main function? What r1 is output in the twoCircles function? What r2 is output in the twoCircles function?

with rad1, the output of the main function is : 28.77 with rad2, the output of the main function is : 3.14 with r1, the output is : 50.27 with r2, the output is : 12.57


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

Structure organisationelle Structure/classification/ fonctions

View Set

3421 Adults II - Emergency, Disaster, & Infection

View Set

Module 3 Quiz- Public Speaking I

View Set

Chapter 26: The Second World War

View Set