Java Ch4 walkthrough

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

58. precedence and associativity of logical operators

! then && then ||. Always enclose the ! operand in parenthesis unless you are applying it to a variable or a simple expression with no other operators. So here's an error thats on another problem: !x > 2 .... so there I tell you to fix it! Its in this Quizlet. Also, the && and || operators rank lower in precedence than the relational operators so that those precedence problems are less likely to occur. Parentheses can be used to force the precedence to be changed.

16. Which of the following strings could be passed to the DecimalFormat constructor to display 12.78 as 12.8%? a. "##0.0%" b. "###.##%" c. "000.0%" d. "#0.00%"

"##0.0%" (pdf)

51. Best to use this operator when determining whether a number is inside a range.

&&

30. What will be displayed after the following statements are executed? int y = 10; if (y == 10) { int x = 30; x += y; System.out.println(x); } a. 40 b. 30 c. 20 d. The code contains an error and will not compile.

40

59b. Comparing String Objects

In most cases, you cannot use the relational operators to compare two string objects. Reference variables contain the address of the object they represent. Unless the references point to the same object, the relational operators will not return true.

55. Code where the statements are executed one after the other without branching off in another direction is called what?

Sequence structure

36. When a character is stored in memory, it is actually the __________ that is stored. a. Unicode number b. ASCII code c. floating-point value d. letter, symbol, or number

Unicode number

67. Utility class

a class that serves the info system as a whole and, usually, does not reflect an object in the real world.

11. A random number, created as an object of the Random class, is always an integer.

f

34. A __________ is a boolean variable that signals when some condition exists in the program. a. sentinel b. flag c. block d. case

flag

64. Switch statement form

