Document 10805473

advertisement
Short Quiz 7 1) Consider the following type system written in C programming language, how many subtypes does the type Structure1 contain? Please specify them and draw how the data structure is stored? struct Structure1 { char c; int i; float f; double d; void func(); }; Ans: 5 2) Consider the following type system written in C++ programming language, how many subtypes does the type X contain? Please specify them and please draw how the data structure is stored? class X .stack { int i; public: X(int ii = 0); void modify(); .heap }; X::X(int ii) { i = ii; } .static data int i void X::modify() { i++; } .text X::X(int ii) X::modify() Ans: 3 Now we consider a class, not an object of X ~~~ 3) Consider the following type system written in Java programming language, how many subtypes does the type List contain? Please draw how the data structure is stored? class List { .stack private Object [] items; private int numItems; public List() .heap { items = new Object[10]; Object[] items numItems = 0; Int numItems } public void AddToEnd(Object ob) { .static data } } .text Ans: 3 Now we consider a class, List() Not an object of List !!! ~~~ AddToEnd() 4) If we have following rules ! ! t1 : T11 ! ! t2 : T11 " T12
! ! t1t2 t3 : T12
! ! t3 : T13
Is the following term/lambda function typeable ? Why? (! r : {x : Nat, y : Bool}! r.y){x = 10, y = 11} Ans: No. the statement is supposed to produce follow types: { Nat, Bool } -­‐> Bool so except for above rules, we need rules of subtypes, and input type of x is incorrect too. 5) Consider the above type systems written in C/C++/Java programming language, please answer where the imperative objects of those type systems are stored before initialization and after initialization ? (Tip: static memory? Or heap? Or stack? ) Ans: before initialization, all data fields are stored in static data section, and functions are stored in text section, therefore we call them class/structure. After initialization, data fields except for static data fields, which are still stored in static section. All other data fields are stored in heap. Place where functions are stored not changed. January
15, 2000
14. I MPERATIVE O BJECTS
6) Assume we have following definition of a class (This need to explicitly annotate terms to control the way their ty
= ! c : Counter(c ! inc unit; c ! inc unit; c ! inc unit; c ! inc unit );
is myinc
a little
annoying. It is the price we pay for “unbundling” all th
programming
language into the simplest and most orthogonal p
that
we
can
study
them
in isolation.
If we write down following instructions, what is tA
he practical
result ? high-level program
( c.set 9; would
myinc c; probably
c.get unit); combine many of these primitive feature
design
venient
macro-constructs.)
Ans: A typical piece of “client code” that manipulates counter ob
the answer is like
an object which has following signature: something
this:
C.r = 10 The function
takes a counter object and increments it three t
messages in succession.
it three
7)What is the x and y value of following expressions in SmallTalk ? | x y | “ this is the beginning of the program ” x: = 775 “ set up os flag “ y:= $t “ now store a value ” Ans: x = 775 , y= value of variable t 8) What is the result of following variable x ? theData = {“A”=>”B’, “B”=>”C”, “C”=>”D”,”D”=>”E”} x = theData[“C”] Ans: “D” 9) What is the value of following array ? theArray := ($x $y $z 101 202 303). Ans: ‘x’, ‘y’, ‘z’, 101, 202, 303 10) How many subtypes in following type system( actually a SmallTalk class) ? What are they? Which is base class and which is derived class ? Payroll subclass: #Payment instanceVariableNames: ‘ eType salary bonus ‘ classVariableNames: ‘ ‘ poolDictionaries: ‘ ‘ ! Payment publicMethods ! eType: aString eType := aString ! eType ^eType salary: n salary := n ! salary: ^ salary ! bonus: bonus := n ! bonus: ^bonus ! ! Ans: 9 fields. Base class is Payment, and derived class is Payroll. 
Download