Quiz 1

January 25, 2005

CMPT 120, Spring 2005

Instructor: Snezana Mitrovic-Minic

 

Total: 15 points + 1 bonus point

 

1.      (1 points) What is an algorithm?

An algorithm is a finite series of instructions for solving a problem.

It should terminate on any input and

the instructions must be precise, complete, and unambiguous.

 

2.      (1 points) What is a difference between pseudocode and program?

Pseudocode is an algorithm written in a language between plain English and

programming language. It cannot be processed by a computer.

Program is an algorithm written in a programming language that can be

processed by the computer.

 

3.      (2 points) What are the values for a, b, c, d, e, after the execution of the following Python program?

a=4

b=2

c = b**a

d= 23/2

e= 24%10

a=10

 

final values:

a = 10

b = 2

c = 16

d = 11

e = 4

 

4.      (6 points) What the following functions return?

a.       len(‘How are you?’)                     Ans: 12

b.       round(1.233)                            Ans: 1

c.       round(1.23188, 3)                     Ans: 1.232

d.       floor(1.6)                                  Ans: 1

e.       ceil(1.2)                                      Ans: 2

f.       float(1)                                        Ans: 1.0

g.       str(1.222)                                 Ans: "1.222"

h.       abs(-1.3)                                    Ans: 1.3

i.        abs(1.3)                                      Ans: 1.3

j.        sqrt(4)                                        Ans: 2

k.       pow(2, 3)                                 Ans: 8

l.        int(3.44)                                     Ans: 3

 

5.      (1 points) Write a Python statement that asks user to enter its name and saves the name in variable called nam.

 

nam = raw_input("Enter your name ")

 

6.  (1 points) What does the following Python program do?

 

grade = 4.33

print ‘Your grade is ‘ +str(grade)

 

Prints the statement: Your grade is 4.33

 

7.      (2 points) Represent numbers -4 and +6 in two’s complement notation

and then add the two binary numbers. Use 4 bits to represent each number.

 

4 is 0100

-4 in two’s complement representation is 1100

6 is 0110

 

Adding 6 and –4

                  0110

                +1100

               --------

                10010

 

Discard the carry-on digit, and the answer is 0010 which is 2

 

8.      (1 point) Represent number 486 as a binary number using the unsigned integer notation. Check the result by converting the binary number to decimal representation. 

 

(486)10 = (111100110) 2

 

9.      Bonus: (1 point) What are the ways to represent characters in the

computer memory?

 

 ASCII and Unicode