GE - Computer Scien LnptIBy
GE - Computer Scien LnptIBy
GE - Computer Scien LnptIBy
Semester : I
Instructions: Attempt any FOUR questions. All questions carry equal marks.
Q1
• Which of the following are valid identifiers? If they are invalid, then mention the
cause of violation.
Nn3
pp_4
from
bv-1
highestOfHeight
2Good2BeTrue
• Write a program to print the sum of the first n terms of the following series:
Q2
7 * 2 - 5 ** 2 // 4 + 8
9 & -11
• Apply Selection Sort on the list [95, 81, 14, 72, 68, 59, 42, 24], to sort
it in the ascending order. Show the contents of the list after each iteration.
• a function that accepts a number and a string as arguments. It should return a list having
n elements such that the ith element of the list has the string repeated i times. For
example, if the arguments are 3 and 'Hello' then the function should return
['Hello', 'HelloHello', 'HelloHelloHello']. Use list comprehension
to generate this list.
• a function that accepts two tuples as arguments and returns a third tuple which has the
alternate elements from the two tuples passed to it as arguments. IndexError should
be raised if the number of elements in the two tuples are not same. The function should
have try … except statements to handle IndexError.
• a function that accepts a list and computes the frequency of each numeric element in
the list. The non-numeric elements should be ignored. The function should return the
answer as a dictionary. For example, if the list is ['Hello', 1.2, 5, [2, 7],
7, 5, 5, 1.2, 7.4] then the output should be {1.2: 2, 5: 3, 7: 1,
7.4: 1}
Q4 What will be the output produced on execution of the following code segments? Justify
your answers.
• s = 0
if i % 2 == 0:
continue
2
else:
s += i
print(s)
print('Green' in color)
allcolor=color.union(primary)
print(allcolor)
print(color.intersection(primary))
print(color.difference(primary))
print(list1[1][-3:-9:-1])
list1[2] = 2001
del list1[2]
print(list1[2])
print(list1)
txt = txt.split('!')
print(txt)
print('!!!'.join(txt))
• def avg(marks):
3
return sum(marks)/len(marks)
try:
mark2 = []
print("Bye")
print(err)
Q5
• A stack is initially empty. Show the contents of this stack on execution of each of the
following operations: push('Jan'), push('Feb'), pop(),
push('Mar'), pop(), pop().
What will happen if another pop() operation is invoked after the execution of the
above sequence of operations?
• Write a function that accepts names of two files: source and target as arguments and
copies alternate characters from the source file to the target file. The function should
return -1 if the source file does not exist.
• Identify the errors in the following pieces of code and rewrite the code after removing
them.
DEF execmain():
x = input("Enter a number:")
if (abs(x) = x):
else:
4
x=*‐1
print('test' * 3.0)
Q6 Define a class Student that keeps track of the academic record of students in a school.
The class should contain the following instance data members:
Include assert statements in appropriate functions to ensure that the marks in the
marksList are >= 0 and <= 100.
• create an object S1 of this class for Rajesh of IX C having roll no. as 19 and marks
as 92, 96, 83, 97 and 91.