Spring 2013 COMP Midterm Exam Solutions March 07, 2013

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

Place your name tag here

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

COMP Introduction to Programming Midterm Review

Final Exam. COMP Summer I June 26, points

BASIC ELEMENTS OF A COMPUTER PROGRAM

Question: Total Points: Score:

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

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

COS 126 General Computer Science Spring Written Exam 1

CIS 110 Introduction to Computer Programming Summer 2018 Midterm. Recitation ROOM :

Question: Total Points: Score:

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

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

Question 2. [5 points] Given the following symbolic constant definition

Prof. Navrati Saxena TA: Rochak Sachan

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

CS 177 Spring 2009 Exam I

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

CSE 1223: Exam II Autumn 2016

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

Final Exam COMP Fall 2004 Dec 16, 2004

Term 1 Unit 1 Week 1 Worksheet: Output Solution

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

University of Massachusetts Amherst, Electrical and Computer Engineering

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Variables, Types, Operations on Numbers

Course Outline. Introduction to java

CS1004: Intro to CS in Java, Spring 2005

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

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

CS 101 Spring 2007 Midterm 2 Name: ID:

Building Java Programs

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

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

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

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

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

Programming with Java

Expressions and Variables

Practice Midterm 1. Problem Points Score TOTAL 50

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

COMP 524 Spring 2018 Midterm Thursday, March 1

COMP 202 Java in one week

COMP 202 Java in one week

CIS 110 Introduction to Computer Programming Summer 2016 Midterm. Recitation # (e.g., 201):

Loops. CSE 114, Computer Science 1 Stony Brook University

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CS 177 Spring 2010 Exam I

Java Basic Programming Constructs

Accelerating Information Technology Innovation

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

Question: Total Points: Score:

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

Chapter Goals. Contents LOOPS

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Name CIS 201 Midterm II: Chapters 1-8

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

Problem Score Max Score 1 Syntax directed translation & type

Introduction to Java Applications

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

A variable is a name for a location in memory A variable must be declared

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

King Saud University College of Computer and Information Sciences Computer Science Department

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Array. Prepared By - Rifat Shahriyar

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

Question: Total Points: Score:

Datatypes, Variables, and Operations

CS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class

1 Short Answer (10 Points Each)

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

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

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Question: Total Points: Score:

Loops and Expression Types

Introduction to Computer Science Midterm 3 Fall, Points

Selected Questions from by Nageshwara Rao

Object-Oriented Programming

1 Short Answer (5 Points Each)

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Q1: Multiple choice / 20 Q2: Arrays / 40 Q3: Functions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

This page intentionally left blank

CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

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

COMP 401 Spring 2013 Midterm 1

Last Name: Circle One: OCW Non-OCW

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name:

Comp 170 Test 1 SAMPLE June 8, 2000

Transcription:

Spring 2013 COMP 110-003 Midterm Exam Solutions March 07, 2013 UNC Honor Pledge: I certify that no unauthorized assistance has been received or given in the completion of this work. Signature: Read this entire first page before beginning the exam. You may NOT use any course materials in completing this exam. You will have 75 minutes to complete this exam. The exam consists of three sections: first, a section of multiple choice questions and short answer questions; second, a section requiring you to trace a short piece of code and write the output produced by the code; finally, a section requiring you to write code. 1) The second section provides three questions of different points. You may choose any TWO questions from them. If you answer all three questions, the two on which you receive the highest scores will be counted in your final score. 2) The third section provides three questions of different points. You may choose any TWO questions from them. If you answer all three questions, the two on which you receive the highest scores will be counted in your final score. 3) If your final score is greater than 100, the extra points will be counted. Pace yourself! If you find that you are stuck for a long time on a question that is not worth many points, move on to another question that you know how to do. Please write your name on every page of the exam. If you use the back side of the paper, write down clearly what question you are answering. Also, make sure to indicate the location of your answer under the question. If something is unclear, raise your hand and ask. Write clearly! Illegible answers will not receive credit. 1

