Self-Text Exercises - Part 2

20. Does the following sequence produce a division by zero?

int j = -1;
if ((j>0 && (1/(j+1)>10) System.out.println(i);

21. Convert the following into a fully parenthezed expression:

bonus + day * rate / correctionFActor * newGuy - penalty

22. What is the output produced by the following?

int n = 10;
while (n>10)
{
System.out.println(n);
n = n - 3;
}

23. What output would be produced in Exercise 22 if the > sign were replaced with <?

 

24. What is the output produced by the following?

int n = 10;
do
{
System.out.println(n);
n = n - 3;
} while (n>0);

25. What output would be produced in Exercise 24 if the > sign were replaced by a <?

 

26. What is the output produced by the following?

int n = -42;
do
{
System.out.println(n);
n = n - 3;
} while n>0;

27. What is the most important difference between a while statement and a do-while statement?

 

28. What is the output of the following?

for (int count = 1; count < 5; count++) System.out.print((2*coiunt) + " ");

 

29. What is the output of the following?

for (int n = 10; n>0; n = n - 2) System.out.println("Hello " + n);

 

30. What is the output of the following?

for (double sample = 2; sample > 0; sample = sample - 0.5) System.out.println(sample + " ");

 

31. Rewrite the following for statement as a while loop (and possibly some additional statements):

int n;
for (n = 10; n>0; n = n - 2) System.out.println("Hello " + n);

 

 

32. What is the output of the following loop? Identify the connection between the value of n and the value of the variable log.

int n = 1024;
int log = 0;
for (int i = 1; i < n; i = i * 2)
log++:
System.out.println(n + " " + log);

33. What is the output of the following loop? Comment on the code. (This is not the same as the previous exercise.)

int n = 1024;
int log = 0;
for (int i = 1; i < n; i = i+2);
log++;
System.out.println(n + " " + log);

34. Predict the output of the following nested loops:

int n, m;
for (n = 1; n <= 10; n++)
for (m = 10; m >=1; m--)
System.out.println(n + " times " + m + " = " + n*m);

35. For each of the following situations, tell which type of loop (while, do-while, or for) would work best:

a. Summing a series, such as 1/2 + 1/3 + 1/4 + 1/5 + ... + 1/10

b. Reading in the list of exam scores for one student.

c. Reading in the number of days of sick leave taken by employees in a department.

36. What is the output of the following?

int number = 10;
while (number > 0)
{ System.out.println(number);
number = number + 3;
}

37. What is the output of the following?

int n, limit = 10;
for (n=1; n < limit; n++)
{
System.out.println("n = " + n);
System.out.println("limit = " + limit);
limit = n + 2;
}

38. What output is produced by the following?

int number = 10;
while (number > 0)
{ number = number + 2;
if (number == 4) break;
System.out.println(number); }
System.out.println("The end.");

39. What is the output produced by the following?

int number = 10;
while (number > 0)
{number = number + 2;
if (number == 4) continue;
System.out.println(number); }
System.out.println("The end.");

40. Fix the following code:

int n = 10;
int sum = 10;
while (n > 1)
{sum = sum + n;
n--; }
System.out.println("The sum of the integers 1 to 10 is " + sum);

41. Add some suitable output statements to the following code so that all variables are traced:

int n, sum = 0;
for (n=1; n<10; n++) sum = sum + n;
System.out.println("1 + 2 + ... + 9 + 10 =" + sum);

 

42. What is the bug in the following code? What do you call this kind of loop bug?

int n, sum = 0;
for (n=1; n < 10; n++) sum = sum + n;
System.out.println("1 + 2 + ... + 9 + 10 = " + sum);

 

43, Write an assertion check that checks to see that the value of the variable time is less than or equal to the value of the variable limit. Both variables are of type int.