Question: Total Points: Score:

Similar documents
Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

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

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

Place your name tag here

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

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

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 003 Fall CS 170 Exam 2. Name (print): Instructions:

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

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:

Instructor: Yu Wang 11/16/2012

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

Question: Total Points: Score:

Question: Total Points: Score:

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

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

CS170 (005): Introduction to Computer Science Exam 2

CS 190 Exam 2 Fall 2014

CS 155 Exam 2 Spring 2015

CS 110 Exam 2 Spring 2011

Exam Percentage: / 55 = %

CS 110 Exam 2 Fall 2011

CS 1301 Exam 1 Fall 2010

CS 1301 Exam 1 Fall 2010

Question: Total Points: Score:

CS 2316 Exam 4 Fall 2011

CS 1301 Exam 3 Spring 2014

Midterm Examination (MTA)

CS 1301 Exam 1 Fall 2011

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

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

Programming with Java

CS 1803 Fall 2010 Exam 1

CS 1301 Exam 1 Spring 2011

CS 1301 Exam 2 A Fall 2015

CS 110 Exam 1 Fall 2011

CS 190 Exam 1 Fall 2010

CS 2316 Exam 1 Spring 2014

CS 1301 Exam 3 Fall 2014

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

CS Exam 3 - Spring 2010

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

CS 1301 Exam 1 Spring 2015

CS 1301 Exam 1 Fall 2014

CS 1301 Exam 1 Spring 2014

CS Exam 2 - Spring 2010

CS Exam 3 - Spring 2010

CS 2316 Exam 4 Fall 2011

CS 2316 Exam 1 Spring 2013

CS 1803 Fall 2010 Exam 1

H212 Introduction to Software Systems Honors

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

CS 1301 Exam 1 Fall 2014

CEN 414 Java Programming

CS 1301 Exam 1 Fall 2013

CS Name : Grading TA:

CS 1301 Exam 2 A Fall 2015

CS 1301 Exam 2 Fall 2014

CS 2316 Exam 4 Fall 2012

Read this before starting!

CS 2316 Exam 4 Spring 2013

CS 1301 Exam 1 Answers Fall 2009

Introduction to Computer Science Midterm 3 Fall, Points

CS 2316 Exam 3 Spring 2013

Combined Assignment Operators. Flow of Control. Increment Decrement Operators. Operators Precedence (Highest to Lowest) Slide Set 05: Java Loops

Loops and Expression Types

CS 2316 Exam 3 Fall 2011

CS 1301 Exam 1 Spring 2014

CS 1301 Exam 2 Fall 2013

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

Chapter 4: Control Structures I

CS 1301 Exam 1 Fall 2013

Chapter 2. Elementary Programming

CS 1301 Exam 3 Spring 2012

Oct Decision Structures cont d

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Lesson 7 Part 2 Flags

Java Coding 3. Over & over again!

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

Handout 4 Conditionals. Boolean Expressions.

CS 101 Fall 2005 Midterm 2 Name: ID:

CS 1301 Exam 1 Fall 2009

COS 126 General Computer Science Spring Written Exam 1

CS 101 Fall 2006 Midterm 1 Name: ID:

Exam 2, Version 2. For the following code, mark True or False for statements 1.8 to 1.10.

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

CS 1301 Exam 2 Spring 2014

CS 2316 Exam 4 Fall 2014

JAVA OPERATORS GENERAL

COMP 202 Java in one week

Transcription:

CS 170 Exam 1 Section 000 Spring 2015 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 o 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 o 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 9 questions on 14 pages including the title page. Please check to make sure all pages are included. You will have 50 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 9 Total Points: 12 4 6 8 12 12 14 14 18 100 Score: 1

1. Describe what function the + operator performs in the following statements; and evaluate the expression. (a) (4 points) Given that: int a = 5, b = 3; a + b Solution: Arithmetic Operators ( Additive operator ), return the sum of the two operands. Returns int, 8. (b) (4 points) a + 1 Solution: Arithmetic Operators ( Additive operator ), the character a will be casted/promoted to integer. Returns int, 98. (c) (4 points) "a" + "b" Solution: concatenate/join two strings a and b. Returns String ab. 2. Describe what function the operators containing the symbol = perform in the following statements. Given that: int a = 1, b = 2; (a) (2 points) a += b Solution: Assignment operator, equivalent to a = a + b; increment a on itself by b; a is 3 after this statement. (b) (2 points) a == b Solution: Equality operator, returns a boolean value; This expression returns false because a is equal to b. 2

3. (6 points) Convert the following binary numbers to decimal equivalents. (Show your work for partial credit) (a) 1101 2 (a) 13 (b) 111010 2 (b) 58 4. (8 points) Convert the following decimal number to their binary. (Show your work for partial credit) (a) 57 (a) 111001 (b) 47 (b) 101111 3

5. (12 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. int i = 7, j = 4; char c1 = a, c2 = B ; double d1 = 6.0, d2 = 3.0; String s1 = "veni", s2="vidi", s3="vici"; Expression Value Type j % i 4 int j + d1 10.0 double d1 + (int)d2 9.0 double i + d2 < j + d1 false boolean i--!= ++j + 1 true boolean c2 + 1 67 int c1 < c2 false boolean c1-1 == 0 true boolean s1 + (c1 + 1) veni98 String 1 + c2 + "" + s2 115vidi String s3 + "c1" vicic1 String s2.charat(1) == s3.charat(3) true boolean s3.charat(2)+1.0 ==s2.charat(2) 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 4

6. In the Emory Clinic we have 2 doctors on duty for student s health issue. They re Dr. Watson and Dr. Crick. Here is a program to query if they re available based on their schedules. Suppose this program compiles and runs without error. We use 1-7 to represent Sun, Mon,..., Sat We use 0-23 to represent 24 hours in a day (0=midnight, 12=noon). import java.util.scanner; public class Clinic { public static void main(string[] args) { Scanner in = new Scanner(System.in); System.out.print("Please input a day of week in integer(1-7): "); int day = in.nextint(); System.out.print("please input a time of day in integer(0-23): "); int time = in.nextint(); boolean watson=false; boolean crick=false; switch ( day ) { case 2: case 4: case 6: if ( time >=9 && time <=20 ) watson = true; if (time >= 5 && time <=11 time >= 18 && time <=23) crick = true; break; case 3: case 5: if ( time >=5 && time <=11 ) watson = true; if ( time >= 13 && time <=20) crick = true; break; case 1: case 7: if (time >=6 && time <=9 time >= 20 && time <= 22 ) watson = true; if (time >=15 && time <=20 ) crick = true; break; 5

default: System.out.println("invalid day!"); break; System.out.println("On day "+day+" at the time "+time+":"); if(watson) System.out.println("Dr. Watson is available!"); if(crick) System.out.println("Dr. Crick is available!"); if(!(watson crick)) System.out.println("no doctor available at this time!"); (a) (6 points) Suppose we want to check if any doctor is available on Tuesday morning, 11am. Write down the output result if we enter 3 for day and 11 for time Solution: On day 3 at the time 11: Dr. Watson is available! grading: 2 points for the output format first message 4pointsforthecorrectnessofworkflow (b) (6 points) Suppose we want to check if any doctor is available on Saturday afternoon, 8pm. Write down the output result if we enter 7 for day and 20 for time Solution: On day 7 at the time 20: Dr. Watson is available! Dr. Crick is available! grading: 2 points for the output format first message 4pointsforthecorrectnessofworkflow 6

7. If letters A, B, C,..., Z are represented as numbers 1, 2, 3,... 26, then K-N-O-W-L-E-D-G-E 11+14+15+23+12+5+4+7+5 = 96 H-A-R-D-W-O-R-K 8+1+18+4+23+15+18+11 = 98 A-T-T-I-T-U-D-E 1+20+20+9+20+21+4+5=100 We want to find other words that equal to 100. Here we have a java program, which read in a word from user, then return the value of this word based on the above transforming rule. If user enters characters other than alphabet, such as numbers or symbols, the program will take only characters before the wrong character. import java.util.scanner; public class Attitude { public static void main(string[] args) { Scanner myscanner = new Scanner(System.in); System.out.print("Please enter a word: "); String word = myscanner.next(); int res = 0; int i = 0; boolean valid = true; while(i < word.length() && valid){ char c = word.charat(i); if (c >= A && c <= Z ){ res += c - 64; else if (c >= a && c <= z ){ res += c - 96; else{ valid = false; System.out.println("Invalid letter detected: " + c); String newword = word.substring(0, i); System.out.println("The word \""+newword+"\" equals "+res); i++; if (i == word.length()){ System.out.println("The word \""+word+"\" equals "+res); 7

(a) (6 points) Write down the output result if we enter the word hard Solution: The word "hard" equals 31 grading: 2 points for the output format first message 4pointsforthecorrectnessofvalue (b) (8 points) Write down the output result if we enter the word h@rd Solution: Invalid letter detected: @ The word "h" equals 8 grading: 2 points for the output format first message 2 points for the output format second message 4pointsforthecorrectnessofvalue 8

8. (14 points) Find the greatest common divisor of 2 integers. Your program should read 2 integer from the user, and then print out the largest common divisors of these two integers. Don t forget that the range of common divisor will be from 1 to the smaller one between the two integers. Examples of running this program: >>> java GCD Please input the first integer:8 Please input the second integer:12 The Greatest Common Divisor of 8 and 12 is: 4 >>> java GCD Please input the first integer:3 Please input the second integer:7 The Greatest Common Divisor of 3 and 7 is: 1 import java.util.scanner; public class GCD { public static void main(string[] args) { Scanner in = new Scanner(System.in); System.out.print("Please input the first integer:"); int a = in.nextint(); System.out.print("Please input the second integer:"); int b = in.nextint(); /*------ Please Write Your Code Below This Line -------*/ 9

/*------ Please Write Your Code Above This Line -------*/ Solution: int i=1; int min=a; if (min>b){ min=b; 10

int gcd=1; while(i<=min){ if( a%i==0 && b%i==0){ gcd=i; i++; System.out.println("The Greatest Common Divisor of "+a+" and "+b+" is: "+gcd); grading: 2points:usevariablescorrectly 2points:findtheminofaandb 2points:initializegcdoutsideloop 4points:loopstartsandterminatescorrectly 2points:conditionsoncommondivisor 2points:output 11

9. (18 points) A DNA sequence is composed of 4 types of character: A, C, G and T. A CG site is a region where a C is directly before of G, and it is of important functions. For example, the sequence ATCGCAATTCGTTCG contains 3 CG site. You want to write a program to help computational biologists identify the number of CG sites in a given DNA string. Your program should read one string, and then count how many times the two characters CG appears in it. Hint: You can assume the user-input string contains only A, C, G and T (no need to have proof-reading functions in your program). Your program will handle only single strand of DNA (no need to handle another complementary string of DNA ). Examples of running this program: >>> java CGFinder Please enter a DNA sequence: ACGTACGT There are 2 CG site(s) in the DNA sequence. >>> java CGFinder Please enter a DNA sequence: AGGGCTT There are 0 CG site(s) in the DNA sequence. import java.util.scanner; public class CGFinder { public static void main(string[] args) { Scanner myscanner = new Scanner(System.in); System.out.print("Please enter a DNA sequence: "); String s = myscanner.next(); /*------ Please Write Your Code Below This Line -------*/ 12

/*------ Please Write Your Code Above This Line -------*/ Solution: int i=0; int ncg=0; while(i<s.length()-1){ if(s.charat(i)== C ){ if(s.charat(i+1)== G ){ ncg++; 13

i++; System.out.println("There re "+ncg+" CG site(s) in the DNA sequence."); grading: 3points:variableinitialization 3points:istartwith0 3points:i+1notgoingbeyondboundary 3 points: counter increment condition correct 3points:correctincrementofthecounter 3points:output,format,etc. 14