You are on page 1of 14

Misr University for Science and Technology

Information Technology College

(Choose the Correct Answer for all the following questions)


Question (1)
1. What does this code output? print((4 + 8) / 2)
a. 6.0
b. 6
c. 8
d. 8.0
2. Fill in the blank to output the quotient of dividing 100 by 42.
print(100...42).
a. //
b. /
c. %
d. **
3. Guess the output of this code: print('I'm learning!').
a. I'm learning!
b. invalid syntax
c. I'm learning!
d. Error
4. Which of the following options results in exactly two lines.
a. "Hello \n world"
b. 'one \' two \' three'
c. "Some \n text \n goes here"
d. "Hello world"
5. Which of these is a valid variable name in Python.
a. a-variable-name
b. a variable name
c. A_VARIABLE_NAME
d. 2avariablename
6. What is the output of this code?
spam = "eggs"
print(spam * 3).
a. eggseggseggs
b. spamspamspam
c. "spamspamspam"
d. " eggseggseggs"
7. What will be the output of this code?
x = “2”
y = “4”
z = (x) + (y)
print(z)

a. 6
b. 24
c. 6.0
d. 42
8. What is the output of this code?
spam = "7"
spam = spam + "0"
eggs = int(spam) + 3
print(float(eggs)).
a. 73.0
b. 73
c. 10.0
d. 703
9. What is the output of this code if the user enter '42' as input:
age = int(input())
print(age+8)
a. 428
b. 50
c. 50.0
d. invalid syntax
10. What are the two Boolean values in Python.
a. true and false
b. Truth and Falsity
c. True and False
d. truth and falsity
11. Comparison operators are also called.........
a. Relational operators

Page 1 of 14
Misr University for Science and Technology
Information Technology College

b. Assignment operators
c. arithmetic operators
d. Logical operators
12.
a. eight
b. five
c. five eight
d. nothing

13.

a. Print all the even numbers between 0 and 8


b. Print all the odd numbers between 1 and 9
c. Print all the even numbers between 2 and 10
d. Print all the odd numbers between 3 and 9

14. Which construct can be used to iterate through a list?


a. Variable assignment
b. Loops
c. if statements
d. Boolean values

15.
a. Line 2
b. Line 3
c. Line 4
d. Line 1

16. Which collection is ordered, changeable, and allows duplicate members?


a. SET
b. LIST
c. TUPLE
d. DICTIONARY

17. Which statement is used to stop a loop?


a. break
b. stop
c. return
d. exit
18. What is the output of

print (list[1:3]) if list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]?


a. [ 'abcd', 786 , 2.23, 'john', 70.2 ]
b. abcd
c. [786, 2.23]
d. None of the above

19. What error is caused by importing an unknown module?


a. ImportError
b. ModuleError
c. UnknownModuleError
d. UnknownImportError

20. How would you close a file stored in a variable "text_file"?


a. text_file.close()
b. close("text_file")
c. close(text_file)
d. text_file.close

21. What happens if you open a file in write mode and then immediately close it?
a. A blank line is written to the file
b. Nothing changes
c. The file contents are deleted
d. The file is deleted

Page 2 of 14
Misr University for Science and Technology
Information Technology College

:Question 2
:Choose True or False (True is the left bubble, False is right bubble before each question on the sheet)

1. In Python, 'Hello', is the same as "Hello"

2. The append method adds an item to the end of an existing list.

3. Using the continue statement inside of a loop causes an error.

4. Indentation is used to define the level of nesting.

5. you can assign a string to a variable, and later assign an integer to the same variable.
6. taking the age of the user as input:
age =(input())
print(age)

7. Adding a string to a number produces an error.

8. Strings can be multiplied by integers

9. Reminder is the quantity produced by the division of two numbers.


10. You can raise a number to multiple powers.

Question 3) Programming Excerpt


