Place your name tag here

Similar documents
Question: Total Points: Score:

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

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

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

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

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

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

Question: Total Points: Score:

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

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

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

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

Instructor: Yu Wang 11/16/2012

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

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

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

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

CS 190 Exam 2 Fall 2014

CS170 (005): Introduction to Computer Science Exam 2

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

CS 155 Exam 2 Spring 2015

CS 1301 Exam 1 Fall 2010

Exam Percentage: / 55 = %

Question: Total Points: Score:

CS 110 Exam 2 Spring 2011

CS 1301 Exam 1 Fall 2010

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

CS 1301 Exam 1 Fall 2014

CS 110 Exam 2 Fall 2011

CS 1301 Exam 1 Fall 2014

JAVA OPERATORS GENERAL

CS 2316 Exam 4 Fall 2011

Prof. Navrati Saxena TA: Rochak Sachan

CSE 1223: Exam II Autumn 2016

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

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

CS 2316 Exam 1 Spring 2014

CS 1301 Exam 1 Spring 2014

CS 1301 Exam 1 Spring 2011

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.

CS 1301 Exam 2 Fall 2014

CS 110 Exam 1 Fall 2011

CS 1301 Exam 1 Fall 2011

CS 190 Exam 1 Fall 2010

CS 1301 Exam 3 Spring 2014

CS 1301 Exam 1 Spring 2014

CS 2316 Exam 1 Spring 2013

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java Simple Data Types

CS Name : Grading TA:

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

Programming with Java

Key Java Simple Data Types

CS 1301 Exam 2 Fall 2013

CS 2316 Exam 4 Fall 2012

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

CS 1301 Exam 1 Spring 2015

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

CS 1803 Fall 2010 Exam 1

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Loops and Expression Types

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Fall 2005 CS 11 Final exam Answers

CS 2316 Exam 4 Fall 2014

CS Exam 2 - Spring 2010

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

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

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.

CS 2316 Exam 4 Fall 2011

Java Simple Data Types

CS 2316 Exam 3 Spring 2013

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS212 Midterm. 1. Read the following code fragments and answer the questions.

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

CS 1803 Fall 2010 Exam 1

CS 1301 Exam 2 A Fall 2015

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

CS 2316 Exam 4 Spring 2013

CS 2316 Exam 4 Fall 2012

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

IEEE Floating-Point Representation 1

CS Exam 3 - Spring 2010

CS 1301 Exam 2 A Fall 2015

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

CS 1301 Exam 2 Spring 2014

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

CS 1301 Exam 3 Fall 2014

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Transcription:

CS 170 Exam 1 Section 001 Spring 2015 Name: Place your name tag here 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 8 questions on 13 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 8 Total Points: 10 10 11 15 5 5 9 10 75 Score: 1

1. (10 points) Definitions. Define 5 of the 6 terms below. You do not need to give a formal definition just a good description. You may include an example if it is helpful to your answer. Be brief, and clearly mark the term you do not want me to grade. Otherwise, I will discard the last term. (a) local variable Solution: A local variable is a variable that is only in scope (is visible or can be used) within the block of code that defined it. (for example, an if statement or method body). Once the function returns, the local variable is lost. (b) keyword Solution: A reserved word which is used by the compiler and has a special meaning in the language structure. Ex: if, int, class, etc (c) argument Solution: A value provided to a function when the function is called. This value is assigned to the corresponding parameter in the function (d) compiler Solution: A reserved word that is used by the compiler to parse the program; you cannot use keywords (such as if, class, and void) as variable or function names (identifiers). (e) ASCII code (or encoding) (f) string Solution: The internal mapping a computer makes between a symbol and a numerical value. Ex: A maps to 65. Solution: A series of characters or symbols. In Java, denoted by use of quotation marks, Ex: "Hello World!" 2

