Unit 4 - Practice Quiz

CSE310

1 Which of the following is true regarding a static nested class in Java?

A. It has access to the non-static members of the enclosing class.
B. It must be instantiated using an instance of the enclosing class.
C. It can be instantiated without an instance of the enclosing class.
D. It cannot contain static members itself.

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?

A. Outer.Inner in = new Outer.Inner();
B. Outer.Inner in = new Outer().new Inner();
C. Outer.Inner in = new Inner();
D. 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:

A. Static
B. Volatile
C. Final or effectively final
D. Transient

4 What is an Anonymous Inner Class useful for?

A. Creating a class that can be reused multiple times across the application.
B. Declaring a class with a constructor that takes multiple arguments.
C. Overriding a method of a class or implementing an interface without creating a separate named subclass.
D. Creating a static nested class implicitly.

5 How do you refer to the current instance of the enclosing class Outer from within a non-static inner class?

A. this.Outer
B. Outer.this
C. super
D. parent

6 Which annotation is used to ensure an interface is a valid Functional Interface?

A. @Lambda
B. @FunctionalInterface
C. @Override
D. @Interface

7 A Functional Interface can contain how many abstract methods?

A. Zero
B. Exactly one
C. One or more
D. Any number

8 Which of the following is the correct syntax for a Lambda expression that takes two integers and returns their sum?

A. (int a, int b) -> { return a + b; }
B. (a, b) => a + b
C. function(a, b) { return a + b; }
D. [a, b] -> a + b

9 What is the return type of the built-in functional interface Predicate<T>'s abstract method?

A. void
B. T
C. boolean
D. int

10 Which built-in functional interface represents an operation that accepts a single input argument and returns no result?

A. Supplier<T>
B. Function<T, R>
C. Consumer<T>
D. Predicate<T>

11 Which operator is used for Method References in Java?

A. ->
B. ::
C. ..
D. =>

12 In the Java Exception hierarchy, which class is the superclass of all errors and exceptions?

A. java.lang.Exception
B. java.lang.Error
C. java.lang.Throwable
D. java.lang.RuntimeException

13 Which of the following is a Checked Exception?

A. NullPointerException
B. ArrayIndexOutOfBoundsException
C. ArithmeticException
D. IOException

14 What is the primary difference between Error and Exception?

A. Error is checked, Exception is unchecked.
B. 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.
C. Exception is fatal, Error is recoverable.
D. There is no difference.

15 Which keyword is used to explicitly throw an exception from a method block?

A. throws
B. throw
C. catch
D. try

16 When using multiple catch blocks, which order must be followed?

A. Superclasses before subclasses.
B. Subclasses before superclasses.
C. The order does not matter.
D. Checked exceptions before unchecked exceptions.

17 The finally block is executed:

A. Only if an exception occurs.
B. Only if no exception occurs.
C. Always, regardless of whether an exception is handled or not (with minor exceptions like System.exit()).
D. Only if the try block contains a return statement.

18 What is the purpose of the throws keyword in a method signature?

A. To handle the exception immediately.
B. To declare that the method might throw the specified exceptions, passing responsibility to the caller.
C. To create a new instance of an exception.
D. To suppress the exception.

19 Which syntax correctly demonstrates the multi-catch feature introduced in Java 7?

A. catch (IOException e1, SQLException e2) { ... }
B. catch (IOException | SQLException e) { ... }
C. catch (IOException || SQLException e) { ... }
D. catch (IOException & SQLException e) { ... }

20 To use the try-with-resources statement, the resource class must implement which interface?

A. Serializable
B. Cloneable
C. AutoCloseable
D. Runnable

21 Consider the following code: try (FileReader fr = new FileReader("file.txt")) { ... }. When is fr.close() called?

A. Only if an exception occurs.
B. Only if the block completes successfully.
C. Immediately after the file is opened.
D. Automatically at the end of the try block, regardless of success or failure.

22 Which class from the java.time package represents a date without a time-zone (e.g., 2023-10-05)?

A. java.util.Date
B. java.time.LocalDate
C. java.time.LocalDateTime
D. java.time.ZonedDateTime

23 Are the classes in the java.time package (introduced in Java 8) mutable or immutable?

A. Mutable
B. Immutable
C. Depends on the specific class
D. Mutable but thread-safe

24 How do you obtain the current date using the java.time API?

A. new LocalDate()
B. LocalDate.today()
C. LocalDate.now()
D. Date.now()

25 Which class is used to format and parse date-time objects in the java.time package?

A. SimpleDateFormat
B. DateTimeFormatter
C. DateFormat
D. TimeFormatter

26 What is the purpose of creating a Custom Exception?