1. Write a Python program to print the following string in a
specific format
Sample String : "Twinkle, twinkle, little star, How I wonder
what you are! Up above the world so high, Like a diamond in
the sky. Twinkle, twinkle, little star, How I wonder what you
are" Output :
Twinkle, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are
print("Twinkle, twinkle, little star, \n\tHow I wonder what you are!
\n\t\tUp above the world so high, \n\t\tLike a diamond in the sky. \
nTwinkle, twinkle, little star, \n\tHow I wonder what you are!")

2. Write a Python program to get the Python version you


are using.

import sys
print (sys.version)

Page 3 of 14
Misr University for Science and Technology
Information Technology College

Write a Python program to display the current date and time .3

import datetime
)(now = datetime.datetime.now

4. Write a Python program which accepts the radius of a circle


from the user and compute the area.

from math import pi


r = float(input ("Input the radius of the circle : "))
print ("The area of the circle with radius " + str(r) + " is: " +
str(pi * r**2))

5. Write a Python program which accepts the user's first and


last name and print them in reverse order with a space
between them.

fname = input("Input your First Name : ")

lname = input("Input your Last Name : ")

print ("Hello " + lname + " " + fname)

6. Write a Python program which accepts a sequence of


comma-separated numbers from user and generate a list
and a tuple with those numbers.

values = input("Input some comma seprated numbers : ")


list = values.split(",")
tuple = tuple(list)
print('List : ',list)
print('Tuple : ',tuple)

7. Write a Python program to accept a filename from the user


and print the extension of that.

filename = input("Input the Filename: ")


f_extns = filename.split(".")
print ("The extension of the file is : " + repr(f_extns[-1]))

Page 4 of 14
Misr University for Science and Technology
Information Technology College

8.Write a Python program to display the first and last colors


from the following list. color_list =
["Red","Green","White" ,"Black"]

color_list = ["Red","Green","White" ,"Black"]

print( "%s %s"%(color_list[0],color_list[-1]))

9. Write a Python program to display the examination


schedule.

exam_st_date = (11,12,2014)
print( "The examination will start from : %i / %i /
%i"%exam_st_date)

10. Write a Python program that accepts an integer (n) and


computes the value of n+nn+nnn.

a = int(input("Input an integer : "))


n1 = int( "%s" % a )
n2 = int( "%s%s" % (a,a) )
n3 = int( "%s%s%s" % (a,a,a) )
print (n1+n2+n3)

11. Write a Python program to print the documents (syntax,


description etc.) of Python built-in function(s).
print(abs.__doc__)

12. Write a Python program to print the calendar of a given


month and year.
Note : Use 'calendar' module.
import calendar

y = int(input("Input the year : "))


m = int(input("Input the month : "))
print(calendar.month(y, m))

13. Write a Python program to print the following here


document.

Page 5 of 14
Misr University for Science and Technology
Information Technology College

print("""
a string that you "don't" have to escape
This
is a ....... multi-line
heredoc string --------> example
""")

Write a Python program to calculate number of days .14


.between two dates
Sample dates : (2014, 7, 2), (2014, 7, 11)
from datetime import date
f_date = date(2014, 7, 2)
l_date = date(2014, 7, 11)
delta = l_date - f_date
print(delta.days)

Write a Python program to get the volume of a sphere with .15


.radius 6
pi = 3.1415926535897931
r= 6.0
V= 4.0/3.0*pi* r**3
print('The volume of the sphere is: ',V)

16. Write a Python program to get the difference between a


given number and, if the number is greater than 17 return
double the absolute difference.

def difference(n):
if n <= 17:
return 17 - n
else:
return (n - 17) * 2

print(difference(22))
print(difference(14))

17. Write a Python program to test whether a number is within


100 of 1000 or 2000.

def near_thousand(n):
return ((abs(1000 - n) <= 100) or (abs(2000 - n) <= 100))
print(near_thousand(1000))
print(near_thousand(900))
Page 6 of 14
Misr University for Science and Technology
Information Technology College

print(near_thousand(800))
print(near_thousand(2200))

18. Write a Python program to calculate the sum of three given


numbers, if the values are equal then return three times of
their sum.

def sum_thrice(x, y, z):

sum = x + y + z

if x == y == z:
sum = sum * 3
return sum

print(sum_thrice(1, 2, 3))
print(sum_thrice(3, 3, 3))

19. Write a Python program to get a new string from a given


string where "Is" has been added to the front. If the given
string already begins with "Is" then return the string
unchanged.

def new_string(str):
if len(str) >= 2 and str[:2] == "Is":
return str
return "Is" + str

print(new_string("Array"))
print(new_string("IsEmpty"))

20.Write a Python program to get a string which is n (non-


negative integer) copies of a given string.

def new_string(str):
if len(str) >= 2 and str[:2] == "Is":
return str
return "Is" + str

print(new_string("Array"))
print(new_string("IsEmpty"))

Page 7 of 14
Misr University for Science and Technology
Information Technology College

Write a Python program to find whether a given number .21 .11


(accept from the user) is even or odd, print out an
.appropriate message to the user

num = int(input("Enter a number: "))


mod = num % 2
if mod > 0:
print("This is an odd number.")
else:
print("This is an even number.")

12. Write a Python program to count the number 4 in a


given list.

def list_count_4(nums):
count = 0
for num in nums:
if num == 4:
count = count + 1

return count

print(list_count_4([1, 4, 6, 7, 4]))
print(list_count_4([1, 4, 6, 4, 7, 4]))

13. Write a Python program to get the n (non-negative


integer) copies of the first 2 characters of a given string.
Return the n copies of the whole string if the length is
less than 2.

def substring_copy(str, n):


flen = 2
if flen > len(str):
flen = len(str)
substr = str[:flen]

result = ""
for i in range(n):
result = result + substr
return result
print(substring_copy('abcdef', 2))

Page 8 of 14
Misr University for Science and Technology
Information Technology College

print(substring_copy('p', 3));

24. Write a Python program to test whether a passed letter is a


vowel or not.

def is_vowel(char):
all_vowels = 'aeiou'
return char in all_vowels
print(is_vowel('c'))
print(is_vowel('e'))

25.Write a Python program to check whether a specified value


is contained in a group of values.

def is_group_member(group_data, n):

for value in group_data:

if n == value:

return True

return False

print(is_group_member([1, 5, 8, 3], 3))

print(is_group_member([5, 8, 3], -1))

26. Write a Python program to read an entire text file.

Contents of text.txt

……..

……..

……….

Python Code:

def file_read(fname):
Page 9 of 14
Misr University for Science and Technology
Information Technology College

txt = open(fname)

print(txt.read())

file_read('test.txt')

27. Converts a number to a list of digits

Example:

def tips_digit(n):
return list(map(int, str(n)))

print(tips_digit(2468))
Output:

[2, 4, 6, 8]

28. Write a Python program to read first n lines of a file.

29. Write a Python program to append text to a file and display


the text.

30. Write a Python program to read last n lines of a file.

31. Write a Python program to read a file line by line and store
it into a list.

32. Write a Python program to read a file line by line store it


into a variable.

33. Write a Python program to read a file line by line store it


into an array.

34. Write a python program to find the longest words.

Page 10 of 14
Misr University for Science and Technology
Information Technology College

35. Write a Python program to count the number of lines in a


text file.

36. Write a Python program to count the frequency of words in


a file.

37. Write a Python program to get the file size of a plain file.

38. Write a Python program to write a list to a file.

39. Write a Python program to copy the contents of a file to


another file

40. Write a Python program to combine each line from first file
with the corresponding line in second file.

41. Write a Python program to read a random line from a file

42. Write a Python program to assess if a file is closed or not.

43. Write a Python program to create a lambda function that


adds 15 to a given number passed in as an argument, also
create a lambda function that multiplies argument x with
argument y and print the result.

r = lambda a : a + 15

print(r(10))

r = lambda x, y : x * y

print(r(12, 4))

44. Write a Python program to sort a list of tuples using


Lambda.

subject_marks = [('English', 88), ('Science', 90), ('Maths', 97),


('Social sciences', 82)]

print("Original list of tuples:")

Page 11 of 14
Misr University for Science and Technology
Information Technology College

print(subject_marks)

subject_marks.sort(key = lambda x: x[1])

print("\nSorting the List of Tuples:")

print(subject_marks)

45. Write a Python program to find those numbers which are


divisible by 7 and multiple of 5, between 1500 and 2700 (both
included).

46. Write a Python program to convert temperatures to and


from celsius, fahrenheit. [ Formula : c/5 = f-32/9 [ where c =
temperature in celsius and f = temperature in fahrenheit ]
Expected Output :
60°C is 140 in Fahrenheit
45°F is 7 in Celsius

47. Write a Python program to guess a number between 1 to


9. Note : User is prompted to enter a guess. If the user
guesses wrong then the prompt appears again until the guess
is correct, on successful guess, user will get a "Well guessed!"
message, and the program will exit.

48. Write a Python program to construct the following pattern,


using a nested for loop.
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Page 12 of 14
Misr University for Science and Technology
Information Technology College

49. Write a Python program that accepts a word from the user
and reverse it.

50. Write a Python program to count the number of even and


odd numbers from a series of numbers.
Sample numbers : numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
Expected Output :
Number of even numbers : 5
Number of odd numbers : 4

51. Write a Python program that prints each item and its
corresponding type from the following list.
Sample List : datalist = [1452, 11.23, 1+2j, True, 'w3resource',
(0, -1), [5, 12], {"class":'V', "section":'A'}]

52. Write a Python program that prints all the numbers from 0
to 6 except 3 and 6.
Note : Use 'continue' statement.
Expected Output : 0 1 2 4 5

53. Write a Python program to get the Fibonacci series


between 0 to 50. Note : The Fibonacci Sequence is the series
of numbers :
0, 1, 1, 2, 3, 5, 8, 13, 21, ....
Every next number is found by adding up the two numbers
before it.
Expected Output : 1 1 2 3 5 8 13 21 34

54. Write a Python program which iterates the integers from 1


to 50. For multiples of three print "Fizz" instead of the number
and for the multiples of five print "Buzz". For numbers which
are multiples of both three and five print "FizzBuzz".
Sample Output :
fizzbuzz
1
2
Page 13 of 14
Misr University for Science and Technology
Information Technology College

fizz
4
buzz

55. Write a Python program which takes two digits m (row) and
n (column) as input and generates a two-dimensional array.
The element value in the i-th row and j-th column of the array
should be i*j.

Page 14 of 14

You might also like