2025 CSCI-2041 Midterm 2 Exam Review Questions with
Correct Answers | 100% Pass Guaranteed | Graded A+ |
Administrator
[COMPANY NAME] [Company address]
, # Midterm 2 Practice Questions
*CSci 2041: Advanced Programming Principles, Fall 2015*
These are some examples of questions similar to those that will
appear on the second midterm.
##1. Program representation and analysis
+ Recall our data types for representing programs and the associated
analysis functions:
```
type expr =
Const of int | Bool of bool
| Add of expr*expr | Mul of expr*expr
| If of expr*expr*expr | Gt of expr*expr | Eq of expr*expr
| Set of string*expr | Name of string
| While of expr*expr | Seq of expr list
| Print of expr | Readint
type result = IntR of int | BoolR of bool | UnitR
type expType = IntT | BoolT | UnitT
type state = (string*result) list
type tyEnv = (string*expType) list
val typeOf : expr -> tyEnv -> expType = <fun>
val eval : expr -> state -> (result * state) = <fun>
```
+ Suppose we were to extend the above representation to allow integer
array values in the program (internally represented as lists of
integers), by adding the following variants to expr:
```
type expr = ...
| NewArr of expr*expr (* NewArr(n,v) makes a new array of n ints set to v *)
| ArrGet of expr*expr (* ArrGet(a,i) returns the item at index i of array a *)
| ArrSet of expr*expr*expr (* ArrSet(a,i,v) returns a copy of a with index i
set to value v *)
| ArrLen of expr (* ArrLen(a) returns the length of array a *)
```
What other types would we need to change in order to evaluate and
typecheck expressions with these features?
+ Recall that a "type judgement" is a rule for assigning a type to an
expression given the types of its subexpressions, e.g:
+ ` e1 : Int , e2 : Int `⇒ `Add(e1,e2) : Int`
+ `Const i : int`
+ `n : t, e : t` ⇒ `Set(n,t) : Unit`
Assuming that array values have type `Array`, what are the type
judgements for each of the new expression variants above?
+ Using these judgements, write the match clauses for each of the new
variants in `typeOf`.
+ Complete the match clasues in `eval` for each of the new variants in
`typeOf`. We'll make accessing an array at an out of bounds index
result in a runtime exception (e.g. in this situation `failwith
"array index out of bounds"` would be an acceptable result of evaluation)
+ Explain the difference between *static* and *dynamic* program
analyses, and give an example of each.
##2. Induction
Consider the following tree data type:
https://www.coursehero.com/file/13271013/midterm2-practicemd/