Docsity

CSE 131B Winter 2003 Programming Concepts Exam - Prof. Richard Ord, Exams of Computer Science

Toggle sidebar
bg1
0
Login name _________________ Name ______________________
Signature ___________________ Student ID __________________
Final
CSE 131B
Winter 2003
Page 1 ___________ (20 points)
Page 2 ___________ (25 points)
Page 3 ___________ (21 points)
Page 4 ___________ (40 points)
Page 5 ___________ (30 points)
Page 6 ___________ (25 points)
Page 7 ___________ (25 points)
Page 8 ___________ (14 points)
Subtotal ___________(200 points)
Page 9 ___________ (10 points)
Extra Credit
Total ___________
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CSE 131B Winter 2003 Programming Concepts Exam - Prof. Richard Ord and more Exams Computer Science in PDF only on Docsity!

Login name _________________ Name ______________________

Signature ___________________ Student ID __________________

Final

CSE 131B

Winter 2003

Page 1 ___________ (20 points)

Page 2 ___________ (25 points)

Page 3 ___________ (21 points)

Page 4 ___________ (40 points)

Page 5 ___________ (30 points)

Page 6 ___________ (25 points)

Page 7 ___________ (25 points)

Page 8 ___________ (14 points)

Subtotal ___________(200 points)

Page 9 ___________ (10 points)

Extra Credit

Total ___________

1. Consider the following psuedocode:

x : integer; -- global

procedure set_x ( n : integer ) x := n;

procedure print_x() output( x ); -- print the value of x

procedure one() x : integer; set_x( 1 ); print_x();

procedure two() set_x( 2 ); print_x();

set_x( 0 ); one(); print_x(); two(); print_x();

What does the program output What does the program output

if the language uses static scoping? (4 points) if the language uses dynamic scoping? (4 points)

_____ _____

_____ _____

_____ _____

_____ _____

Give an example of a converting type cast/conversion (underlying bit pattern needs to be changed). (4 points)

Give an example of an implicit type coercion (type conversion without an explicit cast). (4 points)

Give an example of a non-converting type cast/conversion (underlying bit pattern does not change). (4 points)

3. What major issue distinguishes a macro compared to an inline function? (4 points)

What gets printed? (9 points)

VAR a : INTEGER;

PROCEDURE foo1( VAR x : INTEGER ); BEGIN x := 77;

OUTPUT a; __________________ END foo1;

PROCEDURE foo2( VAR y : INTEGER ); BEGIN y := 66;

OUTPUT a; __________________

foo1( y );

END foo2;

BEGIN

a := 55;

foo2( a );

OUTPUT a; __________________ END.

How does a leaf subroutine differ from a traditional closed subroutine, specifically in the SPARC arch.?

List 2 ways they differ. (8 points)

4. Identify where each of the following program parts live in the Java runtime environment as discussed in

class. (24 points)

public class Foo { private Foo a; a _________________

private static int b; b _________________

public Foo() { Foo() _________________

a = this; this _________________ ++b; } main() _________________

public static void main( String[] args ) { args _________________

int c = 5; c _________________

Foo d; d _________________

d = new Foo(); where d is pointing _________________ d.method( c ); } method() _________________

private void method( int e ) { e _________________

int f; f _________________ f = e; } }

Assume there is a Java class named Fubar that defines

static method s_foo() that returns an int static int s_foo() { ... }

static variable sv that is of type int

instance method i_foo() that returns an int int i_foo() { ... }

instance variable iv that is of type int

instance variable ref that is a reference to a Fubar object and initialized

Fubar ref = new Fubar();

State whether the following initializations are legal (no compiler error)? Explain why or why not. (16 points)

private static int sv = Fubar.s_foo();

private static int sv = ref.i_foo();

private int iv = ref.i_foo();

private int iv = ref.s_foo();

6. Given the following Oberon program, emit the unoptimized SPARC assembly language code that should be

generated for this program. Assume the global variables x, y, and z are allocated in the Data segment (given).

Assume no optimizations โ€“ treat each instruction separately without any knowledge of any previously

computed/loaded/stored values that may still be in a register from a previous instruction. Draw a line between

each group of assembly language instructions that represent the emitted code generated for each instruction and

label them with the instruction number. (25 points)

VAR x, y, z : INTEGER;

PROCEDURE foo( a : INTEGER; VAR b : INTEGER ) : INTEGER; VAR i, j : INTEGER; (**** Local Stack variables โ€“ Do Not Need to Initialize to 0. ****) BEGIN i := a + 5; (* 1 ) j := b - a; ( 2 ) b := i + 7; ( 3 ) RETURN b; ( 4 *) END foo;

BEGIN INPUT x; (* 5 ) INPUT y; ( 6 ) z := foo( x, y); ( 7 *) END.

.global foo, main

.section ".data" .align 4 x: .word 0 y: .word 0 z: .word 0

.section ".text" foo: main: save %sp, -(92 + 8) & -8, %sp save %sp, -96, %sp

7. Given the following pseudocode

read n;

for ( i = 0; i < n; ++i ) { a[i] = n * i;

if ( n > 20 ) b[i] = a[i]; }

the if (n > 20) is a loop invariant. Restructure this code to move it out of the loop to save up to 3 instructions

(cmp, conditional branch, nop) per loop iteration? Also perform any other explicit code improvements such as

redundant loads/stores (unnecessary memory accesses) and strength reduction if possible. No assembly โ€“ just

all high-level code like the above. You may add/change code as part of your code improvements. The idea is

the resulting code is faster to execute for any arbitrary value of n read in at runtime. (15 points)

read n;

Consider the following code:

ld [r1], r add r1, 20, r ld [r1], r3! Destination of this load is a source operand in next instr. ------! Stall 1 cycle (L1 cache hit) while load into r3 completes. add r2, r3, r

Show how to shorten the time required for this code by moving the update of r1 forward into the delay slot of

the second load. Assume r1 is still live (needed) at the end of this code. Make whatever other alterations to

individual instructions to maintain correctness. The idea is to reduce this chunk of code from 5 cycles to 4

cycles. No assembly โ€“ just this virtual register pseudo-assembly code like the above. (10 points)

9. Extra Credit (10 points)

What is the value of each of the following expressions?

char a = "End this, please!"; / char a[] = "End this, please!"; */

"I loved Compilers B!"[6] _____________

a[1] _____________

*a _____________

*(a+12) _____________

*&a[5] _____________

0["This Blows Me Away!"] _____________

Given the following ANSI/ISO C variable definitions, identify which expressions will produce a static semantic

compiler error. Hint: Think modifiable l-value. A ) No compiler error

B ) Compiler error

int i = 5; float f = 1.5; int *iPtr = &i; float *fPtr = &f;

*iPtr = (int) *fPtr; ______

(float *) iPtr = fPtr; ______

fPtr = &(i + f); ______

++( (float *) iPtr ); ______

Scratch Paper