next up previous
Next: About this document ...

CISP365 Practice Midterm 2

Prof. Tak Auyeung

Instructions: You may bring any material that is handwritten or printed prior to the examination to help you. You can also bring a calculator if you think it may help you. However, you can only use the calculator for numerical computations only. You cannot let your calculator compile a program, or to communicate with others.

You, as an individual, are expected to do your own work. This means you cannot seek, receive or otherwise acquire any assistance except clarifications from the professor during an examination. Any communication involving the contents of the subject matter or the examination is considered cheating. Do not initiate or accept such communication, or the result of your examination is automatically voided.

New rules, read this! As of 2003.09.22, I no longer deduct points for wrong answers. Each correct answer is worth one point, each wrong answer is worth zero point, and each unanswered question is also worth zero point. This means you should guess and leave no question unanswered.

As a result, I also need to adjust the letter grade assignment break points. For your individual examinuation, ``A'' means at least 94%, ``B'' means at least 74%, ``C'' means at least 54%, ``D'' means at least 34% and ``F'' means below 34%. The break points for the final grade are now 26.83%, 48.5%, 70.17% and 91.83% as minimums for ``D'', ``C'', ``B'' and ``A'', respectively.

Please note that this change does not affect your letter grade at all, it is just a number game to make some people feel better about guessing.

Make sure you write down you name on the upper right corner first, otherwise I cannot give points to anonymous students!

