Week 1

1) What best defines a "programming language"?

  1. It allows us to control a computer.
  2. It allows us to make a calculation.
  3. It allows to execute a program.
  4. It allows us to express an algorithm.

2) Select the valid identifiers from the following list. State why each of the others is not valid.

  1. 123
  2. $50
  3. 50dollars
  4. abs
  5. hunter
  6. @60cents
  7. a_b
  8. #define

3) We learned about basic programs such as Hello World!
How can we modify this to print a whole message?

Let's print two lines:

4) Write a Java program that creates TWO integer variables to the values of your choice (e.g. 4 and 5, 12 and 10, etc.) and computes and prints the following:

5) Answer the following:

  1. What is the result of the following Java expression: 5 + 6 * 3 ?
  2. How does the order of operations affect the evaluation of the following expression: 4 + 5 * 2 - 3 ?
  3. Evaluate the following Java expression: 10 / 2 + 3 * 5 - 4
  4. What is the output of the following code snippet?
    int a = 2 + 3 * 4 - 1;
    System.out.println("Result: " + a);
  5. Rewrite the following expression using parentheses to explicitly define the order of operations: 8 - 2 * 4 / 2 + 3
  6. What is the value of the variable 'b' after executing the following code snippet?
    int b = 10;
    b += 2 * 3 - 1;

6) Trace the following program:

public static void main(String [] args) {
    int a, b, c, d, e, f, g;
    a = 10;
    b = 27;
    System.out.println("a is " + a + ", b is " + b);

    c = a + b;
    System.out.println("a+b is " + c);
    d = a - b;
    System.out.println("a-b is " + d);
    e = a+b*3;
    System.out.println("a+b*3 is " + e);
    f = b / 2;
    System.out.println("b/2 is " + f);
    g = b % 10;
    System.out.println("b%10 is " + g);
}

7) Write a Java program to print the area and perimeter of a rectangle.

8) Write a Java program to convert temperature from Fahrenheit to Celsius degrees.

Temperature in degrees Celsius (°C) = (Temperature in degrees Fahrenheit (°F) - 32) * 5/9

9) What will be printed by the following program?

public static void main(String [] args) {
      int w, x;
      double y;
  
      w = -9;
      x = Math.abs(w + 5);
      y = Math.sqrt(x);
  
      System.out.printf("%d %d %.2f%n", w,x,y);
}

10) Assuming that the variable x has already been declared and assigned a value, is the following assignment legal? If not why?

double sqrt = Math.sqrt(x);

11) Write a Java program that initializes the radius of a circle and computes and prints the circle's area and circumference (perimeter). (Limit each result to a precision of 3 decimal places)

12) Write a Java expression that would compute the following:

equation