DOC PREVIEW
UVA CS 101 - CS 101 Midterm 2

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 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 8 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 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 101 Fall 2006 Midterm 2 Name: _______________________________ Email ID: ________ 1 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 XX / XX Page 3 ____ / 15 Page 4 ____ / 10 Page 5 ____ / 15 Page 6 ____ / 18 Page 7 ____ / 15 Page 8 ____ / 24 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-4 (lab 2:00–3:30 p.m. Fri) ____ CS 101-2 (lab 7:00–8:30 p.m. Thu) ____ CS 101-5 (lab 10:00–11:30 a.m. Fri) ____ CS 101-3 (lab noon–1:30 p.m. Fri) ____ CS 101-6 (lab 2:00–3:30 p.m. Thu)CS 101 Fall 2006 Midterm 2 Name: _______________________________ Email ID: ________ 2 2. [15 points. 3 points each]. Consider the following class. This question itself is on the next page, and asks you to answer questions about the behavior of this class. class Test1 { public static void main(String args[]) { // line 1 if ((1==2) && bool1()) System.out.println(“Yeah!”); // line 2 if ((.1+.1+.1+.1+.1+.1+.1+.1+.1+.1)==1.0) System.out.println(“Math works!”); else System.out.println(“Math broke.”); // line 3 if (bool1() && bool2()) System.out.println(“They’re so rad!”); // line 4 if (bool2() && bool1()) System.out.println(“They’re so bad!”); // line 5 if (bool2() || bool1()) System.out.println(“Giants of logic and computatability!”); } public static boolean bool1() { System.out.println(“George Boole, my hero! ”); return true; } public static boolean bool2() { System.out.println(“Kurt Godel, never lied, no!”); return false; } } XX / XXCS 101 Fall 2006 Midterm 2 Name: _______________________________ Email ID: ________ 3 Question 2, continued a. What output is produced when the first if statement (line 1) executes? b. Same question for the second if (line 2)? c. Same question for the third if (line 3)? d. Same question for the fourth if (line 4)? e. Same question for the last if (line 5)? ___ / 15CS 101 Fall 2006 Midterm 2 Name: _______________________________ Email ID: ________ 4 3. [10 points] Suppose you’re given the following class. Your job is to write the code in the indicated place. Be very careful. Your program must compile and work correctly to receive full credit. import java.util.*; class Test2 { public static void main(String args[]) { // Your code goes here } } Write the code needed to make the program behave as follows. (1) The program prompts the user to input an integer value; (2) An integer value is read from the standard input; (3) If the value is even, the program outputs the string “Even” otherwise – in the case where the value is odd – (a) if the value is greater than 11, the program outputs “Big and Odd”, and (b) if the value is not greater than 11 the program outputs the string “Small and Odd”. ___ / 10CS 101 Fall 2006 Midterm 2 Name: _______________________________ Email ID: ________ 5 4. [5 points] The following code won’t compile. Why? Your answers should be 20 words or less! class Test3 { public static void doit() { while (false) { int n = 0; } System.out.println(n); } } 5. [10 points total; 5 points each] Consider the following code then answer the questions below. class Test4 { public static void main(String args[]) { int n = 0; foo(n); System.out.println(n); // System.out.println(k); } public static void foo(int x) { int n = 2; int k = 3; ++x; } } a. What does the program print? Explain in one short sentence why. b. What would happen if we were to remove the comment marker “//” before the second println call and then try to compile and run the program? (In other words, what would happen if we included the statement as part of the program?) Explain in one short sentence what happens and why. ___ / 15CS 101 Fall 2006 Midterm 2 Name: _______________________________ Email ID: ________ 6 6. [8 points] What output does the following code produce? Show the output as the program would actually format it. Assume the code is in a main() method in a class. for (int i = 1; i < 4; i++) { for (int j = 1; j < i; j++) { System.out.print(“ ” + i*j); } System.out.println(“”); } 7. [10 points] Write a while loop that accepts integers from the user until the user enters a zero value. Hint: Consider using the break statement. ___ / 18CS 101 Fall 2006 Midterm 2 Name: _______________________________ Email ID: ________ 7 8. [15 points] provide the code for the following dispatch method. Use a single switch statement. This method takes a single integer value as a parameter. If the value is 1 the method should output “Menu choice 1 entered” and then return. If the value is 2, the method should output “Menu choice 2 entered” and then return. If the value is 3 the method should output “Menu choice 3 entered” and then return. Otherwise the method should just return. public static void dispatch(int choice) { // your code goes here }___ / 15CS 101 Fall 2006 Midterm 2 Name: _______________________________ Email ID: ________ 8 9. [8 points, 2 points each] What is the value of each of the following expressions? a. ((true || false) && (false || true)) b. (3 <= 3) c. (1 + 2 * 3 / 2 - 1) d. (new String(“I love CS!”) == new String(“I love CS!”)) 10. [8 points, 4 points each] Consider the method call: foo(i). We’ll assume that foo() has been properly defined, and that i is an


View Full Document

UVA CS 101 - 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 CS 101 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 CS 101 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 CS 101 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?