The baseline is XX, there are XX questions.

  1. What integer value is returned by the following function f when invoked as f('00231')?

    function f(s : string) : integer;
      var
        i : integer;
        x : integer;
      begin
        x := 0;
        for i := 1 to length(s) do
          if (s[i] >= '0') and (s[i] <= '3') then
            x := x * 4 + (ord(s[i]) - ord('0'));
        f := x
      end;
    
    1. 231
    2. 45
    3. $4^5$
    4. $4^6$
    5. 142
  2. Observe the following program:

    type
      n = record w, x : integer end;
    var
      a : array [0..4] of n;
      i,j : integer;
    begin
      for i := 0 to 4 do
        read(a[i].w, a[i].x);
      j := 0;
      for i := 0 to 4 do
        begin
          write(a[j].w,' ');
          j := a[j].x
        end
    end.
    

    Given that the input to this program is as follows:

    23 4 102 2 34 3 32 0 299 1
    

    What is the output of this program?

    1. 4 3 1 2 0
    2. 2 3 4 0 1
    3. 23 299 23 299 23
    4. 23 299 102 34 32
    5. 102 23 34 299 32
  3. What is the value returned by f('this is insane!','s'), given that the function is defined as follows?

    function f(s : string; c : char) : integer;
      var i, x : integer;
      begin
        x := 0;
        for i := 1 to length(s) do
          if s[i] = c then
            x := x + 1;
        f := x
      end;
    
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
  4. What is the printed output of the p('vashTheStempede'), given that procedure p is defined as follows?

    procedure p(s : string);
      var i : integer;
      begin
        for i := 1 to 6 do
          if i <= length(s) then
            write(s[i])
          else
            write('*')
      end;
    
    1. vashTh
    2. vashTh*********
    3. *********empede
    4. vashTheStempede
    5. 123456
  5. Observe the following definitions:

    type
      t1 = 
        record
          a : array [0..9] of integer;
          b : string [20];
          c : integer
        end;
      t2 =
        record
          x : array [0..29] of t1;
          y : array [0..29] of integer;
          z : integer
        end; 
    var
      v1 : t2;
    

    Which of the following expression is not valid? Some expressions may not return a built-in type, but they are still syntactically correct as parameters.

    1. v1.z
    2. v1.y[2]
    3. v1.x[v1.y[v1.z]]].b[2]
    4. v1.a[length(v1.b)]
    5. v1.x
  6. Observe the following definitions:

    type
      t1 = array [0..9] of integer;
      t2 = array [0..2] of t1;
    var
      v1 : t2;
    procedure p1(var x : array of integer);
      begin
        (* ... whatever p1 does *)
      end;
    

    Which of the following invocation does not generate a compile-time error?

    1. p1(v1[2])
    2. p1(v1)
    3. p1(v1[2][1])
    4. p1(v1.t2[0])
    5. p1(v1[1][2][1])
  7. Which of the following procedures open a file already associated with a file variable for reading only?
    1. rewrite
    2. append
    3. assign
    4. close
    5. reset
  8. When a file is closed by the close procedure, what does it mean?
    1. The file is deleted
    2. The file is ready for reading
    3. The file is ready for writing
    4. The program has finalized its access to the file
    5. The file is reset to be have a size of zero
  9. In the following program, what can replace ____ so that the program compiles without any errors?

    (* ... a whole bunch of user defined types *)
    var
      outfile : text;
      x : ____;
    begin
      (* ... to initialize infile for writing *)
      write(outfile, x);
      (* .. other operations)
    end.
    
    1. only boolean
    2. only integer
    3. only real
    4. can be any one of the above
    5. can be any type, including user defined ones
  10. Global variables should not be accessed directly because they can be shared among subroutines, causing much trouble. What is the output of the following program?

    var
      i : integer;
    procedure p1(x : integer);
      begin
        i := x;
        while (i > 0) do
          begin
            write('*');
            i := i - 1
          end;
        writeln
      end;
    
    procedure p2(x : integer);
      begin
        i := 0;
        while (i < x) do
          begin
            p1(i+1);
            i := i + 1
          end
      end;
    
    begin
      p2(3)
    end.
    
    1. ***
    2. *
      **
      ***
      
    3. ***
      **
      *
      
    4. this program does not terminate, it keeps printing **
    5. this program does not terminate, it keeps printing *
  11. What is the output of the following code, assuming variable x has a value of 27?

    if x < 20 then
      writeln('do')
    else if x < 25 then
      writeln('re')
    else if x < 30 then
      writeln('me')
    else if x < 35 then
      writeln('fa')
    else 
      writeln('so')
    
    1. do
      
    2. re
      
    3. me
      
    4. do
      re
      
    5. me
      fa
      so
      
  12. What is the largest value that can be represented by a 16-bit integer variable?
    1. 0
    2. 16
    3. 32768
    4. 32767
    5. 65536
  13. Will the following code compile (despite warnings)? If not, why is that? If it compiles, what is the output of this program?

    var
      bad_global : boolean;
    function f : boolean;
      begin
        f := not bad_global
      end;
    begin
      if f = bad_global then
        writeln('weird')
      else
        writeln('strange')
    end.
    

    1. it does not compile because f = bad_global should have been bad_global = f
    2. the program compiles, but it crashes when it runs
    3. the program compiles, it prints weird
    4. the program compiles, it prints strange
    5. the program compiles, what it prints depends on the value of the uninitialized variable bad_global
  14. What is printed when the following program executes?

    function f1 : integer;
      begin
        f1 := 4
      end;
    
    function f2(x : integer; y : integer) : integer;
      begin
        f2 := f1 * x + y
      end;
    
    begin
      writeln(f2(f2(f1,2),f1+2))
    end.
    
    1. 24
    2. 52
    3. 78
    4. 422
    5. 424
  15. What is the output of the following code, given that x, y and z are integer variables?

    while (x < y) and (y < z) and (z < x) do
      begin
        writeln('*');
        x := x + 1;
        y := y - 1
      end;
    

    1. this is an infinite loop, it keeps printing *
    2. there is a syntax error with this code
    3. this code crashes
    4. this code prints some finite lines of *, but the number of such lines is determined by the values of the variables
    5. this loop is not infinite, and nothing is printed
  16. Given that b is a boolean variable, i and j are integer variables, which of the following statement does not compile?
    1. b := j < i;
    2. b := b or (i <> j);
    3. if b then i := j;
    4. 16a and 16c
    5. all of 16a, 16b and 16c compile
  17. Choose the correct constant $n$ (represented as a decimal number) in the following equation:

    $101.11010_2 = n \times 1.0111010_2$

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
  18. Given that an array of 32 elements is sorted, what is the maximum number of iterations in the loop of binary search needed to confirm that a value does not exist in the array?
    1. 32
    2. 16
    3. 6
    4. 5
    5. 4



next up previous
Next: About this document ...
Tak Auyeung 2003-12-10