2. (10 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. String s1 = "Spring", s2 = "85"; char c1 = 5, c2 = q ; int i1 = 7, i2 = 4; double d1 = 1.0, d2 = 0.75; boolean b1 = true; Expression Value Type 4+1 5 int i1 % i2 3 int (i2+d1+2) / i1 1.0 double!b1 false boolean b1 && i2!= i1 true boolean "c1" + s1 c1spring String c2 + (int)d2 113 int s1.charat(4) + i2 + s2 11485 String (char)(i2 * 21) T char i1--!= ++i2 + 1 true boolean c1 < c2 i1 < i2 true boolean Notes: The parenthesis operator has the highest precedence of all operators. The logical not operator and the casting operator have higher precedence than arithmetic operators, relational/comparison operators, and the other logical operators. Arithmetic operators have higher precedence than relational/comparison or logical operators. Relational/comparison operators have higher precedence than logical operators. Assignment operators have the lowest precedence of all operators 3

3. (11 points) Consider the program below. What will be the output of the program when it is executed? public class Exam1 { public static int method1(int a, double b) { System.out.println("A " + a + " " + b); if (a > b) { System.out.println("B"); b += 1.5; return (int)b; if (b > a) { System.out.println("C"); a -= 2; return a; b += 12.5; return (int)(a+b); public static void method2(int a) { if (a >= 20 && a <= 30) { a += 30; System.out.println("D " + a); return; else { System.out.println("E " + a); System.out.println("F"); public static String method3(string s) { System.out.println("Hello, " + s); if (s.length() > 5) { return s.substring(5); else { return s.substring(2); public static void main(string[] args) { int a = 10; int b = 20; String s1 = method3("hel" + a); method2(b); System.out.println("G " + s1); System.out.println("H " + a + " " + b); a = method1(b, a); System.out.println("I " + a + " " + b); 4

Solution: Hello, Hel10 D 50 G l10 H 10 20 A 20 10.0 B I 11 20 5

4. Short Answer. (a) (2 points) In the following method, how many local variables are there? How many parameter variables are there? public static double mystery(int a, String b, char c) { a = a + c; double d = a; boolean e = a <= c; b += c; return a; Solution: 2 local variables (d and e) 3 parameter variables (a, b, and c) (b) (3 points) Consider the following function: public static int mysteryfunction(int a,int b) { if (a < b) { return a; return b; i. What does this function return if called with mysteryfunction(5,10)? Solution: 5 ii. Write an appropriate comment for this method. Solution: It returns the smaller of two input parameter numbers. In other words, it finds the minimum. It was not enough to restate the code! (c) (2 points) Give the output of the following code fragment: int a = 3; int b = 10; double pi = 3.14159; if (b / a > pi) System.out.println("a"); else if ((double)b / a > pi) System.out.println("b"); else System.out.println("c"); Solution: b 6

(d) (2 points) What does the following method print if it is invoked with the call foo(-1, 8, -3)? public static void foo(int a, int b, int c) { if (b > c) { if (c > 0) { System.out.println("A"); else if (b > 0) { System.out.println("B"); b=c-b; else if (a < 0) { System.out.println("C"); else { System.out.println("D"); if (b > c) { System.out.println("E"); else { System.out.println("F"); else System.out.println("G"); Solution: I ended up giving everyone full credit on this problem due to a significant typo. However, it s been corrected in the version above, and you should understand how the above code gives the answer: B F (e) (4 points) Give the output of the following code fragment: int a = 2; int b = 10; int c = 20; int d = 7; a += c; b %= d; System.out.println("a: " + a); System.out.println("b: " + b); c = ++a + b + d--; System.out.println("c: " + c); System.out.println("d: " + d); 7

Solution: a: 22 b: 3 c: 33 d: 6 8

(f) (2 points) Write a single Java statement which would cause the output: She said, "Hello" to me. to appear in the terminal window. Solution: System.out.println("She said\n\"hello\" to me"); 9

5. (5 points) Prof. Summet has written the following code to convert numerical scores to letter grade for CS170. However, she has 5 syntax errors. Identify the errors by circling them and rewrite the line of code to be syntactically correct. Hint for 1 tricky error: what will the program do if the user enters -5? 1 import java.util.scanner; 2 3 public class Exam1{ 4 public static String grade(int score) { 5 if (score >= 90) 6 return "A"; 7 if (score >= 80) 8 return "B"; 9 if (score >= 70) 10 return "C"; 11 if (score >= 60) 12 return "D"; 13 if (score => 0) 14 return "F"; 15 16 17 public static void main (String[] args){ 18 Scanner in = new Scanner(System.in); 19 System.out.print("Enter a numerical grade: "); 20 int score = in.nextint() 21 String g = Grade(score); 22 System.out.println(score + is a + g); 23 24 Solution: line 19: missing ; line 20: function name is grade not Grade line 21: missing " around String line 13: operator written incorrectly; should be >= lines 5-14: missing a return statement. It s not enough just to add an else since the else will only pair with the last if statement. You need to either 1) add just a return statement like return "Error"; as the last statement of the method or rewrite the entire method to use else if and the last condition simply use an else. Note that the space between main and the opening ( isn t an error since Java is a format-free language. 10

6. (5 points) Write a method named isnegative that takes in an integer parameter and returns a boolean value. If the parameter is negative, the function should return the boolean value true. If the parameter is zero or positive, it should return the boolean value false. Examples: isnegative(-8) returns true isnegative(5) returns false Solution: public static boolean isnegative(int num) { if (num >= 0) return false; else return true; Scoring: 1 point for a correct header 2 points for testing if the number is positive/negative correctly 1 point for returning false if the number is positive or zero 1 point for returning True if the number is negative 11

7. (9 points) Write a method named uniqueproduct which takes 3 integers as input. The function should return the product of the 3 inputs. However, if one of the input values is the same as another of the values, it should return the unique (not duplicated) number. If all three numbers are the same, the function should return -1. Examples: uniqueproduct(2, 3, 4) returns 24 uniqueproduct(2, 2, 3) returns 3 uniqueproduct(1, 1, 1) returns -1 Solution: public static int uniqueproduct(int a, int b, int c) { if (a == b && b == c) { return -1; else { if (a == b) return c; else if (b == c) return a; else if (a == c) return b; else return a * b * c; Scoring: +2 header +2 returns -1 if all are same +4 handles cases where only 2 numbers are the same +1 handles case where all are different 12

8. (10 points) Write a method named strangecat which takes two Strings as input. This function should perform a strange concatenation as described below: If the strings are different lengths, omit initial characters from the longer string so it is the same length as the shorter string. So Hello and Hi yield lohi (both of length 2, omits the initial 3 characters from Hello ). If the strings are the same length, return a String made by repeating the first String. Examples: strangecat("hello", "Hi") returns "lohi" strangecat("java", "Hello") returns "Javaello" strangecat("ho", "Hi") returns "HoHo" Solution: public static String strangecat(string a, String b) { if (a.length() > b.length()) { return a.substring(a.length()-b.length()) + b; else if (a.length() < b.length()){ return a + b.substring(b.length()-a.length()); else { return a+a; Scoring: +2 header +2 handles if b longer than a +2 handles if a longer than b +2 handles if strings are same length +2 omits initial characters correctly (uses substring correctly) 13