ECS 140A Midterm 2 November 19, Name (Last, First):, Sign your name:

Similar documents
About this exam review

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

CSCI 201L Written Exam #1 Fall % of course grade

CS 116 Week 8 Page 1

Midterm II CS164, Spring 2006

Prelim 1. Solution. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

ECE 462 Fall 2011, Third Exam

Midterm Exam CS 251, Intermediate Programming October 8, 2014

CSCI 201L Written Exam #1 Fall % of course grade

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS 415 Midterm Exam Spring SOLUTION

Midterm Exam CS 251, Intermediate Programming March 6, 2015

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

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CS164: Midterm Exam 2

Review of Important Topics in CS1600. Functions Arrays C-strings

Design Patterns: State, Bridge, Visitor

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

CS 345. Functions. Vitaly Shmatikov. slide 1

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

ECE Fall 20l2, Second Exam

Pace University. Fundamental Concepts of CS121 1

( &% class MyClass { }

CS110D: PROGRAMMING LANGUAGE I

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Java. Massimo Dong. November 27, Massimo Dong Java November 27, / 26

CS 415 Midterm Exam Spring 2002

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Result: original lp in main is. Goal was.

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Lecture08: Scope and Lexical Address

EECS168 Exam 3 Review

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

ECE 462 Fall 2011, Second Exam

CS 61B Discussion 5: Inheritance II Fall 2014

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Concurrency Quiz: Java Threads

Computer Science & Engineering 150A Problem Solving Using Computers

CS 161: Object Oriented Problem Solving

Inheritance, and Polymorphism.

CS61B Lecture #12. Programming Contest: Coming up Saturday 5 October. See the contest announcement page, here.

CS 1331 Exam 1 ANSWER KEY

Last name:... First name:... Department (if not D-INFK):...

Computer Science 1 Ah

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Project Compiler. CS031 TA Help Session November 28, 2011

UMBC CMSC 331 Final Exam

Lesson 36: for() Loops (W11D1)

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Programming Language Concepts, cs2104 Lecture 04 ( )

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

JAVA Programming Language Homework I - OO concept

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

Final Exam. COMP Summer I June 26, points

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Declarations and Access Control SCJP tips

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1


CS Exam #1-100 points Spring 2011

Fundamentals of Object Oriented Programming

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Syntax of Variable Declarations. Variables Usages as Expressions. Self-assignment Statements. Assignment. Scoping and Naming Rules

CS 314 Principles of Programming Languages

Semantic Analysis. Lecture 9. February 7, 2018

COMP322 - Introduction to C++

Lecture 5: Methods CS2301

CMSC 331 Second Midterm Exam

Array. Prepared By - Rifat Shahriyar

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

CS111: PROGRAMMING LANGUAGE II

Midterm Exam 2 CS 455, Spring 2011

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

CSE 131 Introduction to Computer Science Fall Exam II

Array. Array Declaration:

Lecture 22: Java. Overall Structure. Classes & Objects. Every statement must end with ';' Carl Kingsford, , Fall 2015

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

(Not Quite) Minijava

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

Lecture 02, Fall 2018 Friday September 7

Examination in Compilers, EDAN65

Java Review: Objects

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

CS 3360 Design and Implementation of Programming Languages. Exam 1

Casting. References. References

Syntax Errors; Static Semantics

1 Lexical Considerations

Transcription:

Name (Last, First):, Sign your name: Student ID Number (last six digits, e.g., 12-3456): Grade: /100 points This exam consists of 4 questions on 10 sides, printed on both sides of 5 pages; be sure you have the entire exam before starting. The point value of each question is indicated at its beginning; the entire exam has 100 points. Individual parts of a multipart question are generally assigned approximately the same point value; exceptions are noted. This quiz is open notes and text, but closed neighbor. Read through all questions first and pace yourself. Be concise and indicate clearly what your answer is. Presentation and simplicity of your answers may affect your grade. Answer each question in the space following the question. If you find it necessary to continue an answer elsewhere, indicate clearly the location of its continuation and label its continuation with the question number. If you are asked to explain in X words and your answer exceeds X words, then (1) we will consider only the first X words in grading your answer and (2) your answer will lose 20% of the points it would have received. Youneed to show work only where requested. Note well: On questions about HW4, assume a working HW4 implementation using standard techniques described in the handout, given code, lecture, and discussion. If you did something different and it s relevant to your answer to a question, be sure you describe what you did. 1

Question 1. (25points ) (a) (8 points) Consider the following program outline in which BB, CC, and QQ represent some code. begin integer x := 5; B: begin integer b1 := 6; BB end B end C: begin integer c1 := 7; CC end C proc Q() begin QQ end Consider forming independently specific programs as indicated in the table below. Unless otherwise stated, assume BB, CC, and QQ are empty. For each such program, the program s outcome is one of the following: CTE compile-time error : the program causes an error when compiled. RTE run-time error : the program compiles without error, but causes an error when run. NORMAL normal : compiles and runs without errors. In answering, focus on the central question: Can the code access a given variable? No tricks are intended. For example, assume that: the syntax is correct; empty code causes no error; and the language allows an invocation of Q to appear before Q s declaration. For each specific program and each kind of scoping indicated below, circle the correct outcome. static scoping dynamic scoping specific program outcome outcome CC is write(x); CTE RTE NORMAL CTE RTE NORMAL CC is write(b1); CTE RTE NORMAL CTE RTE NORMAL QQ is write(b1); and BB is Q(); CTE RTE NORMAL CTE RTE NORMAL QQ is write(b1); and CC is Q(); CTE RTE NORMAL CTE RTE NORMAL (b) (4 points) As seen in class and on the HW regarding the run-time implementation of static scoping, static pointers or the display are used to locate the proper stackframe for a variable in a lexically enclosing block. (Recall block Z is lexically enclosed by block Y if, in the program code, Z is nested within Y.) (circle one per line) This locating activity occurs within the symbol table at compile time. Once the proper stackframe is located, it is searched to find the variable. 2

(c) (13 points) The code below isidentical to that on the parameter passing question on the HW, except there is only one invocation of swap and that invocation uses actual parameters different from those on the HW. begin integer k; integer array a[1:2] proc swap(x, y, z); integer x, y, z; begin write (x, y, z); z:=x;x:=y;y:=z; end end k:=1;a[1] := 2; a[2] := 3; swap(k, a[k], k); write (k, a[1], a[2]); The above program has no "subscript out of range" or other errors. On the lines below, show the output from executing the above program with xbyname, y by value, and z by reference The point value of each answer is indicated below each line. (2 points) (1 point) (1 point) (3 points) (3 points) (3 points) End of Question 1. ( /25points ) 3

<<continue your answers here if necessary>> 4

Question 2. (25points ) Consider this Java code: interface I { abstract class A { class B extends A implements I { public int foo() {return 9; class C extends B { class D { (13 points) For each assignment below, place a check ( ) under the correct choice of legality determined. assignment i=a; i=b; i=c; a=i; a=(a) i; legality determined statically statically dynamically legal illegal (legal orillegal) and the declarations Aa;Bb;Cc;Dd;Ii; Assume each of these variables has been initialized. (8 points) For each method invocation below, place a check ( ) under the correct choice of legality determined. (Here, we re interested below inonly the static checking.) method invocation int n = a.foo(); int n = b.foo(); int n = c.foo(); int n = i.foo(); legality determined statically statically legal illegal d=(d) i; i=new D(); i=new I(); a=new I(); b=new I(); a=new A(); a=new B(); d=(d) new C(); (4 points) Indicate whether each of the following statements is correct. statement Class A must be declared as abstract because it does not define a concrete foo() Class C is abstract because it does not define a concrete foo() correct? (circle one per line) End of Question 2. ( /25points ) 5

Question 3. (25points ) (a) (7 points) Recall from lecture the relationships: is-a, e.g., a Leopard is a ZooAnimal. has-a, e.g., a Bear has a Color or a Bear has a reference to its name. Based on the relationships among classes on the HW, indicate whether each statement is correct. correct? statement (circle one per line) AJumble is-a Seq. AJumble has-a Seq. AJumble is-a JumbleIt. AJumbleIt is-a Jumble. AJumble has-a JumbleIt. AJumbleIt has-a Jumble. AJumbleIt has-a Seq. 6

(b) (12 points) Assume A standard implementation of C++ that uses the VMT approach for implementing dynamic binding (also known as dynamic dispatch), as discussed in lecture. Class C defines a non-virtual method s and a virtual method t. Cc; C* r; // r might point at a C object or an object of a class derived from C For each invocation, circle exactly one choice to indicate whether or not the implementation uses the VMT for the invocation. invocation uses VMT? (circle one per line) c.s() c.t() r s() r t() (*r).s() (*r).t() (c) (6 points) In a language like C++, the implementation of dynamic binding (dispatch) requires the symbol table to be searched at run-time. whether a particular method is a non-virtual method or a virtual method can be determined statically. whether a particular method invocation is a static binding or a dynamic binding can be determined statically. correct? (circle one per line) End of Question 3. ( /25points ) 7

Question 4. (25points ) For each Java program on the next page, indicate if it causes a compile-time error by circling compile-time error EXOR (exclusive or) show the program s output by filling in the provided lines (sufficient lines are provided). Hint: exactly one causes a compile-time error. The programs use: class MyExcpt1 extends Exception { public MyExcpt1(String msg){ super(msg); class MyExcpt2 extends Exception { public MyExcpt2(String msg){ super(msg); 8

public class Main { public static void main(string [] a) { try { d(); catch (MyExcpt1 e) { System.out.println("=M1 caught"); catch (MyExcpt2 e) { System.out.println("=M2 caught"); private static void d() throw new MyExcpt2("from d");//**2** (i) compile-time error EXOR show output: public class Main { public static void main(string [] a) { try { d(); catch (MyExcpt1 e) { System.out.println("=M1 caught"); catch (MyExcpt2 e) { System.out.println("=M2 caught"); throw new MyExcpt1("from catch 2"); private static void d() throw new MyExcpt2("from d");//**2** (ii) compile-time error EXOR show output: public class Main { public static void main(string [] a) try { d(); catch (MyExcpt1 e) { System.out.println("=M1 caught"); throw new MyExcpt2("from catch 2"); catch (MyExcpt2 e) { System.out.println("=M2 caught"); private static void d() throw new MyExcpt2("from d");//**2** (iii) compile-time error EXOR show output: public class Main { public static void main(string [] a) try { d(); catch (MyExcpt1 e) { System.out.println("=M1 caught"); catch (MyExcpt2 e) { System.out.println("+M2 caught"); try { throw new MyExcpt1("from catch 2"); catch (MyExcpt1 f) { System.out.println("=NESTED caught"); System.out.println("-M2 caught"); private static void d() throw new MyExcpt2("from d");//**2** (iv) compile-time error EXOR show output: End of Question 4. ( /25points ) 9

<<continue your answers here if necessary>> 10