DOC PREVIEW
UVA CS 101 - Midterm 2

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 101 Spring 2007 Midterm 2 Name: _______________________________ Email ID: ________ You only need to write your name and e-mail ID on the first page. This exam is CLOSED text book, closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts, so be sure to look over all the questions and plan your time accordingly. Please sign the honor pledge here: Page 1 ____ / 3 Page 2 ____ / 24 Page 3 ____ / 9 Page 4 ____ / 16 Page 5 ____ / 21 Page 6 ____ / 17 Page 7 ____ / 10 Total ____ / 100 Note: When an integer type is required use int, when a floating-point type is required use double. If we don’t specify an aspect of the problem, you can choose it. Note: If you are still writing on the exam after “pens down” is called – even if it is just to write your name – then you will receive a zero on this exam. No exceptions! 1. [3 points] What lab section are you in? ____ CS 101-E ____ CS 101-2 (lab 8:00–9:15 a.m. Thu) ____ CS 101-6 (lab 2:00–3:15 p.m. Thu) ____ CS 101-3 (lab 9:30–10:45 a.m. Thu) ____ CS 101-7 (lab 3:30–4:45 p.m. Thu) ____ CS 101-4 (lab 11:00–12:15 p.m. Thu) ____ CS 101-8 (lab 5:00–6:15 p.m. Thu) ____ CS 101-5 (lab 12:30–1:45 p.m. Thu) ____ CS 101-9 (lab 6:30–7:45 p.m. Thu) Page 1 of 7CS 101 Spring 2007 Midterm 2 Name: _______________________________ Email ID: ________ 2. [4 points] In about 20 words or less, explain how classes and objects differ. 3. [6 points] In about 20 words or less, explain what the halting problem is 4. [6 points] In about 20 words or less, explain why the halting problem is so important. 5. [4 points] In about 20 words or less, explain why we need to use the .equals() method when comparing String objects. 6. [4 points] In about 20 words or less, explain the difference between a while loop and a do-while loop Page 2 of 7 ___ / 24CS 101 Spring 2007 Midterm 2 Name: _______________________________ Email ID: ________ 7. [9 points] Answer the following questions based on the following code: public static void main(String [] args) { Scanner stdin = new Scanner(System.in); System.out.println (“Please enter the number “ + “ of textbooks you read.”); int books = stdin.nextInt(); int browniePoints = 10; if (books < 1) { browniePoints -= 1; } if (books < 3) { browniePoints -= 3; } if (books < 5) { browniePoints -= 5; } else { browniePoints += 10; } System.out.println(“Based on your input you have: ” + browniePoints + “ brownie points.”); } a. What is printed if the user enters: 3? Based on your input you have: ____________________ brownie points. b. What is printed if the user enters: 0? Based on your input you have: ____________________ brownie points. c. What is printed if the user enters: 7? Based on your input you have: ____________________ brownie points. Page 3 of 7 ___ / 9CS 101 Spring 2007 Midterm 2 Name: _______________________________ Email ID: ________ 8. [6 points] Given the following code, write an equivalent nested if statement. Assume that amount1, amount2, and max are all ints which are previously declared and initialized. if( amount1 > 5 && amount2 < 10) { max = (amount1 > amount2) ? amount1 : amount2; System.out.println (“The maximum is: ” + max); } 9. [5 points] Write an statement using the ?: notation that assigns 30 to the variable y if the variable y is greater than 10 (assume all variables are ints and have been properly declared and initialized). 10. [5 points] The following code to sum even numbers less than 100 has a bug. Find and explain the bug in about 20 words or less. int sum = 0; for(int number = 0; number < 100; number += 2) { sum += number; number++; } Page 4 of 7 ___ / 16CS 101 Spring 2007 Midterm 2 Name: _______________________________ Email ID: ________ 11. [7 points] Write a while loop which displays the following numbers (they can be on separate lines, and don’t need the commas): 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 12. [7 points] Convert the following while loop to a for loop: i = 100; while (i > 0) { System.out.println(“i is : “ + i); i--; } [7 points] Write a for loop that will go through a Vector object v (which has been properly declared and initialized already), and print out each String that is in the object. Remember that Vectors have a .size() method that will return the number of elements in the Vector. Page 5 of 7 ___ / 21CS 101 Spring 2007 Midterm 2 Name: _______________________________ Email ID: ________ 13. [8 points] Given the following method signatures, list the return type. a. public static void main(String [] args) { return type: ___________ b. public double cos(int angle) { return type: ___________ c. public Circle() { return type: ___________ d. public static int max(int x, int y) { return type: ___________ e. public String toString() { return type: ___________ 14. [9 points] Consider the following code: if ( i || j ) { if ( j && k ) { System.out.println("A”); } else if ( !k || i ) { System.out.println(“B”); } else { System.out.println(“C”); } } else if (!j && k ) { System.out.println(“D”); } else { if ( i || k ) { System.out.println(“E”); } else { System.out.println(“F”); } } a. What is printed if i is true, j is false, and k is true? b. What is printed if i, j, and k are all false? c. What values of i, j, and k are needed to print D? Page 6 of 7 ___ / 17CS 101 Spring 2007 Midterm 2 Name: _______________________________ Email ID: ________ 15. [10 points] When an object is falling due to gravity, the distance (in meters) it falls is calculated as one half the gravity multiplied by the square of the falling time (in seconds) – in other words, 21*2dgt=, where d is the distance fallen, g is gravity, and t is the time taken. You may assume that gravity is fixed at 9.8 m/s2. Write a method called distanceFallen that accepts the object’s falling time as a double and returns the distance fallen as a double. Page 7 of 7 ___ /


View Full Document

UVA CS 101 - Midterm 2

Documents in this Course
Classes

Classes

53 pages

Recursion

Recursion

15 pages

Iteration

Iteration

88 pages

PLEDGED

PLEDGED

6 pages

Objects

Objects

33 pages

PLEDGED

PLEDGED

11 pages

CS 101

CS 101

42 pages

Classes

Classes

83 pages

Iteration

Iteration

92 pages

Classes

Classes

186 pages

Classes

Classes

208 pages

Hardware

Hardware

21 pages

Arrays

Arrays

70 pages

Load more
Download Midterm 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Midterm 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Midterm 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?