CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

Similar documents
CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Applets and the Graphics class

Data Representation and Applets

Data Representation and Applets

Data Representation and Applets

Chapter 2 Exercise Solutions

Data Representation and Applets

CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name:

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

Practice Midterm 1 Answer Key

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Midterm Examination

Practice Midterm 1. Problem Points Score TOTAL 50

Happy Cinco de Mayo!!!!

Using Java Classes Fall 2018 Margaret Reid-Miller

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

Happy Cinco de Mayo!!!!

Chapter 3. Selections

Chapter 2: Objects and Primitive Data

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2010) - All Sections Midterm Examination

CSCI 2010 Principles of Computer Science. Basic Java Programming. 08/09/2013 CSCI Basic Java 1

Preparation: Get to know the Java coordinate system. 3. How is the background color of the window specified? Copy the exact code:

CSCI 1226 A Test #1. Wednesday, 10 October, 2018 Name: Student #: General Instructions Read and follow all directions carefully.

Objects and Primitive Data

Introduction to Computer Science Unit 2. Notes

CS1004: Intro to CS in Java, Spring 2005

AP Computer Science Unit 1. Programs

AP CS Unit 3: Control Structures Notes

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Methods CSC 121 Fall 2016 Howard Rosenthal

Name CS/120 Sample Exam #1 -- Riley. a) Every program has syntax, which refers to the form of the code, and, which refers to the meaning of the code.

Computer Science 145 Midterm 1 Fall 2016

Object-Based Programming. Programming with Objects

CSC 1051 Algorithms and Data Structures I. Final Examination December 18, Name:

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

Algorithms and Conditionals

Using Classes and Objects

Introduction to Computer Science Unit 2. Notes

Selection Statements and operators

Methods CSC 121 Spring 2017 Howard Rosenthal

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Selection and Repetition Revisited

Lecture Static Methods and Variables. Static Methods


Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

COMP 202 Java in one week

CSC 1051 Data Structures and Algorithms I

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class

Computational Expression

CSE 142 Su01 Midterm 1 Sample Solution page 1 of 5

Computational Expression

COMP 202. Java in one week

Selection Statements and operators

Introduction to Java Applications

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

Designing Classes Part 2

Fundamentals of Programming Data Types & Methods

DUBLIN CITY UNIVERSITY

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

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

CSC 1051 Data Structures and Algorithms I

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

Appendix F: Java Graphics

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

AP Computer Science Unit 1. Writing Programs Using BlueJ

Primitive Data Types: Intro

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

CS110: PROGRAMMING LANGUAGE I

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

Chapter 7 Exercise Solutions

DUBLIN CITY UNIVERSITY

COMP102: Test 2 Model Solutions

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

Higher National Diploma in Information Technology First Year, Second Semester Examination 2015

What did we talk about last time? Examples switch statements

CSC 1051 Data Structures and Algorithms I

CS111: PROGRAMMING LANGUAGE II

Repetition CSC 121 Fall 2014 Howard Rosenthal

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

We now start exploring some key elements of the Java programming language and ways of performing I/O

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Appendix F: Java Graphics

Transcription:

CSC 1051 Algorithms and Data Structures I Midterm Examination October 9, 2014 Name: KEY Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces provided. If you make a mistake or for some other reason need more space, please use the back of pages and clearly indicate where the answer can be found. Good luck!

1. [ /10] Short answer questions. a) How many bits/bytes are needed to store a color picture that is 200 pixels wide and 400 pixels high under each of the following schemes? Express your answer as approximate number of KB or MB, etc., as appropriate. Show your work and fill in the answers below: Total # pixels 80,000 200x400 = 80,000 Bitmap (1bit/pixel) Greyscale (1byte/pixel) RGB (3 bytes/pixel) # bytes _10 KB 80 KB 240 KB # bits 80,000 bits or 80kbits_ 640,000 or 640Kbits 1,920,000 or 2Mbits_ b) List all binary codes that can be made with 3 bits 000 010 011 001 111 100 101 110 c) With n bits we can have 2 n different binary codes