Part 1. (40 points) Answer ALL questions. 1. (1 points) What are the two possible values of a bit? Answer: 0 and 1. 2. (1.5 points) What is the smallest addressable unit of memory? How many bits are in this unit? Answer: Byte. 8 bits. 3. (3 points) What does a compiler do? Why do we need a compiler? Answer: A compiler translates the code written in a programming language to machine instructions. We need it because it s hard for human beings to read and write machine instructions in bits directly. 4. (2.5 points) List 5 primitive types. Answer: Select any five from int, byte, short, long, float, double, char, boolean. 5. (3 points) Which of the following are legal variable names in Java (circle them)? bestfriend 7daysAWeek hello! TOTAL&COST private FIFTYSEVEN57 do new Answer: bestfriend and FIFTYSEVEN57. 6. (5 points) What are the values of the following variables? (a) 3.0 double var1 = 10 / 3; (b) 6 int var2 = (int) (2.5 * 2.6); (c) true boolean var3 =!(3 > 3); (d) true boolean var4 = (121 % 11 == 0) (5 == 5); (e) 2 int var5 = 11 % 3; 7. (3 points) Declare a variable to hold the amount of money in your bank account and initialize it to 245.25. Name your Java variable in a meaningful way so that any programmer would know what value it holds. Your variable name should be at least two words. Answer: double accountbalance = 245.25; 8. (3 points) Assuming that a and b are both int type variables. Describe the difference between a = b, a == b and a += b. Answer: a = b assigns the value of b to the variable a; a == b is a boolean expression and its value represents whether the value of a is equal to the value b; a += b is also an assignment statement which is in fact a = a + b. 2

