Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Similar documents
Question: Total Points: Score:

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

Question: Total Points: Score:

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

Place your name tag here

Question: Total Points: Score:

Question: Total Points: Score:

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

Question: Total Points: Score:

Question: Total Points: Score:

Instructor: Yu Wang 11/16/2012

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

Question: Total Points: Score:

CS 170 Exam 1. Version: B Fall Name (as on OPUS):

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

CS 170 Exam 1. Version: C Fall Name (as on OPUS):

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

CS 190 Exam 2 Fall 2014

CS 170 Exam 2. Section 004 Fall Name (print): Instructions:

CS170 (005): Introduction to Computer Science Exam 2

CS 155 Exam 2 Spring 2015

CS 110 Exam 2 Spring 2011

Exam Percentage: / 55 = %

Question: Total Points: Score:

CS 110 Exam 2 Fall 2011

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

CS 1301 Exam 1 Fall 2010

CS 1301 Exam 1 Fall 2011

CS 190 Exam 1 Fall 2010

CS 1301 Exam 1 Fall 2010

CS 2316 Exam 4 Fall 2011

Your (printed!) Name: CS 1803 Exam 3. Grading TA / Section: Monday, Nov. 22th, 2010

Midterm Examination (MTA)

CS 1803 Fall 2010 Exam 1

CS 1301 Exam 3 Spring 2014

CS 110 Exam 1 Fall 2011

CS 1803 Fall 2010 Exam 1

CS 2316 Exam 4 Fall 2011

CS 1301 Exam 3 Fall 2014

CS 1301 Exam 2 Fall 2014

CS 1301 Exam 1 Fall 2014

CS Exam 2 - Spring 2010

CS Exam 3 - Spring 2010

CS Exam 3 - Spring 2010

CS 1301 Exam 2 Fall 2013

CS 1301 Exam 1 Fall 2014

CS 1301 Exam 1 Spring 2011

AP Computer Science A Unit 2. Exercises

CS 2316 Exam 1 Spring 2013

CS 1301 Exam 1 Spring 2014

CS 1301 Exam 2 A Fall 2015

CS 2316 Exam 4 Fall 2012

CSE 1223: Exam II Autumn 2016

CS Name : Grading TA:

CS 101 Spring 2007 Midterm 2 Name: ID:

CS 1301 Exam 1 Fall 2013

CS 1301 Exam 2 A Fall 2015

Introduction to Computer Science Midterm 3 Fall, Points

CS 2316 Exam 4 Spring 2013

CS 2316 Exam 1 Spring 2014

CS 2316 Exam 4 Fall 2012

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

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

CS 1301 Exam 1 Spring 2014

CS 1301 Exam 1 Fall 2013

CS 2316 Exam 4 Fall 2014

CS 1301 Exam 1 Spring 2015

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

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

CS 1301 Exam 2 Fall 2010

Practice Midterm 1. Problem Points Score TOTAL 50

Question Possible Points Earned Points Graded By GUI 22 SQL 24 XML 20 Multiple Choice 14 Total Points 80

CS 1301 Exam 1 Fall 2009

CS 2316 Exam 3 Fall 2011

Department of Computer Science Purdue University, West Lafayette

St. Edmund Preparatory High School Brooklyn, NY

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

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CS 1301 Exam 2 Spring 2014

CS 2316 Exam 3 Spring 2013

Flow of Control. Chapter 3

CSCE 206: Structured Programming in C++

Controls Structure for Repetition

CS 1301 Exam 3 Spring 2012

AP Computer Science Unit 1. Programs

CS 1316 Exam 1 Summer 2009

Lecture 5: Methods CS2301

CS Exam 2 Name: Your Grading TA: This exam has 7 pages including the title page. Please check to make sure all pages are included.

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

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

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Transcription:

CS 170 Exam 1 Section 003 Fall 2012 Name (print): Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with anyone other than the professor/proctor for ANY reason in ANY language in ANY manner. This exam is closed notes, closed books, and no calculator. Turn all mobile devices off and put them away now. You cannot have them on your desk. Write neatly and clearly indicate your answers. What I cannot read, I will assume to be incorrect. Stop writing when told to do so at the end of the exam. I will take 5 points off your exam if I have to tell you multiple times. Academic misconduct will not be tolerated. Suspected academic misconduct will be immediately referred to the Emory Honor Council. Penalties for misconduct will be a zero on this exam, an F grade in the course, and/or other disciplinary action that may be applied by the Emory Honor Council. Time: This exam has 7 questions on 8 pages including the title page. Please check to make sure all pages are included. You will have 75 minutes to complete this exam. I commit to uphold the ideals of honor and integrity by refusing to betray the trust bestowed upon me as a member of the Emory community. I have also read and understand the requirements and policies outlined above. Signature: Question: 1 2 3 4 5 6 7 Total Points: 10 15 10 12 6 10 12 75 Score: 1

