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

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

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

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

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

Data Representation and Applets

Applets and the Graphics class

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

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

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

Data Representation and Applets

Data Representation and Applets

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

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

Algorithms and Conditionals

Chapter 3. Selections

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

Chapter 2 Exercise Solutions

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

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

Practice Midterm 1 Answer Key

Data Representation and Applets

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

Selection Statements and operators

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

CSC 1051 Data Structures and Algorithms I

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

CSC 1051 Data Structures and Algorithms I

Practice Midterm 1. Problem Points Score TOTAL 50

COMP 202 Java in one week

CS111: PROGRAMMING LANGUAGE II

CSC 1051 Data Structures and Algorithms I

Selection Statements and operators

Full file at

2.8. Decision Making: Equality and Relational Operators

Oct Decision Structures cont d

Java I/O and Control Structures

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

Introduction to Computer Science Unit 2. Exercises

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

Selection and Repetition Revisited

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

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

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

AP Computer Science Unit 1. Writing Programs Using BlueJ

Full file at

Java I/O and Control Structures Algorithms in everyday life

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

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

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

AP Computer Science Unit 1. Programs

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

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 );

Section 2.2 Your First Program in Java: Printing a Line of Text

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Section 2.2 Your First Program in Java: Printing a Line of Text

What did we talk about last time? Examples switch statements

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Selection and Repetition Revisited

CS111: PROGRAMMING LANGUAGE II

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Lecture Set 2: Starting Java

AP COMPUTER SCIENCE A

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

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

AP CS Unit 3: Control Structures Notes

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

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

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

COMP 202. Java in one week

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Program Fundamentals

Example Program. public class ComputeArea {

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

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

Programming with Java

Lecture Set 2: Starting Java

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

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

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Mr. Monroe s Guide to Mastering Java Syntax

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Selection and Repetition

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Introduction to Computer Science Unit 2. Notes

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

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Elementary Programming

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Question: Total Points: Score:

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

COMP-202 Unit 4: Programming with Iterations

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Conditional Programming

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

Midterm Examination (MTA)

Give one example where you might wish to use a three dimensional array

Supplementary Test 1

Transcription:

CSC 1051 Algorithms and Data Structures I Midterm Examination February 24, 2014 Name: KEY 1 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 or the extra blank page at the end and clearly indicate where the answer can be found. Good luck!

1. [ /10] Short answer questions. a) Recall that RGB images use one byte to represent each of the primary color components of each pixel. Using this scheme, approximately how many megabytes are required to represent a picture that is 3000 pixels across and 1000 pixels high? Explain. 3000 x 1000 x 3 = 9 MB b) List all binary codes that can be made with 3 bits 000 010 011 001 111 100 101 110 c) What does RAM stand for? How does it differ from ROM? Random Access Memory RAM can be changed; ROM is read-only and comparatively very small d) Whitespace (i.e., blanks, tabs, new lines) are generally ignored by the Java compiler almost everywhere except when they are part of a : 1. comment 2. String 3. compound statement inside a while loop 4. the start of an if/else 5. variable name (circle all that apply)

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] Short answer questions. a) A physics student gets unexpected results when using the code: F = (G * mass1 * mass2) / (r * r); to compute values according to the formula F =!!!!!!!. Explain the problem and correct the code. Enforce precedence using () b) The code below is supposed to print the numbers from 1 to 10, but it has an error. int count = 1; while (count <= 10) System.out.println (count); count++; i) Describe the error and how to correct it? Missing Braces ii) If the error is not corrected, what, if anything gets printed? 1 is infinitely printed iii) Is this a syntax, runtime, or logical error? Logical

4. [ /10] The following program is supposed to determine whether a positive integer mynum is prime (i.e., has no divisors other than 1 and itself). It does this by checking if any values n are divisors, using a boolean variable gotit to keep track of whether a divisor has been found. import java.util.scanner; public class PrimeTester public static void main (String[] args) Scanner scan = new Scanner (System.in); System.out.println("Please enter a positive integer."); int mynum = scan.nextint(); int n = 2; boolean gotit = false;? System.out.print ("This number is "); if (gotit) System.out.print ("NOT "); System.out.println ("prime.") a)which of these versions of the code should go in the box above? (circle correct one) while (n < mynum) if (mynum % n == 0) gotit = true; else gotit = false; n++; while (n < mynum) if (mynum % n == 0) gotit = true; n++; while (n <= mynum) if (mynum % n == 0) gotit = true; else gotit = false; n++; while (n <= mynum) if (mynum % n == 0) gotit = true; n++; Note: The differences are the < vs. <= and the if/else vs. just if (no else) b) Show the output produced by the program, given to the following inputs: 23 This number is prime 15 This number is NOT prime 2 This number is prime

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 CSC1051 Data Structures and Algorithms I Dr. Papalaskari hand and ball width is the same = 10 Spring pixels 2014