2. [ /10] Refer to the program below. Next to each word in the list, choose the most fitting description: reserved word constant variable method age Variable int Reserved Word nextint Method if Reserved Word scan Variable println Method import Reserved Word public Reserved Word MINOR Constant class Reserved Word import java.util.scanner; public class Age //----------------------------------------------------------------- // Reads the user's age and prints comments accordingly. //----------------------------------------------------------------- public static void main (String[] args) final int MINOR = 21; Scanner scan = new Scanner (System.in); System.out.print ("Enter your age: "); int age = scan.nextint(); System.out.println ("You entered: " + age); if (age < MINOR) System.out.println ("Youth is a wonderful thing. Enjoy."); System.out.println ("Age is a state of mind.");

3. [ /10] What output is produced by the following program? public class OneMoreTime public static void main (String[] args) System.out.print ("Howdy there... "); System.out.println ("I promise\nthis is the \n\ last time\ "); System.out.print ("you have to do this "); System.out.println ("so\nplease count the \\\ s carefully!"); Output: System.out.println ("And numbers... " + 20 + 30); System.out.println ("Is this different?... " + (20 + 30)); Howdy there... I promise this is the "last time" you have to do this so please count the \'s carefully! And numbers... 2030 Is this different?... 50 b) Write a single println statement that would output "Hello" "Goodbye" System.out.println ("\"Hello\"\n\t\"Goodbye\"");

4. [ /10] Given the following declarations: int iresult, num1 = 7, num2 = 3; double fresult, val1 = 9.0; boolean status, part1 = false; What result is stored by each of the following assignment statements? Source code Result stored fresult = (num1 + 2)/ 2; 4 iresult = num1 % num2; 1 fresult = val1 / 2; 4.5 fresult = (double) num1 / 2; 3.5 status = part1 && (num1 > num2); false status = part1 (num1 > num2); true

5. [ /10] Below is the code for the Snowman applet and the image it produces. a) Sketch the image produced by modifying the code indicated by the arrow, as follows: final int TOP = 20; b) Add some code to the program below to make the snowman look like he is holding a green ball, i.e.: add a hand 10 pixel long horizontal line, start at arm add a green ball, 10 by 10 pixels, resting on the hand. be sure the ball is green, but the hand is black see illustration below import javax.swing.japplet; import java.awt.*; public class Snowman extends JApplet public void paint (Graphics page) final int MID = 150; final int TOP = 50; page.setcolor (Color.cyan); page.fillrect (0, 0, 300, 175); // sky page.setcolor (Color.blue); page.fillrect (0, 175, 300, 50); // ground page.setcolor (Color.yellow); page.filloval (-40, -40, 80, 80); // sun page.setcolor (Color.white); page.filloval (MID-20, TOP, 40, 40); // head page.filloval (MID-35, TOP+35, 70, 50); // upper torso page.filloval (MID-50, TOP+80, 100, 60); // lower torso page.setcolor (Color.black); page.filloval (MID-10, TOP+10, 5, 5); page.filloval (MID+5, TOP+10, 5, 5); // left eye // right eye page.drawarc (MID-10, TOP+20, 20, 10, 190, 160); // smile page.drawline (MID-25, TOP+60, MID-50, TOP+40); // left arm page.drawline (MID+25, TOP+60, MID+55, TOP+60); // right arm page.drawline (MID-20, TOP+5, MID+20, TOP+5); // brim of hat page.fillrect (MID-15, TOP-20, 30, 25); // top of hat //*** code for left hand holding green ball goes here *** page.drawline (MID-60, TOP+40, MID-50, TOP+40); // left hand page.setcolor (Color.red); page.filloval (MID-60, TOP+30, 10, 10); // red ball in hand hand and ball width is the same = 10 pixels

6. [ / 10] What gets printed? Please show output as it will appear or indicate NO OUTPUT or INFINITE LOOP. int a = 4; while (a > 0) System.out.println(a); a++; Output: 4 5 6 INFINITE LOOP int a = 4; while (a > 0) System.out.println(a); a = a - 2; Output: 4 2 int a = 1; while (a < 4) a++; System.out.println(a); Output: 2 3 4 int a = 4; while (a < 4) System.out.println (a); a++; Output: NO OUTPUT