1. (10 points) Define 5 of the 6 terms below. You do not need a formal definition, just a good description. Limit your answers to less than 50 words per term. You may use an example if it would be helpful. Clearly mark the term you do not wish to include; otherwise, I will discard the last term. (a) encoding Solution: agreement on a representation of some facts by specific numbers mapping between numbers and specific meanings (b) algorithm Solution: precise recipe or steps for solving a problem (c) binary number (or binary number system) Solution: representation of numbers using single bits which have two possible values: on/off, 1/0, true/false. (d) concatenate Solution: appending one string to another which results in a single string. Example "He" + "llo" => "Hello" (e) overflow error Solution: memory does not have enough space (bits) to represent the value you want to store. (f) nested statements Solution: Putting block statement (conditional or loop, usually) inside of another block (again, usually a condition or loop) Ex: if (x < 3) { if (x < 10) {... 2

2. (15 points) Evaluate each expression. Then give the result of the evaluation and the data type of the result. If the expression cannot be evaluated or is not proper Java syntax, you may simply write error for the value. The first row has been done for you. Expression Value Type 4+1 5 int 4+2.0*5 3*2/4 3-4/8.0 (int)3-4/8.0 (int)(3-4/8.0) 3-4%8 "CS170" - "170" "Hello" + 6 * 2.0 "Hello" + 6 + 2.0 "Hello" + "6 + 2.0" true 4==3 3 < 4 <= 10!(4 == 5) 6 > 5 && 4 > 5 m - 4 (char)( m - 4) 14.0 double 1 int 2.5 double 2.5 double 2 int -1 int error Hello12.0 String Hello62.0 String Hello6 + 2.0 String true boolean error true boolean 105 int i char 3

3. (10 points) Indicate the output of each of the println statements below. If the statement would generate an error, simply write error. Indicate spaces in your answer by writing. Be clear in your answers, and do not use quotation marks unless the computer would print them. String word = "CS170 class"; int num = 6; System.out.println("Hello" + word.charat(1) + word.charat(8)); System.out.println(word.substring(4,9)); System.out.println(9 + Integer.parseInt("9") + "\"" + 6 + 2 * num + "\""); System.out.println(word.length() + " " + num + 6); System.out.println(word.charAt(word.length())); Solution: Note: - indicates a space in the answer HelloSa 0-Cla 18"612" 11-66 error -> index out of range 4

4. (12 points) Assume the statements below are part of a Java program which compiles and runs. What is the output if the user types 35? Scanner in = new Scanner(System.in); int num = in.nextint(); if(num > 10) { System.out.println("one"); else if (num > 20) { System.out.println("two"); else if (num > 40) { System.out.println("try"); else { System.out.println("fry"); System.out.println("sly"); if (num > 40) { System.out.println("one"); else if (num > 20) { System.out.println("two"); else if (num > 10) { System.out.println("try"); else { System.out.println("fry"); System.out.println("sly"); if (num > 10) { System.out.println("one"); if (num > 20) { System.out.println("two"); if (num > 40) { System.out.println("try"); else { System.out.println("fry"); Solution: one sly two sly one two fry 5

5. Consider the following program (with the lines numbered for convenient reference). 1: public class Bugs { 2: public static void main(string args[]) { 3: int limit = 100; 4: int oddnum = 1; 5: while (oddnum!= limit) { 6: System.out.println(oddnum + "is a number under " + limit); 7: oddnum += 2; 8: 9: 10: Theintentoftheprogramwastoprintouttheoddnumbersbetween1and100. However, there is a bug in the program as written and it generates the (abbreviated) output shown below: 1 is a number under 100 3 is a number under 100... 97 is a number under 100 99 is a number under 100 101 is a number under 100 103 is a number under 100... (forever) (a) (2 points) Explain what the error is in the program. Solution: This program has an infinite loop in it because the termination condition of the loop will never be fulfilled. The oddnum variable will only ever contain odd numbers and the limit variable is even. (b) (4 points) What line(s) of code would you change to eliminate this error? Change the line(s) to make the program function correctly, assuming that limit is any valid positive integer. Solution: Need to change the condition in line 5. It should be something like: while (oddnum <= limit) 6

6. (10 points) Consider the program below which reads in two integers from the user. These integers are stored in the variables a and b. Write code so that the program prints "YES! 10!" if either a or b is the number 10 or if their sum is 10. Otherwise, it should print "NO! Not 10!". You may assume the user always enters valid integers. import java.util.scanner; public class SumTwo { public static void main(string args[]) { Scanner in = new Scanner(System.in); int a = in.nextint(); int b = in.nextint(); /*----------- Your code here -------------*/ Solution: if (a == 10 b == 10 a+b == 10) { System.out.println("Yes! 10!"); else { System.out.println("No! Not 10!"); Scoring: +3 detects if a is 10 +3 detects if b is 10 +3 detects if a+b is 10 +1 prints correctly 7

7. (12 points) The following program allows the user to enter an integer. The program should then sum up the even numbers between 0 and the number the user entered (inclusive) and print out the result. For example, if the user entered 10, the program should sum up the numbers 0, 2, 4, 6, 8, and 10 and print out the sum of 30. If the user entered 7, the program would sum up 0, 2, 4, and 6 and print the sum of 12. However, your program should work for any valid integer the user types. You may assume the user enters a valid, positive integer. import java.util.scanner; public class SumUp { public static void main(string args[]) { Scanner in = new Scanner(System.in); int limit = in.nextint(); /*---------------Your code here-----------*/ Solution: Many possible solutions. Here are two: int sum = 0; for (int i = 0; i <= limit; i++) { if (i%2 == 0) { //number is even sum = sum + i; System.out.println(sum); Could also be done with a while loop: int sum = 0; int count = 0; while (count <= limit) { sum = sum + count; count = count + 2; System.out.println(sum); Scoring: +2 works for any number +2 keeps track of sum +4 only sums up even numbers +2 includes limit if limit is even(inclusive) +2 prints out sum 8