Midterm Review Questions (with Answers)

The midterm exam will be broken into two components:

The lab-based portion will require you to write code on the lab machines, which is to be turned in via Canvas by the end of the class. It will be similar in style to the rest of the assignments in the course. You may NOT reference your previous submissions, and you may NOT access the Internet for any reason other than to access the course materials and submit your solution via Canvas. Violations of these rules will result in a 0 on the exam.

The written portion will require you to read and understand code, as well as answer short-answer questions related to programming. You may also be asked to write short amounts of code.

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.

The exam will focus on topics that have been covered since the previous exam. However, since everything with programming builds on itself, you should also be familiar with the topics covered on the prior exam.

Questions

  1. What is the output of the following code?
    int x = 7;
    if (x > 8) {
      System.out.println("Hello");
    } else if (x < 10) {
      System.out.println("Something: " + x);
    } else {
      System.out.println("Goodbye");               
    }
    
    Something: 7
    
  2. What is wrong with the following code, if anything?
    public static void foo(int x) {
      if (x == 8) {
        System.out.println(x);
      } else {
        System.out.println(42);
      }
      return 12;
    }
    
    Cannot return a value from a method with a void return type.
    
  3. Write a call to a method named blah with the parameters 5 and 6.
    blah(5, 6)
    
  4. What is wrong with the following code, if anything?
    public static int doSomething(int input) {
      switch (input) {
      case 1:
        return 2;
      case 2:
        return 3;
      }
    }
    
    Possible for this method not to return anything, even though it's supposed to return an int.
    For example, if the input is 4, no case will match.
    
  5. What does the following method return underneath the input 1?
    public static int compute(int input) {
      int retval = 0;
      switch (input + 1) {
      case 0:
        retval++;
      case 1:
        retval += 2;
      case 2:
        retval += 3;
      case 3:
        retval += 6;
        break;
      case 4:
        retval += 12;
      }
      return retval;
    }
    
    9
    
  6. What is wrong with the following code, if anything?
    public static void baz(int input) {
      if (input == 27) {
        return;
      } else {
        System.out.println(input);
      }
    }
    
    No problems (return without a value can be used for void methods).
    
  7. What is wrong with the following code, if anything?
    public static int[] qwerty(int[] input) {
      return input[0];
    }
    
    qwerty is supposed to return an array of integers (int[]), but instead it returns an int (input[0] gets an int).
    
  8. Define a method named add3 which takes three ints as parameters and returns their sum.
    public static int add3(int x, int y, int z) {
      return x + y + z;
    }
    
  9. What is the output of the following code?
    int[] a = new int[]{5, 4, 3, 2};
    for (int x = 0; x < a.length - 1; x++) {
      System.out.println(a[x] + 1);
    }
    
    6
    5
    4
    
  10. What is the output of the following code?
    int[] b = new int[]{9, 8, 7};
    for (int x = b.length - 1; x >= 0; x--) {
      System.out.println(b[x]);
    }
    
    7
    8
    9
    
  11. What is wrong with the following code, if anything?
    int[] c = new int[0];
    for (int x = 0; x <= c.length; x++) {
      System.out.println(c[x]);
    }
    
    Goes out of bounds.
    The condition should be x < c
    
  12. What is the output of the following code?
    int x = 3;
    int y = 6;
    int z = 10;
    if (x > 2) {
      if (y < 8) {
        if (z > 5) {
          System.out.println(1);
        }
      } else {
        System.out.println(2);
      }
    } else {  
      System.out.println(3);
    }
    
    1
    
  13. What is the output of the following code?
    int x = 0;          
    while (x < 5) {
      boolean b = false;
      if (x == 2) {
        b = true;
      }
      System.out.println(b);
      x++;
    }
    
    false
    false
    true
    false
    false
    
  14. What is the output of the following code?
    int x = 0;          
    while (x < 5) {
      System.out.println("hi");
      x = 100;
      System.out.println("bye");
    }
    
    hi
    bye
    
  15. Write a method named randomOneThroughTen that returns a random number between 1 and 10, inclusive (i.e., both 1 and 10 are themselves possible numbers). Do not use a seed value.
    public static int randomOneThroughTen() {
      Random r = new Random();
      return r.nextInt(10) + 1;
    }
    
  16. Write a method named otherRandomOneThroughTen that returns a random number between 1 and 10, inclusive (i.e., both 1 and 10 are themselves possible numbers). otherRandomOneThroughTen takes a seed value as a parameter, and this seed value should be used as the initial value for random number generation.
    public static int otherRandomOneThroughTen(long seed) {
      Random r = new Random(seed);
      return r.nextInt(10) + 1;
    }
    
  17. What is wrong with the following code, if anything?
    public static void something(int x) {
      int x = 27;
      System.out.println("42");
    }
    
    x is already declared as a parameter; both declarations of x are in the same scope.
    
  18. What does the following code print, assuming it is run in jGrasp with jGrasp's “Run“ command?
    public static void useX1(int x) {
      x = 5;
      System.out.println(x);
    }
    public static void useX2(int x) {
      useX1(x);
      System.out.println(x + 1);
      x = 8;
    }
    public static void main(String[] args) {
      int x = 42;
      useX2(x);
      System.out.println(x);
    }
    
    5
    43
    42
    
  19. List the types of the following expressions. If the expression has no type (it is ill-typed, so it'd produce a compile-time error), say so.
  20. List the values produced by the following expressions.
  21. What is wrong with the following code, if anything?
    public static int p1(int x, int y) {
      return x + y;
    }
    public static void main(String[] args) {
      p1(5);
    }
    
    p1 takes two int arguments, but only one is provided in the call in main.
    
  22. What is wrong with the following code, if anything?
    public static int p2(int x) {
      return x;
    }
    public static void main(String[] args) {
      p2(5, 6);
    }
    
    p2 takes one int argument, but two are provided in the call in main.
    
  23. What is wrong with the following code, if anything?
    public static String p3(String x) {
      return x;
    }
    public static void main(String[] args) {
      p3(5);
    }
    
    p3 takes a String argument, but an int argument is passed in main.
    
  24. What is wrong with the following code, if anything?
    public static String p4(int x) {
      return x;
    }
    
    p4 tries to return an int, but it's supposed to return a String based on its declaration.
    
  25. What is wrong with the following code, if anything?
    public static int p5(int x) {
      return Integer.parseInt(x);
    }
    
    Integer.parseInt takes a String, but it's being passed an int
    
  26. Write a method named firstPlusLast that will take an array of integers and return the sum of the first and last integers. For example, if the first integer in the array is 3 and the last integer is 5, then it should return 8. If the array only contains one element (e.g., 7), then the first and last integer in that array is the same (so for 7, it should return 14). If the array is empty, it should return 0.
    public static int firstPlusLast(int[] a) {
      if (a.length == 0) {
        return 0;
      } else {
        return a[0] + a[a.length - 1];
      }
    }
    
  27. Convert the following for loop into a while loop.
    for (int x = 0; x < 10; x++) {
      System.out.println(x);
    }
    
    int x = 0;
    while (x < 10) {
      System.out.println(x);
      x++;
    }
    
  28. Convert the following while loop into a for loop.
    int x = 10;
    while (x > 7) {
      System.out.println(x);
      x--;
    }
    
    for(int x = 10; x > 7; x--) {
      System.out.println(x);
    }
    
  29. Convert the following for loop into a do...while loop.
    for (int x = 0; x != 3; x++) {
      System.out.println(x);  
    }
    
    int x = 0;          
    do {
      System.out.println(x);
      x++;
    } while (x != 3);
    
  30. For each loop below, state the number of iterations each loop will perform. If the loop is an infinite loop, state “infinite”.
  31. Write a method named isMultipleOfThree that will return true if a given int value is a multiple of 3, else false.
    public static boolean isMultipleOfThree(int input) {
      return input % 3 == 0;
    }
    
  32. Consider the following code and test suite:
    public static int doSomethingStrange(int input) {          
      if (input == 7) {
        return input;
      } else if (input == 8 || input < 5) {
        return input + 1;
      } else if (input > 25 && input < 100) {
        return input + 2;
      } else {
        return input + 3;
      }
    }
    @Test
    public void test1() {
      assertEquals(doSomethingStrange(7), 7);
    }
    @Test
    public void test2() {
      assertEquals(doSomethingStrange(-1), 0);
    }
    @Test
    public void test3() {
      assertEquals(doSomethingStrange(50), 52);
    }
    
    The test suite above misses certain behaviors in the code. Add the minimum number of tests needed to cover all behaviors, with one assertEquals call per test.
    @Test
    public void test4() {
      assertEquals(doSomethingStrange(8), 9);
    }
    @Test
    public void test5() {
      assertEquals(doSomethingStrange(100), 103);
    }
    
  33. What is wrong with the following code, if anything?
    public static int testMe(int x) {
      return x + 5;
    }
    @Test
    public void test1() {
      assertEquals(testMe(7), 12);
    }
    public void test2() {
      assertEquals(testMe(0), 5);
    }
    
    Second test is missing @Test annotation.
    
  34. What is wrong with the following code, if anything?
    public class Foo {
      private int x;
      public Foo(int y) {
        int x = y;
      }
      public int getX() {
        return x;
      }
    }
    
    The code compiles and runs, but probably doesn't work as desired.
    The line “int x = y;” in the constructor declares a new local variable named “x”, instead of assigning 
    to the instance variable “x”.
    As such, the instance variable “x” is never assigned, so it will implicitly get initialized to 0 by Java.
    
  35. What is wrong with the following code, if anything?
    public class Class1 {
      private int x;
      public Class1(int y) {
        y = x;
      }
    }
    
    Intention seems to be x = y; this does not set the instance variable x.
    
  36. What is wrong with the following code, if anything?
    public class Baz {
      private int x;
      public Baz(int y) {
        x = y;
      }
      public static int getX() {
        return x;
      }
    }
    
    getX() is declared static, so it does not have access to the non-static instance variable x.
    
  37. What is wrong with the following code, if anything?
    public class Blah {
      public static int x;
      public Blah(int y) {
        x = y;
      }
      public int getX() {
        return x;
      }
    }
    
    Technically nothing; the code will compile since instance methods have access to static items.
    However, it's strange; getX() above could have been declared static for instance, and the name getX() 
    implies that this is a getter for an instance variable.
    
  38. What is wrong with the following code, if anything (assume classes Foo and Bar are defined in Foo.java and Bar.java, respectively)?
    public class Foo {
      public Foo() {}
      private int doSomething() {
        return 0;
      }
    }
    public class Bar {
      public static void main(String[] args) {
        Foo f = new Foo();
        f.doSomething();
      }
    }
    
    The doSomething method is marked as private in Foo, so it cannot be accessed in Bar, as Bar 
    is not the same class as Foo.
    
  39. What is the output of the main method of Class4 below?
    public class Class2 {
      public Class2() {}  
      public void m() {
        System.out.println("foo");
      }
    }
    
    public class Class3 {
      public Class3() {}  
      public void m() {
        System.out.println("bar");
      }
    }
    
    public class Class4 {
      public static void main(String[] args) {
        Class2 x = new Class2();
        Class3 y = new Class3();
    
        x.m();
        y.m();
      }
    }
    
    foo
    bar
    
  40. What is the output of the main method of Class5 below?
    public class Class5 {
      private int x;
      public Class5(int y) {
        x = y;
      }
      public void foo(int x) {
        System.out.println(x);
      }
      public void bar(int y) {
        System.out.println(x);
      }
      public static void main(String[] args) {
        Class5 obj = new Class5(7);
        obj.foo(1);
        obj.bar(2);
      }
    }
    
    1
    7