Midterm CSE 131 Winter 2014

Similar documents
Midterm CSE 131 Winter 2012

Midterm CSE 131 Winter 2013

Midterm CSE 131 Fall 2014

Midterm CSE 131 Winter 2015

Final CSE 131 Fall 2014

Final CSE 131 Fall 2015

Final CSE 131 Winter 2012

Final CSE 131 Winter 2010

CSE 30 Fall 2013 Final Exam

CSE 30 Winter 2014 Final Exam

Final CSE 131B Spring 2004

CSE 30 Fall 2012 Final Exam

Midterm CSE 131B Spring 2005

CSE 30 Winter 2009 Final Exam

CSE 30 Spring 2007 Final Exam

CSE 30 Fall 2007 Final Exam

CSE 30 Spring 2006 Final Exam

CSE 30 Fall 2006 Final Exam

Final CSE 131B Spring 2005

Chapter 2 Bits, Data Types, and Operations

Do not start the test until instructed to do so!

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Chapter 2 Bits, Data Types, and Operations

Data Representation and Binary Arithmetic. Lecture 2

Do not start the test until instructed to do so!

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Do not start the test until instructed to do so!

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Chapter 2 Bits, Data Types, and Operations

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Chapter 8. Characters and Strings

CS 159 Credit Exam. What advice do you have for students who have previously programmed in another language like JAVA or C++?

Chapter 2 Bits, Data Types, and Operations

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

Fundamentals of Programming (C)

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Chapter 2 Bits, Data Types, and Operations

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

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

ASSIGNMENT 5 TIPS AND TRICKS

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Appendix A Developing a C Program on the UNIX system

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Final Exam Practice Questions

CMPSC 311- Introduction to Systems Programming Module: Strings

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

Number Representations

Chapter 3. Information Representation

The Binary Number System

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

6.096 Introduction to C++ January (IAP) 2009

Simple Data Types in C. Alan L. Cox

Midterm CSE 131B Spring 2006

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

Oberon Data Types. Matteo Corti. December 5, 2001

This is great when speed is important and relatively few words are necessary, but Max would be a terrible language for writing a text editor.

Chapter 2 Number System

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

User s Manual. Xi3000 Scanner. Table of Contents

Lecture (09) x86 programming 8

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Number Systems Base r

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

JAVA OPERATORS GENERAL

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Under the Hood: Data Representation. Computer Science 104 Lecture 2

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

Fundamentals of Programming

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Midterm Exam, Fall 2015 Date: October 29th, 2015

PureScan - ML1. Configuration Guide. Wireless Linear Imager Wireless Laser scanner - 1 -

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Variables and data types

Question Points Score Total: 100

CSE 5A Final Fall 2006

Configuration Manual PULSAR C CCD SCANNER. Table of Contents

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

Final CSE 131B Winter 2003

Characters Lesson Outline

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

3. Provide the routing table of host H located in LAN E, assuming that the host s network interface is called i1. ARes/ComNet

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

The following are the data types used in the C programming language:

Xi2000-BT Series Configuration Guide

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

2D BARCODE SCANNER CA-SC-20200B

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

KB232. PS/2 Keyboard RS-232 Adapter Part # SA0008 (Version 3.0) Copyright 2003 L3 Systems, Inc. Redmond

n NOPn Unary no operation trap U aaa NOP Nonunary no operation trap i

Transcription:

Student ID Login Name _ Name Signature Midterm CSE 131 Winter 2014 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 (21 points) (36 points) (28 points) (16 points) (18 points) (20 points) Subtotal (139 points = 100%) Page 7 Extra Credit (7 points) Total This exam is to be taken by yourself with closed books, closed notes, no electronic devices. You are allowed one side of an 8.5"x11" sheet of paper handwritten by you. 0

1. Given the following CUP grammar snippet (assuming all other Lexing and terminals are correct): Expr ::= Des AssignOp Expr {: System.out.println("Z"); :} Des {: System.out.println("A"); :} ; Des ::= T_STAR {: System.out.println("B"); :} Des {: System.out.println("C"); :} T_AMPERSAND {: System.out.println("D"); :} Des {: System.out.println("E"); :} T_PLUSPLUS {: System.out.println("F"); :} Des {: System.out.println("G"); :} Des2 {: System.out.println("H"); :} ; Des2 ::= Des2 {: System.out.println("I"); :} T_PLUSPLUS {: System.out.println("J"); :} Des3 {: System.out.println("K"); :} ; Des3 ::= T_ID {: System.out.println("L"); :} ; AssignOp ::= T_ASSIGN {: System.out.println("M"); :} ; What is the output when parsing the follow expression (you should have 18 lines/numbers in your output): x = *y = z++ Output In the above grammar, does the post-increment operator have left-to-right associativity or right-to-left associativity? If variable z is defined to be type float *, what types must variables y and x be defined for this expression to be semantically correct? _ y; _ x; 1

