Midterm CSE 131B Spring 2006

Similar documents
Midterm CSE 131B Spring 2005

Final CSE 131B Spring 2005

Final CSE 131B Winter 2003

Final CSE 131B Spring 2004

Midterm CSE 131 Winter 2012

Midterm CSE 131 Winter 2013

This exam is to be taken by yourself with closed books, closed notes, no calculators.

Midterm CSE 131 Winter 2014

C++ for Java Programmers

DECLARAING AND INITIALIZING POINTERS

Final CSE 131 Fall 2014

Final CSE 131 Winter 2010

CSE 5A Final Fall 2006

Homework #3 CS2255 Fall 2012

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Arrays, Pointers and Memory Management

CS201- Introduction to Programming Current Quizzes

Assist. Prof. Dr. Caner ÖZCAN

Intermediate Code Generation

Final CSE 131 Winter 2012

CSE 30 Fall 2013 Final Exam

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

FORM 2 (Please put your name and form # on the scantron!!!!)

Midterm CSE 131 Winter 2015

Data Representation and Storage. Some definitions (in C)

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CSE 30 Fall 2012 Final Exam

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

CSE 30 Winter 2014 Final Exam

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

CS61C : Machine Structures

Pointers and Arrays 1

Chapter 2: Basic Elements of Java

CSE 30 Spring 2007 Final Exam

Introduction to Programming Using Java (98-388)

System Software Assignment 1 Runtime Support for Procedures

Problem Score Max Score 1 Syntax directed translation & type

Language comparison. C has pointers. Java has references. C++ has pointers and references

Midterm CSE 131 Fall 2014

HW1 due Monday by 9:30am Assignment online, submission details to come

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

Final CSE 131 Fall 2015

Introduction to Computer Science Midterm 3 Fall, Points

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

CSCE 5610: Computer Architecture

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Chapter Two MIPS Arithmetic

CSE351 Spring 2018, Final Exam June 6, 2018

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

CSE I-Sem)

22c:111 Programming Language Concepts. Fall Types I

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

PROGRAMMAZIONE I A.A. 2017/2018

MIDTERM EXAMINATION - CS130 - Spring 2003

Chapter 11: Pointers

CSE 30 Winter 2009 Final Exam

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

3.3 Structures. Department of CSE

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

CSE 30 Fall 2007 Final Exam

Week 3 Lecture 2. Types Constants and Variables

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Pointer Data Type and Pointer Variables

CSE 30 Spring 2006 Final Exam

Fundamentals of Programming Session 20

Character Strings. String-copy Example

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Pointers. Variable Declaration. Chapter 10

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

4. Inputting data or messages to a function is called passing data to the function.

Computer Programming CS F111