6. [ /10] What gets printed by the following program? // Guess what this does public class Midterm public static void main(string args[]) int x = 10; int a = 20; int b = 30; x = a; a = b; b = 40; String mood = "happy"; Output System.out.println(" Welcome to the \nmidterm \"experience\""); System.out.println(" x = " + x + " a = " + a + " b = " + b); System.out.print(" and this: " + (2 + 3)); System.out.print(" Try this: " + 2 + 3); System.out.println(" and this also: " + "2 + 3"); System.out.println(" This is a " + mood + " " + mood + " day!"); Welcome to the Midterm "experience" x = 20 a = 30 b = 40 and this: 5 Try this: 23 and this also: 2+3 This is a happy happy day!

7. [ /10] Given the following declarations: int iresult, num1= 5, num2 = 2; double fresult, val1 = 8.0; Show the results if the following assignment statements are executed (or write ERROR if the statement causes an error). The resulting value of the expression that will be stored in the variable and its type The kind of data conversion, i.e., one of the following: o none (no data conversion of any kind) o automatic (through assignment or mixed type experession) o cast (specify whether widening or narrowing) Note that there may be none or more than one conversion be sure to list all, if any FOR EACH ANWER WRITE AN EXPLANATION fresult = num1; Source code value stored in variable 5.0 Type of value double Data Conversion(s) int -> d (automatic) fresult = (double) num1 / num2; 2.5 double num1 -> cast (Widening) num2 -> automatic (Widening) iresult = (int) val1; 8 int cast -> int fresult =(double)(num1 / num2); 2.0 double cast -> double

8. [ / 10] What gets printed? Please show output as it will appear, or indicate NO OUTPUT, or show some of the output followed by INFINITE LOOP. int a = 6; while (a < 8) a++; System.out.println(a); Output: 7 8 int a = 6; Output: infinite Loop while (a < 8) System.out.println(a); a--; int a = 6; while (a > 0) System.out.println(a); a = a - 2; Output: 6 4 2 int a = 6; while (a < 7) System.out.println (a); a++; Output: 6

9. ( / 10) Construct an algorithm that inputs a number num and then prints Hello that many times. After the Hello s are printed, print a goodbye message. Example: If num (i.e., the input) is 5, the algorithm should print something like this: Hello Hello Hello Hello Hello Goodbye 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. input num input count count = 1 count = 0 count = count + 1 num = num + 1 if (count < num) else while (count <= num) while (count!= 5) while (count <= 5) print Hello print num print Goodbye input num count = 0 while (count < num) print "Hello" count = count +1 print "Goodbye"

10. ( / 10) Write a complete Java program that asks the user to input a value representing a number of seconds, and then prints the equivalent amount of time as a combination of hours, minutes, and seconds. (For example, 9999 seconds is equivalent to 2 hours, 46 minutes and 39 seconds.) Be sure to write a complete Java program, including class definition, variable and constant declarations, as appropriate, comments, and proper indentation, to make it readable. import java.util.scanner; public class Seconds public static void main(string[] args) Scanner scan = new Scanner(System.in); int seconds = scan.nextint(); int hours = seconds / 3600; seconds = seconds - (hours * 3600); int min = seconds / 60; seconds = seconds - (min * 60); System.out.print(hours + " " + min + " " + seconds);

CSC 1051 Algorithms and Data Structures I Midterm Examination February 24, 2014 Name: KEY 2 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 or the extra blank page at the end and clearly indicate where the answer can be found. Good luck!

1. [ /10] Short answer questions. e) Recall that RGB images use one byte to represent each of the primary color components of each pixel. Using this scheme, approximately how many megabytes are required to represent a picture that is 3000 pixels across and 1000 pixels high? Explain. 3000 x 1000 x 3 = 9 MB f) List all binary codes that can be made with 3 bits 000 010 011 001 111 100 101 110 g) What does RAM stand for? How does it differ from ROM? Random Access Memory RAM can be changed; ROM is read-only and comparatively very small h) Whitespace (i.e., blanks, tabs, new lines) are generally ignored by the Java compiler almost everywhere except when they are part of a : 6. comment 7. String 8. compound statement inside a while loop 9. the start of an if/else 10. variable name

(circle all that apply) 2. [ /10] Refer to the program below. Next to each word in the list, choose the most fitting description: reserved word constant variable method nextint Method if Reserved Word scan Variable println Method import Reserved Word public Reserved Word MINOR Constant class Reserved Word age Variable int 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] Short answer questions. a) A physics student gets unexpected results when using the code: F = (G * mass1 * mass2) / (r * r); to compute values according to the formula F =!!!!!!!. Explain the problem and correct the code. Enforce precedence using () b) The code below is supposed to print the numbers from 1 to 10, but it has an error. int count = 1; while (count <= 10); System.out.println (count); count++; i) Describe the error and how to correct it? Incorrect semicolon after while ii) If the error is not corrected, what, if anything gets printed? Nothing infinite loop iii) Is this a syntax, runtime, or logical error? Logic

