Exam 1 Review

Topics covered on the exam will include (but are not limited to) the following:

    • Java syntax
      • know where to place ; and how to use {}
    • Components of a Java class
      • data members
      • constructor (s)
      • methods
      • encapsulation - making data members private
    • Elements of a method
      • return type
      • parameter list
      • naming methods
      • method implementation
      • calling methods
      • pass by value and implications when passing references to objects
    • Variables
      • declaring variables
      • specifying variable type
      • primitive types versus reference types
      • NullPointerException
    • Statements and mathematical expressions
      • assignment versus comparison (= versus ==)
    • Object instantiation
      • use of the new operator
    • Conditionals (if statements)
      • use of >, <, =, !, &&, and ||
      • else-if and else
    • Iteration (for, while, do-while)
      • control variable
      • initialization, condition, and update
      • break and continue
    • ArrayList
    • Strings
      • compareTo method
      • split method
      • replace/replaceAll method

You are expected to have read all sections of the text list here.

Here are some sample exam questions:

    1. Explain the main parts of a method header.
    2. Write a method that takes as input an ArrayList of integers and returns the sum of all elements in the list.
    3. True or False: All classes should provide getters and setters for all data members.
    4. Explain the difference between == and .equals.
    5. Identify at least two errors in the following class definition.
    6. public class Score {
    7. private String initials;
    8. private int score;
    9. public Score(String initials, int score) {
    10. this.initials = initials;
    11. score = score;
    12. }
    13. public String getScore() {
    14. return score;
    15. }
    16. }
  1. Explain why the following line of code would result in a compiler error:
  2. double d = new String(“Hello”);
  3. Implement a while loop that print exactly the same output as the for loop below:
  4. for(int i = 0; i < 10; i++) {
  5. System.out.println(i);
  6. }
  7. Explain the error in the constructor defined below and specify how you would fix the problem:
  8. public class Test {
  9. private String s;
  10. public Test(String s) {
  11. s = s;
  12. }
  13. }