- Date: 30/09/2023
- Score: 12/20
Question # | Correct |
---|---|
1 | ✅ |
2 | ✅ |
3 | ❌ |
4 | ❌ |
5 | ✅ |
6 | ✅ |
7 | ✅ |
8 | ❌ |
9 | ✅ |
10 | ✅ |
11 | ❌ |
12 | ✅ |
13 | ✅ |
14 | ✅ |
15 | ✅ |
16 | ❌ |
17 | ❌ |
18 | ❌ |
19 | ❌ |
20 | ✅ |
❓ Which of the following pairs fills in the blanks to make this code compile❓
5: public void read() _________ SQLException {
6: ________ new SQLException();
7: }
A. throw on line 5 and throw on line 6
B. throw on line 5 and throws on line 6
C. throws on line 5 and throw on line 6
D. throws on line 5 and throws on line 6
E. None of the above. SQLException is a checked exception and cannot be thrown
F. None of the above. SQLException is a runtime exception and cannot be thrown
❓
- C✅✅✅✅
❓ Which of the following changes when made independently would make this code compile (choose all that apply)
1 : public class StuckTurkeyCage implements AutoCloseable {
2 : public void close() throws Exception {
3 : throw new Exception("Cage door does not close");
4 : }
5 : public static void main(String[] args) {
6 : try (StuckTurkeyCage t = new StuckTurkeyCage()) {
7 : System.out.println("put turkeys in");
8 : }
9 : }
10: }
A. Remove throws Exception from the declaration on line 2
B. Add throws Exception to the declaration on line 5
C. Change line 8 to } catch (Exception e) {}
D. Change line 8 to } finally {}
E. None of the above will make the code compile
F. The code already compiles as is
❓
- The code does NOT compile as is!
- We need to catch the checled exception!
- B, C✅✅✅✅
- Option D is incorrect as the exception would still be unhandled
❓ Which of the following fills in the blank to make the code compile? (Choose all that apply)
public static void main(String[] args) {
try {
throw new IOException();
} catch (___________________) { }
}
A. FileNotFoundException | IOException e
B. FileNotFoundException e | IOException e
C. FileNotFoundException | RuntimeException e
D. FileNotFoundException e | RuntimeException e
E. IOException | RuntimeException e
F. IOException e | RuntimeException e
❓
- A - false, FileNotFoundException is a IOException
- B - false, invalid syntax
- C - true
- D - false, invalid syntax
- E - true
- F - false, invalid syntax
- C,E❌❌❌❌
- CORRECT ANSWER: E
- Option C is incorrect as FileNotFoundException is not thrown so can't be caught!
❓Which of the following are true statements (choose all that apply)
A. A traditional try
statement without a catch block requires a finally block
B. A traditional try
statement without a finally block requires a catch block
C. A traditional try
statement with only one state with only one statement can omit {}
D. A try-with-resources statement without a catch block requires a finally block
E. A try-with-resources statement without a finally block requires a catch block
F. A try-with-resources statement with only one statement can omit the {}
- A - true
- B - true
- C - I don't THINK so
- D - true
- E - false
- F - I don't THINK so
- A,B,D❌❌❌❌
- CORRECT ANSWER: A,B
- Try-with-resources does not need a catch/finally block!
❓ What is the output of the following code?
import java.io.*;
public class AutocloseableFlow {
static class Door implements AutoCloseable {
public void close() {
System.out.println("D");
}
}
static class Window implements Closeable {
public void close() {
System.out.println("W");
throw new RuntimeException();
}
}
public static void main(String[] args) {
try (Door d = new Door(); Window w = new Window()) {
System.out.print("T");
} catch (Exception e) {
System.out.print("E");
} finally {
System.out.print("F");
} } }
A. TWF
B. TWDF
C. TWDEF
D. TWF
followed by an exception
E. TWDF
followed by an exception
F. TWEF
followed by an exception
G. The code does not compile
❓
- The code DOES compile
- TWDEF is printed
- C✅✅✅✅
- After both resources are opened, T is printed
- The resources are closed, so WD is printed
- Then the catch/finally are run, so EF is printed!
❓ What is the output of the following code?
import java.io.*;
public class AutocloseableFlow {
static class Door implements AutoCloseable {
public void close() {
System.out.println("D");
}
}
static class Window implements Closeable {
public void close() {
System.out.println("W");
throw new RuntimeException();
}
}
public static void main(String[] args) {
try {
Door d = new Door(); Window w = new Window()
}
{
System.out.print("T");
} catch (Exception e) {
System.out.print("E");
} finally {
System.out.print("F");
} } }
A. TWF
B. TWDF
C. TWDEF
D. TWF
followed by an exception
E. TWDF
followed by an exception
F. TWEF
followed by an exception
G. The code does not compile
❓
- Code does not compile due to missing semi-colon:
- G✅✅✅✅
❓ What is the result of running java EchoInput hi there
with the following code?
public class EchoInput {
public static void main(String [] args) {
id(args.length <= 3) assert false;
System.out.println(args[0] + args[1] + args[2]);
}
}
A. hithere
B. The assert
statement throws an AssertionError
C. The code throws an ArrayIndexOutOfBoundsException
D. The code compiles and runs successfully, but there is not output
E. The code does not compile
- The command does not enable the assertions, so no assertion exception is thrown
- Therefore it prints nothing an ArrayIndexOutOfBoundsException is thrown
- C✅✅✅✅
❓Which of the following command lines cause this program to fail on the assertion (Choose all that apply)
public class On {
public static void main(String[] args) {
String s = null;
assert s != null;
}
}
A. java -da On
B. java -ea On
C. java -da -ea:On On
D. java -ea -da:On on
E. The code does not compile
- The code DOES compile
- B❌❌❌❌
- CORRECT ANSWER: B,C
- The -da means -disableAssertions. The colon lets you specify a specific class to enable/disable assertions
- C disables all assertions but then enables it for the
On
class!
❓Which of the following prints OhNo
with the assertion failure when the number is negative? (Choose all that apply)
A. assert n < 0: "OhNo2";
B. assert n < 0; "OhNo";
C. assert n < 0 ("OhNo");
D. assert(n < 0): "OhNo";
E. assert(n < 0, "OhNo");
- A - valid
- D - valid
- A, D✅✅✅✅
❓ Which of the following are true of the code? (Choose all that apply)
4: private int addPlusOne(int a, int b) {
5: boolean assert = false;
6: assert a++ > 0;
7: assert b > 0;
8: return a + b;
9: }
A. Line 5 does not compile
B. Line 6 and 7 do not compile because they are missing the String message
C. Line 6 and 7 do not compile because of missing parentheses
D. Line 6 is an appropiate use of an assertion
E. Line 7 is an appropiate use of an assertion
- A - true, assert is a keyword
- B - false
- C - false
- D - false
- E - true
- A,E✅✅✅✅
❓Which of the following are runtime exceptions (choose all that apply)
A. Exception
B. IllegalStateException
C. IOException
D. MissingResourceException
E. DateTimeParseException
F. SQLException
- A - false
- B - true
- C - false
- D - false
- E - true
- F - false
- B,E❌❌❌❌
- CORRECT ANSWER: B,D,E
- MissingResourceException IS a runtime exception!
❓ Which of the following can legally fill in the blank? (choose all that apply)
public class AhChoo {
static class SneezeException extends Exception { }
static class SniffleException extends SneezeException { }
public static void main(String[] args) throws SneezeException {
try {
throw new SneezeException();
} catch (SneezeException e) {
__________
throw e;
} } }
A. // leave line blank
B. e = new Exception();
C. e = new RuntimeException();
D. e = new SneezeException();
E. e = new SniffleException();
F. None of the above; the code does not compile.
- The code does compile
- A - valid
- B - invalid, e is of type SneezeException
- C - invalid
- D - valid
- E - valid
- A,D,E✅✅✅
- Since SneezeException is caught, only exceptions of same type or subtypes can be assigned to the variable
❓Which of the following can legally fill in the blank? (Choose all that apply)
public class AhChoo {
static class SneezeException extends Exception { }
static class SniffleException extends SneezeException { }
public static void main(String[] args) throws SneezeException {
try {
throw new SneezeException();
} catch (SneezeException | RuntimeException e) {
____________
throw e;
}
A. // leave line blank
B. e = new Exception();
C. e = new RuntimeException();
D. e = new SneezeException();
E. e = new SniffleException();
F. None of the above; the code does not compile.
- A✅✅✅✅
- Since a multi-catch is used, the variable in catch block is effectively final and cannot be reassigned!
❓Which of the following can legally fill in the blank? (Choose all that apply)
public class AhChoo {
static class SneezeException extends Exception { }
static class SniffleException extends SneezeException { }
public static void main(String[] args) throws SneezeException {
try {
throw new SneezeException();
} catch (SneezeException | SniffleException e) {
____________
throw e;
}
A. // leave line blank
B. e = new Exception();
C. e = new RuntimeException();
D. e = new SneezeException();
E. e = new SniffleException();
F. None of the above; the code does not compile.
- F✅✅✅✅
- A multicatch can catch both a superclass and subclass!
❓ Which of the following are checked exceptions? (Choose all that apply)
class One extends RuntimeException{}
class Two extends Exception{}
class Three extends Error{}
class Four extends One{}
class Five extends Two{}
class Six extends Three{}
A. One
B. Two
C. Three
D. Four
E. Five
E. Six
- Checked exceptions extends
Exception
but notRuntimeException
- A - false
- B - true
- C - false, Error is not an Exception
- D - false as A is RuntimeException
- E - true
- F - false, as Three is not an Exception
- B,E✅✅✅✅
❓ What is the output of the following?
public class SnowStorm {
static class Walk implements AutoCloseable {
public void close() {
throw new RuntimeException("snow");
}
}
public static void main(String[] args) {
try (Walk walk1 = new Walk(); Walk walk2 = new Walk();) {
throw new RuntimeException("rain");
} catch(Exception e) {
System.out.println(e.getMessage()
+ " " + e.getSupressed().length);
} } }
A. rain 0
B. rain 1
C. rain 2
D. snow 0
E. snow 1
F. snow 2
F. The code does not compile
- There is an unecessary semi-colon in the try clause
- F ❌❌❌❌
- CORRECT ANSWER: C
- The exception in the try block becomes the primary exception. The two supressed exceptions are from trying to close the two resources!
❓ Fill in the blank: A class that implements ____________ may be in a try-with-resource statement? (Choose all that apply)
A. AutoCloseable
B. Closeable
C. Exception
D. RuntimeException
E. Serializable
- I think all of these can be in a try-with-resource statement!
- A❌❌❌❌
- CORRECT ANSWER: A,B
- Closeable was the original interface for IO classes. AutoCloseable was added in Java 7 along with try-with-resources. Closeable extends AutoCloseable
❓ Which pairs fill in the blanks? The close()
method is not allowed to throw a(n) _________ in a class that implements ___________. (Choose all that apply)
A. Exception, Autocloseable
B. Exception, Closeable
C. IllegalStateException, AutoCloseable
D. IllegalStateException, Closeable
E. IOException, AutoCloseable
F. IOException, Closeable
- A - false
- B - false
- C - true
- D - true
- E - false
- F - true
- C,D,F❌❌❌❌
- CORRECT ANSWER: B
- The Closeable interface throws IOException, hence it can not throw Exception in its implementations!
- IllegalStateException is a runtime exception and can be thrown by any method.
❓ Which of the following cannot fill in the blank? (Choose all that apply)
public void read() throws SQLException {
try {
readFromDatabase();
} catch (________________ e) {
throw e;
}
}
public void readFromDatabase() throws SQLException { }
A. Exception
B. RuntimeException
C. SQLException
D. SQLException | IOException
E. SQLException | RuntimeException
- I have no clue!
- B,E❌❌❌❌
- CORRECT ANSWER: D
- SQLException must be caught! A, C, D, E all catch it!
- Option B is not allowed, as it does not handle the exception!
❓ Which of the following is true when creating your own exception class?
A. One or more constructors must be coded.
B. Only checked exceptions may be created
C. Only unchecked exception may be created
D. The toString()
method must be coded
E. None of the above
- E✅✅✅✅