2. Give the order of the typical C compilation stages and on to actual execution as discussed in class 0 prog.exe/a.out (Executable image) 1 cpp (C preprocessor) 2 Program Execution 3 ld (Linkage Editor) 4 Object file (prog.o) 5 Assembly file (prog.s) 6 Loader 7 ccomp (C compiler) 8 as (Assember) 9 Source file (prog.c) 10 Segmentation Fault (Core Dump) / General Protection Fault gcc > > > > > > > > > > Given the C following definitions (same rules in our Reduced-C compiler) struct S1 { int a; }; struct S2 { int a; }; struct S1 a; struct S2 b; indicate whether each of the following statements will cause an equivalence compiler error or not. _ a = b; A) Error B) No Error _ a.a = b.a; _ a = (struct S1)b; _ (struct S2)a = b; _ a = *(struct S1 *)b; _ *(struct S2 *)a = b; _ a = *(struct S1 *)&b; _ *(struct S2 *)&a = b; Using Reduced-C syntax, define an array of an array of ints with dimensions 8x4 named bar such that bar[7][3] = 42; is a valid expression. This will take two lines of code. Modifiable L-vals, Non-Modifiable L-vals, R-vals Using the Reduced-C Spec (which closely follows the real C language standard), given the definitions below, indicate whether each expression evaluates to either a A) R-val B) Modifiable L-val C) Non-Modifiable L-val function : int * foo() { /* Function body not important. */ } structdef R1 { int a; float b; }; float[9] a; R1 b; R1 * c; int * d; b.a++ *d+1 &b (int)a[3] c->a % b.a foo() &a[2] (R1 *)foo() a[1] = *foo() ++*d++ *foo() a[7] *&b a *d++ 2

3. Given the following Reduced-C code and list of statements, indicate for each numbered statement the type of error that should be reported according to the Project I spec for this quarter (which is similar to C++ rules). Use the letters associated with the available errors in the box on the right, or choose A for No error. int * [5] a; const int b = 5; bool c, d; function : int foo(){ /*... */ return 0; } 1) b = 3; 2) &&a[0]; 3) a[b-2] = &b; 4) (int)c = 4; 5) &foo(); 6) *a[foo()] = b; A) No error. B) Operand to ++ is not a modifiable L-value. C) Type of index expr in array not equivalent to int. D) Left-hand operand is not assignable (not a modifiable L-value). E) Non-addressable argument to address-of operator. 7) a[2] = (int *) &c; 8) ++b; 9) (c = d) = true; 10) c = d = true; 11) (*a[0])++ = *a[1]; 12) *a[0]++ = *a[1]; State whether constant folding can be performed by the compiler according to this quarter's Reduced-C spec in the following Reduced-C statements function : void foo() { const int a = 5; int b = 3; A) No constant folding B) Constant folding const int c = a + 10; int[53 + c] d; b = d[d[2] + c]; d[-2 + (a * b)] = c; int e = d[a + c]; d[5-2 + c] = e; } b = d[e + a]; e = d[13 + b]; Using the Right-Left rule write the C definition of a variable named foo that is a pointer to a 2-d array of 3 rows by 14 columns where each element is a pointer to a function that takes a pointer to a pointer to a long as a single parameter and returns a pointer to an array of 5 elements where each element is a pointer to a struct bar. 3

4. Consider the following struct definitions in Reduced-C (similar to C/C++). Specify the size of each struct on a typical RISC architecture (like ieng9) or 0 if it is an illegal definition. structdef FOO1 { int a; float b; FOO1 *c; int[2] d; }; structdef FOO2 { int a; float b; int *c; FOO2[2] d; }; structdef FOO3 { FOO3 a; float b; int *c; int[2] d; }; Size Size Size Identify whether each of the following will cause an underlying bit pattern change. int a = 5; float b = -4.20; int * ptr1; float * ptr2; void foo( float x, float & y ) { /* */ } A) Yes Underlying bit pattern change B) No No underlying bit pattern change a = (int) b; foo( a, b ); b = a; a = * (int *) & b; ptr1 = (int *) & b; ptr2 = (float *) ptr1; Given the C array declaration C int b[4][2]; Mark with a B all the memory location(s) where we would find the array element b: b[1][1] low memory Each box represents a byte in memory. high memory Given the following Reduced-C code below, fill in the blanks of the compile error that should be reported according to this quarter's Project I spec. Use the letters associated with the words in the box below. typedef float F1; typedef F1 F2; typedef int I1; typedef I1 I2; I1 x; I2 y; F2 z; A. I1 B. I2 C. int D. F1 E. F2 F. float G. 5-hour Energy H. Sleep I. equivalent J. modifiable K. addressable L. assignable x = z = y; // Compile error reported here. Assume this stmt is inside a function. Value of type not to variable of type. 4

