CS 430 Midterm Exam

Form A

 

Name ____________________________

 

 

  1. Given the program below  written as Ada code what will be output using

 

with ada.text_io;
  
with ada.integer_text_io;
  
use ada.text_io;
  
use ada.integer_text_io;
   
procedure scope1 is
      a, b: integer;
      
function p return integer is
         a,p : integer;
     
begin -- point 1
         a := 0;
         b := 1;
         p := 2;
        
return p;
     
end p;
                     
      
procedure print is
      begin --  point2
         put(a);
         new_line;
         put(b);
         new_line;
     
end print;
  
     
procedure q is
         b : integer;
     
begin -- point 3
         a:= 3;
         b := 4;
         print;
     
end q;
  
  
begin
      a := p;
      q;
  
end scope1;

 

 

static scope rules

dynamic scope rules

 

 

 

 

 

 

  1. Assume the language  C,  Java, Ada, or Pascal.  Give as precise binding times as you can for the following attributes:

the maximum number of significant digits in a real number

 

the size of an array variable

 

 

the location of a local variable

 

 

the value of a constant

 

 

the location of a function

 

 

 

 

  1. The advantage of short-circuit evaluation of Boolean or logical expressions is that it can prevent certain runtime errors from occurring.  Describe how short-circuit evaluation  behaves with the and operator.

 

 

 

 

 

 

 

  1. Given the following Pascal like pseudo-code examples,  which is the one where short circuit evaluation  can prevent an error from occurring and what error might occur with the other one?

if ( p <> nil  and p^.data  = 10) then

     

 

 

 

 

 

 

if (p^.data = 10 and p <> nil) then

     

 

 

 

 

 

 

 

 

  1. How would the following FORTRAN statement be written in Pascal? 

                            GO TO (53, 42, 1, 13, 20) X

 

 

 

 

 

 

 

 

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

 

 num := 5;

 for i in 1..num loop

    ada.integer_text_io.put (i);

    num := 3;

  end loop;

 

 

 

  1. How many times would i be output if the corresponding Pascal loop were executed?

 

 

 

 

  1. For each of the following parameter passing mechanisms, tell a language that uses it.

 

parameter passing by value

 

 

parameter passing by reference

 

 

parameter passing by result

 

 

parameter passing by value-result

 

 

 

 

  1. Tell what it means for a grammar to be ambiguous.

 

 

 

 

 

  1. Prove that the following grammar is ambiguous 

 

<S> ::= <A>

<A> ::=  <A> + <A>

<A> ::=  <id>

<id> ::=  a | b | c

 

 

 

 

 

 

 

  1. What are three (3) capabilities  we gain from studying a variety of programming languages?

 

 

 

 

 

 

 

 

 

 

 

  1. In order for high level programming languages to be run on computers they must be translated into the machine language of the computer they are running on. 

 

What are the two methods of translation we have talked about? 

 

 

 

 

 

 

 

Describe the major difference between them.

 

 

 

 

 

  1. Assuming that X has been declared to be a floating point  number  and assigned the value  98.7  and that N has been declared to be an integer and assigned the value 25,

 

What is the result of adding  X and N  and storing the result in N in  FORTRAN? 

Describe what happens

 

 

Tell why it happens

 

 

What is the result of adding  X and N  and storing the result in X in  Ada? 

Describe what happens

 

 

Tell why it happens

 

  1. When using files in Pascal,  if you neglect to close the files when you are done with them, what problem may occur?

 

 

 

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

 

 

 

 

 

 

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.    

statement:

 

 

comment:

statement:

 

 

comment:

 

 

  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?

 

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, F8.3, 1X, I5, 1X, F3.1)

              STOP

             END

X

Y

I

Z

 

Output

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. Name and briefly describe what happens in the two phases of the Syntax Analysis phase of the compilation process?

 

 

 

 

 

 

  1. What was the goal of the developers of XSL?

 

 

 

 

 

FORTRAN and Pascal are examples of procedural or imperative languages.  What type of language is  XSLT?

 

 

 

 

 

 

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

reserved words:

 

 

 

key words:

 

 

 

 

Which does each of the following languages use?

 FORTRAN

 

 

 Pascal

 

 

Ada

 

 

Java

 

 

 

 

 

  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.

Language

 

feature

opinion