A. To override the finally block behavior.
B. To capture and handle business-logic specific errors not covered by standard Java exceptions.
C. To make the application run faster.
D. To avoid using try-catch blocks.

27 To create a custom checked exception, which class should you extend?

A. java.lang.Error
B. java.lang.RuntimeException
C. java.lang.Exception
D. java.lang.Throwable

28 Which command-line flag is used to enable Assertions in Java?

A. -assert
B. -ea (or -enableassertions)
C. -da
D. -check

29 What is the correct syntax for an assertion statement?

A. assert expression; or assert expression : errorMessage;
B. assertion(expression);
C. check expression;
D. assert(expression, errorMessage);

30 Assertions should be used for:

A. Checking arguments of public methods.
B. Testing internal invariants and assumptions in the code.
C. Handling user input errors.
D. Replacing standard exception handling.

31 When overriding a method that throws an exception, the overriding method can throw:

A. Any exception.
B. Only the same exception or its subclasses (or no exception).
C. Any superclass of the original exception.
D. Only runtime exceptions.

32 What happens if a runtime exception is not caught?

A. The compiler throws an error.
B. The program continues silently.
C. It propagates up the call stack and eventually terminates the thread/program if not handled.
D. It is automatically converted to a checked exception.

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?

A. It overwrites the original exception.
B. It is ignored completely.
C. It is added as a suppressed exception to the original exception.
D. Both exceptions are thrown simultaneously.

34 Which method of the Throwable class prints the exception name, message, and the sequence of method calls that led to the error?

A. getMessage()
B. toString()
C. printStackTrace()
D. getStackTrace()

35 Which of the following is NOT a valid access modifier for a local class?

A. Abstract
B. Final
C. Public
D. None (default)

36 Which functional interface would be most appropriate for a lambda that takes a String and returns its integer length?

A. Consumer<String>
B. Supplier<Integer>
C. Function<String, Integer>
D. Predicate<String>

37 If you want to sort a list of strings using a lambda, which interface is the lambda implementing?

A. Runnable
B. Comparable
C. Comparator
D. Iterator

38 What is the equivalent of the following lambda: () -> new ArrayList<>() using a method reference?

A. ArrayList::new
B. ArrayList::create
C. new::ArrayList
D. ArrayList()

39 In the context of the Java Date/Time API, what does Duration measure?

A. A date-based amount of time (e.g., 2 years, 3 months).
B. A time-based amount of time (e.g., 34.5 seconds).
C. The current time zone.
D. A specific point on the timeline.

40 What happens if you declare a variable inside a lambda expression with the same name as a local variable in the enclosing scope?

A. It shadows the outer variable.
B. It results in a compile-time error.
C. It is allowed but discouraged.
D. The outer variable becomes null.

41 Which of the following is true about a static nested class referencing this?

A. this refers to the enclosing class instance.
B. this refers to the static nested class instance itself.
C. this cannot be used in a static nested class.
D. this refers to the main method.

42 What is the result of LocalDate.of(2023, 1, 32)?

A. Creates a date for February 1st, 2023.
B. Creates a date for January 31st, 2023 (clamped).
C. Throws DateTimeException.
D. Returns null.

43 Can an interface be a Functional Interface if it declares default methods?

A. No, it must strictly have only one method.
B. Yes, as long as it has exactly one abstract method.
C. No, default methods are not allowed in Java.
D. Only if the default methods are static.

44 Which exception is thrown when an application attempts to use null in a case where an object is required?

A. ClassNotFoundException
B. IllegalAccessException
C. NullPointerException
D. InstaniationException

45 What is the purpose of System.err compared to System.out?

A. It is faster.
B. It is used specifically for outputting error messages, often allowing IDEs/Consoles to highlight them differently.
C. It writes to a file instead of the console.
D. It automatically logs time stamps.

46 In Exception Handling, if a return statement is present in both try and finally blocks, which one is returned?

A. The one in the try block.
B. The one in the finally block.
C. Both are returned.
D. Compile time error.

47 Which class is used to represent a timestamp on the timeline in Machine Time (UTC)?

A. java.time.LocalDate
B. java.time.Instant
C. java.time.Period
D. java.util.Calendar

48 Can a lambda expression modify a local variable defined outside its body?

A. Yes, always.
B. Yes, if the variable is static.
C. No, local variables must be effectively final.
D. Yes, if the variable is public.

49 Which of the following creates an ArithmeticException?

A. int x = 5 / 0;
B. int x = null;
C. int[] x = new int[-5];
D. 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?

A. It allows access to private members of the enclosing class.
B. It decouples the nested class from the instance of the enclosing class, saving memory if the link is not needed.
C. It allows the class to be extended.
D. It makes the code faster to compile.