Solution Manual for Introduction to Java Programming and Data Structures, Comprehensive Version, 12th Edition

Preview Extract
Name:_______________________ Covers Chapters 1-3 75 mins SAMPLE TEST CSCI 1301 Introduction to Programming Georgia Southern University Instructor: Dr. Y. Daniel Liang I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class. Signed by ___________________ Date ___________________ Part A. A.1 (1 pt) Write a statement to declare a constant named PI with value 3.14159. A.2 (2 pts) Write an if-else statement that increase salary by 10% if age is greater than or equal to 50, and 5% if age is less than 35, and 8% otherwise. A.3 (2 pts) Write a conditional expression that assigns 1 to x if y is greater than 0 and -1 to x otherwise. Part B. (10 pts) Show the output of the following code: (write the output next to each println statement if the println statement is executed in the program). public class Test { public static void main(String[] args) { System.out.println((int)(Math.random() * 10) / 10); System.out.println(Math.pow(3, 2)); System.out.println(35 % 7); System.out.println(3 + 4.5 * 2 < 2 * 9.5); int number = 14; if (number % 7 == 0) System.out.println(8 * number); System.out.println(2 * number); int x = 1942; System.out.println(x / 100); System.out.println(x % 100); System.out.println(x + " is " + ((x % 2 == 0) ? "even" : "odd")); 1 int y = -5; ++y; System.out.println(y); int value = 453; int d1 = value % 10; int d2 = (value / 10) % 10; int d3 = (value / 100) % 10; System.out.println("" + d1 + d2 + d3); } } Part C: 1. (10 pts) (Random month) Write a program that randomly generates an integer between 1 and 12 and displays the English month name January, February, โ€ฆ, December for the number 1, 2, โ€ฆ, 12, accordingly. 2 2. (10 pts) (Palindrome number) Write a program that prompts the user to enter a three-digit integer and determines whether it is a palindrome number. A number is palindrome if it reads the same from right to left and from left to right. Here is a sample run of this program: Enter a three-digit integer: 121 121 is a palindrome Enter a three-digit integer: 123 123 is not a palindrome 3 Name:_______________________ Part D: Multiple Choice Questions: (1 pts each) (Please circle your answers on paper first. After you finish the test, enter your choices online to LiveLab. Log in and click Take Instructor Assigned Quiz. Choose Quiz1. You have 5 minutes to enter and submit the answers.) 1 The keyword _______ must be used to declare a constant. A. const B. double C. final D. static E. int 2 You can define a constant twice in a block. A. true B. false 3 A Java statement ends with a __________. A. period (.) B. closing brace C. semicolon (;) D. comma (,) 4 The assignment operator = is left-associative. A. true B. false 5 What is y after the following switch statement? int x = 0; int y = 0; switch (x + 1) { case 0: y = 0; case 1: y = 1; default: y = -1 } A. -1 B. 1 C. 2 D. 0 4 6 Which of the Boolean expressions below has incorrect syntax? A. (x > 0) || (x 4) C. !(x > 0) && (x > 0) D. (x != 0) || (x = 0) 7 You can always convert an if statement to a switch statement. A. false B. true 8 The default case must be specified in a switch statement. A. false B. true 9 Which of the following expression is equivalent to (x > 1). A. !(x = 1) B. x >= 1 C. !(x < 1) D. !(x <= 1) 10 Analyze the following two code fragments. (i) int x = 5; if (0 < x) && (x < 100) System.out.println("x is between 1 and 100"); (ii) int x = 5; if (0 < x && x < 100) System.out.println("x is between 1 and 100"); A. The second fragment has a syntax error. B. The first fragment has a syntax error. C. Both fragments produce the same output. D. Both fragments compile, but produce different results. 11 Analyze the following code. int x = 0; int y = ((x 0)) ? 1: -1; 5 A. y becomes -1 after the code is executed. B. y becomes 1 after the code is executed. Keys: 1. C 2. B 3. C 4. B 5. A 6. D 7. A 8. A 9. D 10. B 11. A Please double check your answer before clicking the Submit button. Whatever submitted to LiveLab is FINAL and counted for your grade. Have you submitted your answer to LiveLib? ______________ What is your score? ______________ Solution: A.1 Write a statement to declare a constant named PI with value 3.14159. Answer: final double PI = 3.14159; A.2 Write an if-else statement that increase salary by 10% if age is greater than or equal to 50, and 5% if age is less than 35, and 8% otherwise. Answer: if (age >= 50) 6 salary = salary * 1.1; else if (age >= 35) salary = salary * 1.08; else salary = salary * 1.05; A.3 Write a conditional expression that assigns 1 to x if y is greater than 0 and -1 to x otherwise. x = (y >= 0) ? 1 : -1; Part B: 0 9.0 0 true 112 28 19 42 1942 is even -4 354 Part C: 1. import java.util.Scanner; public class Exercise03_04 { public static void main(String[] args) { int number = (int)(Math.random() * 12) + 1; // or int number = (int)(System.currentTimeMillis() % 12 + 1); // or int number = (int)(Math.random() * 12) + 1; if (number == 1) System.out.println(“Month is Januaray”); else if (number == 2) System.out.println(“Month is Feburary”); else if (number == 3) System.out.println(“Month is March”); else if (number == 4) System.out.println(“Month is April”); else if (number == 5) System.out.println(“Month is May”); else if (number == 6) System.out.println(“Month is June”); else if (number == 7) System.out.println(“Month is July”); else if (number == 8) System.out.println(“Month is August”); 7 else if (number == 9) System.out.println(“Month is September”); else if (number == 10) System.out.println(“Month is October”); else if (number == 11) System.out.println(“Month is November”); else // if (number == 12) System.out.println(“Month is December”); } } 2. import java.util.Scanner; public class Exercise03_12 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print(“Enter a three-digit integer: “); int number = input.nextInt(); if (number / 100 == number % 10) System.out.println(number + ” is a palindrome”); else System.out.println(number + ” is not a palindrome”); } } 8

Document Preview (8 of 698 Pages)

User generated content is uploaded by users for the purposes of learning and should be used following SchloarOn's honor code & terms of service.
You are viewing preview pages of the document. Purchase to get full access instantly.

Shop by Category See All


Shopping Cart (0)

Your bag is empty

Don't miss out on great deals! Start shopping or Sign in to view products added.

Shop What's New Sign in