cosc 1306 computer literacy for science majors jehan-françois pâris [email protected]
TRANSCRIPT
FIRST REVIEW SESSION
First Question
• We saw in class that Pascal’s calculating machine was not a commercial success. Why does it remain important?
Answer
• It showed that adding/subtracting machines could be built:– Proof of concept
Second Question
• Implement the Boolean function a and (b or c) using switches assuming that closed switches correspond to true values.
Answer
• Tip:– When you see an and, put the switches in
series (one after the other)– When you see an or, put the switches in
parallel
Answer
a
b
c
Third Question
• What characterizes a von Neumann computer?
Answer
• A von Neumann computer has a memory in which it stores both its programs and its data
Fourth Question
• Can you find examples of (a) a time-sharing system that is not interactive and (b) an interactive system that is not a time-sharing system?
Answer
(a) I do not have any great example
(a) Your PC: interactive but not shared
Fifth Question
• What is the most likely reason your computer takes so much time to boot?
Answer
• It has to bring too many things in main memory– The kernel– All the other programs that are set to be
loaded in main memory a boot time• Often junkware
Sixth Question
• You are going to the University Center with a nerd who claims he has an algorithm for finding out what is the best available flavor at the ice cream shop. Can you guess his algorithm?
Answer
• Getting samples of all flavors being offered that day before making a decision
Seventh Question
• Describe a practical heuristic for the same problem
• Why is it a heuristic?
Answer
• Thinking of your favorite flavors and maybe asking for a sample of an interesting flavor
• You are very likely to get a flavor you will like but could have missed an even better tasting flavor
Eighth Question
• You have to search for an item in a list of 20 of them.
• Does it make a big difference if you use linear search or binary search?
• Would it be true for a list of 50,000 items?
Answer
• No, it would not make a big difference for a list of 20 items– Linear search is the best solution
• Simpler• Yes, it would make a big difference for a list of
50,000 items– Binary search is the best solution
• Much faster (average of 16 steps vs. 25,000)
Ninth Question
• Which of the following items are (a) constants, (b) reserved words (c) variables and (d) expressions?
• 3.5 _________ elif _________while _______ 44 _________input/output _____ side-car _________False ________ rhubarb _________'27’ _________ “indeed” _________
Answer
• Which of the following items are (a) constants, (b) reserved words (c) variables and (d) expressions?
• 3.5 __(a)_____ elif ___(b)_____while __(b)____ 44 ___(c)______input/output _(d)_ side-car __(d)____False __(a)_____ rhubarb __(c)____'27’ ____(a)____ “indeed” __(a)____
Tenth Question
• What is probably wrong with the following Python code?
• nfingers = input(“How many fingers do you have?”)if nfingers != 20 : print(“Did you lose some in an accident?’)
Answer
• Two things– The string in the second print statement is not
properly terminated• Will be caught by interpreter
– We compare a string value with an integer constant• Not caught by interpreter
Eleventh Question
• What will the following Python program print?– code = 5
if code == 1 print(“Hello!”)print(“Goodbye!”)
Answer
• The interpreter will catch the missing semicolon but
– code = 5if code == 1 : print(“Hello!”)print(“Goodbye!”)
will print Goodbye!
Twelfth Question
• Write a Python program prompting for two floating point numbers and computing their average
What we need to do
• Prompt for first number, read it and convert to float
• Prompt for first number, read it and convert to float
• Compute average• Display average with 3 decimals
Answer
• # average.pya = float(input("Enter first number: "))b = float(input("Enter second number: "))average = (a + b)/2print(“Average is %.3f" % average)
Thirteenth Question
• What are the values of the variables a, b, c, d after the following Python code is executed?
• a = 4 * 2 – 0.5b = 2c = a // bd = a == c
• a = _________ b = _________c = _________ d = _________
Answer
• a = 4 * 2 – 0.5b = 2c = a // bd = a == c
• a = ___7.5___ b = ___2_____c = ___3_____ d = __False____
Fourteenth Question
• What is wrong with the following Python code?– i = 1
while i > 0 : print(“Going though the loop!”) i = i + 1
Answer
• The program
– i = 1while i > 0 : print(“Going though the loop!”) i = i + 1
will never end as the variable i is incremented instead of being decremented
Fifteenth Question
• What is wrong with the following Python code?
– i = 5while i > 0 : print(“Going though the loop!”)i = i - 1
Answer
• The program
– i = 5while i > 0 : print(“Going though the loop!”) i = i - 1
will never end as the variable i is not decremented inside the loop