0% found this document useful (0 votes)
2 views3 pages

Final Sample Web

The document is an exam paper for CMPE-CMSE 107, containing multiple-choice and programming questions related to Python. It includes tasks such as identifying hardware and software, writing Python code for file handling, and manipulating lists and tuples. The exam tests knowledge on various programming concepts and requires students to complete code snippets and solve problems.

Uploaded by

basharqadah2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Final Sample Web

The document is an exam paper for CMPE-CMSE 107, containing multiple-choice and programming questions related to Python. It includes tasks such as identifying hardware and software, writing Python code for file handling, and manipulating lists and tuples. The exam tests knowledge on various programming concepts and requires students to complete code snippets and solve problems.

Uploaded by

basharqadah2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CMPE-CMSE 107 Final Exam Page 1

A. Multiple Choice Questions Q6- (5p) Mark each item by


H if it is hardware, and, S if it is software:
Q1- (4p) List elements can be Monitor [ ]; WhatsApp [ ]; Keyboard [ ];
FlashDisk [ ]; IDLE-shell [ ]; MS-Edge [ ];
a. lists and tuples.
b. integers and floating point numbers. HSH / HSS
c. strings.
Q7- (10p) The file “data.txt” contains lines with
d. all choices are true. D
exactly one decimal digit as a single element in
each line. Write a Python program to find both the
count and the sum of these numbers.
Q2-(4p) The built in range(I,J,K) function is used ___________________________________
a. to create a list of integers from I to J, but not
including J, with increments K. ___________________________________
b. to create a list of floating-point numbers from I to J
with increments K. ___________________________________
Answer:
c. to create a list of integers from I to K, but not 1. open file data.txt to read
___________________________________
including K, with increments J.
2. set count=0, sum=0
d. to create a list of integers from I to J with ___________________________________
3. for each line of the file:
increments K.
A 3.1 increment count
___________________________________
3.2 add the integer
value of line on sum
___________________________________
4. Display count and sum
Q3- (4p) If you write ListB=ListA, ___________________________________
a. ListB.append(100) does NOT mean that
___________________________________
ListA.append(100)
b. ListB[0]=5 also means ListA[0]=5 ___________________________________
c. ListA.insert(0,100) is the same as
ListB.insert(100,0)
Q8- (10p) Write a Phyton code that asks user to enter
d. ListA and ListB can be modified independently 20 integers from keyboard, finds and displays how
B many of them are greater than zero by using for
and if statements.
GTZ = 0 #greater than zero variable
Q4- (4p) Consider the list
X=[1,2,3,["AB","BC","CD"],4,5], then ___________________________________

a. the expression (["AB"] in X) is evaluated to be


___________________________________
True.
b. the string "AB" can be accessed as X[4][1] Answers:
___________________________________
c. the string "AB" can be accessed as X[3][0] GTZ=0
for i in range(20):
___________________________________
d. the expression ("AB" in X) is evaluated to be True
n=int(input("number?: "))
C if n>0:
___________________________________
count +=1
print( count)
___________________________________

Q5- (4p) Consider the Tuple Y=(1,2,3,4,[5,6,7],8)


a. Y[4].append(10) is a valid operation. Q9-(5p) Create the list evens that contains even
numbers in between 23 and 99 by using the range
b. Y[1]=Y[2] is a valid operation
function.
c. Y[4]=10 is a possible assignment.
d. Y.append(10) is a valid operation.
A Answer:
evens=list(range(23,99))
CMPE-CMSE 107 Final Exam Page 2

Q10-(9p) Text file "data.txt" contains words and Q12- ( 5p) Given W= 120.1684, write a formatted
integer numbers. print statement to print W within ten spaces and
a) Complete the program to build and display the list right adjusted form as "W=120.17".
of all alphabetic words, The symbol  denotes the space character .
b) Complete the last print line to get the list of words
without repetition by converting it to a set and then
to a list.
Answers:
1 rfile = open("data.txt") print("Weight =%10.2f %s"% (W, B))
3 wordlist=[]
4 for s in rfile:
5 for splt in s.split():
6 if splt.isalpha():
Q13- (9p) The following program is prepared to read
7 ______________________________ two 4-digit integer numbers from keyboard, N1
8 print( "list of words:", _____________ ) and N2, and check if the sum of digits of N1 is
9 print( "words nonrepeating:", _________) equal to sum of digits of N2. For example: N1 =
4109, N2 = 6107. Sum of digits of N1=14, sum of
digits of N2 = 14.
7 Answer:
___________________________________ Some program lines are left empty, fill in them
7 wordlist.append(splt) with appropriate Python statements and write your
8 8 wordlist
___________________________________ answers within the given box.
9 list(set(wordlist))
9 ___________________________________

___________________________________
Q11- ( 15p)
a) Write a function "listsqr" that squares all integer
___________________________________
elements of a list (specified as argument). You
may use method "is_integer()" to test an
___________________________________
element is an integer or not.
___________________________________

___________________________________
___________________________________

___________________________________
___________________________________
Answer:
___________________________________
___________________________________
1 def listsqr( L):
2 for k in L:
___________________________________
3 if k.is_integer():
4 k=k*k
___________________________________
5 5 return L
___________________________________
Answer:
Sum1=Sum1+int(i)
b) Call your new function to create the sqrL list that
contains the squares of the integers in the list Sum2=Sum2+int(j)
L=[1,3,8,21,55].
Sum1 == Sum2

Answer:
sqrL=listsqr(L)
CMPE-CMSE 107 Final Exam Page 3

Q14- (5p c1a) Q15- (12p) In the code given below, the numbers in
The following Python code fragment finds the number the text file "fruits.txt" shall be found and written
of each word in a given string. But there is a to the text file "numbers.txt". Complete the missing
semantic error in the code. When the code runs, it parts accordingly.
finds the number of each word as one. Find and
write the line number and the correction of the
error in the code.

Example fruits.txt numbers.txt


of files: apple: 3 3
pine: 1 1
orange: 2 2
Missing parts in order:
line_____
Answer: at line-10
incorrect part____________________________ __________________________
dict_count.keys() shall be input_words
or change line-12 to __________________________
correction _____________________________
dict_count[word]=input_words.count(word) Answers:
line words word letter
__________________________
or change line-12 to
dict_count[searchWord]=input_words.count(searchWord) __________________________

=============================== Good Luck================================

You might also like