4. [ /10] The following program is supposed to determine whether a positive integer mynum is prime (i.e., has no divisors other than 1 and itself). It does this by checking if any values n are divisors, using a boolean variable gotit to keep track of whether a divisor has been found. import java.util.scanner; public class PrimeTester public static void main (String[] args) Scanner scan = new Scanner (System.in); System.out.println("Please enter a positive integer."); int mynum = scan.nextint(); int n = 2; boolean gotit = false;? System.out.print ("This number is "); if (gotit) System.out.print ("NOT "); System.out.println ("prime.") a)which of these versions of the code should go in the box above? (circle correct one) while (n <= mynum) if (mynum % n == 0) gotit = true; else gotit = false; n++; while (n <= mynum) if (mynum % n == 0) gotit = true; n++; while (n < mynum) if (mynum % n == 0) gotit = true; else gotit = false; n++; while (n < mynum) if (mynum % n == 0) gotit = true; n++; Note: The differences are the < vs. <= and the if/else vs. just if (no else) b) Show the output produced by the program, given to the following inputs: 33 This number is NOT prime 13 This number is prime 1 This number is prime

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 MID = 50; b) Add some code to the program below to make the snowman look like he is holding a red ball, i.e.: add a hand 10 pixel long horizontal line, start at arm add a red ball, 10 by 10 pixels, resting on the hand. be sure the ball is red, 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 red 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 CSC1051 Data Structures and Algorithms I Dr. Papalaskari hand and ball width is the same = 10 Spring pixels 2014

6. [ /10] What gets printed by the following program? // Guess what this does public class Midterm public static void main(string args[]) int x = 1; int a = 2; int b = 3; x = a; a = b; b = 4; String mood = "happy"; Output System.out.println(" Welcome to the \nmidterm \"experience\""); System.out.println(" x = " + x + " a = " + a + " b = " + b); System.out.print(" Try this: " + 2 + 3); System.out.print(" and this: " + (2 + 3)); System.out.println(" and this also: " + "2 + 3"); System.out.println(" This is a " + mood + " " + mood + " day!"); Welcome to the Midterm "experience" x = 2 a = 3 b = 4 Try this: 23 and this: 5 and this also: 2+3 This is a happy happy day!

7. [ /10] Given the following declarations: int iresult, num1= 5, num2 = 2; double fresult, val1 = 8.0; Show the results if the following assignment statements are executed (or write ERROR if the statement causes an error). The resulting value of the expression that will be stored in the variable and its type The kind of data conversion, i.e., one of the following: o none (no data conversion of any kind) o automatic (through assignment or mixed type experession) o cast (specify whether widening or narrowing) Note that there may be none or more than one conversion be sure to list all, if any FOR EACH ANWER WRITE AN EXPLANATION fresult = num1; Source code value stored in variable 5.0 Type of value double Data Conversion(s) Int -> double automatic fresult = (double) num1 / num2; 2.5 double Num1 Cast (Widening) Num2 Automatic iresult = val1; ERROR fresult =(double)(num1 / num2); 2.0 double num1/num2 none cast to double widening

8. [ / 10] What gets printed? Please show output as it will appear, or indicate NO OUTPUT, or show some of the output followed by INFINITE LOOP. int a = 5; while (a < 8) a++; System.out.println(a); Output: 6 7 8 int a = 5; Output: infinite loop while (a < 8) System.out.println(a); a--; int a = 5; while (a > 0) System.out.println(a); a = a - 2; Output: 5 3 1 int a = 5; while (a < 5) System.out.println (a); a++; Output:No Output

9. ( / 10) Construct an algorithm that inputs a number num and then prints Hello that many times. After the Hello s are printed, print a goodbye message. Example: If num (i.e., the input) is 5, the algorithm should print something like this: Hello Hello Hello Hello Hello Goodbye 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. input num input count count = 1 count = 0 count = count + 1 num = num + 1 if (count < num) else while (count <= num) while (count!= 5) while (count <= 5) print Hello print num print Goodbye input num count = 1 while (count <= num) print "Hello" count = count +1 print "Goodbye"

10. ( / 10) Write a complete Java program that asks the user to input an integer representing a number of days and then calculates and prints the equivalent as a number of weeks and days. For example, if the user inputs 18 for the number of days, the output should state that it is equivalent to 2 weeks and 4 days. Be sure to write a complete Java program, including class definition, variable and constant declarations, as appropriate, comments, and proper indentation, to make it readable. import java.util.scanner; public class Days public static void main(string[] args) Scanner scan = new Scanner(System.in); int days = scan.nextint(); int weeks = days/7; days = days % 7); System.out.println(weeks + " " + days);