9. (2 points) Suppose you have the variable str of type String with data How are you? Write out the data stored in str and place the index of each character below the string. Answer: H o w a r e y o u? 0 1 2 3 4 5 6 7 8 9 10 11 10. (6 points) Give the return type and value of the following method calls on str (defined in the above question). (a) str.length() return type: int, value: 12 (b) str.equalsignorecase("how ARE YOU") return type: boolean, value: false (c) str.indexof("ou") return type: int, value: 9 (d) str.lastindexof(" ") return type: int, value: 7 (e) str.charat(6) return type: char, value: e (f) str.substring(1, 6) return type: String, value: ow ar 11. (3 points) What is wrong with this piece of code? (Circle the error and tell me why it s wrong) int count = 0; while (count < 100) if (count % 5 == 0) System.out.println(count); count++; Answer: The right code is: int count = 0; while (count < 100) { if (count % 5 == 0) System.out.println(count); count++; The code in the question will fall into an infinite loop. 3

12. (3 points) What is wrong with this piece of code? (Circle the error and tell me why it s wrong) do { System.out.print("The programming language, "Java", "); while (false); System.out.println("is named after the Java coffee"); Answer: The right code is: do { System.out.print("The programming language, \"Java\", "); while (false); System.out.println("is named after the Java coffee"); We must use \ to print quotation marks. 13. (4 points) What is wrong with this piece of code? (Circle two errors and tell me why they re wrong) public int absolutevalue(int num) { if (num < 0); return -num; else if (num > 0) return num; Answer: The right code is: public int absolutevalue(int num) { if (num < 0) return -num; else return num; (1) There shouldn t be a semicolon after the if line; (2) The case that num is equal to 0 is not considered. 4

Part 2. (20 points) Choose any TWO questions from question 14-16. Pay close attention to the point values of these questions. You may attempt 16 or 20 points depending on the questions you choose. You will receive partial credit for correct written reasoning procedures. 14. (8 points) Write the output of the method dosomething(). public void swap(int a, int b) { int temp = a; a = b; b = temp; public void dosomething() { int a = 2, b = 3; a = b; b = a; System.out.println(a + "," + b); int c = 2, d = 3; int temp = c; c = d; d = temp; System.out.println(c + "," + d); int e = 2, f = 3; swap(f, e); System.out.println(e + "," + f); Answer: The output is respectively: 3,3 3,2 2,3 5

15. (8 points) Write the output for the piece of code. int a = 2, b = 2, c = 2; for (int i = 0; i < 3; i++) { a += a; b *= b; c /= c; System.out.println(a + ", " + b + "," + c + "."); Answer: The output is: 16, 256, 1. 16. (12 points) Write the output of test(1), test(3) and test(5). public void test(int k) { String t = "The quick brown fox jumps over the lazy dog"; for (int i = 0; i < k; i++) { t = t.substring(t.indexof(" ") + 1); System.out.println(t.substring(0, t.indexof(" "))); Answer: The output is: quick fox over 6

Part 3. (40 points) Choose any TWO questions from question 17-19. Question 17 and 18 have two possible versions. You can choose to implement the basic version or the advanced version. You will receive BOTH basic and extra credits if you implement the advanced version. You may attempt 35, 40, 45 or 50 points depending on the questions and versions you choose. Please indicate what version you are implementing. You will receive full credit for code that can be compiled and generate correct answers. You will lose points for either syntax or logical errors. You will receive partial credit for correct structures, statements or reasoning procedures. 17. (15 points for basic version) Write a method divisors(int N) that returns the number of divisors of a positive integer N that is passed in to the method. A divisor of an integer n, also called a factor of n, is an integer which divides n without leaving a remainder. For example, the number 100 has 9 divisors (1, 2, 4, 5, 10, 20, 25, 50, 100). Therefore divisors(100) should return 9. It is easy to test whether a number is a divisor of another number by the modulus operation. For example again, 5 is a divisor of 100 because 100 mod 5 is equal to 0; 6 is not a divisor of 100 because 100 mod 6 is equal to 4. (5 extra points for advanced version) The advanced version requires that the loop will execute for no more than Math.sqrt(N) times, where Math.sqrt(N) calculates the square root of N (with return type double). For example, your program should not loop for more than 10 times to calculate divisors(100). (Hint: If 2 is a divisor of 100, then 100/2 = 50 is also a divisor of 100.) Answer: The basic version is: public int divisors(int N) { int count = 0; for (int i = 1; i <= N; i++) { if (N % i == 0) count++; return count; The advanced version is: public int divisors(int N) { for (int i = 1; i <= Math.sqrt(N); i++) { if (N % i == 0) count += 2; if (i == Math.sqrt(N)) count--; return count; 7

18. (20 points for basic version) Write a method equalstrings(string a, String b) that returns whether String a and String b are exactly the same. In other words, you are implementing.equals() method. For example, equalstrings("abcd", "abcd") should return true, and equalstrings("abcd", "abce") should return false. You are required to use only.length() and.charat() methods in your method. You are not allowed to use.equals(),.indexof(),.lastindexof() or.compareto(). (Hint: You must compare the characters in the strings one by one.) (5 extra points for advanced version) The advanced version requires that the method will return true if the two strings only differ from the spaces in the end. For example, equalstrings("abcd", "abcd ") should return true. However, equalstrings(" abcd", "abcd ") will still return false. You are not allowed to use.trim(). Answer: The basic version is: public boolean equalstrings(string a, String b) { boolean result = true; if (a.length()!= b.length()) { result = false; else { for (int i = 0; i < a.length(); i++) { if (a.charat(i)!= b.charat(i)) result = false; return result; 8

(Question 18 cont d:) The advanced version is: public static boolean equaltrimstrings(string a, String b) { boolean result = true; String LStr, SStr; if (a.length() >= b.length()) { LStr = a; SStr = b; else { LStr = b; SStr = a; for (int i = 0; i < LStr.length() - SStr.length(); i++) { if (LStr.charAt(i)!= SStr.charAt(i)) result = false; if (LStr.length()!= SStr.length()) { for (int i = SStr.length(); i < LStr.length(); i++) { if (LStr.charAt(i)!= ' ') result = false; return result; 9

19. (25 points) Write a method pi() that returns the value of π, the ratio of a circle s circumference to its diameter. The value of π can be calculated by this infinite series: π 4 = 1 1 1 3 + 1 5 1 7 + 1 9 1 11 + 1 13 1 15 Your method should use this infinite series to calculate the value of π. Because there are infinite numbers of terms in the series, you are required to compute the sum of the first 10000 terms in this series. Answer: One possible version: public double pi() { double qpi = 0; for (int i = 1; i <= 10000; i++) { if (i % 2!= 0) { qpi += 1 / (double) (2 * i - 1); else { qpi -= 1 / (double) (2 * i - 1); return qpi * 4; Another possible version: public double pi() { double pi = 0; double term = 4; for (int i = 1; i <= 10000; i++) { pi += term; term *= -(double) (2 * i - 1) / (2 * i + 1); return pi; 10

(Question 19 cont d:) The third possible version: public double pi() { double qpi = 0; double divisor = 1; boolean odd = true; while (divisor <= 20001) { if (odd) { qpi += (1 / divisor); odd = false; else { qpi -= (1 / divisor); odd = true; divisor += 2; return qpi * 4; The fourth possible version: public double pi() { double qpi = 0; double divisor = 1; for (int i = 0; i < 10000; i++) { qpi += (1 / divisor); qpi -= (1 / (divisor + 2)); divisor += 4; return qpi * 4; This is the last page of the solution. 11