Docsity

Exam 1 with Solution Key - Basic Java Program | CIS 200, Exams of Computer Science

Toggle sidebar
bg1
CodeLab (conditionals, loops) due at midnight
Exam 1 tomorrow during class
No labs this week
Proj 3 due next Friday
Topics:
Basic Java program
Variables and operations
Printing, user input (including conversions)
Conditional statements (if, if/else, if/else if, switch)
Loops (while, do-while, for, nested loops)
Types of problems:
Full Java programs
“Code fragments”
Reading code – what is printed
What is wrong
CIS 200 Exam 1 (50 points)
Tuesday, September 15, 2009
Name: ______________________________________
This test is closed-books and closed-notes. Please do your own
work.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Exam 1 with Solution Key - Basic Java Program | CIS 200 and more Exams Computer Science in PDF only on Docsity!

CodeLab (conditionals, loops) due at midnight Exam 1 tomorrow during class No labs this week Proj 3 due next Friday Topics: Basic Java program Variables and operations Printing, user input (including conversions) Conditional statements (if, if/else, if/else if, switch) Loops (while, do-while, for, nested loops) Types of problems: Full Java programs “Code fragments” Reading code – what is printed What is wrong CIS 200 Exam 1 (50 points) Tuesday, September 15, 2009 Name: ______________________________________ This test is closed-books and closed-notes. Please do your own work.

  1. (6 pts) Write a complete Java program (everything that would go in the .java file) that prints the following information to the screen. Your information should be displayed exactly as it is below. CIS 200: Exam 1 Tues., Sept. 15, 2009 public class Test { public static void main(String[] args) { System.out.println(“CIS 200: Exam 1”); System.out.println(“Tues., Sept. 15, 2009”); } }
  1. (10 pts) Suppose we have declared and initialized the following variables: int num1 = 52; int num2 = 11; double dub = 3.3; char letter = ‘d’; Show what is printed in each of the following code fragments (assume they are executed in order). Please show your work. a) int value = (num1%num2)/3 + (int)dub; System.out.println(value); (52%11)/3 + 3 8/3 + 3 2 + 3 5 b) value = 33%20 - valuevalue + num2; System.out.println(value); 33%20 – 55 + 11 13 – 25 + 11 -12 + 11 - c) boolean result = (dub < 4) && (letter == ‘D’) System.out.println(result); (3.3 < 4) && (‘d’ == ‘D’) true && false

false d) int sum = 0; for (int i = 1; i <= num2; i += 2) { switch (i) { case 1: System.out.println(“first”); case 2: System.out.println(“second”); case 4: System.out.println(“fourth”); case 8: System.out.println(“eighth”); default: System.out.println(“other”); } sum += i; } System.out.println(“Sum is “ + sum); (num2 = 11) sum = 1, 4, 9, 16, 25, 36 I = 1, 3, 5, 7, 9, 11, 13 Prints: first second fourth eighth other other other other

  • Loop will go 11 times. Make just < 10.
  • Will count all numbers <= 10. Condition needs to be: num <= 20 && num >= 10
  • Will add up the numbers in that range, not count them. Inside if, need just: count++
  • Count will always be one bigger than happened. Start count at 0, not 1.
  • Loop will end prematurely if enter number outside of range. Get rid of else.
  1. (12 pts) Write a code fragment with nested for loops that will print out: 10 10 8 10 8 6 10 8 6 4 10 8 6 4 2 all on separate lines. Do not declare any variables other than the loop counters in the for loops. The outer loop should step through the 5 lines you want to print, and the inner loop should step through the numbers you want to print on the current line. for (int i = 10; i >= 2; i-=2) { for (int j = 10; j >= i; j-=2) { System.out.print(j + “ “); } System.out.println();