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 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.
For both portions of the exam, you may bring in two 8 1/2 x 11 inch sheets of paper containing handwritten notes, and you may write on both sides of the paper. If you so choose, you can bring in different sheets of paper for each portion of the exam (they are on separate days, and you can keep it). It is recommended to write down anything you feel like you need to memorize. Historically, some students will also write down selected solutions to prior labs.
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.
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
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.
blah
with the parameters 5
and 6
.
blah(5, 6)
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.
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
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).
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).
add3
which takes three int
s as parameters and returns their sum.
public static int add3(int x, int y, int z) { return x + y + z; }
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
int[] b = new int[]{9, 8, 7}; for (int x = b.length - 1; x >= 0; x--) { System.out.println(b[x]); }
7 8 9
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
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
int x = 0; while (x < 5) { boolean b = false; if (x == 2) { b = true; } System.out.println(b); x++; }
false false true false false
int x = 0; while (x < 5) { System.out.println("hi"); x = 100; System.out.println("bye"); }
hi bye
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; }
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; }
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.
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
5 + 3
5l + 3
"42"
"42" + 1
5 + 5.2
5.2 + 5l
"hi" + true
1 + true
5 + 3
: int
5l + 3
: long
"42"
: String
"42" + 1
: String
5 + 5.2
: double
5.2 + 5l
: double
"hi" + true
: String
1 + true
: Error5 - 2
5 * 2
5 / 2
5 % 2
6 - 2
6 * 2
6 / 2
6 % 2
5.0 - 2
5.0 * 2
5.0 / 2
6.0 - 2
6.0 * 2
6.0 / 2
5 - 2.0
5 * 2.0
5 / 2.0
6 - 2.0
6 * 2.0
6 / 2.0
5 - 2
: 3
5 * 2
: 10
5 / 2
: 2
5 % 2
: 1
6 - 2
: 4
6 * 2
: 12
6 / 2
: 3
6 % 2
: 0
5.0 - 2
: 3.0
5.0 * 2
: 10.0
5.0 / 2
: 2.5
6.0 - 2
: 4.0
6.0 * 2
: 12.0
6.0 / 2
: 3.0
5 - 2.0
: 3.0
5 * 2.0
: 10.0
5 / 2.0
: 2.5
6 - 2.0
: 4.0
6 * 2.0
: 12.0
6 / 2.0
: 3.0
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.
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.
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.
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.
public static int p5(int x) { return Integer.parseInt(x); }
Integer.parseInt takes a String, but it's being passed an int
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]; } }
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++; }
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); }
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);
for (int x = 0; x < 5; x++) { ... }
for (int x = 0; x < 6; x += 2) { ... }
for (int x = 5; x >= 0; x--) { ... }
for (int x = 3; x > 10; x--) { ... }
for (int x = 3; x >= 0; x) { ... }
for (int x = 0; x < 5; x++) { ... }
: 5for (int x = 0; x < 6; x += 2) { ... }
: 3for (int x = 5; x >= 0; x--) { ... }
: 6for (int x = 3; x > 10; x--) { ... }
: 0for (int x = 3; x >= 0; x) { ... }
: infiniteisMultipleOfThree
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; }
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); }
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.
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.
public class Class1 { private int x; public Class1(int y) { y = x; } }
Intention seems to bex = y
; this does not set the instance variablex
.
public class Baz { private int x; public Baz(int y) { x = y; } public static int getX() { return x; } }
getX()
is declaredstatic
, so it does not have access to the non-static instance variablex
.
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 declaredstatic
for instance, and the namegetX()
implies that this is a getter for an instance variable.
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(); } }
ThedoSomething
method is marked asprivate
inFoo
, so it cannot be accessed inBar
, asBar
is not the same class asFoo
.
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
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