Mid1-ITC-Fall-2015 - DONE
Mid1-ITC-Fall-2015 - DONE
Serial No:
CS101
Mid I
Introduction to Computing Total Time: 1 Hour
Monday, September 14, 2015 Total Marks: 80
Course Instructor
Dr. Sibt ul Hussain, Dr. Abul Malik, ________________
Signature of Invigilator
Ms. Uzma Maroof
Page 1 of 11
National University of Computer and Emerging Sciences
School of Computing Fall 2015 Islamabad Campus
Q. No. 1
25
(a). For each expression at left, indicate its value in the right column. List a
value of appropriate type. e.g., 7 for an integer, 7.0 for a real, "hello" for a String,
Expression Value 10
-(6 + 3 - 2 * 3)
15 % 6 + 5 % 5 + 12 % 7 % 3
9 / 2 / 2.0 + 9 / 2.0 / 2
5 + 6 + 7 % 8**2**3 + 9 + 2 * 3
not(3 < 7 and -1 != 8)
31 / 2 / 10.0 + 10 / (5 / 2.0)
5 >= 5 * 6 + 2 or 9 > 4 * 5
(2.5 = 5 / 2.0) * 10
True or Talse for a boolean or write error if there is any.
(b) For each run and input below, write the output that is produced.
Dry run: 5
y =0
n = input("Enter an Integer: ")
while n!=0:
x=n%10
y=y*10+x
n=n/10
print(y)
Run 1
Enter an Integer: 452
Output:
Page 2 of 11
National University of Computer and Emerging Sciences
School of Computing Fall 2015 Islamabad Campus
Run 2:
Enter an Integer: 1462
Output:
5
Dry run:
a=input('Enter a Number: ')
b=input('Enter another Number: ')
if a % 2 != 0:
a = a * 2
if a > 10 :
b=b+1;
elif a < 10:
a=a-1
b=b-1;
print(str(a)+" "+str(b))
Runs Outputs
Input for Run1: Output of Run1:
Enter a Number: 12
Enter another Number: 12
Page 3 of 11
National University of Computer and Emerging Sciences
School of Computing Fall 2015 Islamabad Campus
Enter another Number: 4
Input for Run3: Output of Run3:
Enter a Number: 5
Enter another Number: 8
Input for Run4: Output of Run4:
Enter a Number: 3
Enter another Number: 42
a=input('Enter a Number')
5
b = 1
c = 0
while b <= a:
if b % 2 == 0:
c=c+1
b = b * 10
c=c+1
print( c + 1)
Runs Outputs
Input for Run1: Output of Run1:
Enter a Number: 4097
Page 4 of 11
National University of Computer and Emerging Sciences
School of Computing Fall 2015 Islamabad Campus
Q. No. 2 25
a) Write an if statement for the following situation: If an integer variable
currentNumber is multiple of 5, change its value so that it is now 3
3
times currentNumber plus 1, otherwise change its value so that it is
now half of currentNumber.
if currentNumber % 5 == 0:
currentNumber = currentNumber*3 +1
else:
currentNumber = currentNumber/2
2
b) Write an if/else statement to check the input given by the user in variable city
is “Lahore” otherwise show him message that his input is wrong.
if city != "Lahore":
print "Your input is wrong"
if not (-10 <= grade <= 10) and not (100 <= grade <= 1000):
x = "High"
d) Write a while loop that displays sum of the first n odd numbers:
5
1 + 3 + 5 + . . . + 2n − 1 …
Page 5 of 11
National University of Computer and Emerging Sciences
School of Computing Fall 2015 Islamabad Campus
sum = 0
counter = 1
NextNumber = 1
while counter <= n:
sum = sum + NextNumber
NextNumber = NextNumber + 2
counter = counter + 1
e) Write a while loop that displays the following sequence: 1, 4, 9, 16, 25, 36, 49, 64
3
counter = 1
f) Write a while loop that finds number of times a whole number n can be divided
by 4 (using integer division) before reaching 1. For instance, if user gives 10 as
input the result should be 1 and if a user gives 64 as input the result should be 3. 5
n = input()
counter = 0
while n >= 4:
n =n//4
counter = counter + 1
print counter
Page 6 of 11
National University of Computer and Emerging Sciences
School of Computing Fall 2015 Islamabad Campus
g) What will be the output of following statements, write errors if they results in syntax
error. Please write your output in the second column.
5
Code Output
w=---5 100
q=20
print (-w*+q)
a=5*8 80
b=8*5
e=a+b
f=e/4
print(e)
Now write a program that gets a starting value from the user and then prints the
Syracuse sequence for that starting value.
x = int(input())
while x != 1:
print (x)
if x%2 == 0:
x = x/ 2
else:
x = 3*x + 1
print (x)
Page 8 of 11
National University of Computer and Emerging Sciences
School of Computing Fall 2015 Islamabad Campus
Q. No. 4 Write the code (using a loop) that takes size of square as input and
prints a square of that size using ‘*’ characters. Note you are only allowed to use a
single loop. 10
For example:
Size = input()
print ('*'*Size)
line = 1
while line <= Size-2:
print ('*'),
print(' '*(Size-2)),
print ('*')
line = line + 1
print ('*'*Size)
Solution 2:
Page 9 of 11
National University of Computer and Emerging Sciences
School of Computing Fall 2015 Islamabad Campus
You may assume that all parameter values are valid: the hours are both between 0 and
23, and the minute parameters are between 0 and 59. You may also assume that both
times represent times in the same day, e.g. the first time won't represent a time today
while the second time represents a time tomorrow. Note that the second time might be
earlier than the first time; in such a case, your program should print "Invalid Input".
Solution:
hr1=input('Enter First Hour: ')
min1=input('Enter First Minutes: ')
hr2=input('Enter Second Hour: ')
min2=input('Enter Second Minutes: ')
time1=hr1*60+min1
time2=hr2*60+min2
print('\n')
if time1 > time2:
print('Invalide input')
elif time2-time1 >=45:
print ("you can take your lunch")
else:
Page 10 of 11
National University of Computer and Emerging Sciences
School of Computing Fall 2015 Islamabad Campus
print ("you cannot take your lunch")
Page 11 of 11