7. [ / 10] Let s look at the problem of repeatedly obtaining input and performing a calculation, for example, computing the area of a circle given its radius, using the following algorithm: Variables: radius, area Algorithm: input radius area= radius*radius* PI print area Rewrite this algorithm, modifying it so that it uses a while structure to repeat the processing of each input in two different ways. a) Compute the areas of 5 circles (exact count). Variables: radius, area, count Algorithm: count= 1 while (count <= 5) input radius area = radius * radius* PI print area count = count + 1 b) Keep computing circle areas until user inputs - 1 for the radius (sentinel value) Variables: radius, area Algorithm: input radius while (radius!= - 1) area= radius * radius* PI print area input radius

8. [ / 10] Suppose you look up a method in the Java API and find this method heading: void getthisdone(string x, double y) a) Fill in the following information about this method: Method name: getthisdone Method return type: void Required paramaters for the method: (how many? of what type(s)?) two, a String and a double b) Suppose this method is defined in class Midterm and you have an object mid1 of that class. Give an example of the use of the dot operator to invoke this method. (Hint: You need to use Midterm or mid1 but not both.) getthisdone.mid1( hello world, 4.5);

9. [ / 10] a) Suppose the String variable message is already initialized. Write a Java code fragment that prints out message, backwards. int count = message.length() - 1; while (count >= 0) System.out.print(message.charAt(count)); count--; b) Write some code that uses the Math class to compute and print out the value of cos(π/2). System.out.println(Math.cos( Math.PI /2) ); c) Write code to declare and instantiate an object of the Random class (call the object reference variable rand). Random rand = new Random(); d) Write an expression using the nextint method that generates random numbers in the range 1 to 1000, including the endpoints. Use the version of the nextint method that accepts a single integer parameter. rand.nextint(1000) + 1

10. ( / 10) Construct an algorithm that inputs 10 positive numbers and prints the maximum of these numbers. Example: If the numbers input are 44 7 31 22 53 16 21 48 72 60, the output should be: max = 72 Directions: Write your algorithm by rearranging and structuring elements chosen from the list below, using indentation to show structure. Do not use anything else and note that not all of these are needed, but you may use one of them more than once, if necessary. num = 0 count =0 count = 1 max = 0 count = count + 1 num = max max = num print max = max input num input max input count if (num > max) if (count < max) else while (count <= 10) while (count < 10) while (num < max) while (num!=max) count = 0 max = 0 while (count < 10) input num if (num > max) max = num count = count + 1 print max = max ALTERNATIVELY we can count from 1 to 10: count = 1 max = 0 while (count <= 10) input num if (num > max) max = num count = count + 1 print max = max

REFERENCE MATERIAL 20 45 Y 20 Y 10 Drawing a Line 150 Start% x%%%%%%%y% End% x%%%%%%%y% page.drawline (10, 20, 150, 45); page.drawline (150, 45, 10, 20); Drawing an Oval 175 80 50 page.drawoval (175, 20, 50, 80); X X Drawing a Rectangle 50 X 20 40 100 Y Start% x%%%%%%%y% Width%%%%Height% page.drawrect (50, 20, 100, 40); Drawing an Arc An arc is defined by an oval, a start angle, and an arc angle: Fall 2014

REFERENCE MATERIAL Random class double nextdouble() Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 int nextint() Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. int nextint(int n) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. Some methods of the Math class static double abs(double a) Returns the absolute value of a double value. static double cos(double a) Returns the trigonometric cosine of an angle. static double pow(double a, double b) Returns the value of the first argument raised to the power of the second argument. static double random() Returns a double value greater than or equal to 0.0 and less than 1.0. static long round(double a) Returns the closest long to the argument. static double sin(double a) Returns the trigonometric sine of an angle. static double sqrt(double a) Returns the correctly rounded positive square root of a double value. String class boolean isempty() Returns true if, and only if, length() is 0. char charat(int index) Returns the char value at the specified index. int length() Returns the length of this string. String touppercase() Converts all of the characters in this String to upper case using the rules of the default locale. Fall 2014