Section - Computer Science. int main() {! int a=10,b=20;! printf("a:%d B:%d\n",a,b);! a=(a+b)-(b=a);! printf("a:%d B:%d\n",a,b);!

Computer Systems Lecture 9

CIS 341 Midterm March 2, Name (printed): Pennkey (login id): Do not begin the exam until you are told to do so.

CSCI Compiler Design

MIDTERM EXAMINATION - CS130 - Spring 2005

Solution Manual for Data Structures and Problem Solving Using C++ 2nd edition by Mark A. Weiss

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

More about BOOLEAN issues

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

CS 415 Midterm Exam Spring 2002

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Problem Solving and 'C' Programming

First of all, it is a variable, just like other variables you studied

CS 61c: Great Ideas in Computer Architecture

CSE 431S Type Checking. Washington University Spring 2013

LECTURE 17. Expressions and Assignment

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Do not turn the page until 5:10.

The following expression causes a divide by zero error:

Lexical Considerations

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

CSE 351 Midterm - Winter 2015 Solutions

CMSC 202. Pointers Dynamic Memory Allocation

Compilers Project 3: Semantic Analyzer

Transcription:

Signature Login Name Name Student ID Midterm CSE 131B Spring 2006 Page 1 Page 2 Page 3 Page 4 Page 5 _ (19 points) _ (26 points) _ (20 points) _ (20 points) _ (15 points) Subtotal Page 6 Extra Credit _(100 points) _ (5 points) Total _ 0

1. Using the following type and variable declarations, indicate the size of each of the variables (in bytes) in the space provided. In the event that a type definition for a variable is an error/indeterminate size, write the word "ERROR" in the blank. (2 pts each) TYPE rec1 = RECORD a : INTEGER; b : REAL; c : ARRAY 10 OF POINTER TO rec1; d : POINTER TO rec1; e : POINTER TO REAL; TYPE rec2 = POINTER TO RECORD a : ARRAY 900, 900 OF REAL; b : RECORD a : REAL; b : INTEGER; c : ARRAY 20 OF rec2; TYPE rec3 = RECORD a : INTEGER; b : REAL; c : RECORD d: rec3; e : POINTER TO rec3; TYPE foo1 = ARRAY 10, 10 OF rec2; TYPE foo2 = RECORD a : foo1; b : rec1; c,d : rec2; VAR myrec1 : rec1; VAR myrec2 : rec2; VAR myrec3 : rec3; VAR myfoo1 : foo1; VAR myfoo2 : foo2; Using the Right-Left rule write the C/C++ definition of a variable named fubar that is a pointer to a function which takes two arguments, a pointer to a char and a pointer to a pointer to a float, and returns a pointer to an array of 4 elements where each array element is of type pointer to struct foobaz. (9 pts) 1

2. The types in Oberon variable definitions are often unnecessary in the sense that it is possible to infer variables' types and detect type errors simply from their use. For each of the following program fragments, find a set of types that makes it legal, and write an Oberon definition for each variable. If there is more than one possible type, choose only one. If there is none, write "NONE". Assume all arrays are of size 8. (2 pts each) a[b^] := b; VAR a : ; VAR b : ; IF a[b] # c& THEN b := c + d; END VAR a : ; VAR b : ; VAR c : ; VAR d : ; IF (a # b) OR c THEN b := c; END VAR a : ; VAR b : ; VAR c : ; IF a[b]& = c THEN c^ := b / d; END VAR a : ; VAR b : ; VAR c : ; VAR d : ; 2

3. Given the following type, variable, and function definitions, state whether each function call would match (A) the first (B) the second (C) the third (D) none (illegal call) (E) more than one of the definitions (ambiguous call) ( 2 pts each) TYPE peter = REAL; TYPE lois = peter; VAR chris : lois; VAR meg : INTEGER; FUNCTION quagmire (brian : peter, stewie : INTEGER) : INTEGER; (* first *) BEGIN RETURN 0; END quagmire; FUNCTION quagmire (REF brian: INTEGER, stewie : peter) : INTEGER; (* second *) BEGIN RETURN 0; END quagmire; FUNCTION quagmire (brian : peter, REF stewie : lois) : INTEGER; (* third *) BEGIN RETURN 0; END quagmire; BEGIN quagmire(chris, meg); quagmire(meg, chris); quagmire(meg + meg, chris + chris); quagmire(meg, meg); quagmire(meg, chris + meg); quagmire(meg + meg, chris); quagmire(chris + meg, chris + meg); quagmire(chris, chris); RETURN 0; END. Although we did not allow it in this quarter's project specification, record/struct assignment is relatively straight forward requiring same/equal types for both operands to the binary assignment operator ( := ). It certainly is defined in the C family of languages. So... if assignment is defined for records/structs in languages like C, why is record equality ( equals / not equals ) not defined? From the compiler's point of view, why is record/struct equality difficult? (4 pts) 3

4. Given the following array definition /* C */ (* Oberon *) double x[50][20]; VAR x : ARRAY 50,20 OF LONGREAL; write the assembly level address calculation expression taking into account scalar arithmetic to access x[i][j] x[i,j] (( x + _ ) + ) The result is the address of where we can find this array element. (8 points) Assume the array access was in a nested loop construct to print each array element in order like the following: for ( i = 0; i < 50; ++i ) for ( j = 0; j < 20; ++j ) print( x[i][j] ); Why would using a traversal pointer be more efficient? (2 pts) Replace the above nested loop code by writing the definition for a traversal pointer (named ptr), the statement to assign/initialize the traversal pointer, and the loop code to print each element in the array above using this traversal pointer. If you prefer to use pseudo-oberon syntax instead of C syntax, you can assume pointer arithmetic is defined the same as in C. (10 pts) 4

5. Assume the following definitions are correct: (2 pts each) TYPE rec1 = RECORD ptr : POINTER TO INTEGER; TYPE rec2 = RECORD ptr : POINTER TO rec1; VAR ptr : POINTER TO rec2; VAR a, b : INTEGER; a) What type is ptr^.ptr^.ptr? b) What type is ptr^? c) What type is ptr^.ptr^.ptr^? d) What type is ptr^.ptr? For the following, assume each statement is independent of each other. You cannot use the result of a previous statement in the answer for a subsequent question. You can only use the above vars and types. You cannot use NIL. e) Write the Oberon statement to have ptr in rec1 point to variable b. f) Write the Oberon statement to assign variable a the value of the integer pointed to by ptr in rec1. a := g) Write a valid statement with variable c (defined below) on the lhs (left hand side) of the assignment statement. You cannot use c or NIL. VAR c : POINTER TO rec1; c := Do you promise to have a good time tomorrow at Sun God Festival... or wherever you may be tomorrow... oh, even at Richard's Discussion Section before hitting SGF hard? (1 pt) 5

Extra Credit (5 points) Given the following definitions: int i1 = 5; /* Assume i1 is at memory location 4000 */ int i2 = 10; /* Assume i2 is at memory location 5000 */ float f = 1.5; /* Assume f is at memory location 6000 */ int *i1ptr = &i1; /* Assume i1ptr is at memory location 7000 */ int *i2ptr = &i2; /* Assume i2ptr is at memory location 8000 */ float *fptr = &f; /* Assume fptr is at memory location 9000 */ i1ptr = (int *) fptr; What is the value of i1ptr after this statement executes? ++fptr; What is the value of fptr after this statement executes? i1ptr = i2ptr; What is the value of i1ptr after this statement executes? i1 = *&*i1ptr; What is the value of i1 after this statement executes? i2 = **&i2ptr + 1; What is the value of i2 after this statement executes? Assume each instruction is executed in the order given. 6

Scratch Paper 7