SE323 Final&As
SE323 Final&As
QUESTION:
Define the terms exception handling and continuation. List three continuation possibilities in
language exception design. How does C++/Java address continuation design issue?
ANSWER
The special processing that may be required when an exception is detected is called exception handling.
After an exception handler executes, either control can transfer to somewhere in the program outside of
the handler code or program execution can simply terminate. We simply call this continuation.
1. Termination
2. Resume reexecuting the statement that raised the exception,
3. Resume with the statement after the statement that raised the exception
C++/Java Continuation:
After a handler has completed its execution, control flows to the first statement following the try construct
(the statement immediately after the last handler in the sequence of handlers of which it is an element).
class B extends A {
public int x;
B() { x = 5; }
public int getx() { return x; }
}
A a = new B();
(2 pts)
class P <E> { ANSWER:
private E x;
public E getx() {return x;}
public P(E arg) {x = arg;}
}
CHAPTER 11 ADT
QUESTION:
Implement a ADT called SafeBox to store only ONE valuable: Gold. Write two constructors to create an
empty SafeBox or with one valuable in it. One can add or remove valuable to the SafeBox. Write a
main to create a Gold class and store SafeBox. Do not do any error checking.
ANSWER:
class Gold {
private int grade; // 18, 22, 24
public Gold (int c) { grade = c;}
int getGrade() {return grade;}
}
QUESTION:
Add Exception Handling to the remove method. If there is no item in the SafeBox and you want to
remove something raise an exception and handle it.
ANSWER:
package safeBoxNoGeneric;
import java.lang.NullPointerException;
public class SafeBox {
private Gold element;
public SafeBox (Gold arg) { element = arg;}
public SafeBox () {element = null;}
Gold remove () {
if (element == null) {
throw new NullPointerException();
}
Gold val = element; element = null; return val;
}
void add (Gold arg) { element = arg;}
public static void main(String[] args) {
Gold g = new Gold(18);
void fun1() { The calling sequence for this program for execution to
float a; reach fun2 is
. . .
main calls fun3
}
void fun2() { fun3 calls fun3
int b, d; fun3 calls fun1
. . . <-------- 1 fun1 calls fun2
}
void fun3() {
float d;
}
void main() {
char a, *f, g;
. . .
}
ANSWER:
QUESTION: Show the stack with all activation record instances, including static and dynamic chains, when
execution reaches position 1 in the following skeletal program. Assume Bigsub is at level 1.
QUESTION: Give the (chain_offset, local_offset) pairs for all the variables visible at position 1.
procedure Bigsub is
MySum : Float;
procedure A is
X : Integer;
procedure B(Sum : Float) is
Y, Z : Float;
begin -- of B
. . .
C(Z)
. . .
end; -- of B
begin -- of A
. . .
B(X);
. . .
end; -- of A
procedure C(Plums : Float) is
begin -- of C
. . . <------------------ 1
end; -- of C
L : Float;
begin -- of Bigsub
. . .
A;
. . .
end; -- of Bigsub
ANSWER:
CHAPTER 9 Subprograms
QUESTION: Consider the following program written in some language using C-like syntax:
void exchange(int n1, int n2) {
int store;
store = n2;
n2 = n1;
n1 = store;
}
void main() {
int value = 4, list[5] = {7, 6, 3, 1, 2};
exchange(value, list[0]);
exchange(list[2], list[3]);
exchange(list[value], value);
}
For each of the following parameter-passing methods, what are all of the values of the variables value
and list after each of the three calls to exchange? Use the initial values of list and value before
every call to exchange.
a) Pass-by-value
b) Pass-by-reference
c) Pass-by-value-result
ANSWER:
When an expression of one type is used in a context where a different type is expected, one normally
gets a type error. BUT Many languages allow things like this.
int a; double b, c;
...
c = a + b;
type conversions (explicit)
type coercions (implicit)
float stuff[100];
float *p;
p = stuff;
The terms STATIC and DYNAMIC are generally used to refer to things bound before run time and at
run time, respectively.
With STATIC (LEXICAL) SCOPE RULES, a scope is defined in terms of the physical (lexical) structure of
the program.
Most programming languages today employ static scope rules.
With DYNAMIC SCOPE RULES, bindings depend on the current state of program execution. They
cannot always be resolved by examining the program because they are dependent on calling
sequences.
example: static vs dynamic scope rules
int a
procedure first:
a := 1
procedure second:
int a
first()
a := 2; second(); write(a)
If static scope rules are in effect, the program prints a 1. If dynamic scope rules are in effect, the
program prints a 2.
A binding is an association between two entities, such as a name and the thing it names.
Binding Time is the point at which a binding is created or, more generally, the point at which any
implementation decision is made.
Examples include
language design time
program structure, possible types
language implementation time
binding type to a representation. i.e. floating point
compile time
bind a variable to a type
link time
layout of whole program in memory
load time
choice of physical addresses
run time
value/variable bindings, sizes of strings
The period of time from creation to destruction is called the LIFETIME of a binding. The textual
region of the program in which the binding is *active* is its SCOPE.
CHAPTER 4 Lexical and Syntax Analysis
Lexical Analysis, Parsing
Lexical Analyzer is responsible for tokenizing the source program.
Lexical Analyzer can be built based on Finite State Automata.
A parser is a language *recognizer*.
Top down Parsing: Recursive descent parser
CHAPTER 3 Describing Syntax and Semantics
BNF, Ambiguity, Precedence, Associativitiy
Terminology:
Context Free Grammar
symbols
terminals (tokens)
non-terminals
start symbol
production
derivation (see example below)
left-most
right-most
sentential form
It's particularly important that it be UNAMBIGUOUS -- no two parse trees for the same string.
<expr> -> <factor> | <expr> - expr> (ambiguous)
phases of compilation:
character stream
scanner (lexical analysis)
token stream
parser (syntax analysis)
drives the whole process
parse tree
semantic analysis
abstract syntax tree (AST)
intermediate code generation
intermediate form of some sort
optimization
target code
machine code