Show the output produced by the following segments of code. Assume that all variables are declared as int. 1. i = 2; 2. i = 2; while (i <= 15) sum = 0; { while (i <= 15) System.out.println(i); { i = i + 2; sum = sum + i; } i = i + 2; } System.out.println(sum); 3. i = 40; 4. i = 40; j = 10; j = 10; while (i > j) while (i > j) { { i = i - 10; System.out.println(i); System.out.println(i); i = i - 10; } } 5. i = 30; 6. i = 10; j = 10; j = 10; if (i > 10 + j) if (i > 10 + j) while (i > j) while (i > j) { { i = i - 10; i = i - 10; System.out.println(i); System.out.println(i); } } else else while (j >= i) while (j >= i) { { j = j - 10; j = j - 10; System.out.println(j); System.out.println(j); } } 7. i = 30; 8. for (i = 1 ; i <= 10 ; i++) j = 10; { while (i >= j) if (i == 2) { System.out.println("hello"); if (i > j + 10) else if (i == 3) System.out.println("yes"); System.out.println("goodbye"); else else if (i == 4) System.out.println("no"); System.out.println("why"); i = i - 10; if (i < 5) } System.out.println("help"); if (i > 6) System.out.println("blue"); } 9. w = 0; 10. w = 0; for (h = -2; h <= 5; ++h) for (h = 10; h > 0; --h) w = w + h; System.out.print(w); System.out.println(w); 11. n = 1; 12. for (x = 1; x <= 5; x++) for (k = 2; k <= 5; k++) System.out.println(x); { System.out.println(x); n = k - 2 * 3; System.out.println(k + " " + n); } 13. Given the following program segment: sum = 0; finished = false; count = 0; while (count <= 8 && !finished) { number = getkeybd.nextInt(); if (number > 0) sum += number; else if (number == 0) finished = true; count++; } System.out.println(sum + " " + count); What would be the output given this data? 9 -3 4 6 0 5 6 0 5 14. What does the following program segment do? punctuation = 0; letters = 0; digits = 0; System.out.println("enter any character or '/' to quit: "); tempstr = getkeybd.next(); character = tempstr.charAt(0); while (character != '/') { if (character == ',' || character == '.') punctuation++; else if (character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z') letters++; else if (character >= '0' && character <= '9') digits++; System.out.println("enter any character or '/' to quit: "); tempstr = getkeybd.next(); character = tempstr.charAt(0); } System.out.println(punctuation + " " + letters + " " + digits); 15. Input a character and a number from the user. Print "number" number of lines of output with the character printed once on each line. 16. Given the following code, change the loop to an event controlled loop that will exit when the user enters a negative number of hours as the sentinel: System.out.println("Enter the number of employees: "); numEmployees = getkeybd.nextInt(); totpay = 0; empCount = 0; while (empCount < numEmployees) { System.out.println("Hours: "); hours = getkeybd.nextInt(); System.out.println("Rate: $"); rate = getkeybd.nextDouble(); pay = hours * rate; System.out.println("Employee pay is : $ " + pay); totpay += pay; empCount++; } System.out.println("Total payroll is $ " + totpay); 17. Write the statements to read in a group of exam scores ranging in value from 0 to 100. Your program should count and print the number of outstanding scores (90 to 100), satisfactory scores (70 to 89), and the number of unsatisfactory scores (0 to 69). Stop reading exam scores when a negative value is entered.
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2013