0% found this document useful (0 votes)
16 views2 pages

Mid Sem Questions

The document contains solutions to programming questions. The first question asks the user for an hour and number of hours ahead, and prints the new hour. The solution uses logic and comments. The second question evaluates math expressions, explaining when expressions are illegal or giving step-by-step workings.

Uploaded by

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

Mid Sem Questions

The document contains solutions to programming questions. The first question asks the user for an hour and number of hours ahead, and prints the new hour. The solution uses logic and comments. The second question evaluates math expressions, explaining when expressions are illegal or giving step-by-step workings.

Uploaded by

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

Q.

Write a program that asks the user for an hour between 1 and 12 and for how many hours in the
future they want to go. Print out what the hour will be that many hours into the future. An example
is shown below. [5]
Enter hour: 8
How many hours ahead? 5
New hour: 1 o'clock

Solution:

time_by_user=int(input("Enter hour:(Always Positive value and non-zero")) #converted to integer


value

time_to_move_ahead=int(input ("How many hours ahead?")) #converted to integer

new_time=time_by_user+time_to_move_ahead-12 #formula to move ahead as per the time given


by user

if new_time<0:

print("new hour",+time_by_user+time_to_move_ahead,"o' clock")

elif new_time==0:

print("new hour 12 o'clock")

else:

print("new hour",new_time, "o' clock")

Main points to be checked:

1. Variable names are logical


2. Comments are added at desired location.
3. User is asked to enter only positive and nonzero value
4. If value become 0 its displayed properly.
5. As given “o’clock” is added in the print statement.

Q.Show the result of evaluating each expression. Be sure that the value is in the proper form to
indicate its type (int or float). If the expression is illegal, explain why [1+2+2]

a) 4.0 / 10.0 + 3.5 * 2

Solution: 4.0 / 10.0 + 3.5 * 2 =0.4 + 7.0 =7.4 output is float. (Each step carry marks giving just final
value will give only 0.5 marks)

b) sqrt (5.0 - - - 4.5) + 7 *3 (yes there are three minus symbols its not a typing error)

Solution: Two answers are possible if someone has written that error will be generated because
import math and math.sqrt is not written he/she will get full marks.

Also if you have assumed that everything is included then answer will be

sqrt (5.0 - - - 4.5) + 7 *3

= sqrt(0.5)+7*3
= 0.707+21 .

= 21.707 output is float (Each step carry marks giving just final value will give only 0.5 marks)

c) 3 ** 2 // 3 + 10% 3

=9//3+10%3

=3+1

=4 Output is integer (Each step carry marks giving just final value will give only 0.5 marks)

You might also like