Exercises: - SIUE
Exercises:
1. Write a fragment of code that will test whether an integer variable score contains a valid test score. Valid test scores are in the range 0 to 100.
|Solution: |
| |
|if( score >= 0 && score 5)
System.out.println(“A”);
else if (x < 10)
System.out.println(“B”);
else
System.out.println(“C”);
What is displayed if x is
a. 4; b. 5; c. 6; d. 9; e. 10; f. 11
|Solution: |
| |
|a) B |
|b) B |
|c) A |
|d) A |
|e) A |
|f) A |
5. Consider the following fragment of code:
if (x > 5)
{
System.out.println(“A”);
if (x < 10)
System.out.println(“B”);
}
else
System.out.println(“C”);
What is displayed if x is
a. 4; b. 5; c. 6; d. 9; e. 10; f. 11
|Solution: |
| |
|a) C |
|b) C |
|c) A B |
|d) A B |
|e) A |
|f) A |
6. We would like to assess a service charge for cashing a check. The service charge depends on the amount of the check. If the check amount is less than $10, we will charge $1. If the amount is greater than $10 but less than $100, we will charge 10% of the amount. If the amount is greater than $100, but less than $1,000, we will charge $5 plus 5% of the amount. If the value is over $1,000, we will charge $40 plus 1% of the amount. Use a multibranch if-else statement in a fragment of code to compute the service charge.
|Solution: |
| |
|if(amount < 10.0) |
|serviceCharge = 1.0; |
|else if(amount < 100.0) |
|serviceCharge = 0.1 * amount; |
|else if(amount < 1000.0) |
|serviceCharge = 5.0 + 0.05 * amount; |
|else |
|serviceCharge = 40.0 + 0.01 * amount; |
| |
|This code is in Fragments.java. |
7. What is the value of each of the following boolean expressions if x is 5, y is 10,
and z is 15?
a. (x < 5 && y > x)
b. (x < 5 || y > x)
c. (x > 3 || y < 10 && z == 15)
d. (!(x > 3) && x != z || x + y == z)
|Solution: |
| |
|a) false |
|b) true |
|c) true |
|d) true |
| |
|This code is in Fragments.java. |
8. The following code fragment will not compile. Why?
if !x > x + y
x = 2 * x;
else
x = x + 3;
|Solution: |
| |
|We need to add a couple pairs of parentheses. The boolean expression for an if statement must be in parentheses. And we must|
|apply the not operator to a boolean value. |
|if (!(x > x + y)) |
9. Consider the boolean expression ((x > 10) || (x < 100)). Why is this expression probably not what the programmer intended?
|Solution: |
| |
|This expression will always evaluate to true. |
10. Consider the boolean expression ((2 < 5) && (x < 100)). Why is this expression probably not what the programmer intended?
|Solution: |
| |
|This 2 ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- amendment of solicitation modification of
- speech recognition in noise test sprint for h 3 profile
- lab assignment one part i convert scores to letter grades
- egre 254 virginia commonwealth university
- generating c code from a simulink model
- exercises siue
- metafor visualizing stories as code
- air force core personnel document cpd
- ocps teacherpress
Related searches
- writing exercises for 5th graders
- printable english exercises for beginners
- writing exercises for 4th graders
- writing exercises for 7th graders
- writing exercises for 7th grade
- writing exercises for 6th graders
- reading comprehension exercises for adults
- english exercises for beginners pdf
- creative writing exercises for kids
- english grammar exercises free printables
- english grammar exercises free online
- vocabulary exercises for adults