Docsity

Quiz1 Practice & Answers, Slides of Algorithms and Programming

Toggle sidebar
bg1
These problems were generated by TAs and instructors in previous semesters.
They may or may not match the actual difficulty of problems on Quiz1.
Algorithms and Abstraction
1. As you learned in lecture, computer science boils down to two main ideas:
algorithms and abstraction.
1) Identify/Define the difference between algorithms and abstraction, and provide an
example for each.
2) What are the 3 key components of a good algorithm?
Answer:
1) Algorithms: algorithms are a set of instructions to solve a given task or problem.
An example of an algorithm is a food recipe or a “how-to” manual
Abstraction: technique used to make complex systems manageable by changing
how much detail is used to represent/interact with the system
An example of abstraction is your internet browser. Instead of needing to know
the exact destination of the information you want to see on the internet, you can
search websites by their url or by keywords relating to it.
2) Key components:
Input: Should have a specified input needed in the beginning
Steps: Should have a clearly-described process for how the algorithm runs
Output: Should have a specified output produced at the end
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Quiz1 Practice & Answers and more Slides Algorithms and Programming in PDF only on Docsity!

These problems were generated by TAs and instructors in previous semesters. They may or may not match the actual difficulty of problems on Quiz1.

Algorithms and Abstraction

  1. As you learned in lecture, computer science boils down to two main ideas: algorithms and abstraction.
    1. Identify/Define the difference between algorithms and abstraction, and provide an example for each.
    2. What are the 3 key components of a good algorithm? Answer:
    3. Algorithms: algorithms are a set of instructions to solve a given task or problem. An example of an algorithm is a food recipe or a “how-to” manual Abstraction: technique used to make complex systems manageable by changing how much detail is used to represent/interact with the system An example of abstraction is your internet browser. Instead of needing to know the exact destination of the information you want to see on the internet, you can search websites by their url or by keywords relating to it.
    4. Key components: Input: Should have a specified input needed in the beginning Steps: Should have a clearly-described process for how the algorithm runs Output: Should have a specified output produced at the end

Programming Basics

  1. For each of the following Python expressions, identify the data type of the value it will evaluate to.
    1. 3
    2. “3”
    3. 3 < 3
    4. “3” + “3”
    5. 3.0 * 3.
    6. 3 / 3 Answer:
    7. Integer (int)
    8. Floating point number (float)
    9. String (str)
    10. Boolean (bool)
    11. String (str)
    12. Floating point number (float)
    13. Floating point number (float)

Data Representation

  1. Answer the following questions regarding number systems:
    1. What is a number system?
    2. How many values can a specific digit have in a decimal number? In a binary number?
    3. Let’s say that we are representing length in imperial units. You are given inches, feet, and yards. How could you represent 10,000 inches in this system (show your work)? (hint: 12 inches=foot, 3 feet=yard 1760 feet=mile) Answer:
    4. A way of representing numbers using symbols
    5. Decimal: 10 (0,1,2,3,4,5,6,7,8,9) Binary: 2 (0,1)
    6. 10000 [inches] // 36 [inches] = 277 [yards] 10000 [inches] % 36 [inches] = 28 [inches] 28 [inches] // 12 [inches] = 2 [feet] 28 [inches] % 12 [inches] = 4 [inches] Yards Feet Inches 277 2 4 277 yards and 2 feet and 4 inches is equivalent to 10,000 inches (277yards + 2 feet + 4 inches) == (10000 inches)
  2. Complete the following conversions and show appropriate work:
    1. Convert 10111011 from binary to decimal
    2. Convert 11111111 from binary to decimal
    3. Convert 01101110 from binary to decimal
    4. Convert 57 from decimal to 8-bit binary
    5. Convert 63 from decimal to 8-bit binary
    6. Convert 88 from decimal to 8-bit binary Answer:
    7. 187 (1+2+8+16+32+128=187)
    8. 255 (1+2+4+8+16+32+64+128=255 OR note that the next bit would be 256, so we know making all 8 bits 1 will be 255)
    9. 110 (2+4+8+32+64=110)
    10. 00111001 (57=32+16+8+1)
    11. 00111111 (63=32+16+8+4+2+1)
    12. 01011000 (88=64+16+8)
  1. The following questions relate to interpreting binary numbers as abstracted types
    1. Answer the following questions relating to abstracting colors: a) How many bytes are needed to represent an RGB value? b) What are the highest and lowest values for any color? c) Convert the color purple to an RGB value. Provide the values in decimal and binary. d) Convert 11010101 11110000 00110011 to a color (You can use this link to determine what color is appears to be: https://www.w3schools.com/colors/colors_rgb.asp)
    2. Answer the following questions relating to abstracting text (ASCII table: http://www.asciitable.com/): a) Convert 00111010 00101001 to ASCII characters. b) Convert ‘CMU cmu’ into 8-bit binary (neglect the quotation marks). Answers:
    3. Colors: a) 3 bytes (24 bits, 3 8-bit values) b) 255 is the highest value, 0 is the lowest c) Purple is (R=255,G=0,B=255) or 11111111 00000000 11111111 d) (213, 240, 51)->yellowish
    4. Text: a) :) (00111010=58, 00100101=41) b) 01000011 01001101 01010101 00100000 01100011 01101101 01110101 ‘C’=67 ‘M’=77 ‘U’=85 ‘ ‘=32 (space!) ‘c’=99 ‘m’=109 ‘u’=
  1. Consider the following function: def helloThere(name): print("Hello there,", name, "!")
  1. What will you see in the shell if you call print(helloThere("Stella"))?
  2. Does this function have side effect(s)? If so, state the side effect(s).
  3. Does this function have a returned value? If so, state the returned valued.
  4. What do we call the statement helloThere("Stella")? What do we call the string "Stella" in that statement? Answer:
    1. Shell output: Hello there, Stella! None
    2. Yes, the print statement in the function is the side effect
    3. Yes, the returned value is None. (all functions return None by default if another value is not specified)
    4. helloThere("Stella") -> function call "Stella" -> input or argument are both acceptable (parameter is incorrect, the variable name is the parameter)
  1. Consider the following algorithm for a function: Algorithm:
    1. Input a number n
    2. If n is greater than or equal to 0 do the following, otherwise skip to step 3: a. If n is divisible by 10 then print out the value of n followed by " is cool number”, otherwise skip to step 2b b. If step 2a did not occur, check if n is divisible by 4, and if so print out the value of n followed by " is a strange number”, otherwise skip to step 2c c. Skip to step 4
    3. Print the value of n followed by " is a negative number!!”
    4. Print the string "ALL DONE" Identify the argument(s), returned value, and side effect(s) of this algorithm. Then write the corresponding Python function called divisionCheck. Answer: The argument to the function is n. The returned value will be None, since we don’t return anything in the function. The side effects are the possible print statements. def divisionCheck(n): if (n >= 0): if ((n % 10) == 0): print(n, "is a cool number") elif ((n%4) == 0): print(n, "is a strange number") else: print(n, "is a negative number!!") print("ALL DONE")