CS441 Midterm-2: Name
CS441 Midterm-2: Name
Name:
1. Answer the following questions concerning programming languages:
a. Why do you suppose that variable-length argument lists are so seldom supported
by high-level programming languages?
The variables-length arguments are against to strong coupling and also make the
optimization difficult.
C, C++ support void pointers which should be changed to a type( typecaseted) before
using it – which is violation on strongly typed. The explicit type cast allow
coversions of types .
The programming languages which are strongly but dynamically typed are: Python
and Perl.
When the named constants are really in order like days in a week or they have
any relation to previous type and could be collected in a logic sequence, then
enumeration is the best way representing such a collection of named constants.
Using subrange range checking is done in compile time rather than runtime.
This study source was downloaded by 100000895332053 from CourseHero.com on 02-24-2025 11:04:53 GMT -06:00
https://www.coursehero.com/file/24321249/Midterm2-Solution-1doc/
If the type of a variable is converted to another type (like narrowing and broadening)
is called type conversion. Type coercion is for implicit operand type conversions. The
non-converting type casts is when change of type without altering the underlying bits.
e. What is the difference between a pointer and an address? What are dangling
references? How are they created, and why are they a problem? Discuss the
comparative advantages of tombstones and locks and keys as a means of solving
the problem.
Pointer holds the address of another variable. A pointer has address too. If a pointer
points to an address, which is no longer valid, it is called a dangling reference.
An activation record is the data used by the subroutine while it is active that is not the
subroutine’s code. An activation record instance is the data for a particular call to a
subroutine
g. What are the two steps in locating a nonlocal variable in a static-scoped language
with stack-dynamic local variables and nested subprograms?
o Find the correct activation record instance
o Determine the correct offset within that activation record instance
h. What’s the two potential problems with the static chain method?
1. A nonlocal reference is slow if the number of scopes between the reference and
the declaration of the referenced variable is large
2. Time-critical code is difficult, because the costs of nonlocal references are not
equal, and can change with code upgrades and fixes
i. What are the differences between the deep access and shallow access methods of
implementing dynamic scoping?
Deep Access - nonlocal references are found by searching the activation record
instances on the dynamic chain. Length of chain cannot be statically determined
Every activation record instance must have variable names
In case of shallow access names and values are stored in a global table. Using this
method, space is allocated for every variable name that is in the program(one space
for variable temp though there might be several declarations of temp in the different
methods). When a sub-routine is called it saves the current value of the variable and
replaces it with the value in its current scope and restores the value of the variable
while exiting.
This study source was downloaded by 100000895332053 from CourseHero.com on 02-24-2025 11:04:53 GMT -06:00
https://www.coursehero.com/file/24321249/Midterm2-Solution-1doc/
void main () {
int x = 3;
x = x + fun(&x);
}
What is the value of x after the assignment statement in main?
a. If operands are evaluated left to right
b. If operands are evaluated right to left
a. x = 3 + 4 = 7
b. x = 8 + 4 = 12
3. Given the following Java-like code, what is the output of the program when each of the
following scoping rules is applied? Explain your answers.
a. Static scope
b. Dynamic scope
public class A {
int x = 0;
void fa() {
int x = 1;
fb();
}
void fb() {
println("x = " + x)
}
public static void main(String[] args) {
new A().fa();
}
}
Suggested Solution
Static Scope: The output is 0, since the variable x is declared as an instant variable
of class A which is initialized to 0.
Dynamic Scope: The output is 1, since the local variable x of method fa() is closer
to the method fb() on the system stack when the prinln() statement is executed at
runtime
void fun1(void) {
int b, c, d;
// call another function
}
void fun2(void) {
int c, d, e;
// call another function
}
void fun3(void) {
int d, e, f;
// call another function
This study source was downloaded by 100000895332053 from CourseHero.com on 02-24-2025 11:04:53 GMT -06:00
https://www.coursehero.com/file/24321249/Midterm2-Solution-1doc/
}
void main () {
int a, b, c;
// call another function
}
Given the following calling sequences and assuming that dynamic scoping is used, what
variables are visible during execution of the last function called? Include with each
visible variable the name of the function in which it was defined.
Suggested Solution:
This study source was downloaded by 100000895332053 from CourseHero.com on 02-24-2025 11:04:53 GMT -06:00
https://www.coursehero.com/file/24321249/Midterm2-Solution-1doc/
(b) In your selected language, suggest programming techniques that could diminish the
risk of generating garbage and dangling references. What language constructs in your
selected language can be used to support your techniques (if any)?
Suggested Solution
void main () {
This study source was downloaded by 100000895332053 from CourseHero.com on 02-24-2025 11:04:53 GMT -06:00
https://www.coursehero.com/file/24321249/Midterm2-Solution-1doc/
int value = 2, list[5] = {1, 3, 5, 7, 9};
swap (value, list[0]);
swap (list[0], list[1]);
swap (value, list[value]);
}
void swap (int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}
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 swap?
a. passed by value: The values remain the same
b. passed by reference: value = 2 list= {3,1,5,7,9}
c. passed by value-result: value = 2 list = {3,1,5,7,9}
This study source was downloaded by 100000895332053 from CourseHero.com on 02-24-2025 11:04:53 GMT -06:00
https://www.coursehero.com/file/24321249/Midterm2-Solution-1doc/
x = y = z = 3.1415;
Suggested solution:
There is a hidden truncation lurking in here. Because = is right associative, first
3.1415 is assigned to z, then the same value is assigned to the int y, causing a
truncation. This truncated value (3.0) is then assigned to the floating-point variable x.
This study source was downloaded by 100000895332053 from CourseHero.com on 02-24-2025 11:04:53 GMT -06:00
https://www.coursehero.com/file/24321249/Midterm2-Solution-1doc/
Powered by TCPDF (www.tcpdf.org)