5. Show the memory layout of the following C struct definition taking into consideration the SPARC data type memory alignment restrictions discussed in class. Fill bytes in memory with the appropriate struct member/field name. For example, if member/field name p takes 4 bytes, you will have 4 p's in the appropriate memory locations. If the member/field is an array, use the name followed by the index number. For example, some number of p[0]s, p[1]s, p[2]s, etc. If the member/field is a struct, use the member name followed by its member names (e.g. p.a, p.b). Place an X in any bytes of padding. Structs and unions are padded so the total size is evenly divisible by the most strict alignment requirement of its members. struct foo { short a; char b[4]; double c; short d; }; struct fubar { char e[5]; int * f; struct foo g; char h[3]; char i; }; struct fubar fubaz; low memory fubaz: high memory If struct fubar had been defined as union fubar instead, what would be the sizeof(union fubar)? _ What is the sizeof( struct fubar )? _ What is the offsetof( struct fubar, g.b[3] )? _ What is the resulting type of the following expression? * (char *) (& ( ((struct foo *) fubaz.e )->d ) + 2) Write the equivalent expression that directly accesses this value/memory location without all the fancy casting and & operators. fubaz. 5

6. Fill in the names of the 5 main areas of the C Runtime Environment as laid out by most Unix operating systems (and Solaris on SPARC architecture in particular) as discussed in class. Then state what parts of a C program are in each area. low memory high memory Which 3 areas of the above are essentially determined at compile time and are part of an executable image (for example, an a.out or.exe file)? 1) 2) 3) Give the order of the phases of compilation in a typical C compiler as discussed in class 0 Scanner (Lexical Analysis) 1 Parser (Semantic Analysis) 2 Parser (Syntax Analysis) 3 Target language file (for ex., prog.s) 4 Source language file (for example, prog.c) 5 Intermediate Representation(s) 6 Code generation (for ex., Assembly) > > > > > > 6

Extra Credit What gets printed when the following C program is executed? #include <stdio.h> int main() { char a[] = "Rohan"; char *ptr = a; printf( "%c\n", *(ptr++ + 2) + 1 ); printf( "%c\n", *(a+2) = *++ptr + 5 ); printf( "%c\n", *++ptr ); printf( "%c\n", *++ptr + 9 ); printf( "%c\n", *a = ptr[-sizeof(ptr)] + 1); _ printf( "%d\n", --ptr - a ); printf( "%s\n", a ); _ } return 0; A portion of the C Operator Precedence Table Operator Associativity ++ postfix increment L to R -- postfix decrement [] array element () function call -------------------------------------- * indirection R to L ++ prefix increment -- prefix decrement & address-of sizeof size of type/object (type) type cast -------------------------------------- * multiplication L to R / division % modulus -------------------------------------- + addition L to R - subtraction --------------------------------------.. -------------------------------------- = assignment R to L Hexadecimal - Character 00 NUL 01 SOH 02 STX 03 ETX 04 EOT 05 ENQ 06 ACK 07 BEL 08 BS 09 HT 0A NL 0B VT 0C NP 0D CR 0E SO 0F SI 10 DLE 11 DC1 12 DC2 13 DC3 14 DC4 15 NAK 16 SYN 17 ETB 18 CAN 19 EM 1A SUB 1B ESC 1C FS 1D GS 1E RS 1F US 20 SP 21! 22 " 23 # 24 $ 25 % 26 & 27 28 ( 29 ) 2A * 2B + 2C, 2D - 2E. 2F / 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7 38 8 39 9 3A : 3B ; 3C < 3D = 3E > 3F? 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G 48 H 49 I 4A J 4B K 4C L 4D M 4E N 4F O 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W 58 X 59 Y 5A Z 5B [ 5C \ 5D ] 5E ^ 5F _ 60 61 a 62 b 63 c 64 d 65 e 66 f 67 g 68 h 69 i 6A j 6B k 6C l 6D m 6E n 6F o 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w 78 x 79 y 7A z 7B { 7C 7D } 7E ~ 7F DEL 7

Scratch Paper 8

Scratch Paper 9