CIS 1068 Program Design and Abstraction Spring2015 Midterm Exam 1. Name SOLUTION

Similar documents
CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Agenda & Reading. Class Exercise. COMPSCI 105 SS 2012 Principles of Computer Science. Arrays

Definition of Regular Expression

COMP 423 lecture 11 Jan. 28, 2008

box Boxes and Arrows 3 true 7.59 'X' An object is drawn as a box that contains its data members, for example:

Lexical analysis, scanners. Construction of a scanner

ASTs, Regex, Parsing, and Pretty Printing

CSCE 531, Spring 2017, Midterm Exam Answer Key

cisc1110 fall 2010 lecture VI.2 call by value function parameters another call by value example:

Reference types and their characteristics Class Definition Constructors and Object Creation Special objects: Strings and Arrays

CMPSC 470: Compiler Construction

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

Fig.25: the Role of LEX

Compilers Spring 2013 PRACTICE Midterm Exam

Product of polynomials. Introduction to Programming (in C++) Numerical algorithms. Product of polynomials. Product of polynomials

Fall 2018 Midterm 1 October 11, ˆ You may not ask questions about the exam except for language clarifications.

Dr. D.M. Akbar Hussain

Fall 2017 Midterm Exam 1 October 19, You may not use any books, notes, or electronic devices during this exam.

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

Discussion 1 Recap. COP4600 Discussion 2 OS concepts, System call, and Assignment 1. Questions. Questions. Outline. Outline 10/24/2010

Mid-term exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Oct 25, Student's name: Student ID:

Spring 2018 Midterm Exam 1 March 1, You may not use any books, notes, or electronic devices during this exam.

Functor (1A) Young Won Lim 8/2/17

Slides for Data Mining by I. H. Witten and E. Frank

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

Functor (1A) Young Won Lim 10/5/17

Lists in Lisp and Scheme

License Manager Installation and Setup

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

Presentation Martin Randers

From Dependencies to Evaluation Strategies

Union-Find Problem. Using Arrays And Chains. A Set As A Tree. Result Of A Find Operation

Pointers and Arrays. More Pointer Examples. Pointers CS 217

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

Summer Review Packet For Algebra 2 CP/Honors

Lexical Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Example: 2:1 Multiplexer

CS201 Discussion 10 DRAWTREE + TRIES

How to Design REST API? Written Date : March 23, 2015

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have

Simplifying Algebra. Simplifying Algebra. Curriculum Ready.

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Ma/CS 6b Class 1: Graph Recap

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or

Agilent Mass Hunter Software

Data sharing in OpenMP

EXPONENTIAL & POWER GRAPHS

Unit 5 Vocabulary. A function is a special relationship where each input has a single output.

What are suffix trees?

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis

CS 241 Week 4 Tutorial Solutions

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples

10.5 Graphing Quadratic Functions

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

Calculus Differentiation

Midterm 2 Sample solution

ECE 468/573 Midterm 1 September 28, 2012

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications

Virtual Machine (Part I)

Ma/CS 6b Class 1: Graph Recap

Phylogeny and Molecular Evolution

CMSC 331 First Midterm Exam

INDIAN COMMUNITY SCHOOL INFORMATICS PRACTICES 2012

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lecture Writing Classes

MTH 146 Conics Supplement

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University of the Negev

Reducing a DFA to a Minimal DFA

Graphing Conic Sections

Exam #1 for Computer Simulation Spring 2005

9.1 apply the distance and midpoint formulas

TO REGULAR EXPRESSIONS

CS 430 Spring Mike Lam, Professor. Parsing

Lily Yen and Mogens Hansen

Symbol Table management

Position Heaps: A Simple and Dynamic Text Indexing Data Structure

Lecture Overview. Knowledge-based systems in Bioinformatics, 1MB602. Procedural abstraction. The sum procedure. Integration as a procedure

Section 10.4 Hyperbolas

Lexical Analysis: Constructing a Scanner from Regular Expressions

Start Here. Remove all tape and lift display. Locate components

Suffix Tries. Slides adapted from the course by Ben Langmead

Fall 2018 Midterm 2 November 15, 2018

PARALLEL AND DISTRIBUTED COMPUTING

