[ Home | Syllabus | Course Notes | Assignments | Search]
Rules of taking this test
Honor Pledge: I pledge that I have neither given nor
received unauthorized help in taking this exam. Dr. Wild Signature: __________________________________________________________ Printed Name: __________________________________________________________
|
. Net contains a garbage collection system
a) Suppose you are running a small .NET program on a computer with a lot of
memory so that there is no garbage collection performed while the program is
running. Are there any circumstances where such a program could get into
trouble? justify your answer (5)
////
Couple of points
1) a small program does not mean is doesn't use a lot of memory (e.g. int
a[100000000000];)
2) If you require more memory than is available (real and virtual, managed
or unmanaged), you will get a memory exception
3) Garbage collection in .NET only reclaims space used by obsolete MANAGED
objects.
4) if managed objects include unmanaged objects - then freeing the managed
object also frees the unmanaged object (by calling the finalize method)
5) running out of memory for managed objects starts garbage collection.
running out of memory for unmanaged objects does NOT trigger .NET's garbage
collection
Problem 1) If the program uses a lot of unmanaged resources (such as bitmap, graphic
devices) it is possible it could run out of these resources which may be
owned by obsolete managed objects that have not yet been garbage collected.
//// If you say there is no problem -2 asking for unmanaged kernel objects
could lead to out of resource condition
//// if say .NET loses objects or has dangling pointers -3
//// if you say problem with infinite recursion if no garbage collection -3
//// if does not consider the case where there are no coding errors and
unmanaged resources are released in the finalize method but there still is a
problem -2
Problem 2) use of file handles could cause problems if try to open again
before previous object is garbage collected
b) The imageview
(fig 4.11) program contains the following code
Bitmap bitmap = new Bitmap (FileName);
if (MyBitmap != null)
MyBitmap.Dispose (); // Important!
MyBitmap = bitmap;
//// Would the program still work if you replaced this code with the following statement? explain any differences in behavior if any (5 points)
MyBitmap = new Bitmap (FileName);
//// this is an instance of not
explicit freeing the unmanaged resource which a bitmap object wraps, thus it
is possible to run out of space for the underlying unmamanged "bitmap"s
this space is NOT managed by the garbage collector and thus it is possible
to run out and NOT trigger the managed object garbage collection process..
From the book
"The final item in this list is an important one because without
it, ImageView would leak memory like a sieve. Rather than open an image file
and assign the Bitmap reference to MyBitmap
in one statement, like this:
MyBitmap = new Bitmap (FileName);
ImageView does this:
Bitmap bitmap = new Bitmap (FileName); if (MyBitmap != null) MyBitmap.Dispose (); // Important! MyBitmap = bitmap;
See what’s happening? Bitmap
objects encapsulate bitmaps, which are unmanaged resources that can consume
a lot of memory. By calling Dispose on
the old Bitmap after opening a new one,
ImageView disposes of the encapsulated bitmap immediately rather than wait
for the garbage collector to call the Bitmap’s
Finalize method."
//// if say program no longer works correctly -5 -
since it will work fine as long as there is memory - if you explain unmanaged
objects only take 2 off if say does not work
//// if say no change -2
//// if don't discuss that bitmaps are unmanaged objects -2
//// if say garbage collect will not take place -3
//// if say unmanaged resources are not garbage collected - that depends on
whether the wrapping class frees the unmanaged resource in its finalize
method which BitMap objects do -2
//// if say there is a memory leak but do not clarify that garbage
collection is not triggered for a memory leak of unmanaged resources -2
----- the following applies to both parts
//// If you don't distinguish managed and
unmanaged objects -3
//// if don't distinguish that out of memory for managed objects triggers GC
but for unmanaged objects does not (This is the critical argument for both
parts of this question
//// you can gain or loss points for correct or incorrect statements about
this area that have not be identified above- this question is a test of your
understanding of managed/unmanaged objects and garbage collection - when it
occurs and what triggers it
//// for example - discussion of Dispose - dealing with non-deterministic
garbage collection
CIL Given the following program in CIL (Common Intermediate
Language),
a) generate a plausible c# program that might have generated this
program (this process is called "reverse engineering") Your
solution should be able to be compiled. (8)
public class class1{ ////should be class1 by comment -2 public class1() { //// not necessary } static void Main() { //// must be Main -1 int x; //// if wrong order -1 int a = 3; //// if wrong values -1 int e = 5; int b = 71; int z = 2; int d = 6; x = ( a * e * (b + ( d * z /3))); -3 //// x has the value 1125 } } b) What observations can you make about the architecture of CIL? (2)
1) addressing of first four local variables different (seems to have registers for first four variables and shorter instructions to store and load them) 2) short constants handled differently than longer ones (1 location vs two) //// must have one of the two above plus another one.
.method private hidebysig static void Main() cil managed { .entrypoint // Code size 28 (0x1c) .maxstack 4 .locals init ([0] int32 x, [1] int32 a, [2] int32 e, [3] int32 b, [4] int32 z, [5] int32 d) IL_0000: ldc.i4.3 IL_0001: stloc.1 IL_0002: ldc.i4.5 IL_0003: stloc.2 IL_0004: ldc.i4.s 71 IL_0006: stloc.3 IL_0007: ldc.i4.2 IL_0008: stloc.s z IL_000a: ldc.i4.6 IL_000b: stloc.s d IL_000d: ldloc.1 IL_000e: ldloc.2 IL_000f: mul IL_0010: ldloc.3 IL_0011: ldloc.s d IL_0013: ldloc.s z IL_0015: mul IL_0016: ldc.i4.3 IL_0017: div IL_0018: add IL_0019: mul IL_001a: stloc.0 IL_001b: ret } // end of method class1::Main
Consider the following code
using System; struct Point { public int X; public int Y; public Point (int x, int y){ this.X = x; this.Y = y; } } class Rectangle { public Point CornerTopLeft = new Point(); public Point CornerBottomRight = new Point(); public Rectangle () {} } class Class1 { static void Main(string[] args) { Point p1 = new Point(4,3); Point p2; p2 = p1; p1.X = 56; p1.Y = 85; Rectangle r1 = new Rectangle(); Rectangle r2; r1.CornerTopLeft = p1; r1.CornerBottomRight = p2; r2 = r1; r2.CornerTopLeft = new Point(1,0); Console.WriteLine("({0},{1})",p2.X,p2.Y); Console.WriteLine("({0},{1})",p1.X,p1.Y); Console.WriteLine("({0},{1}) and ({2},{3})", r1.CornerTopLeft.X,r1.CornerTopLeft.Y, r1.CornerBottomRight.X,r1.CornerBottomRight.Y); Console.WriteLine("({0},{1}) and ({2},{3})", r2.CornerTopLeft.X,r2.CornerTopLeft.Y,r2.CornerBottomRight.X,r2.CornerBottomRight.Y); } } a) what will this program print? (5)
(4,3) (56,85) (1,0) and (4,3) (1,0) and (4,3) b) explain why the program behaves this way (5) value-type for "struct Point" and reference-type for "class Rectangle" (3 points). gives deep vs shallow copy.(2 points) //// Do not distinguish between memory allocation behavior of value type and reference type -3 //// Do not mention deep vs shallow copy -2
Consider figure 4.11 (the imageview
program)
a) There are three calls to "invalidate" in this program. Describe
the changes in behavior if they are removed (describe each effect separately)
(3)
1) after dialog returns
"OK" - if removed -
depends on mode size of previous image and new image
a) FIT TO WINDOW will redraw part covered by dialog box.
b) native size as above except//// when adding scroll bars shows entire
image - or taking away
2) OnFitToWindow
handler
Only part under dialog box will be redrawn
3)OnNativeSize
handler
Only part under dialog box will be redrawn
////
must say only part on dialog box redrawn (2)
//// must have one of depends on scroll bars, size (that is something else
that forces a redraw implicitly (1)
b) Is there a way to see the image even though these "invalidate"
statements are removed? Explain (3)
//// cover/uncover, minimize/un-minimize
//// if does not give a non-program way -1
c) Change this program to remove the OnPaint function and instead write code
to draw the image in place of the removed "Invalidate" statements.
(4)
//// does not have graphics device - would not compile -3
//// opens in fit to window (1)
//// opens in native (1)
//// switch between native and fit (1)
//// switch between fit and native (1)
////
does not show native size if need scroll bars
Suppose you have a plain text file containing
"key=value" pairs one per line (e.g. Telephone=(757)683-4679).
Write a program which will extract all the key/value pairs putting them into
a Hashtable using a regular expression to extract the information from each
line of the text file. Then print all values whose key is "Telephone".
Also use a regular expression to verify that the value has the proper format
(that is area code in parentheses, a single
dash ('-') before the last four digits. there can be any number of spaces
after the right parenthesis - for example (757) 683-4679
is valid, (757) 6834679 and (757) callodu are not valid). If the value does
not have the proper format, print the
message "does not have the correct format" after the value. (10)
-------
Clarification: a Hash Table only allows one entry for a given key. I will
accept any reasonable solution that using a regular expression to verify the
format of the phone number value. You can verify before or after the hash
table entry. Your solution should not crash if there is a multiple entry.
//// regular expression to extract key/value pairs (3)
//// regular expression to validate phone number Regex reg= new Regex("^\\([0-9]{3}\\)[ ]*[0-9]{3}-[0-9]{4}$");
(4)
//// allows junk characters at end -1
//// allows junk characters at beginning -1
//// does not require parenthesis -1
//// Use of hashtable (2)
//// If don't store keys other than telephone -2
//// If validate non telephone values -1
//// does not verify all phone numbers if duplicates -1
//// Other (1)
//// if allow multiple entries with same key +2
//// if won't compile -3
//// should handle lines which do not contain
"="
MyComicsDataList.aspx (figure 6.7), contains the following statement
<asp:LinkButton CommandName="Select" RunAt="server" CommandArgument='<%# DataBinder.Eval (Container.DataItem, "Comment") %>' Text='<%# DataBinder.Eval (Container.DataItem, "Title") + " " + DataBinder.Eval (Container.DataItem, "Number") %>' />
a) what would the effect be of changing this statement to (5 points)
<asp:LinkButton RunAt="server" CommandArgument='<%# DataBinder.Eval (Container.DataItem, "Comment") %>' Text='<%# DataBinder.Eval (Container.DataItem, "Title") + " " + DataBinder.Eval (Container.DataItem, "Number") %>' /> //// Will not display comment(-3) //// does not set selected item (-1)(no background color change (-1)) //// if say event is not triggered -2 //// if only say no changes without specifying what the difference in behavior is -3
b) what would be effect of changing this statement to(5 points)
<asp:LinkButton CommandName="Select" RunAt="server" Text='<%# DataBinder.Eval (Container.DataItem, "Title") + " " + DataBinder.Eval (Container.DataItem, "Number") %>' /> //// Will not display comment(5) - displays empty string //// if say event is not triggered -2
Change the data source for MyComicsRepeater.aspx to use an
XML file instead of the SQL data base. Show the code changes and show one
entry in the XML file (10)
//// handle CGC rating from boolean (-2)
//// if changed CGC on display from "YES/NO" -1
//// change data source (-4)
//// XML entry (-6)
//// if does not work -3
//// if making unnecessary changes -2 (made it harder to
test and made changes back to data base unnecessarily difficult)
//// not preserving case - since XML is case sensitive tags must match case
of DataItem names -2
//// missing fields -3
//// not mappath -2
In testlist2.cs
change the "Enumerator" class so that the states are enumerated in reverse
(alphabetic) order (but don't change the values of the "states"
array (10)
//// changes
public class Enumerator : IEnumerator { protected int index = 51; protected string[] States = { "AL","AK","AR","AZ","CA","CO","CT","DC","DE","FL","GA","HI","IA","ID","IL","IN","KS", "KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK", "OR","PA","RI","SC","SD","TN","TX","UT","VA","VT","WA","WI","WV","WY"}; public object Current { get { if (index == 51) index = (States.Length-1); // Just in case return States[index]; } } public bool MoveNext () { if (index > 0 ) { index --; return true; } return false; } public void Reset () { index = 51; } } //// Must be coded so that initially and after Reset, Current or MoveNext/Current both return the first element //// here are the tests I ran
States s =
new States();//// if causes crash -2
//// if wrong first time -2
//// if wrong after Reset -2
//// if wrong after Reset/MoveNext -2
//// Misc -2
In convert2.aspx,
a) what would be effect(s) if you changed the following two statements
Currencies.DataTextField = "Currency";
Currencies.DataValueField = "Exchange";
//// to (5 points)
Currencies.DataTextField =
"Exchange";
Currencies.DataValueField = "Currency";
//// would display the exchange rate instead of the
currency name -2
////The statement
decimal rate = Convert.ToDecimal (Currencies.SelectedItem.Value);
//// fails - throws exception, prints "ERROR" because uses
name instead of rate in computation. -3
//// must say displays "ERROR" -1