Practice Problems for Midterm II

1. What is wrong with the following Java program? I wanted it to compute the sum of only the odd integers from 1 to 50. You will need to make 2 changes to get it to work. // program to compute sum of odd integers from 1 to 50. class sum {

public static void main(String [] args) {

int x; int sum = 0;

for (x = 1; x != 50; x++) sum = sum + x;

System.out.println("Sum = " + sum); } // ends main } // ends the class

2. What is the final value of x after each of the following pieces of code? a. int x = 2;

for (int n = 1; n < 3; n++) x = x * x;

b. char x = ' '; String name = new String("Lyndon B. Johnson"); int pos;

for (pos = 1; pos < name.length( ); pos++) if (name.charAt(pos) == '.') x = name.charAt(pos-1); 3. What output is produced by the following program: class problem3 { public static void main(String []args) {

int a = 2, b = 5; int j, k;

for (j = 1; j <= a; j++) { for (k = 1; k <= b; k++) { if ((j % 2) == 1) System.out.print (" - "); else System.out.print (" + k + " "); } System.out.println(); } return; } //ends main } //ends the class

4. Write a program that reads integers count and marker from the input. It then reads count integers. Of these numbers, the program prints the first integer after any time marker appears. The last number != marker. Ex: Input of "8 0 0 2 3 0 0 -3 0 10" should result in output of "2 0 -3 10"

5. What is the output of the following programs? (a) class test1 { public static void main(String [] args) { int Counter1 = 0 ; int Counter2 = 0 ;

2/8 int i,j; for (i = 1; i <= 3; ++i) { ++Counter1; for (j = 1; j <= 5; ++j) { ++Counter2; } } System.out.println("Counter1: " + Counter1); System.out.println("Counter2: " + Counter2); return; } }

(b) class test1 { public static void main(String [] args) { final int TableSize = 10; int i = 0; int ValueSquared = 1;

System.out.println("i" + "\t " + " 2 ** i"); while (i <= TableSize) { System.out.println (i + "\t\t" + ValueSquared); ValueSquared *= 2; ++i; } return; } }

(c) Rewrite the while loop in part (b) using a for-loop.

6. class test6 { public static void main(String [] args) { float Area, MyRadius; Scanner in = new Scanner(System.in); System.out.println("Enter radius: ");

3/8 MyRadius = in.nextInt(); Area = CircleArea (MyRadius); System.out.println("Circle with radius " + MyRadius + " has area " + Area); return; } // ends main

//CircleArea(): compute area of radius r circle static float CircleArea (float r) { final float Pi = 3.1415f; /*THIS*/ return Pi * r * r; } } // ends class

Answer the following questions assuming user input is 10. (a). At the point of execution denoted by THIS, what is the value of MyRadius? (b). At the point of execution denoted by THIS, what is the value of r? (c) What value is returned by the function CircleArea? (d). What is the output of the above program?

7. Suppose the following definitions are in effect; boolean P = false; boolean Q = true; boolean R = true; Evaluate and give the values of the following expressions: a. P && Q || ! P && ! Q b. P || Q && ! P || ! Q c. 0 = = 1 || 0 < 1 d. P && (Q || R) e. ! ! P

4/8 8. static float diff(float a, float b) // This function return the positive difference between a and b (a) What does float (the one before diff) signify in this function heading? (b) How many arguments must be used to call the preceding function diff? (c) Arguments to diff should be of which type? (d) Write the body of this function. (e) Write a main function that calls diff to compute the difference between 10 and 8.

1. True or False ?

a) Variables must be initialized before use.

b) If the two operands in an expression have different types, the compiler will always do an automatic casting to match types.

c) A loop Boolean expression is executed the same number of times as the loop body.

d) A double can be assigned to an int (int x = 9.0;) e) The following Java identifiers are considered the same. temperature TEMPERATURE

2. Short answers: a) Write a statement that defines a String variable and initializes it with “Hello” b) Write a statement that compares two String variables str1 and str2 (x  y) c) Write a statement that evaluates and assigns the result to a variable result (a  b) / c

d) Write four program statements that increments the value of an integer variable total.

e) Write a Java condition which is true only when an int variable x lies between the values 1 and 31 (inclusive), and false otherwise.

5/8 3. Given the following variable definitions, what value and type do the following expressions evaluate to? If you show your work process clearly, partials maybe assigned.

int m = 1, n = 5; float x = 2.0, y = 3.5; double z = 10.0;

a z * (m - n) Value: ______Type: ______)

b 10 % n + x Value: ______Type: ______)

c (m + 2) / 2 Value: ______Type: ______)

d (x + z) / ((int) y) Value: ______Type: ______)

e !((x!=y) && (m>n)) Value: ______Type: ______)

4. Consider the following statements: switch (grade) { case ‘A’ : points = 4.0; break; case ‘B’ : points = 3.0; break; case ‘C’: points = 2.0; break; case ‘D’ : points = 1.0; break; default : points = 0.0; }

6/8 (a) What do the break statements do?

(b) If we remove the first and second break statements, how does that affect the value of points if grade is ‘A’ or ‘B’?

(c) Translate the above statements to an equivalent if statement.

5. What is the output of the following code fragments?

(a) System.out.println(“50 plus 25 is” + 50 + 25);

(b)

int n = 10; for (int i = 0; i < n; i++) { if (i % 2 == 0) System.out.println(i); }

(c) for (int num =0; num <= 200; num += 2) System.out.println(num);

(d) int limit = 100, num = 15, num2 = 40; if (limit <= limit) { if (num1 == num2) System.out.println(“lemon”); System.out.println(“lime”); } System.out.println(“grape”);

6. Writing programs

(a) Write a program that asks the user for the Celsius temperature, and then converts it to Fahrenheit using the formula F = (9/5) C + 32. The program outputs the Fahrenheit value.

(b) Write a program that prints the digits of a 4-digit integer n in reverse order.

(c) Write a program that reads values representing a time duration in hours, minutes and seconds, and then prints the equivalent total number of seconds.

7/8 (d) Write a program that reads a value representing a number of seconds, then print the equivalent amount of time as a combination of hours, minutes and seconds.

(e) Write a program that reads three integers that represent the lengths of the sides of a triangle. The method prints “Isosceles” if the triangle is isosceles (two sides are equal) but not equilateral.

(f) Write a program that takes two integers, and prints “divisible” if the first integer is evenly divisible by the second.

(g) You are given two variables d and e, each of type int, and the third variable b of type boolean. Give a code fragment that sets b to true if d and e are both even or both odd, and sets b to false otherwise.

8/8