CMPSC 101 Homework 1 – Basic input/output and computation solution

$20.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (2 votes)

Problem 1. Indicate whether the following variables are valid or invalid: [5 points]
(a) my name
(b) new_sum
(c) 21guns
(d) simon&garfunkel
(e) _total
Problem 2. What will be the output of the following code? [5 points]
total = 99
print(“Total = “, “total”)
Problem 3. For the given assignment statements, what will be the Python data type of the
variables? [5 points]
(a) val1 = 20.00
(b) val2 = 5
(c) val3 = “10”
(d) val4 = 1.618
(e) val5 = “hello”
CMPSC 101 Instructor: Ishan Behoora
Introduction to Programming in Python Spring 2017
Problem 4: Write python statements for the following arithmetic operations: [10 points]
(a) Add 5 to variable b and assign the result to variable a
(b) Subtract 10 from variable b and assign the result to variable a
(c) Multiply variable b by 7 and assign the result to variable a
(d) Divide variable b by 10 and assign the result to variable a
(e) Increment the value in variable a by 5
Problem 5: Choose the correct answer for the following questions: [10 points]
1. Python is a(n):
A. Assembly language
B. Machine language
C. High-level language
D. Operating System
2. The identifiers myvar and myVar, when used in the same python program refer to the
same variable.
A. True
B. False
3. What will be the output of the following program, if you provide 1, 2 and 3 as inputs in
separate lines?
print(“Enter three numbers: ”)
num1 = eval(input())
num2 = eval(input())
num3 = eval(input())
avg = (num1 + num2 + num3) / 3
print(avg)
A. 1
B. 2
C. 2.0
D. <Error
4. What will be the output of the following code?
x = 1
x = x + 2.5
print(x)
A. 1
B. 3
C. 3.5
D. <Error
CMPSC 101 Instructor: Ishan Behoora
Introduction to Programming in Python Spring 2017
5. What will be the output of the following code?
a = 3
b = 11
b = a
a = b
print(a,b)
A. 3 11
B. 11 3
C. 3 3
D. 11 11
Problem 6. Write a program to calculate perimeter of a circle using the formula: ????????? =
2 × ?? × ??????. The program should prompt the user to enter the radius of the circle. Use PI
= 3.14. Make sure your code works for any given radius. [20 points]
Sample program output:
Enter radius of circle: 50
Perimeter = 314.0
Problem 7: Write a program to convert temperature from Celsius to Fahrenheit scale using the
formula: ? =
9
5
? + 32. Prompt the user to enter the temperature in degree Celsius and display
the equivalent temperature in degree Fahrenheit. Make sure your code works for any given
temperature value. [20 points]
Sample program output:
Enter temperature in degree Celsius: 23.6
23.5 C is equivalent to 74.48 F
Problem 8: Write a program to calculate the time taken by a car to reach from one city to another.
Prompt the user to enter the names of the two cities, distance between the two cities in miles
(mention the names of the cities), and average speed of the car (in miles/hour). Display the
expected time required (in hours) to travel from the source to the destination city. Make sure
your code works for any given distance and speed. [25 points]
Sample program output:
Enter source city: State College
Enter destination city: New York
Enter distance between State College and New York (miles): 240
Enter average speed of the car (mph): 60
Expected time required to travel from State College to New York is 4.0 hours
CMPSC 101 Instructor: Ishan Behoora
Introduction to Programming in Python Spring 2017
The following files should be uploaded on Canvas:
1. hw1_solution.pdf
This file should contain answers for questions 1-5 and screenshots of code and output for
questions 6-8.
2. q6.py
This file should have the code for problem 6, and should be runnable in Python3.
3. q7.py
This file should have the code for problem 7, and should be runnable in Python3.
4. q8.py
This file should have the code for problem 8, and should be runnable in Python3.