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"); }
public static void foo(int x) { if (x == 8) { System.out.println(x); } else { System.out.println(42); } return 12; }
blah
with the parameters 5
and 6
.
public static int doSomething(int input) { switch (input) { case 1: return 2; case 2: return 3; } }
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; }
public static void baz(int input) { if (input == 27) { return; } else { System.out.println(input); } }
public static int[] qwerty(int[] input) { return input[0]; }
add3
which takes three int
s as parameters and returns their sum.
int[] a = new int[]{5, 4, 3, 2}; for (int x = 0; x < a.length - 1; x++) { System.out.println(a[x] + 1); }
int[] b = new int[]{9, 8, 7}; for (int x = b.length - 1; x >= 0; x--) { System.out.println(b[x]); }
int[] c = new int[0]; for (int x = 0; x <= c.length; x++) { System.out.println(c[x]); }
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); }
int x = 0; while (x < 5) { boolean b = false; if (x == 2) { b = true; } System.out.println(b); x++; }
int x = 0; while (x < 5) { System.out.println("hi"); x = 100; System.out.println("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.
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 void something(int x) { int x = 27; System.out.println("42"); }
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 + 3
5l + 3
"42"
"42" + 1
5 + 5.2
5.2 + 5l
"hi" + true
1 + true
5 - 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
public static int p1(int x, int y) { return x + y; } public static void main(String[] args) { p1(5); }
public static int p2(int x) { return x; } public static void main(String[] args) { p2(5, 6); }
public static String p3(String x) { return x; } public static void main(String[] args) { p3(5); }
public static String p4(int x) { return x; }
public static int p5(int x) { return Integer.parseInt(x); }
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
.
for
loop into a while
loop.
for (int x = 0; x < 10; x++) { System.out.println(x); }
while
loop into a for
loop.
int x = 10; while (x > 7) { System.out.println(x); x--; }
for
loop into a do...while
loop.
for (int x = 0; x != 3; x++) { System.out.println(x); }
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) { ... }
isMultipleOfThree
that will return true
if a given int
value is a multiple of 3
, else false
.
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.
public static int testMe(int x) { return x + 5; } @Test public void test1() { assertEquals(testMe(7), 12); } public void test2() { assertEquals(testMe(0), 5); }
public class Foo { private int x; public Foo(int y) { int x = y; } public int getX() { return x; } }
public class Class1 { private int x; public Class1(int y) { y = x; } }
public class Baz { private int x; public Baz(int y) { x = y; } public static int getX() { return x; } }
public class Blah { public static int x; public Blah(int y) { x = y; } public int getX() { return x; } }
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(); } }
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(); } }
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); } }