Chapter 2 (2.1 - 2.6): Data and Expressions Self-Review questions

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Expression a is valid. Expression b is invalid because there are two open parentheses but only one close parenthesis. Similarly with expres- sion c, where there are two open parentheses but no close parenthesis. Expression d might be a valid algebraic expression in an algebra book, but it is not a valid expression in Java. There is no operator between the operands 2 and ( 4 ).

SR 2.26 For each of the following expressions state whether they are valid or invalid. If invalid, explain why. a. result = ( 5 + 2 ); b. result = ( 5 + 2 * ( 15 - 3 ); c. result = ( 5 + 2 (; d. result = ( 5 + 2 ( 4 ) );

After the sequence of statements, the value in result is 8.

SR 2.27 What value is contained in the integer variable result after the following sequence of statements is executed? result = 27; result = result + 3; result = result / 7; result = result * 2;

After the sequence of statements, the value in result is 8. Note that even though result was set to base + 3, changing the value of base to 7 does not retroactively change the value of result.

SR 2.28 What value is contained in the integer variable result after the following sequence of statements is executed? int base; int result; base = 5; result = base + 3; base = 7;

An assignment operator combines an operation with assignment. For example, the += operator performs an addition, then stores the value back into the variable on the left-hand side.

SR 2.29 What is an assignment operator?

After executing the statement, weight holds the value 83. The assign- ment operator −= modifies weight by first subtracting 17 from the cur- rent value (100), then storing the result back into weight.

SR 2.30 If an integer variable weight currently holds the value 100, what is its value after the following statement is executed? Explain. weight -= 17;

A widening conversion tends to go from a small data value, in terms of the amount of space used to store it, to a larger one. A narrowing conversion does the opposite. Information is more likely to be lost in a narrowing conversion, which is why narrowing conversions are consid- ered to be less safe than widening ones.

SR 2.31 Why are widening conversions safer than narrowing conversions?

The conversions are: a. widening,

SR 2.32 Identify each of the following conversions as either a widening conver- sion or a narrowing conversion. a. int to long

The conversions are: b. narrowing,

SR 2.32 Identify each of the following conversions as either a widening conver- sion or a narrowing conversion. b. int to byte

The conversions are: c. widening,

SR 2.32 Identify each of the following conversions as either a widening conver- sion or a narrowing conversion. c. byte to short

The conversions are: d. widening,

SR 2.32 Identify each of the following conversions as either a widening conver- sion or a narrowing conversion. d. byte to char

The conversions are: e. widening.

SR 2.32 Identify each of the following conversions as either a widening conver- sion or a narrowing conversion. e. short to double

During the execution of the statement, the value stored in value is read and transformed into a float as it is being copied into the memory location represented by result. But the value variable itself is not changed, so value will remain an int variable after the assignment statement.

SR 2.33 Assuming result is a float variable and value is an int variable, what type of variable will value be after the following assignment statement is executed? Explain. result = value;

During the execution of the statement, the value stored in result is read and then transformed into an int as it is being copied into the memory location represented by value. But the result variable itself is not changed, so it remains equal to 27.32, whereas value becomes 27.

SR 2.34 Assuming result is a float variable that contains the value 27.32 and value is an int variable that contains the value 15, what are the val- ues of each of the variables after the following assignment statement is executed? Explain. value = (int) result;

The results stored are a. 3 integer division is used since both operands are integers.

SR 2.35 Given the following declarations, what result is stored by each of the following assignment statements. int iResult, num1 = 17, num2 = 5; double fResult, val1 = 12.0, val2 = 2.34; a. iResult = num1 / num2;

The results stored are b. 3.0 integer division is used since both operands are integers, but then assignment conversion converts the result of 3 to 3.0.

SR 2.35 Given the following declarations, what result is stored by each of the following assignment statements. int iResult, num1 = 17, num2 = 5; double fResult, val1 = 12.0, val2 = 2.34; b. fResult = num1 / num2;

The results stored are c. 2.4 floating point division is used since one of the operands is a floating point.

SR 2.35 Given the following declarations, what result is stored by each of the following assignment statements. int iResult, num1 = 17, num2 = 5; double fResult, val1 = 12.0, val2 = 2.34; c. fResult = val1 / num2;

The results stored are d. 3.4 num1 is first cast as a double; therefore, floating point divi- sion is used since one of the operands is a floating point.

SR 2.35 Given the following declarations, what result is stored by each of the following assignment statements. int iResult, num1 = 17, num2 = 5; double fResult, val1 = 12.0, val2 = 2.34; d. fResult = (double) num1 / num2;

The results stored are e. 2 val1 is first cast as an int; therefore, integer division is used since both operands are integers.

SR 2.35 Given the following declarations, what result is stored by each of the following assignment statements. int iResult, num1 = 17, num2 = 5; double fResult, val1 = 12.0, val2 = 2.34; e. iResult = (int) val1 / num2;

The corresponding lines of the GasMileage program are a. import java.util.Scanner;

SR 2.36 Identify which line of the GasMileage program does each of the following. a. Tells the program that we will be using the Scanner class.

The corresponding lines of the GasMileage program are b. Scanner scan = new Scanner(System.in);

SR 2.36 Identify which line of the GasMileage program does each of the following. b. Creates a Scanner object.

The corresponding lines of the GasMileage program are c. Scanner scan = new Scanner(System.in);

SR 2.36 Identify which line of the GasMileage program does each of the following. c. Sets up the Scanner object scan to read from the standard input stream.

The corresponding lines of the GasMileage program are d. miles = scan.nextInt();

SR 2.36 Identify which line of the GasMileage program does each of the following. d. Reads an integer from the standard input stream.

Under the stated assumptions, the following code will ask users to enter their age and store their response in value. System.out.print("Enter your age in years: "); value = myScanner.nextInt();

SR 2.37 Assume you already have instantiated a Scanner object named myScanner and an int variable named value as follows in your program: Scanner myScanner = new Scanner(System.in); int value = 0; Write program statements that will ask the user to enter their age, and store their response in value.

The output produced by the code fragment is One Two Three

SR 2.4 What output is produced by the following code fragment? System.out.println("One "); System.out.print("Two "); System.out.println("Three ");

The output produced by the code fragment is Ready Set Go

SR 2.5 What output is produced by the following code fragment? System.out.print("Ready "); System.out.println(); System.out.println("Set "); System.out.println(); System.out.println("Go ");

The output produced by the statement is It is good to be 10 The + operator in the sub-expression (5 + 5) represents integer addi- tion, since both of its operands are integers. If the inner parentheses are removed, the + operators represent string concatenation and the output produced is It is good to be 55

SR 2.6 What output is produced by the following statement? What is pro- duced if the inner parentheses are removed? System.out.println("It is good to be " + (5 + 5));

An escape sequence is a series of characters that begins with the back- slash (\) and that implies that the following characters should be treated in some special way. Examples: \n represents the newline character, \t represents the tab character, and \" represents the quotation character (as opposed to using it to terminate a string).

SR 2.7 What is an escape sequence? Give some examples.

System.out.println("\"I made this letter longer than " + "usual because I lack the time to\nmake it short.\"" + "\n\tBlaise Pascal");

SR 2.8 Write a single println statement that will output the following exactly as shown (including line breaks and quotation marks). "I made this letter longer than usual because I lack the time to make it short." Blaise Pascal

A variable declaration establishes the name of a variable and the type of data that it can contain. A declaration may also have an optional initialization, which gives the variable an initial value.

SR 2.9 What is a variable declaration?

A string literal is a sequence of characters delimited by double quotes.

SR. 2.1 What is a string literal?

Both the print and println methods of the System.out object write a string of characters to the monitor screen. The difference is that, after printing the characters, the println performs a carriage return so that whatever's printed next appears on the next line. The print method allows subsequent output to appear on the same line.

SR. 2.2 What is the difference between the print and println methods?

A parameter is data that is passed into a method when it is invoked. The method usually uses that data to accomplish the service that it provides. For example, the parameter to the println method indicates what char- acters should be printed.

SR. 2.3 What is a parameter?

Given those variable declarations, the answers are: a. Five variables are declared: count, value, total, MAX_VALUE, and myValue.

SR 2.10 Given the following variable declarations, answer each question. int count = 0, value, total; final int MAX_VALUE = 100; int myValue = 50; a. How many variables are declared?

Given those variable declarations, the answers are: b. They are all of type int.

SR 2.10 Given the following variable declarations, answer each question. int count = 0, value, total; final int MAX_VALUE = 100; int myValue = 50; b. What is the type of these declared variables?

Given those variable declarations, the answers are: c. count, MAX_VALUE, and myValue are each given an initial value.

SR 2.10 Given the following variable declarations, answer each question. int count = 0, value, total; final int MAX_VALUE = 100; int myValue = 50; c. Which of the variables are given an initial value?

Given those variable declarations, the answers are: d. Yes, it is legal. myValue is a variable of type int and 100 is an int literal.

SR 2.10 Given the following variable declarations, answer each question. int count = 0, value, total; final int MAX_VALUE = 100; int myValue = 50; d. Based on the above declarations, is the following assignment state- ment legal? Explain. myValue = 100;

Given those variable declarations, the answers are: e. No, it is not legal. MAX_VALUE is declared as a final variable and therefore it cannot be assigned a value other than its initial value.

SR 2.10 Given the following variable declarations, answer each question. int count = 0, value, total; final int MAX_VALUE = 100; int myValue = 50; e. Based on the above declarations, is the following assignment state- ment legal? Explain. MAX_VALUE = 50;

The variable name you choose should reflect the purpose of the variable. For example: int numCDs = 0;

SR 2.11 Your program needs a variable of type int to hold the number of CDs in a music collection. The initial value should be zero. Write a declara- tion statement for the variable.

The variable name you choose should reflect the purpose of the variable. Since the number of feet in a mile will not change, it is a good idea to declare a constant. For example: final int FT_PER_MILE = 5280;

SR 2.12 Your program needs a variable of type int to hold the number of feet in a mile (5,280). Write a declaration statement for the variable.

First, by carefully choosing the name of the constant, you can make your program more understandable than if you just use the literal value. Second, using a constant ensures that the literal value represented by the variable will not be inadvertently changed somewhere in the program. Third, if you ever do have to rewrite the program using a different literal value, you will only need to change that value once, as the initial value of the constant, rather than many places throughout the program.

SR 2.13 Briefly describe three reasons for using a constant in a program instead of a literal value.

Primitive data are basic values such as numbers or characters. Objects are more complex entities that usually contain primitive data that help define them.

SR 2.14 What is primitive data? How are primitive data types different from objects?

An integer variable can store only one value at a time. When a new value is assigned to it, the old one is overwritten and lost.

SR 2.15 How many values can be stored in an integer variable?

The four integer data types in Java are byte, short, int, and long. They differ in how much memory space is allocated for each and therefore how large a number they can hold.

SR 2.16 What are the four integer data types in Java? How are they different?

Java automatically assigns an integer literal the data type int. If you append an L or an l on the end of an integer literal, for example 1234L, Java will assign it the type long.

SR 2.17 What type does Java automatically assign to an integer literal? How can you indicate that an integer literal should be considered a different type?

Java automatically assigns a floating point literal the data type double. If you append an F or an f on the end of a floating point literal, for example 12.34f, Java will assign it the type float.

SR 2.18 What type does Java automatically assign to a floating point literal? How can you indicate that a floating point literal should be considered a different type?

A character set is a list of characters in a particular order. A character set defines the valid characters that a particular type of computer or program- ming language will support. Java uses the Unicode character set.

SR 2.19 What is a character set?

The original ASCII character set supports 27 5 128 characters, the extended ASCII character set supports 28 5 256 characters, and the UNICODE character set supports 216 5 65,536 characters.

SR 2.20 How many characters are supported by the ASCII character set, the extended ASCII character set, and the Unicode character set?

The result of 19%5 in a Java expression is 4. The remainder operator % returns the remainder after dividing the second operand into the first. The remainder when dividing 19 by 5 is 4.

SR 2.21 What is the result of 19%5 when evaluated in a Java expression? Explain.

The result of 13/4 in a Java expression is 3 (not 3.25). The result is an integer because both operands are integers. Therefore, the / operator per- forms integer division, and the fractional part of the result is truncated.

SR 2.22 What is the result of 13/4 when evaluated in a Java expression? Explain.

After executing the statement, diameter holds the value 20. First, the current value of diameter (5) is multiplied by 4, and then the result is stored back in diameter.

SR 2.23 If an integer variable diameter currently holds the value 5, what is its value after the following statement is executed? Explain. diameter = diameter * 4;

Operator precedence is the set of rules that dictates the order in which operators are evaluated in an expression.

SR 2.24 What is operator precedence?

a. 15+7*3 = 15+21 =36

SR 2.25 What is the value of each of the following expressions? a. 15 + 7 * 3

b. (15+7)*3 = 22*3 = 66

SR 2.25 What is the value of each of the following expressions? b. (15 + 7) * 3

c. 3*6+10/5+5 = 18+2+5 = 25

SR 2.25 What is the value of each of the following expressions? c. 3 * 6 + 10 / 5 + 5

d. 27%5+7%3 = 2+1 = 3

SR 2.25 What is the value of each of the following expressions? d. 27 % 5 + 7 % 3

e. 100/2/2/2 = 50/2/2 = 25/2 = 12

SR 2.25 What is the value of each of the following expressions? e. 100 / 2 / 2 / 2

f. 100/(2/2)/2 = 100/1/2 = 100/2 = 50

SR 2.25 What is the value of each of the following expressions? f. 100 / ( 2 / 2) / 2


संबंधित स्टडी सेट्स

UNIV 1001 UOP Graded Quiz Unit 3

View Set

Chp 10: Reshaping of Medieval Europe

View Set

Chapter 9. Games and Strategic Behavior

View Set

Ch 4-6 Test Review/ Drivers Ed./ Jenkins/

View Set