- Date: 28/12/2023
- Score: 13/20 (65%)
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
❓
- Line 5 needs
throws
- Line 6 needs
throw
- 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
❓
- C and D are false, the syntax is acceptable
- B - false, not necessary
- F❌❌❌❌❌
- CORRECT ANSWER: B,C
- Option D is also false as the exception would remain unhandled!
- The only way the code would compile is if we had either:
public static void main(String[] args) throws Exception {
try (StuckTurkeyCage t = new StuckTurkeyCage()) {
// anything
}
}
- OR we caught the exception in the try-with-resources block:
try (StuckTurkeyCage t = new StuckTurkeyCage()) {
System.out.println("put turkeys in");
} catch (Exception e) {}
❓ 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 - FileNotFoundException IS a IOExcpetion, so false will not compile
- B - false, incorrect syntax
- C - false, IOException still needs to be caught
- D - false, incorrect syntax
- E - true
- F - false, incorrect syntax
- E✅✅✅✅
❓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 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 - false, the curly braces are required
- D - false, it requires neither catch or finally
- E - false as above
- F - false, curly braces are required
- A,B✅✅✅✅✅
❓ 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 close methods are called in reverse order
- It first prints
TWD
first - It then prints
EF
- Not too confident about this one!!!
- C✅✅✅✅✅
- After the window is closed so w is printed, it throws an exception
- But the door still needs to be closed so D is printed
- The exceptiuon is then caught so E 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
❓
- The syntax is incorrect!
- 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
- While the args length is indeed less than or equal to 3, the assertions have not been enabled!
- The assert false is not triggered
- An ArrayIndexOutOfBoundsException is thrown
- The code does compile but an exception is thrown before anything can be printed
- 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
- A - false, this disables assertion
- B - true
- C - true
- D - false, assertions h
- B,C ✅✅✅✅
❓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 - true
- B - false, incorrect syntax
- C - false, incorrect syntax
- D - true
- E - false, incorrect syntax
- 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
- The code will not compile because assert is a keyword
- A❌❌❌❌❌
- CORRECT ANSWER: A,E
- I didn't read all the options properly
:(
❓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, RuntimeExceptions extend this class
- B - true
- C - false, this is a checked exception
- D - false, checked exception
- E - true
- F - false, checked exception
- 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 type of e can not change
- A - true
- B - false, you cannot write
SneezeException e = new Exception()
- C - false, SneezeException is not a runtime exception!
- D - true
- E - true
- need to double check this!
- A,D,E✅✅✅✅
❓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.
- The code DOES compile
- However you cannot reassign e to a new exception!!!
- A✅✅✅✅
❓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.
- The code will not compile as SniffleException IS a SneezeException
- F✅✅✅✅
❓ 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
- A - false
- B - true
- C - false
- D - false
- E - true
- F - false
- 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
- Im kinda guessing here
- F❌❌❌❌❌
- CORRECT ANSWER: C
- The exception which is caught is
rain
and the 2snow
exceptions are printed!!!
❓ 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
- A❌❌❌❌
- CORRECT ANSWER: A,B
- Closeable extends AutoClosable!!!
- So both Closeable and Autocloseable can be used with try-with-resources
❓ 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 class which implements AutoCloseable, the close() method is not allowed to throw a checked exception
- A - true
- B - false
- C - false, runtime exceptions can be thrown by any method
- D - false as above
- E - true
- F - false
- A,E❌❌❌❌
- CORRECT ANSWER: B
- The
Closeable
interface has aclose()
method which does not throw a checked exception, hence implementations can not throw exceptions neither!!!
❓ 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
- The checked exception must be caught!
- A - true
- B - false
- C - true
- D - true
- E - true
- A,C,D,E❌❌❌❌
- CORRECT ANSWER: D
- I misread the question and still got it wrong
- Since the method declares a checked exception is thrown, we are not required to catch it at all!
- D can not be put in the blank because the exception is not declared anywhere
❓ 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
- A - false
- B - false
- C - false
- D - false
- E - true
- E✅✅✅✅✅