Assignment 2CMSPC 461: Programming Language ConceptsProf. Suman Saha, Spring 2023Due: 11:59 PM, 10thFeb, 2023Instructions:You need to submit your homework to Gradescope. Do every problem on...

Finish before due day


Assignment 2 CMSPC 461: Programming Language Concepts Prof. Suman Saha, Spring 2023 Due: 11:59 PM, 10thFeb, 2023 Instructions: You need to submit your homework to Gradescope. Do every problem on a separate page and mark them before submitting. If the questions are not marked or are submitted with incorrect page-to-question mapping, the question will not be graded. Make sure your name and PSU ID is legible on the first page of your assignment. We highly recommend you follow the latex/doc template (which can be found on canvas) for the home- work submissions, however, if you are writing and scanning make sure you maintain proper handwriting and use stationary of high contrast. **(Kindly refer syllabus for late submission and academic integration policies.) Question 1 (4+4 = 8 pt) Consider the following two codes. Code A: void globalCounter() { static int count = 0; count++ ; printf("count= %d \n" , count); return 0; } int main() { for(int i=0;i<5;i++){ globalcounter();="" }="" return="" 0;="" }="" code="" b:="" void="" globalcounter()="" {="" int="" count="0;" printf("count="%d" \n"="" ,="" count);="" count--;="" return="" 0;="" 1/5="" }="" int="" main()="" {="" for(int="" i=""><3;i++){ globalcounter(); } return 0; } 1. how is code a different from code b, answer in terms of variable (i.e variable count) scope? 2. what will code a and code b output as the result? question 2 (3+1+1+1+1 = 7 pt) 1. is c a statically scoped language? justify your answer with proper reasoning or supporting examples. 2. what is the scope and lifetime of the following in a c function: a) static local variable b) static global variable c) non-static local variable d) non-static global variable question 3 (4 + 2 + 2 = 8 points) consider the following pseudo-code. assume that the language has one global scope, one scope per function and one scope for each braced code block. int x = 10; int third(int x) { print x; { int x = 30; second(); } } int second() { print x; } int first() { int x = 25; third(x); } first(); 1. (4 pts) draw the symbol tables in separation for all the scopes in this program. assume that each table contains two columns: name and type. 2/5 2. (2 pts) if the language uses static scoping rules, what’s the expected output from the print statement? justify your answer by showing the hierarchy of the symbol tables at the print statement. 3. (2 pts) if the language uses dynamic scoping rules, what’s the expected output from the print state- ment? justify your answer by showing the hierarchy of the symbol tables at the print statement. question 4 (6 points) consider the following pseudo-code with nested functions. draw a picture of the run-time stack when the control reaches the end of the last call to f2 (i.e., the control reaches line 5, via the call at the line 7, via the call at line 13). include dynamic and static links and storage for local variables and parameters. you do not have to show the storage for any other control information or temporaries. but you need to specify execution frames for each function.. 1 int x = 3; 2 int y = 2; 3 int f1(int b, int c) { 4 int f2(int n) { 5 return n+3; 6 } 7 b = f2(c) + 2*b; 8 return b; 9 } 10 11 int f3(int a) { 12 int b = 5; 13 return f1(b, a); 14 } 15 16 print f3(f1(x,y)); 3/5 question 5 (5+2+2+2 = 11 points) consider the following c++ program: 1 int count = 0 2 3 void red() { 4 count += 1 5 } 6 7 void blue(funr){ 8 int count = 0 9 funr() 10 print "blue", count 11 } 12 13 void green(){ 14 int count = 0 15 blue(red) 16 print "green", count 17 18 } 19 20 void main() { 21 green() 22 print "countervalue", count 23 } what would it print if this language uses: justify your answer by showing the hierarchy of symbol tables at the print statement (assume that each table contains two columns: name and type). (5 points**) **(note: symbol tables (st) carry 5 points - we recommend approaching the below questions by cre- ating all the st and then referring the sts to answer the below questions.) 1. (2 pts) static scoping. justify your answer with a symbol table hierarchy. 2. (2 pts) dynamic scoping using shallow binding. justify your answer with a symbol table hierarchy. 3. (2 pts) dynamic scoping using deep binding. discuss clearly what happens at line 15, which symbol table is passed? question 6 (4+6 = 10 points) consider the following class instances in a c++ program: 1 static myclass a; 2 3 int main() 4 { 5 myclass *b = foo(); 6 ... 7 delete b; 8 return 0; 4/5 9 } 10 11 myclass* foo() 12 { 13 myclass d; 14 myclass* c = new myclass(); 15 return c; 16 } a) (4pt) what is the storage allocation (static/stack/heap) for the myclass objects that are reference to by a, c and d? how about the storage for the pointers b and c? b) (6pt) consider one execution of the program above. the execution trace, a sequence of program state- ments executed at run time, of this program is 5 13 14 15 6 7 8 for each myclass object that is reference to by a, c and d, write down its lifetime (use a subset of execution trace, e.g., 6 7 8 to represent the lifetime). optional question: it wont be graded but is for you to check your understanding. (0 points) (a) during the execution of a java program: a) can a variable be visible but not allocated? b) can a variable be allocated but not visible? explain your answers with proper reasoning. suggested reads, which can help you: a) chapter 3 of the text book. b) brief summary on scope (b) in a java class, a member can have access level modifiers including public, private, protected, and package-private (with no explicit modifier). describe the difference between those modifiers in terms of the scope of the member being modified. references **(you may refer any other reference as well): https://docs.oracle.com/javase/tutorial/reflect/class/classmodifiers.html https://www.javatpoint.com/access-modifiers 5/5 https://www.scaler.com/topics/c/scope-visibility-lifetime-of-variable-in-c/ https://docs.oracle.com/javase/tutorial/reflect/class/classmodifiers.html https://www.javatpoint.com/access-modifiers globalcounter();="" }="" return="" 0;="" }="" 1.="" how="" is="" code="" a="" different="" from="" code="" b,="" answer="" in="" terms="" of="" variable="" (i.e="" variable="" count)="" scope?="" 2.="" what="" will="" code="" a="" and="" code="" b="" output="" as="" the="" result?="" question="" 2="" (3+1+1+1+1="7" pt)="" 1.="" is="" c="" a="" statically="" scoped="" language?="" justify="" your="" answer="" with="" proper="" reasoning="" or="" supporting="" examples.="" 2.="" what="" is="" the="" scope="" and="" lifetime="" of="" the="" following="" in="" a="" c="" function:="" a)="" static="" local="" variable="" b)="" static="" global="" variable="" c)="" non-static="" local="" variable="" d)="" non-static="" global="" variable="" question="" 3="" (4="" +="" 2="" +="" 2="8" points)="" consider="" the="" following="" pseudo-code.="" assume="" that="" the="" language="" has="" one="" global="" scope,="" one="" scope="" per="" function="" and="" one="" scope="" for="" each="" braced="" code="" block.="" int="" x="10;" int="" third(int="" x)="" {="" print="" x;="" {="" int="" x="30;" second();="" }="" }="" int="" second()="" {="" print="" x;="" }="" int="" first()="" {="" int="" x="25;" third(x);="" }="" first();="" 1.="" (4="" pts)="" draw="" the="" symbol="" tables="" in="" separation="" for="" all="" the="" scopes="" in="" this="" program.="" assume="" that="" each="" table="" contains="" two="" columns:="" name="" and="" type.="" 2/5="" 2.="" (2="" pts)="" if="" the="" language="" uses="" static="" scoping="" rules,="" what’s="" the="" expected="" output="" from="" the="" print="" statement?="" justify="" your="" answer="" by="" showing="" the="" hierarchy="" of="" the="" symbol="" tables="" at="" the="" print="" statement.="" 3.="" (2="" pts)="" if="" the="" language="" uses="" dynamic="" scoping="" rules,="" what’s="" the="" expected="" output="" from="" the="" print="" state-="" ment?="" justify="" your="" answer="" by="" showing="" the="" hierarchy="" of="" the="" symbol="" tables="" at="" the="" print="" statement.="" question="" 4="" (6="" points)="" consider="" the="" following="" pseudo-code="" with="" nested="" functions.="" draw="" a="" picture="" of="" the="" run-time="" stack="" when="" the="" control="" reaches="" the="" end="" of="" the="" last="" call="" to="" f2="" (i.e.,="" the="" control="" reaches="" line="" 5,="" via="" the="" call="" at="" the="" line="" 7,="" via="" the="" call="" at="" line="" 13).="" include="" dynamic="" and="" static="" links="" and="" storage="" for="" local="" variables="" and="" parameters.="" you="" do="" not="" have="" to="" show="" the="" storage="" for="" any="" other="" control="" information="" or="" temporaries.="" but="" you="" need="" to="" specify="" execution="" frames="" for="" each="" function..="" 1="" int="" x="3;" 2="" int="" y="2;" 3="" int="" f1(int="" b,="" int="" c)="" {="" 4="" int="" f2(int="" n)="" {="" 5="" return="" n+3;="" 6="" }="" 7="" b="f2(c)" +="" 2*b;="" 8="" return="" b;="" 9="" }="" 10="" 11="" int="" f3(int="" a)="" {="" 12="" int="" b="5;" 13="" return="" f1(b,="" a);="" 14="" }="" 15="" 16="" print="" f3(f1(x,y));="" 3/5="" question="" 5="" (5+2+2+2="11" points)="" consider="" the="" following="" c++="" program:="" 1="" int="" count="0" 2="" 3="" void="" red()="" {="" 4="" count="" +="1" 5="" }="" 6="" 7="" void="" blue(funr){="" 8="" int="" count="0" 9="" funr()="" 10="" print="" "blue",="" count="" 11="" }="" 12="" 13="" void="" green(){="" 14="" int="" count="0" 15="" blue(red)="" 16="" print="" "green",="" count="" 17="" 18="" }="" 19="" 20="" void="" main()="" {="" 21="" green()="" 22="" print="" "countervalue",="" count="" 23="" }="" what="" would="" it="" print="" if="" this="" language="" uses:="" justify="" your="" answer="" by="" showing="" the="" hierarchy="" of="" symbol="" tables="" at="" the="" print="" statement="" (assume="" that="" each="" table="" contains="" two="" columns:="" name="" and="" type).="" (5="" points**)="" **(note:="" symbol="" tables="" (st)="" carry="" 5="" points="" -="" we="" recommend="" approaching="" the="" below="" questions="" by="" cre-="" ating="" all="" the="" st="" and="" then="" referring="" the="" sts="" to="" answer="" the="" below="" questions.)="" 1.="" (2="" pts)="" static="" scoping.="" justify="" your="" answer="" with="" a="" symbol="" table="" hierarchy.="" 2.="" (2="" pts)="" dynamic="" scoping="" using="" shallow="" binding.="" justify="" your="" answer="" with="" a="" symbol="" table="" hierarchy.="" 3.="" (2="" pts)="" dynamic="" scoping="" using="" deep="" binding.="" discuss="" clearly="" what="" happens="" at="" line="" 15,="" which="" symbol="" table="" is="" passed?="" question="" 6="" (4+6="10" points)="" consider="" the="" following="" class="" instances="" in="" a="" c++="" program:="" 1="" static="" myclass="" a;="" 2="" 3="" int="" main()="" 4="" {="" 5="" myclass="" *b="foo();" 6="" ...="" 7="" delete="" b;="" 8="" return="" 0;="" 4/5="" 9="" }="" 10="" 11="" myclass*="" foo()="" 12="" {="" 13="" myclass="" d;="" 14="" myclass*="" c="new" myclass();="" 15="" return="" c;="" 16="" }="" a)="" (4pt)="" what="" is="" the="" storage="" allocation="" (static/stack/heap)="" for="" the="" myclass="" objects="" that="" are="" reference="" to="" by="" a,="" c="" and="" d?="" how="" about="" the="" storage="" for="" the="" pointers="" b="" and="" c?="" b)="" (6pt)="" consider="" one="" execution="" of="" the="" program="" above.="" the="" execution="" trace,="" a="" sequence="" of="" program="" state-="" ments="" executed="" at="" run="" time,="" of="" this="" program="" is="" 5="" 13="" 14="" 15="" 6="" 7="" 8="" for="" each="" myclass="" object="" that="" is="" reference="" to="" by="" a,="" c="" and="" d,="" write="" down="" its="" lifetime="" (use="" a="" subset="" of="" execution="" trace,="" e.g.,="" 6="" 7="" 8="" to="" represent="" the="" lifetime).="" optional="" question:="" it="" wont="" be="" graded="" but="" is="" for="" you="" to="" check="" your="" understanding.="" (0="" points)="" (a)="" during="" the="" execution="" of="" a="" java="" program:="" a)="" can="" a="" variable="" be="" visible="" but="" not="" allocated?="" b)="" can="" a="" variable="" be="" allocated="" but="" not="" visible?="" explain="" your="" answers="" with="" proper="" reasoning.="" suggested="" reads,="" which="" can="" help="" you:="" a)="" chapter="" 3="" of="" the="" text="" book.="" b)="" brief="" summary="" on="" scope="" (b)="" in="" a="" java="" class,="" a="" member="" can="" have="" access="" level="" modifiers="" including="" public,="" private,="" protected,="" and="" package-private="" (with="" no="" explicit="" modifier).="" describe="" the="" difference="" between="" those="" modifiers="" in="" terms="" of="" the="" scope="" of="" the="" member="" being="" modified.="" references="" **(you="" may="" refer="" any="" other="" reference="" as="" well):="" https://docs.oracle.com/javase/tutorial/reflect/class/classmodifiers.html="" https://www.javatpoint.com/access-modifiers="" 5/5="" https://www.scaler.com/topics/c/scope-visibility-lifetime-of-variable-in-c/="" https://docs.oracle.com/javase/tutorial/reflect/class/classmodifiers.html="">
Feb 09, 2023
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here