The final 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 a different sheet of paper for each portion of the exam (they are on separate days, and you can keep it).
The exam is cumulative, but it is biased towards material covered since the last exam (labs 13-24). The exam will be composed of questions based off of the following sources:
All topics which could potentially be on the exam are somehow covered by one of the above sources.
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 Class2 { private int x; public Class2(int x) { this.x = x; } } public class Class3 extends Class2 {}
Class3
does not define a constructor that takes anint
.
public class Class4 {} public class Class5 extends Class4 { private int x; public Class5(int x) { super(); this.x = x; } }
Nothing wrong; Class4
implicitly has a no-argument constructor defined.
It is ok if subclasses add new constructors.
public class Class6 { private int x; public Class6(int x) { this.x = x; } } public class Class7 extends Class6 { public Class7(int x) { super(x); } public int getX() { return x; } }
x
isprivate
inClass6
, so it cannot be accessed inClass7
.
public class Class8 { protected int x; public Class8(int x) { this.x = x; } } public class Class9 extends Class8 { public Class9(int x) { super(x); } public int getX() { return x; } }
No problems;Class9
now has access tox
, sincex
isprotected
.
public abstract class Class10 { public abstract void m(); } public class Class11 extends Class10 {}
Class11
does not overridem
.
public abstract class Class12 { public abstract void m(); } public abstract class Class13 extends Class12 {}
No problem; sinceClass13
isabstract
it does not need to implement allabstract
methods.
public interface Interface1 { public void m(); } public abstract class Class14 implements Interface1 {}
No problem; sinceClass14
is declaredabstract
, it does not need to overridem
.
public interface Interface2 { public void m(); } public class Class15 implements Interface2 {}
Class15
is notabstract
and does not overridem
.
public class Class16 {} public class Class17 extends Class16 { public static void m() { Class16 x = new Class17(); } }
No problems.Class17
is-aClass16
, so an instance ofClass17
can be assigned to a variable of typeClass16
.
public class Class18 { private int x; public Class18(int x) { this.x = x; } public static int getX() { return x; } }
getX()
is declaredstatic
, so it does not have access to the non-static instance variablex
.
public class Class19 { public static int x; public Class19(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.
main
method of Class22
below?
public class Class20 { public void m() { System.out.println("foo"); } } public class Class21 extends Class20 { public void m() { System.out.println("bar"); } } public class Class22 { public static void main(String[] args) { Class20 x = new Class20(); Class20 y = new Class21(); Class21 z = new Class21(); x.m(); y.m(); z.m(); } }
foo bar bar
main
method of Class24
below?
public class Class23 extends Exception {} public class Class24 { public static void throwException() throws Class23 { throw new Class23(); } public static void main(String[] args) { System.out.println(1); try { System.out.println(2); throwException(); System.out.println(3); } catch (Class23 e) { System.out.println(4); } System.out.println(5); } }
1 2 4 5
main
method of Class26
below?
public class Class25 extends Exception {} public class Class26 { public static void throwException() throws Class25 { throw new Class25(); } public static void main(String[] args) { System.out.println(1); try { System.out.println(2); throwException(); System.out.println(3); } catch (Class25 e) { System.out.println(4); } finally { System.out.println(5); } System.out.println(6); } }
1 2 4 5 6
public class Class27 {} public class Class28 { public static void m() throws Class27 { throw new Class27(); } }
Class27
does not inherit fromException
.
public class Class29 extends Exception {} public class Class30 { public static void m() { throw new Class29(); } }
Methodm
ofClass30
is not annotated withthrows Class29
, but it does throwClass29
.
public class Class31 extends Exception {} public class Class32 { public static int m() throws Class31 { return 42; } }
Methodm
ofClass32
is annotated to throwClass31
, but it will never throwClass31
.
public class Class33 extends Exception {} public class Class34 { public static void throwsException() throws Class33 { throw new Class33(); } public static int m() throws Class33 { try { throwsException(); } catch (Class33 e) { return 1; } return 0; } }
While aClass33
exception is thrown within them
method ofClass34
, this exception will always be caught, som
will never throw it. Thethrows Class33
annotation onm
needs to be removed.
public class Class35 extends Exception {} public class Class36 { public static void throwsException() throws Class35 { throw new Class35(); } public static int m() throws Class35 { try { throwsException(); } catch (Class35 e) { return 1; } } }
This will not compile, as Java is not smart enough to know thatthrowsException
will always throw an exception. IfthrowsException
happened to not throw an exception, methodm
would not return anything. However,m
must return anint
.