Web.cs.dal.ca
Tutorial 5 - Answers
1. Identify which of the following are invalid and which are reserved words in Java.
| |Valid |Reserved |
|int |‡ |‡ |
|74ElmStreet |INVALID - variable names can't begin with a digit | |
|L$&%# |INVALID - variable names can't include special | |
| |characters (%#&) | |
|boolean |‡ |‡ |
|Boolean |‡ | |
|_number |‡ | |
|INT |‡ | |
|public |‡ |‡ |
|Private |‡ | |
|Joe |‡ | |
|j1 |‡ | |
|k*2 |INVALID - variable names can't include special | |
| |characters (*) | |
2. Let p, q, r be declared to be int. Write a single Java statements which do the following: Part a) is done for you:
a) if p and q are divisible by r, print out p/r and q/r
Answer
if ((p % r == 0) && (q % r == 0) )
System.out.println( p/r + " " + q/r);
b) if p is less than q and q is less then r, print out p
if ((p < q) && (q < r))
System.out.println (p);
c) if p is odd, or: if q is odd and r is even, then print p and r
if ((p%2 != 0) || ((q % 2 != 0) && (r % 2 == 0)))
System.out.println(p + " " + r);
d) if p is equal to q and neither r = p nor r = q holds, print r
if ((p==q) && !((r==p) || (r==q)))
System.out.println(r);
OR
if ((p == q) && ((r!=p) || (r!=p))
System.out.println(r);
3. For the following exercises, indicate the output that will be produced. Assume the following declarations are made just before each exercise. That is, assume these initializations are in effect at the beginning of each problem.
final int MIN=10, MAX=20;
int num = 15;
| |Output |
| |1 |
|for (int i=1; i10; i--) |No output (i=10 and 10 is not greater than 10) |
|System.out.println(i); | |
| | |
|for (int i=1; i ................
................
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.