CS 430 Midterm Exam

Form B

 

Name _________KEY___________________

 

 

  1. Given the program below written as Pascal code what will be output using           12 points

 

             procedure scope2 is

                    a, b: integer;

                    function p return integer is

                          a : integer;

                          begin –- point 1

                                    a := 0;

                                    b := 1;

                                    return 2;

                      end p;

                     

                           procedure print is

                         begin --  point2

                             put(a);

                                    new_line;

                                    put(b);

                                    new_line;

                                    put(p);

                                    new_line;

                             end print;

                  

                             procedure q is

                                    b, p : integer;

                              begin – point 3

                                  a:= 3;

                                         b := 4;

                                  p := 5;

                                         print;

                             end q;

 

                        begin

                             a := p;

                                    q;

                             end scope2;

 

static scope rules

 

 

3

1

2

dynamic scope rules

 

 

3

4

5

 

 

 

 

  1. What type of binding generally occurs at                                  5 points

language design time

type

 

language implementation time

size of a type (i.e. # of bits)

 

compile time

name

type

load time

location

 

run time

value

location of subprograms

 

 

  1. Rewrite the ada case statement below, as a Java case statement                       7 points

 

case x-1 is

  when 0 =>

     y := 0;

     z := 2;

  when 2..5 =>

      y := 3;

      z := 1;

  when 7 | 9 =>

      z := 10;

  when other =>

      null;

end case;

 

 

switch (x-1)

{

  case 0:  System.out.println (x);

           break;

  case 2:

  case 3:

  case 4:

  case 5: System.out.println (x);

          break;

  case 7:

  case 9: System.out.println (x);

          break;

  default:  System.out.println (" default ");

            break;           

} // end switch

 

 

 

 

  1. How many times would i be output if the following Ada loop was executed?          5 points

 

 num := 5;

 for i in 1..num loop

    ada.integer_text_io.put (i);

    num := 3;

  end loop;

 

 

5

 

  1. How many times would i be output if the corresponding Java loop was executed?

5 points

 

3

 

  1. How do each of the following languages pass their parameters?                        9 points

FORTRAN

by reference

Java

by value

   Pascal

by reference or by value

   Ada

by reference, by value, by result, by value/result

 

  1. Given the following grammar:  G = ( {S,A}, {0,1}, P, S)  which represented by the  production rules below  where is the empty string (which has length 0.)

 

  S      0A1

  A     0A1

  A      

 

Generate 3 valid sentences in this language.                                                      6 points

             S   

         /   |   \

        0    A   1

              |

            

        

                   01

             S   

         /   |   \

        0    A   1

           / | \

          0 A  1

              |

            

 

0011

 

 

 

             S   

         /   |   \

        0    A   1

           / | \

          0 A  1

           / | \

           0 A 1

              |

             

 

000111

 

 

  1. Tell in words what the sentences in this language look like.                   4 points

 

At least one 0 followed by one 1 but may be any number of 0’s followed by an equivalent number of 1’s 

OR   0n1n  where  n >= 1  

 

 

  1. What are three (3) desirable characteristics of a programming language?    6 points

readability  (which includes overall simplicity, orthogonality, control structures, data types & structures and syntax)

writability (which includes simplicity and orthogonality, support for abstraction, expressivity)

reliability (which includes type checking, exception handling, aliasing, readability and writeability)

cost (of training programmers, of writing programs, of compiling, of executing, of the language implementation systems, of poor reliability, of maintaining programs)

 

 

 

 

  1. The two common methods of translation from high level programming language to machine language are compilation and interpretation.   Tell what methods are usually used by each of the following languages:                                                                 4 points

FORTRAN

compilation

Java

compilation and interpretation

Pascal

compilation

 

  1. Pascal has a structured data type called a record.    FORTRAN IV’s only structured data type is an array.   Given the following Pascal declaration for an array of records, write the necessary FORTRAN declarations to manipulate the same data.      6 points

 

InfoRecord = Record

      number : integer;

      earnings, tax : real;

end;

EmployeeRecords = Array [1..100] of InfoRecord;

dimension number (100)

dimension earnings (100)

dimension tax (100)

 

 

 

 

 

  1. What are the five basic types of statements that we generally expect to find in a programming language.                                                          5 points

input

output

assignment

iteration

selection

 

  1. For two (2) of these types of statement, provide a syntactically correct example in FORTRAN, Pascal or Ada  and write a comment explaining the syntax/behavior of the statement.                                                                                 8 points

       

statement:

FORTRAN assignment

x = 5.2

comment:

the value 5.2 is stored in the location referenced by x

statement:  Pascal  output statement

writeln (x);

 

comment:

the value stored in the location referenced by x will be retrieved and printed to the screen.

 


 

  1. Given the following FORTRAN code and a line of input consisting of the digits shown below with no spaces between them ,  what would be stored in the variables and what would the output be?                                                                           4 points

1234567898765432123456789

 

           READ (5, 13)  X, Y, I, Z

  13       FORMAT (F3.2, 1X, F7.3, I4, F2.1)

            WRITE (6, 14) X,Y,I,Z

  14       FORMAT (1X, F5.2, 2X, F7.2, 1X, I5, 1X, F3.1)

              STOP

             END

X

1.23

Y

5678.987

I

6543

Z

2.1

 

Output                                                                          5 points

 

 

1

.

2

3

 

 

5

6

7

8

.

9

9

 

 

6

5

4

3

 

2

.

1

 

 

  1. What is a potential problem that occurs when using DOS redirection to send a program’s output to a file?

the default for output is to the screen.  Thus, all of the output will go to the file, even the prompts

 

 

  1. What is the difference between reserved words and key words in a programming language?

                                                                                                      4 points

reserved words:

 

are words that may not be used as identifiers

 

 

key words:

 

may be used as identifiers.  their meaning is determined by context.

 

 

Which does each of the following languages use?                               4 points

 FORTRAN

key words

 

 Pascal

reserved words and pre-defined terms

 

Ada

reserved words

 

Java

reserved words

 

 


  1. What is XML?                                                               1 point

 

 

What is the output of an  XLST program?                                  1 point

 

 

 

  1. What are the steps (phases) in the compilation process when going from a source program to an object program?                                                                        3 points

 

   Syntax analysis (consisting of lexical analysis and parsing)   and translation

 

  1.  What is the major difference between the two common types of subprograms found in programming languages (e.g. FORTRAN's function and subroutine)

4 points

functions return a single value – their parameters are most often passed by value (i.e. copy)

procedures or subroutines return 0, 1, or many values.  Their values are returned through the parameter list and their parameters are generally passed by reference.

 

  1. Each of the languages we have looked at so far (Java, Pascal, FORTRAN, Ada)  has some features that you may like and others that you may dislike.   Pick a  language and a feature and tell what you like or dislike about it.                                             8 points

Language

feature

opinion