Skip to content

Back to Basics (Weeks 1–7)

You should be able to answer these questions correctly every time, without hesitation.

  1. Write statements to declare the following variables:
    • light: integer
    • primary: java.awt.Color
    • scores: array of integers
    • names: array of strings
  2. Write statements that assign a value to each variable from #1.
  3. Rewrite the code from #1 and #2 using one line for each variable.
  4. Rewrite the code to initialize scores and names with specific values of your choice.
  5. Write statements to do the following:
    • Declare and initialize a random double between 0 (inclusive) and 100 (exclusive).
    • Convert the random double to an integer and assign the resulting value to light.
  6. Write the following loops:
    • Print the word “Hello” 5 times.
    • Print each character in a string variable named message.
    • Double each value in an integer array named ratings.
    • Print each object in a List<String> named groceries.
  7. Write the following statements:
    • Throw an IllegalStateException with an error message of your choice.
    • Call a method named surprise() and catch an IllegalStateException.
  8. Write all the code for a class named Marker:
    • Attributes are label (char) and position (java.awt.Point).
    • Note: java.awt.Point contains public attributes x and y.
    • default constructor, explicit value constructor, and (deep) copy constructor
    • getters and setters, override equals(), override toString() (with both attributes)
    • static method that creates and returns a List of 3 Marker objects of your choice.