Solutions to the Practice Questions#

Algorithms#

  1. An algorithm is a well-ordered set of instructions that can be completed by an entity (for example, a computer). Each instruction must be doable (means that the entity can do it) and unambiguous (means that there is one and only one result). Some computing scientists argue that the set of instructions must halt, however, this would preclude our operating systems from being algorithms.

  2. Here is a sample. There are an infinite number of correct answers.

    1. Open eyes.
    2. Slide alarm button to the right.
    3. Place feet onto the floor.
    4. Stand up.
    5. Walk to the closet.
    6. Select garments to wear.
    7. Walk to the bathroom.
    8. Put on the selected garments.
    9. Walk to the kitchen.
    10. Open the fridge.
    11. Take out the milk.
    12. Open the cupboard to the left of the fridge.
    13. Take out rounded O cereal.
    14. Open the cupboard across from the fridge.
    15. Take out a bowl.
    16. Place the cereal and the milk into the bowl.
    17. Eat.
    18. Done.

    Problem: each statement is still vague or ambiguous, and not necessarily doable. For example, the first statement says to “Open eyes” – whose eyes? what does “open” mean? how far should the eyes be opened?

  3. Here is one of many possible solutions:

    1. Get length of one side and save it in firstLength.
    2. Get length of other side and save it in secondLength.
    3. Determine whether one of firstLength or secondLength is the length of the hypotenuse, and calculate appropriately.
      i. If firstLength is the length of the hypotenuse then set thirdLength to the square root of (firstLength squared minus secondLength squared).
      ii. If secondLength is the length of the hypotenuse then set thirdLength to the square root of (secondLength squared minus firstLength squared).
      iii. Otherwise Set thirdLength to the square root of (firstLength squared plus secondLength squared).
    4. Done.
    Problem: Is each instruction doable? Can the entity square items? Or find the square root of an item? Does the entity know what a hypotenuse is?
  4. The instructions in the second task are far less vague, so more likely to be able to be done by a machine. At the same time, humans may find taking a square root difficult. It is likely that the instruction in the first task could be done by a person, provided they are in the same sleeping location that the instructions were assuming. A machine, on the otherhand, would struggle with the ambiguity.

Back to Questions

Execution#

  1. Parts:

    Program visualization picture
  2. It will print the following to standard output:

    Hello Computing Students!
    Hope you like this course.
    Programming is a lot of fun.
    
  3. The results will change.
    a.

    Hope you like this course.
    Hello Computing Students!  
    Programming is a lot of fun.
    

    b.

    Programming is a lot of fun.
    Hope you like this course.
    Hello Computing Students!    
    

    c. through e.
    The program would no longer compile. The structure is no longer valid. You cannot run the program.

  4. Sequential means “in order” from top to bottom. The Java instructions will complete in the order given. Notice how in 3a and 3b that order is changed, and the output changes to match.

  5. Syntax, as in English, is the structure of the program. Those parts must be there and in the specific order, for example, the semicolon at the end of each statement, the matching brackets, and even that the statements have to be inside the appropriate place. Semantics is the meaning of the program. We saw in question 3 that the meaning changed based on the ordering. We also saw in question 3, parts c to f, that incorrect syntax will not allow a program to run.

  6. JavaWelcome.java (note that the name is case-sensitive)

Back to Questions

Variables#

  1. a. valid, but little meaning
    b. valid, but little meaning (do not use for a class name, as it starts with a lowercase letter)
    c. valid, but little meaning (do not use for a variable name, as it starts with an uppercase letter)
    d. valid, good
    e. invalid, has a symbol that is not the underscore, an alpha letter or a number
    f. valid, but don’t use except as a special constant
    g. invalid, starts with a number
    h. valid, good
    i. invalid, reserved word

  2. numStudents

  3. final double INTEREST_RATE = 0.124; (Note: interest rates likely are not changed at run-time, so I’m assuming a constant.)

  4. a. banana will be -24; papaya will be -32
    b. banana will be 4; papaya will be 0
    c. banana will be -24; papaya will be -31
    d. banana will be -25; papaya will be -33

Back to Questions

Visualization#

  1. Here’s a diagram:

    Program visualization picture
  2. Here’s a diagram:

    Program visualization picture

    We don’t know what value is sitting in memory at ruby at this point. Do not assume it to be 0. Make sure that you can explain why the values in jade and emerald differ.

Back to Questions