CMIS 102 Interactive questions

By Nicholas Duchon

Copyright 2014, updated 3/27/2014

Outline:

  1. Acceptable Declarations
  2. Acceptable Assignments
  3. Undeclared or Unitialized Variables?
  4. Programs: Input
  5. Branching Tests

Acceptable Declarations (1)

Are the following declarations acceptable, and if not, why not?
  1. Declare a as integer - Answer:
  2. Declare b as float - Answer:
  3. Declare inputFlag as boolean - Answer:
  4. Declare "this is a wonderful day" as String - Answer:
  5. Declare thisIsTheFirstLetter as character - Answer:
  6. Declare 432 as integer - Answer:
  7. Declare 432 as float - Answer:
  8. Declare 4abc as integer - Answer:
  9. Declare car24 as integer - Answer:
  10. Declare car24WhereAreYou as String - Answer:
  11. Declare car 24 Where Are You as String - Answer:
  12. Declare carA, carB, carC as float - Answer:

Acceptable Assignments (2)

Given the following variable declarations, are the assignment statements below acceptable? Of not, why not?
You should assume that all the variables have been given values before each of these statements is to be executed.
       Declare ma, mb, mc, md as integer
       Declare fa, fb, fc, fd as float
       Declare ca, cb, cc, cd as charater
       Declare sa, sb, sc, sd as String
       Declare ba, bb, bc, bd as boolean
  1. ma = 24
    Answer:
  2. mb = ma / 5
    Answer:
  3. fc = 17.443
    Answer:
  4. mc = 17.443
    Answer:
  5. fd = mb / 8
    Answer:
  6. fa = ma / 17.43
    Answer:
  7. ca = 'r'
    Answer:
  8. ca = '%'
    Answer:
  9. ca = "one"
    Answer:
  10. cb = "8"
    Answer:
  11. sb = "This is my home!"
    Answer:
  12. sc = '2'
    Answer:
  13. sc = "("
    Answer:
  14. sc = "one" + " fine" + " day"
    Answer:
  15. ba = true
    Answer:
  16. bb = false
    Answer:
  17. bc = (ma == 495)
    Answer:
  18. bd = (fa < 23) AND (fb > 449)
    Answer:

Undeclared or Unitialized Variables? (3)

For the following programs, are they valid, or do they suffer from undeclared variable(s), or uninitialized variable(s)?
  1. main
      declare x, y, z as integer
      input x, y
      z = 25*x - 17*y
      write "z is: " + z
    end main
    Answer:
  2. main
      declare x, y, z as integer
      input a, b
      c = 19*a + b%5
      write "c is: " + c
    end main
    Answer:
  3. main
      declare x, y, z as integer
      input x, y
      x = 6*y / (2*x - 8*z)
      write "x is: " + x
    end main
    Answer:

Programs: Input (4)

Write a program using global variables and modules (input, compute and display) to do the following:
  1. Ask the user for 3 (float) numbers, compute their product, and display the result.
    Answer:
  2. Ask the user for a first and last name, and write the name as last name first with a comma between them.
    Answer:
  3. Ask the user for two integers, compute their sum, difference, average and the remainder of the first divided by the second, and display the results of those calculations.
    Answer:

Branching Tests (5)

Given the following program fragments, what are good sets of test values for the variables, and what values for the assigned variables should be expected?
You should also draw a flow chart for these program fragments.
  1. if (x > 5)
      y = 1
    else
      y = 3
    end if
    Answer:
  2. if (x > 5)
      y = 1
    else if (x > 2)
      y = 3
    else if (x > 1)
      y = 22
    else
      y = 98.6
    end if

    Answer:
  3. if (x > 5)
      if (y < 7)
        z = 4
      else
        z = 8
      end if
    else
      if (y >= 17)
        z = 3.33
      else
        z = 3.14
      end if
    end if
    Answer: