Lab Exam 1 Review Questions (with Answers)

The exam will require you to write code with your laptop by using jGRASP. This code will be turned in via Canvas by the end of the class. It will be similar in style to the lab assignments. You may NOT reference your previous submissions, and you may NOT access the internet for any reason other than to access the lecture slides and submit your solution via Canvas. Violations of these rules will result in a 0 on the exam.

The exam will be open-note and you are allowed to access all the class slides.

The review below, in addition to everything you wrote for your labs, is intended to be comprehensive. All topics which could potentially be on the exam are somehow covered by this review or the labs. The one exception is JUnit tests: these will not be on the exam since you've only had one assignment on them.

Questions

  1. Write a complete program that will print Hello. The name of the class should be Hello. Be sure to include main.
    public class Hello {
      public static void main(String[] args) {
        System.out.println("Hello");
      }
    }
    
  2. Consider the following equation:
    (1 + 3)(2 x 7)
    

    Write an equivalent Java expression.

    Math.pow(1 + 3, 2 * 7)
    
  3. Consider the following equation:
    (7 - 2) x 28
    

    Write an equivalent Java expression.

    (7 - 2) * Math.pow(2, 8)
    
  4. Write a code snippet that will:
    1. Read in two values of type String from the user
    2. Concatenate the two values together
    3. Print out the concatenated value
    Scanner input = new Scanner(System.in);
    String first = input.nextLine();
    String second = input.nextLine();
    String concat = first + second;
    System.out.println(concat);
    
  5. Write a code snippet that will:
    1. Read in two values of type String from the user
    2. Print out the two values, one per line, using only a single System.out.print/System.out.println statement
    Scanner input = new Scanner(System.in);
    String first = input.nextLine();
    String second = input.nextLine();
    System.out.println(first + "\n" + second);
    
  6. Write a code snippet that calls a method named mystery, with the following actual parameters:
    1. A value of type int holding the value 5
    2. A value of type String holding the text foo
    3. A value of type long holding the value 23
    4. A value of type double holding the value 3.14
    mystery(5, "foo", 23l, 3.14)
    
  7. Write a method named promptAndReadInt which will:
    1. Prompt the user to enter an integer, using the text "Enter an integer:"
    2. Read in an integer from the user
    3. Return the integer read in
    public static int promptAndReadInt() {
      Scanner input = new Scanner(System.in);
      System.out.print("Enter an integer:");
      int readIn = input.nextInt();
      return readIn;
    }
    
  8. Write a method named promptAndReadIntWithPrompt which will:
    1. Prompt the user to enter an integer, using a prompt given as a String parameter
    2. Read in an integer from the user
    3. Return the integer read in
    public static int promptAndReadIntWithPrompt(String prompt) {
      Scanner input = new Scanner(System.in);
      System.out.print(prompt);
      int readIn = input.nextInt();
      return readIn;
    }
    
  9. Reimplement the promptAndReadInt method, this time by calling the promptAndReadIntWithPrompt method.
    public static int promptAndReadInt() {
      return promptAndReadIntWithPrompt("Enter an integer:");
    }
    
  10. Write a method named readAndAddTwo which will:
    1. Read in two integers from the user
    2. Add the two integers together
    3. Print out the result of the addition
    The method should not return anything.
    public static void readAndAddTwo() {
      Scanner input = new Scanner(System.in);
      int first = input.nextInt();
      int second = input.nextInt();
      System.out.println(first + second);
    }