首页 JAVA练习题含答案-answer to pratice 3

JAVA练习题含答案-answer to pratice 3

举报
开通vip

JAVA练习题含答案-answer to pratice 3JAVA练习题含答案-answer to pratice 3 Chapter 3 Flow of Control , Multiple Choice 1) An if selection statement executes if and only if: (a) the Boolean condition evaluates to false. (b) the Boolean condition evaluates to true. (c) the Boolean condition is shor...

JAVA练习题含答案-answer to pratice 3
JAVA练习题含答案-answer to pratice 3 Chapter 3 Flow of Control , Multiple Choice 1) An if selection statement executes if and only if: (a) the Boolean condition evaluates to false. (b) the Boolean condition evaluates to true. (c) the Boolean condition is short-circuited. (d) none of the above. Answer: B (see page 97) 2) A compound statement is enclosed between: (a) [ ] (b) { } (c) ( ) (d) < > Answer: B (see page 98) 3) A multi-way if-else statement (a) allows you to choose one course of action. (b) always executes the else statement. (c) allows you to choose among alternative courses of action. (d) executes all Boolean conditions that evaluate to true. Answer: C (see page 100) 4) The controlling expression for a switch statement includes all of the following types except: (a) char (b) int (c) byte (d) double Answer: D (see page 104) Copyright ? 2006 Pearson Education Addison-Wesley. All rights reserved. 1 2 5) To compare two strings lexicographically the String method ____________ should be used. (a) equals (b) equalsIgnoreCase (c) compareTo (d) == Answer: C (see page 112) 6) When using a compound Boolean expression joined by an && (AND) in an if statement: (a) Both expressions must evaluate to true for the statement to execute. (b) The first expression must evaluate to true and the second expression must evaluate to false for the statement to execute. (c) The first expression must evaluate to false and the second expression must evaluate to true for the statement to execute. (d) Both expressions must evaluate to false for the statement to execute. Answer: A (see page 116) 7) The OR operator in Java is represented by: (a) ! (b) && (c) | | (d) None of the above Answer: C (see page 116) 8) The negation operator in Java is represented by: (a) ! (b) && (c) | | (d) None of the above Answer: A (see page 116) 9) The ____________ operator has the highest precedence. (a) * (b) dot (c) += (d) decrement Answer: B (see page 123) . Chapter 3 Flow of Control 3 10) The looping mechanism that always executes at least once is the _____________ statement. (a) if…else (b) do…while (c) while (d) for Answer: B (see page 132) 11) A mixture of programming language and human language is known as: (a) Algorithms (b) Recipes (c) Directions (d) Pseudocode Answer: D (see page 134) 12) When the number of repetitions are known in advance, you should use a ___________ statement. (a) while (b) do…while (c) for (d) None of the above Answer: C (see page 141) 13) To terminate a program, use the Java statement: (a) System.quit(0); (b) System.end(0); (c) System.abort(0); (d) System.exit(0); Answer: D (see page 148) , True/False 1) An if-else statement chooses between two alternative statements based on the value of a Boolean expression. Answer: True (see page 96) 2) You may omit the else part of an if-else statement if no alternative action is required. Answer: True (see page 97) 3) In a switch statement, the choice of which branch to execute is determined by an expression given in parentheses after the keyword switch. Answer: True (see page 104) 4) In a switch statement, the default case is always executed. Answer: False (see page 104) 4 5) Not including the break statements within a switch statement results in a syntax error. Answer: False (see page 104) 6) The equality operator (==) may be used to test if two string objects contain the same value. Answer: False (see page 111) 7) Boolean expressions are used to control branch and loop statements. Answer: True (see page 116) 8) The for statement, do…while statement and while statement are examples of branching mechanisms. Answer: False (see page 130) 9) An algorithm is a step-by-step method of solution. Answer: True (see page 134) 10) The three expressions at the start of a for statement are separated by two commas. Answer: False (see page 137) , Short Answer/Essay 1) What output will be produced by the following code? public class SelectionStatements { public static void main(String[] args) { int number = 24; if(number % 2 == 0) System.out.print("The condition evaluated to true!"); else . Chapter 3 Flow of Control 5 System.out.print("The condition evaluated to false!"); } } Answer: The condition evaluated to true! 2) What would be the output of the code in #1 if number was originally initialized to 25? Answer: The condition evaluated to false! 3) Write a multi-way if-else statement that evaluates a persons weight on the following criteria: A weight less than 115 pounds, output: Eat 5 banana splits! A weight between 116 pounds and 130 pounds, output: Eat a banana split! A weight between 131 pounds and 200 pounds, output: Perfect! A weight greater than 200 pounds, output: Plenty of banana splits have been consumed! Answer: if(weight <= 115) System.out.println("Eat 5 banana splits!"); else if(weight <= 130) System.out.println("Eat a banana split!"); else if(weight <=200) System.out.println("Perfect!"); else System.out.println("Plenty of banana splits have been consumed!"); 4) Write an if-else statement to compute the amount of shipping due on an online sale. If the cost of the purchase is less than $20, the shipping cost is $5.99. If the cost of the purchase over $20 and at most $65, the shipping cost is $10.99. If the cost of the purchase is over $65, the shipping cost is $15.99. Answer: 6 if(costOfPurchase < 20) shippingCost = 5.99; else if((costOfPurchase > 20)&&(costOfPurchase <= 65)) shippingCost = 10.99; else shippingCost = 15.99; 5) Evaluate the Boolean equation: !( ( 6 < 5) && (4 < 3)) Answer: True 6) Write Java code that uses a do…while loop that prints even numbers from 2 through 10. Answer: int evenNumber = 2; do { System.out.println(evenNumber); evenNumber += 2; }while(evenNumber <= 10); 7) Write Java code that uses a while loop to print even numbers from 2 through 10. Answer: int evenNumber = 2; while(evenNumber <= 10) { System.out.println(evenNumber); . Chapter 3 Flow of Control 7 evenNumber += 2; } Answer: 8) Write Java code that uses a for statement to sum the numbers from 1 through 50. Display the total sum to the console. Answer: int sum = 0; for(int i=1; i <= 50; i++) { sum += i; } System.out.println("The total is: " + sum); 9) What is the output of the following code segment? public static void main(String[] args) { int x = 5; System.out.println("The value of x is:" + x); while(x > 0) { x++; } System.out.println("The value of x is:" + x); 8 } Answer: . 10) Discuss the differences between the break and the continue statements when used in looping mechanisms. Answer: When the break statement is encountered within a looping mechanism, the loop immediately terminates. When the continue statement is encountered within a looping mechanism, the current iteration is terminated, and execution continues with the next iteration of the loop. Programming projects: 1. Write a complete Java program that prompts the user for a series of numbers to determine the smallest value entered. Before the program terminates, display the smallest value. Answer: import java.util.Scanner; public class FindMin { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int smallest = 9999999; String userInput; boolean quit = false; . Chapter 3 Flow of Control 9 System.out.println("This program finds the smallest number" + " in a series of numbers"); System.out.println("When you want to exit, type Q"); while(quit != true) { System.out.print("Enter a number: "); userInput = keyboard.next(); if(userInput.equals("Q") || userInput.equals("q")) { quit = true; } else { int userNumber = Integer.parseInt(userInput); if(userNumber < smallest) smallest = userNumber; } 10 } System.out.println("The smallest number is " + smallest); System.exit(0); } } 2. Write a complete Java program that uses a for loop to compute the sum of the even numbers and the sum of the odd numbers between 1 and 25. public class sumEvenOdd { public static void main(String[] args) { int evenSum = 0; int oddSum = 0; //loop through the numbers for(int i=1; i <= 25; i++) { if(i % 2 == 0) { //even number . Chapter 3 Flow of Control 11 evenSum += i; } else { oddSum += i; } } //Output the results System.out.println("Even sum = " + evenSum); System.out.println("Odd sum = " + oddSum); } } /** * Question2.java * * This program simulates 10,000 games of craps. * It counts the number of wins and losses and outputs the probability * of winning. * * Created: Sat Mar 05, 2005 * * @author Kenrick Mock * @version 1 */ public class Question2 { 12 private static final int NUM_GAMES = 10000; /** * This is the main method. It loops 10,000 times, each simulate * a game of craps. Math.random() is used to get a random number, * and we simulate rolling two dice (Math.random() * 6 + 1 simulates * a single die). */ public static void main(String[] args) { // Variable declarations int numWins = 0; int numLosses = 0; int i; int roll; int point; // Play 10,000 games for (i=0; i 0; z--) { temp = temp * (x/z); } // end of for () sum += temp; } // end of for () System.out.println(sum); sum = 1.0; } // end of for () System.out.print("For n equal to 50, the result of " + "calculating e^x is: "); for ( int n = 1; n <= 50; n++) { temp = 1.0; for ( double z = n; z > 0; z--) { temp = temp * (x/z); } // end of for () sum += temp; } // end of for () System.out.println(sum); sum = 1; System.out.print("For n equal to 100, the result of " + "calculating e^x is: "); for ( int n = 1; n <= 100; n++) { temp = 1.0; for ( double z = n; z > 0; z--) { temp = temp * (x/z); } // end of for () sum += temp; } // end of for () System.out.println(sum); 16 System.out.println("\nEnter Q to quit or Y to go again."); keyboard.nextLine(); // Clear buffer line = keyboard.nextLine(); } while (line.charAt(0)!='q' && line.charAt(0)!='Q'); // end of while () } } // Question7 .
本文档为【JAVA练习题含答案-answer to pratice 3】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_079973
暂无简介~
格式:doc
大小:48KB
软件:Word
页数:21
分类:生活休闲
上传时间:2018-03-04
浏览量:15