Rational Numbers---Adding Fractions With Like Denominators.

Information Retrieval and Organisation

Assignment 4. Due 09/18/17

View, evaluate, and publish assignments using the Assignment dropbox.

CSC141, Computer Science I, Instructor: Dr. Zhen Jiang, Test 2

Topic 2: Lexing and Flexing

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

Applied Databases. Sebastian Maneth. Lecture 13 Online Pattern Matching on Strings. University of Edinburgh - February 29th, 2016

COMBINATORIAL PATTERN MATCHING

Basics of Logic Design Arithmetic Logic Unit (ALU)

Engineer To Engineer Note

Transcription:

CIS 1068 Progrm Design nd Astrction Spring2015 Midterm Exm 1 Nme SOLUTION Pge Points Score 2 15 3 8 4 18 5 10 6 7 7 7 8 14 9 11 10 10 Totl 100 1 P ge

1. Progrm Trces (41 points, 50 minutes) Answer the questions elow out ech snippet of code. For the ske of revity, I hve left off the lines "pulic clss <ClssNme> {" nd "pulic sttic void min(string [] rgs) {" from most of these questions. Pretend they re there.. (4 points) Numer Clcultion: wht re the vlues of w, x, y, nd z fter these lines execute? int w; int x = 3, y = 8; int z = y % x; y += 1 + z; w x y z? 3 11 2. (3 points) Vlue of oolen vrile nd precedence order: wht re the vlues of,, nd c fter these lines execute? oolen ; oolen = 2 < 5 / 2 && true; oolen c = (! && 1 > 6 % 2); c. (3 points) Type Comptiility: wht re the vlues of,, nd c fter these lines execute? int = 2 + 3 / 2; doule = 2 + 3 / 2; doule c = 2 + 3 / 2.0;? 3 flse true d. (1 point) String: wht does this code disply on the screen? c c 3.0 3.5 int = 0; oolen = flse; String s = ""; System.out.println( + s + ); 0flse e. (4 points) Arry: wht re the vlues of,, c, nd d fter these lines execute? int [ ] ; int [ ] = null; int [ ] c = new int[4]; int [ ] d = {2, 3, 4; c[2] = 1; d[0] = d[1] d.length; c d? null 0 3 4 0 0 1 0 2 P ge

f. (8 points) For ech column, show wht the code displys on the screen. Code Output: int x = 2; int y = 3; int z = 4; z=5 if(x + y > z) z ++; System.out.println("z=" + z); int x = 1; int y = 2; oolen = true; true oolen c = flse; if( && x >= y) { System.out.println(x); else if (c x < y) { System.out.println(); else System.out.println(c); int [] x = {1, 2, 3; int [] y = {1, 3, 5, 7; if(x.length>y.length) { x[y.length-1] = y[y.length-1]; 17 else if (x[2] > y[2]) { y[3] = x[2]; else { y[1] = x[1] + x[2]; System.out.println(y[1]+y[2]+y[3]); oolen = true; int i = 1; 2 while () { true = i ++ <2; 3 System.out.println(i); flse System.out.println(); 3 P ge

g. (18 points) Wht re the vlues of,, nd c t the end of the code in ech column? Code Vlue of,, nd c int [] = {3, -2, 4, 5; int = 0, c = 0; for( =0; <.length; ++) { 10 5 3 2 c = c + []; 4 for( =0; <.length; ++) { [] = c / (+1); c 10 int [] = {1, -2, 4, 3; int = [0]; int c = 0; for( c=0; c <.length; c++) { 1 1 4 4 if ([c] > ) { = [c]; [c] = ; c 4 4 String = "You-see-you- drive-you."; String u = "you"; String v = "they"; int =.indexof(u); String c = ""; -1 "." while(>=0) { String word =.sustring(0,); c = c + word + v; =.sustring(+u.length()); =.indexof(u); c = c + ; c "You-see-they-drive-they." 4 P ge

2. Fill the lnks (8 points, 10 minutes) For the requirement specified in the elow, complete the corresponding loop progrm so tht the computer cn print out the expected result.. Write progrm to print out the result of 1+2+3+4+ +99. int totl = 1 ; int i = 2; while (i<100){ totl=totl+i; i++; System.out.print(totl);. Write progrm to print out the result of 1+2+4+8+ +4096. int totl = 0 ; int i = 1; while (i <= 4096 ){ totl=totl+i; i *= 2; System.out.print(totl); c. Write progrm to print out the result of 1+2+4+8+ +4096. int totl = 1 ; int i = 1; while (i<3000){ i *= 2; totl=totl+ i ; System.out.print(totl); d. Write progrm tht reds in n integer n from the keyord, nd displys whether n is "perfect numer" or not.a numer is "perfect" if it is equl to the sum of ll of its fctors (not including itself s fctor, ut including 1 s fctor). 6 is the first perfect numer, ecuse its fctors re 1, 2, nd 3, nd 1+2+3 = 6. int n = keyord.nextint(); int totl = 1; for( int fctor = 2 ; fctor < n ; fctor++ ) if (n%fctor ==0) totl += fctor; System.out.println(n == totl); 5 P ge

3. Writing short progrms (39 points, 40 minutes) For ech prolem elow, write some Jv code to solve it. You do not need to define clss or the min method, or import ny clsses. For this question, you should ssume tht ll necessry clsses (e.g., Scnner, Arrys) hve een imported. Furthermore, ssume tht the following line precedes every question elow, for you to use if you wnt: Scnner keyord = new Scnner(System.in);. (7 points) Write code tht reds in word W nd numer N from the user (i.e., vi keyord). Your code should disply W on N lines. For exmple, if the user enters "hello" for W nd 3 for N, your code should disply: String W = keyord.nextline(); int N = keyord.nextint(); for(int i = 0; i< N; i++) System.out.println(W);. (7 points) Write code tht reds in n integer N vi the keyord, nd displys ll the products of 7, from 1 x 7 up to N x 7. For instnce, if the user enters 4, the progrm should disply: int N = keyord.nextint(); for( int i = 1; i<=n; i++) System.out.println(i+ x7= +i*7); c. (11 points) Write code tht reds in string, nd displys the result of true or flse whether this word (in string) is dollr word. A dollr word is one whose totl vlue is exctly 100 when ech of its chrcter is ssigned vlue ccording to its position in the lphet: =1, =2,, z=26. Some exmples of dollr word re: contented, cookout, mittens, nd shdowing. String = keyord.nextline(); int totl=0; =.tolowercse(); for (int i = 0; i<.length(); i++) totl +=.chrat(i)- +1; System.out.println(totl==100); c. (14 points) Write code tht reds in numer C nd numer R from the user (i.e., keyord), nd displys figure with R rows nd 2xC columns of "$" nd chrcters. For instnce, if the user enters 5 for C nd 4 for R, your progrm should disply the following shpe of "$"s: int C = keyord.nextint(); int R = keyord.nextint(); for( int i = 0; i < R; i++){ int j; for(j = 0; j < C-i; j++) 6 P ge

System.out.print( $ ); for(j = 0; j < 2*i; j++) System.out.print( ); for(j = 0; j < C-i; j++) System.out.print( $ ); System.out.println(); 4. Chllenge: Writing full Jv progrm (12 points, 20 minutes) Write complete Jv progrm clled SecondMAX.jv tht reds in numer of ints from the keyord, nd store them in n rry. After tht, find the second minimum vlue in this rry, nd disply it on the screen. The numer of ins, i.e., the length of the rry, must e entered y the user, efore the rry element input strts. import jv.util.scnner; pulic clss SecondMx{ pulic sttic void min(string [] rgs){ Scnner k = new Scnner (System.in); int size = k.nextint(); int [] rr = new int[size]; int iggest, iggest2nd; for (int i = 0; i<size; i++) rr [i] = k.nextint(); if (rr[0]<rr[1]){ iggest = rr[1]; iggest2nd = rr[0]; else{ iggest = rr[0]; iggest2nd = rr[1]; for (int i =2; i<size; i++){ if(rr[i]>iggest){ iggest2nd = iggest; iggest = rr[i]; else if (rr[i]>iggest2nd) { iggest2nd = rr[i]; System.out.println(iggest2nd); 7 P ge