Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS-UY 1114 / Python

First Midterm Exam – 6 March 2018

1)   (15 points; 5 each) Perform the following conversions

a.    Convert the binary number (10100111) to decimal:

(Do calculations here):                                                        ____________________

b.   Convert the decimal number (195) to hexadecimal: ___________________

c.   Convert the hexadecimal number (17) to binary: ____________________ (please show all 8 bits)

2)   (10 pts) What is the output from the following code?

var = 10;

acc = 0;

for i in range(1,var):

if i%3==0:

acc = i**2;

else:

acc +=i;

print(The answer is: “,acc);

The answer is: _______________

3)   (10 Points) What is the output from the following code?

acc = 0;

for i in range(5,15,5):

var = i;

while var>0:

var//=2;

acc+=var;

print("i=",i," var=",var);

print("acc=",acc);

(15 Points) What is the output from the following code?

x = 24;

y = 52;

while y != 0:

temp=y;

y = x%y;

x = temp;

print("x=",x,"y=",y);

print(x);

(30 points) Given a positive integer number, write a program to print the total number of times each digit between 0 and 9 appears in the number.  If a given number does not appear in the input, do not print a zero for that value.  Leading zeros will be dropped, as is usual with an int.  (Note, no credit will be granted for using string processing, the input and processing must be done as an Integer!!!!)

(the following are some examples)

What is your number: 123

1 appears 1 time(s).

2 appears 1 time(s).

3 appears 1 time(s).

What is your number: 0121139

1 appears 3 time(s).

2 appears 1 time(s).

3 appears 1 time(s).

9 appears 1 time(s).

What is your number: 100

0 appears 2 time(s).

1 appears 1 time(s).

What is your number: 20180306

0 appears 3 time(s).

1 appears 1 time(s).

2 appears 1 time(s).

3 appears 1 time(s).

6 appears 1 time(s).

8 appears 1 time(s)

6)   (20 points) An approximate value of pi can calculated with a series as below: pi = 4 * [ 1  1/3 + 1/5  1/7 + 1/9 ... + ((-1)n)/(2n+1)]

Write a program to ask the user for a value of n and print the resulting value of pi as calculated using the above.