Unit 4 - Practice Quiz
CSE310
1 Which of the following is true regarding a static nested class in Java?
2
Given a class Outer and an inner class Inner, what is the correct syntax to instantiate the inner class from outside the Outer class?
Outer.Inner in = new Outer.Inner();
Outer.Inner in = new Outer().new Inner();
Outer.Inner in = new Inner();
Outer.Inner in = Outer.new Inner();
3 Variables used inside a local inner class or a lambda expression that are defined in the enclosing scope must be:
4 What is an Anonymous Inner Class useful for?
5
How do you refer to the current instance of the enclosing class Outer from within a non-static inner class?
this.Outer
Outer.this
super
parent
6 Which annotation is used to ensure an interface is a valid Functional Interface?
@Lambda
@FunctionalInterface
@Override
@Interface
7 A Functional Interface can contain how many abstract methods?
8 Which of the following is the correct syntax for a Lambda expression that takes two integers and returns their sum?
(int a, int b) -> { return a + b; }
(a, b) => a + b
function(a, b) { return a + b; }
[a, b] -> a + b
9
What is the return type of the built-in functional interface Predicate<T>'s abstract method?
void
T
boolean
int
10 Which built-in functional interface represents an operation that accepts a single input argument and returns no result?
Supplier<T>
Function<T, R>
Consumer<T>
Predicate<T>
11 Which operator is used for Method References in Java?
->
::
..
=>
12 In the Java Exception hierarchy, which class is the superclass of all errors and exceptions?
java.lang.Exception
java.lang.Error
java.lang.Throwable
java.lang.RuntimeException
13 Which of the following is a Checked Exception?
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException
IOException
14
What is the primary difference between Error and Exception?
Error is checked, Exception is unchecked.
Error indicates serious problems that a reasonable application should not try to catch, while Exception indicates conditions that a reasonable application might want to catch.
Exception is fatal, Error is recoverable.
15 Which keyword is used to explicitly throw an exception from a method block?
throws
throw
catch
try
16
When using multiple catch blocks, which order must be followed?
17
The finally block is executed:
System.exit()).
18
What is the purpose of the throws keyword in a method signature?
19 Which syntax correctly demonstrates the multi-catch feature introduced in Java 7?
catch (IOException e1, SQLException e2) { ... }
catch (IOException | SQLException e) { ... }
catch (IOException || SQLException e) { ... }
catch (IOException & SQLException e) { ... }
20 To use the try-with-resources statement, the resource class must implement which interface?
Serializable
Cloneable
AutoCloseable
Runnable
21
Consider the following code: try (FileReader fr = new FileReader("file.txt")) { ... }. When is fr.close() called?
22
Which class from the java.time package represents a date without a time-zone (e.g., 2023-10-05)?
java.util.Date
java.time.LocalDate
java.time.LocalDateTime
java.time.ZonedDateTime
23
Are the classes in the java.time package (introduced in Java 8) mutable or immutable?
24
How do you obtain the current date using the java.time API?
new LocalDate()
LocalDate.today()
LocalDate.now()
Date.now()
25
Which class is used to format and parse date-time objects in the java.time package?
SimpleDateFormat
DateTimeFormatter
DateFormat
TimeFormatter
26 What is the purpose of creating a Custom Exception?
finally block behavior.
27 To create a custom checked exception, which class should you extend?
java.lang.Error
java.lang.RuntimeException
java.lang.Exception
java.lang.Throwable
28 Which command-line flag is used to enable Assertions in Java?
-assert
-ea (or -enableassertions)
-da
-check
29 What is the correct syntax for an assertion statement?
assert expression; or assert expression : errorMessage;
assertion(expression);
check expression;
assert(expression, errorMessage);
30 Assertions should be used for:
31 When overriding a method that throws an exception, the overriding method can throw:
32 What happens if a runtime exception is not caught?
33
In a try-with-resources statement, if an exception is thrown in the try block AND an exception is thrown while closing the resource, what happens to the exception from the close() method?
34
Which method of the Throwable class prints the exception name, message, and the sequence of method calls that led to the error?
getMessage()
toString()
printStackTrace()
getStackTrace()
35 Which of the following is NOT a valid access modifier for a local class?
36 Which functional interface would be most appropriate for a lambda that takes a String and returns its integer length?
Consumer<String>
Supplier<Integer>
Function<String, Integer>
Predicate<String>
37 If you want to sort a list of strings using a lambda, which interface is the lambda implementing?
Runnable
Comparable
Comparator
Iterator
38
What is the equivalent of the following lambda: () -> new ArrayList<>() using a method reference?
ArrayList::new
ArrayList::create
new::ArrayList
ArrayList()
39
In the context of the Java Date/Time API, what does Duration measure?
40 What happens if you declare a variable inside a lambda expression with the same name as a local variable in the enclosing scope?
41
Which of the following is true about a static nested class referencing this?
this refers to the enclosing class instance.
this refers to the static nested class instance itself.
this cannot be used in a static nested class.
this refers to the main method.
42
What is the result of LocalDate.of(2023, 1, 32)?
DateTimeException.
null.
43 Can an interface be a Functional Interface if it declares default methods?
44
Which exception is thrown when an application attempts to use null in a case where an object is required?
ClassNotFoundException
IllegalAccessException
NullPointerException
InstaniationException
45
What is the purpose of System.err compared to System.out?
46
In Exception Handling, if a return statement is present in both try and finally blocks, which one is returned?
try block.
finally block.
47 Which class is used to represent a timestamp on the timeline in Machine Time (UTC)?
java.time.LocalDate
java.time.Instant
java.time.Period
java.util.Calendar
48 Can a lambda expression modify a local variable defined outside its body?
49
Which of the following creates an ArithmeticException?
int x = 5 / 0;
int x = null;
int[] x = new int[-5];
Object x = new String("s"); Integer y = (Integer) x;
50 What is the main benefit of using a static nested class over a non-static inner class?