switch (test expression) { case value_1: //statements... break: case value_2: //statements... break: On and on till default: //statement }

62. What is the short hand way to write this: if(x> y) Z = 10; else Z = 5;

x > y ? Z = 10 : Z = 5;

22. What will be the value of x after the following statements are executed? int x = 75; int y = 60; if (x > y) x = x - y; a. 60 b. 75 c. 15 d. 135

15

41. Which of the following statements will create an object from the Random class? a. randomNumbers() = new Random(); b. Random myNumber = new Random(); c. myNumber = new Random(); d. Random = new randomNumbers();

b. (Random myNumber = new Random(); )

19. The boolean expression in an if statement must evaluate to a. degrees or radians b. true or false c. positive or negative d. left or right

b. (t or f)

20. A flag may have the values a. defined or undefined b. true or false c. of any range of integers d. of any Unicode character

b. (t or f)

3. In a switch statement, if two different values for the CaseExpression would result in the same code being executed, you must have two copies of the code, one after each CaseExpression.

f

5. All it takes for an AND expression to be true is for one of the subexpressions to be true.

f

52. When an if statement is nested in the else clause of another statement, the only time the inner if statement is executed is when the Boolean expression of the outer if statement is true. o True o False

f

6. The DecimalFormat class is part of the Java API so it is automatically available to your programs.

f

7. When two strings are compared using the String class's compareTo method, the comparison is not case sensitive.

f

8. When testing for character values, the switch statement does not test for the case of the character.

f

53. Fix this: if (!x > 20) and, btw, what is wrong with it?

if ( ! (X > 20) ) it is asking "is the logical complement of x greater than 20?" The ! operator can only be applied to Boolean expressions. This statement would cause a compiler error.

35. The __________ statement is used to create a decision structure which allows a program to have more than one path of execution. a. block b. if c. null d. flag

if (decides whether a section of code executes or not. This uses a Boolean to decide.. if it does not have a block of curly braces, it is ended by the first ";" encountered. If curly braces are used to group conditionally executed statements, the "if" is ended by the closing curly brace )

17. Which of the following strings could be passed to the DecimalFormat constructor to display 12.78 as 12.8? a. "###.#" b. "###.0" c. "000.0" d. "#0.00%"

"000.0" (?)(pdf)

60. In the string class, what are two methods that we commonly use that are case sensitive? How can we change that?

"equals" and "compareTo" ....equalsIgnoreCase.....compareToIgnoreCase

18. A block of code is enclosed in a set of a. braces, { } b. parentheses, ( ) c. brackets, [ ] d. double quotes, " "

(braces,) { }

45. flag variable...

... is a Boolean variable that signals when some condition exists in the program. When it is is set to false, it indicates the condition does not yet exist. When it is set to true, it means the condition does exist.

24. What will be the value of discountRate after the following statements are executed? double discountRate = 0.0; int purchase = 100; if (purchase > 1000) discountRate = 0.05; else if (purchase > 750) discountRate = 0.03; else if (purchase > 500) discountRate = 0.01; a. 0.0 b. 0.05 c. 0.03 d. 0.01

0.0

27. What will be the value of discountRate after the following statements are executed? double discountRate; char custType = 'B'; switch (custType) { case 'A': discountRate = 0.08; break; case 'B': discountRate = 0.06; case 'C': discountRate = 0.04; default: discountRate = 0.0; } a. 0.08 b. 0.06 c. 0.04 d. 0.0

0.0 (I think I'm starting to understand the answer. There is no break statement on the other ones. Look prior to and after page 242)

25. What will be the value of discountRate after the following statements are executed? double discountRate = 0.0; int purchase = 1250; char cust = 'N'; if (purchase > 1000) if (cust == 'Y') discountRate = 0.05; else discountRate = 0.04; else if (purchase > 750) if (cust == 'Y') discountRate = 0.04; else discountRate = 0.03; else discountRate = 0.0; a. 0.0 b. 0.04 c. 0.05 d. 0.03

0.04 (this is a good one and it took me a minute to understand it.)

59a. Order of precedence(A more complete view)

1- ! ...2- ("*asterisk"/ % )...3- ... + - ....4- < > <= >= ...5- ...== != ...6- ...&& ...7- ...|| ...8- ... = += -= *= /= %=

23. What will be the value of bonus after the following statements are executed? int bonus, sales = 10000; if (sales < 5000) bonus = 200; else if (sales < 7500) bonus = 500; else if (sales < 10000) bonus = 750; else if (sales < 20000) bonus = 1000; else bonus = 1250; a. 750 b. 1250 c. 500 d. 1000

1000

29. What will be displayed after the following statements are executed? int x = 65; int y = 55; if (x >= y) { int ans = x + y; } System.out.println(ans); a. 10 b. 120 c. 100 d. The code contains an error and will not compile.

120

26. What will be the value of x after the following statements are executed? int x = 10; switch (x) { case 10: x += 15; case 12: x -= 5; break; default: x *= 3; } a. 30 b. 20 c. 25 d. 526.

20 (this is another fall through case like on page 242.)

51. The conditional operator takes this many operands.

3 ( called ternary

31. What will be displayed after the following statements are executed? int hours = 30; double pay, payRate = 10.00; pay = hours <= 40 ? hours*payRate : hours*payRate*2.0; System.out.println(pay); a. 300.0 b. 600.0 c. 400.0 d. The code contains an error and will not compile.

300.0

54. What is the statement part of this expression: if ( expression ) statement

A conditionally executed statement. It only happens if the expression is true.

48. "if" statement structure

A decision structure. if (Boolean expression) { // Statements to execute when the expression is true (first branch) }

49. null statement

An empty statement that does nothing.

65. Method decomposition

Breaking one method up into several simpler methods (helper or utility methods) to create a more understandable design.(some of this is on the PowerPoint)

44. Why is it good advice to indent all the statements inside a set of braces?

By indenting the conditionally executed statements, you are causing them to stand out visually. This is so you can tell at a glance what part of the program the if statement executes.

46. &&

It takes two boolean expressions as operands and creates a Boolean expression that is true only when both sub expressions are true.

68. How would you get a randomly generated number?

Java API has a class named "random" for this purpose. Use this statement to get it: import java.util.Random; Random randomNumbers = new Random( ); // creates an Instance

28. What will be the value of ans after the following statements are executed? int x = 40; int y = 40; if (x = y) ans = x + 10; a. 30 b. 80 c. 50 d. The code contains an error and will not compile.

The code contains an error and will not compile.(why?- needs == )

47. Relational Operators

Used to compare two values so they have 2 operands which means the relational operators are binary. The <= and >= operators test for more than one relationship. Operators include ==,<,>,<=,>=, !=

33. Enclosing a group of statements inside a set of braces creates a. an expression b. a block of statements c. a relational operator d. a conditional statement

a block of statements

38. Which of the following statements determines whether the variable temp is within the range of 0 through 100 (inclusive)? a. if (temp >= 0 && temp <= 100) b. if (temp > 0 && temp < 100) c. if (temp >= 0 || temp <= 100) d. if (temp > 0 || temp < 100)

a. ( ... if (temp >= 0 && temp <= 100). ...)

15. Which of the following expressions could be used to perform a case-insensitive comparison of two String objects named str1 and str2? a. str1.equalsIgnoreCase(str2) b. str1.equalsInsensitive(str2) c. str1 != str2 d. str1 || str2

a. ( str1.equalsIgnoreCase(str2) )

37. Java requires that the boolean expression being tested by an if statement be enclosed in a. a set of parentheses b. a set of braces c. a set of double quotes d. a set of brackets

a. (a set of parentheses)

21. What will be the values of ans, x, and y after the following statements are executed? int ans = 35, x = 50, y = 50; if (x >= y) { ans = x + 10; x -= y; } else { ans = y + 10; y += x; } a. ans = 60, x = 0, and y = 50 b. ans = 45, x = 50, and y = 0 c. ans = 45, x = 50, and y = 50 d. ans = 60, x = 50, and y = 100

a. (ans = 60, x = 0, and y = 50)

39. Which of the following expressions will determine whether x is less than or equal to y? a. x <= y b. x => y c. x >= y d. x =< y

a. (x <= y)

63. switch statement

allows you to use an ordinal value to determine how a program will branch. In many cases, using a switch statement can simplify a complex combination of if-else statements. It can evaluate a char, byte, short, int, or string value and make decisions based on the value. A "break" statement ends the "case" statement and is optional. If there is no "break", then program execution continues into the next "case".

50. nested "if" statement

an if statement that appears inside another if statement

32. Which of the following expressions determines whether the char variable, chrA, is not equal to the letter 'A'? a. chrA == 'A' b. chrA != 'A' c. chrA || 'A' d. chrA.notEquals(A)

chrA != 'A'

13. __________ operators are used to determine whether a specific relationship exists between two values. a. Assignment b. Arithmetic c. Logical d. Relational

d. (Relational)

56. The trailing else in an if/else if statement has a similar purpose as the __________ section of a switch statement.

default(from Quizlet) (The trailing else is optional but often used)

61. Variable Scope( a local one)

dictates what portions the code can "see" or use a variable in, typically derived from where the variable was first created. This does not have to be at the beginning of the method. It does terminate at the end of the method. When a program enters a section of code where a variable has scope, that variable has come "into scope", which means the variable is visible to the program.

66. Private methods can only be accessed by ______.

methods of the same class

40. Which of the following expressions will generate a random number in the range of 1 through 10? a. myNumber = randomNumbers.nextInt(10); b. myNumber = randomNumbers.nextInt(1) + 10; c. myNumber = randomNumbers.nextInt(11) - 1; d. myNumber = randomNumbers.nextInt(10) + 1;

myNumber = randomNumbers.nextInt(10) + 1;

69. Some methods of the Random class

nextDouble( ) returns the next random number as a "double" within the range of 0.0 and 1.0. nextFloat( ) returns the next random number as a float within the range of 0.0 and 1.0. nextInt( ) Returns an integer which is -2, 147, 483, 648 to + 2, 147, 483, 648. nextInt(int n) this method accepts an integer argument, "n". It returns a random number as an "int". The number will be within the range of 0 to n.

42. Select all that apply. Which method of the Random class will return a random number within the range of 0.0 and 1.0? a. nextDouble() b. nextLong() c. nextFloat() d. nextInt(int n)

nextDouble() and nextFloat()

43. Conditionally executed

performed only when a certain condition is true

14. If str1 and str2 are both String objects, which of the following expressions will correctly determine whether or not they are equal? a. str1 = str2 b. str1 && str2 c. str1.equals(str2) d. str1 += str2

str1.equals(str2)

1. An important style rule that should be used when writing if statements is to write the conditionally executed statement on the line after the if statement.

t

2. The if-else statement will execute one group of statements if its boolean expression is true or another group if its boolean expression is false.

t

4. All it takes for an OR expression to be true is for one of the subexpressions to be true.

t

9. If the expression on the left side of the && operator is false, the expression on the right side will not be checked

t

10. Unicode is an international encoding system that is extensive enough to represent all the characters of all the world's alphabets. What does it mean when they say the characters are ordinal?

t (ordinal= they have order in the Unicode character set. The characters can be compared to each other. Therefore, "A(65)"= is not the same as "a((97)". )

57. if-else-if

tests a series of conditions. It is often simpler to test a series of conditions with the "if else if" statement than with a set of nested "if else" statements.

12. In an if-else statement, if the boolean expression is false a. no statements or blocks are executed b. the statement or block following the else is executed c. the first statement or block is executed d. all the statements or blocks are executed

the statement or block following the else is executed


Ensembles d'études connexes

Chapter 1 Quiz - General Insurance

View Set

AP Psychology Major Figures in Psychology

View Set

Macro 2315 Final Exam practice questions Bishop

View Set

Lesson 4: Grounding Electrodes and the Grounding Electrode System

View Set

Patho PrepU Ch. 8: Disorders of Fluid and Electrolyte and Acid Base Balance

View Set

Business law chapter 1,2,3 true or false

View Set