Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
- Consider the following code snippet:
Question: What operators does the code contain?arrayOfInts[j] > arrayOfInts[j+1]
Answer:[]
,>
,[]
,+
- Consider the following code snippet:
int i = 10; int n = i++%5;
- Question: What are the values of
i
andn
after the code is executed?
Answer:i
is 11, andn
is 0.]
- Question: What are the final values of
i
andn
if instead of using the postfix increment operator (i++
), you use the prefix version (++i)
)?
Answer:i
is 11, andn
is 1.
- Question: What is the value of
i
after the following code snippet executes?Answer:int i = 8; i >>=2;i
is 2.
- Question: What is the value of
i
after the following code snippet executes?Answer:int i = 17; i >>=1;i
is 8.
- Exercise: Write a program that tests whether a floating point number is zero. (Hint: You shouldn't generally use the equality operator
==
with floating point numbers, since floating point numbers by nature are hard to match exactly. Instead, test whether the number is close to zero.)
Solution:FloatTest
- Exercise: Write a program that calculates the number of US dollars equivalent to a given number of French francs. Assume an exchange rate of 6.85062 francs per dollar. If you want to control the format of the numbers your program displays, you can use the
DecimalFormat
class, which is discussed in Customizing Formats
Solution:CurrencyExchange
- Write a program that uses the bits in a single integer to represent the true/false data shown in the following figure:
Include in the program a variable named status
, and have the program print the meaning ofstatus
. For example, ifstatus
is 1 (only bit 0 is set) the program should print something like this:ready to receive requests
- Exercise: Show your code.
Solution:Bits
- Exercise: What is the output when status is 8?
Solution:unrecoverable error occurred- Exercise: What is the output when status is 7?
Solution:ready to receive requests processing a request error recovery in progress
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |