java quizes and lab

profileromadallas
labassignments.docx

1. Describe what happens if, in HelloWorld.java, you omit

a. main

a. String

a. HelloWorld

a. System.out

a. println

1. Describe what happens if, in HelloWorld.java, you omit

b. the ;

b. the first "

b. the second "

b. the first {

b. the second {

b. the first }

b. the second }

1. Describe what happens if, in HelloWorld.java, you misspell (by, say, omitting the second letter)

c. main

c. String

c. HelloWorld

c. System.out

c. println

1. I typed in the following program. It compiles fine, but when I execute it, I get the error java.lang.NoSuchMethodError: main. What am I doing wrong?

public class Hello {

public static void main() {

System.out.println("Doesn't execute");

}

}

1. Modify Excercise 2.2 so that it rolls 3 dice instead of 2.

http://math.hws.edu/javanotes8/c2/ex2-ans.html (link for exercise 2.2 code)

6. Suppose that a and b are int values. What does the following sequence of statements do?

int t = a;

b = t;

a = b;

7. Suppose that a and b are int values. Simplify the following expression: (!(a < b) && !(a > b))

8.The exclusive or operator ^ for boolean operands is defined to be true if they are different, false if they are the same. Give a truth table for this function.

9. Why does 10/3 give 3 and not 3.33333333?

10. What do each of the following print?

a. System.out.println(2 + "bc"); prints: 2bc

   b. System.out.println(2 + 3 + "bc"); prints: 5bc

   c. System.out.println((2+3) + "bc"); prints: 5bc

   d. System.out.println("bc" + (2+3)); prints: bc5

   e. System.out.println("bc" + 2 + 3); prints: bc23

Explain each outcome.

Bonus: A physics student gets unexpected results when using the code

double force = G * mass1 * mass2 / r * r;

to compute values according to the formula F = Gm1m2 / r2. Explain the problem and correct the code.