Java 17 Interview Questions & Answers Explained: Practice Test Core Java Interview java Java 17 Java MCQ by devs5003 - December 22, 2024May 17, 20250 Last Updated on May 17th, 2025 In this article, we will explore Java 17 Interview Questions & Answers with detailed explanation. We can also consider this article as a complete practice test for the concepts discussed. We have covered all types of questions such as concept-based, code-based and scenario-based. Some of the questions are based on features of Java 21 as well. On going through all the questions, you will have both conceptual & practical knowledge of the topics. These questions will make you aware about not only Java 17 features but other lower/higher version’s features also. You must go through the explanation part of each question which will help you to clear your additional concepts. ♠ If you want to explore concepts on Java 17 new features first, kindly visit detailed article on Java 17 new features. Java 17 Interview Questions & Answers Explained [Concept-Based | Single-Select] Q#1. Which of the following is a final feature in Java 17? A) Record classes B) Sealed classes C) Pattern Matching for instanceof D) Pattern Matching for switch Expressions and Statements Answer: B) Sealed classes Explanation: (A) Record classes: Record classes first previewed in Java SE 14 and became permanent in Java SE 16 release. (B) Sealed classes: Sealed classes first previewed in Java SE 15. This feature became permanent in Java SE 17 release. This means that it can be used in any program compiled for Java SE 17 without enabling preview features. (C) Pattern Matching for instanceof: This feature first previewed in Java SE 14 and became permanent in Java SE 16 release. (D) Pattern Matching for switch Expressions and Statements: This feature first introduced as a preview feature for Java SE 17 release under JEP 406, but not a final feature. [Code-Based | Single-Select] Q#2. What will the following code produce when executed in Java 17? public sealed interface Vehicle permits Car, Bike {} public final class Car implements Vehicle {} public final class Bike implements Vehicle {} class Truck implements Vehicle {} A) Compilation succeeds B) Compile-time error due to Truck not being listed in permits C) Runtime error due to an incomplete hierarchy D) Compile-time error due to sealed being unsupported Answer: B) Compile-time error due to Truck not being listed in permits Explanation: (A): Incorrect. Truck is not declared in the permits clause of Vehicle, violating sealed class rules. (B): Correct. All subclasses must be explicitly declared in the permits clause when using sealed types. (C): Incorrect. This issue is detected at compile time, not runtime. (D): Incorrect. Java 17 supports sealed classes and interfaces via JEP 409. [Concept-Based | Single-Select] Q#3. Which of the following is a valid approach to replace if-else using a switch statement in Java 17? A) switch (obj) { case String s -> System.out.println(s); default -> System.out.println("Not a String"); } B) switch (obj) { case Integer i -> System.out.println(i * 2); case Double d -> System.out.println(d + 1.5); default -> System.out.println("Not a Number"); } C) switch (obj) { case String s -> System.out.println("String"); case null -> System.out.println("Null"); default -> System.out.println("Not a String"); } D) All of the above Answer: D) All of the above Explanation: (A): Valid. Type pattern for String with default is correct. (B): Valid. Matches Integer and Double types separately with default. (C): Valid. Includes null handling in switch. (D): Correct. All options are valid. [Code-Based | Multi-Select] Q#4. Which of the following code snippets is correctly written in the context of sealed classes/interfaces in Java 17? A) public sealed class Shape permits Circle, Square {} B) public non-sealed class Circle extends Shape {} C) public sealed interface Animal non-permits Circle, Square {} D) public sealed class Vehicle permits {} Answer: A) and B) Explanation: (A): Correct syntax for sealed classes. Shape restricts its subclasses to Circle and Square. Correct. (B): A subclass (Circle) can be declared non-sealed, allowing further subclassing. Correct. (C): There is no ‘non-permits’ clause, but ‘permits’ introduced in Java 17. Incorrect. (D): The permits clause is empty, which is not a valid syntax. Incorrect. [Concept-Based | Multi-Select] Q#5. Which of the following statements are true about type patterns in switch? A) They improve readability by avoiding instanceof and casting. B) They allow any type of object in case label. C) They can handle null D) They support primitive types. Answer: A), B), and C) Explanation: (A): Correct. Type patterns eliminate redundant instanceof checks and casting. (B): Correct. Any type of object in case label is allowed. (C): Correct. case null can handle null values explicitly. (D): Incorrect. Primitive types are not supported in type patterns. Type patterns are introduced to handle patterns which involve reference types. Primitive types, being non-reference types, do not fit well in this pattern-matching mechanism. [Concept-Based | Multi-Select] Q#6. Which features are showcased in this Java 17 code snippet? public sealed class Shape permits Circle, Rectangle {} public final class Circle extends Shape {} public final class Rectangle extends Shape {} A) Strong encapsulation B) Sealed classes C) Restricted inheritance D) Pattern Matching for switch Answer: B) and C) Explanation: (A): Incorrect. Strong encapsulation refers to JDK internals, not sealed classes. (B): Correct. Sealed classes (JEP 409) restrict subclassing. (C): Correct. Restricted inheritance is a key feature of sealed classes. (D): Incorrect. There’s no usage of switch here. [Concept-Based | Single-Select] Q#7. Which of the following statements is false in the context of sealed classes in Java 17? A) A record class can be included in the permits clause of a sealed class or interface. B) All subclasses must be in the same module as the sealed class. C) All non-record subclasses must be either final, or sealed, or non-sealed. D) Subclasses must implement a common method. Answer: D) Subclasses must implement a common method Explanation: (A): Incorrect. A record class can be included in the permits clause of a sealed class or interface. (B): Incorrect. They must be in the same module as the sealed class (if the sealed class is in a named module) or in the same package (if the sealed class is in the unnamed module. (C): Incorrect. All non-record subclasses must have exactly one of the mentioned modifiers. If subclass is a record type class, it doesn’t require final modifier as it’s final by default. (D): Correct. Subclasses don’t require implementing a common method. [Scenario-Based | Multi-Select] Q#8. You are implementing a module to represent different types of memberships for a product. The hierarchy is as shown below in the code snippet. Which statements about this hierarchy are correct? sealed interface Membership permits Premium, Regular, Free {} sealed class Premium implements Membership permits Gold, Platinum {} final class Gold extends Premium {} final class Platinum extends Premium {} final class Regular implements Membership {} non-sealed class Free implements Membership {} A) Membership cannot have any additional subclasses without modifying its permits B) Premium can have further subclasses like Silver without modifying its own hierarchy. C) Gold and Platinum cannot have further subclasses. D) Free can have subclasses like TrialFree. Answer: A), C), and D) Explanation: (A): Correct. Membership is sealed, so it cannot have subclasses beyond those listed in its permits clause. (B): Incorrect. Premium is sealed, so additional subclasses like Silver would require updating its permits clause. (C): Correct. The final modifier on Gold and Platinum prohibits further subclassing. (D): Correct. The non-sealed modifier on Free allows further subclassing. [Concept-Based | Single-Select] Q#9. What is the main limitation of using record classes in the permits clause of a sealed class? A) Record classes cannot implement multiple sealed interfaces. B) Record classes cannot have any constructors. C) Record classes cannot have mutable fields. D) Record classes are implicitly final and cannot have further subclasses. Answer: D) Record classes are implicitly final and cannot have further subclasses. Explanation: (D): correct because record classes are implicitly final, meaning they cannot be subclassed further. (A): incorrect because record classes can implement multiple interfaces, as long as they follow the sealed class or interface hierarchy. (B): incorrect because record classes can define constructors if needed. (C): incorrect because immutability is a feature, not a limitation. [Code-Based | Single-Select] Q#10. What is the effect of the following code snippet in Java 17? public sealed interface Animal permits Dog, Cat {} public non-sealed class Dog implements Animal {} public final class Cat implements Animal {} A) Dog can have subclasses B) Cat can have subclasses C) Animal can have additional subclasses other than Dog & Cat D) Dog cannot have subclasses Answer: A) Dog can have subclasses Explanation: (A): Correct. non-sealed allows further subclassing. (B): Incorrect. final prevents subclassing for Cat. (C): A sealed class/interface can’t have subclasses other than which are declared in permits clause (D): A non-sealed class can have subclasses. [Code-Based | Multi-Select | Java 21] Q#11. Which of the following patterns are valid in a switch statement? A) case Double d when d > 0.5 B) case null C) case String s when s.startsWith("D") D) case Employee e when e.getEmpSalary()> 10000 Answer: A), B), C), and D) Explanation: (A): Correct. A when clause with a Double type pattern is valid. The when clause comes under a concept called ‘Guarded Pattern‘ introduced in Java 21. (B): Correct. case null is also supported. (C): Correct. String type pattern with a when clause is valid. (D): Correct. Any type pattern with a when clause is valid. [Concept-Based | Single-Select] Q#12. Which of the following is not a good practice in improving code readability while using type patterns in a switch? A) Using default as the last branch. B) Using instanceof C) Using when clauses for conditions. D) Handling all possible null cases. Answer: B) Using instanceof checks. Explanation: (A): Incorrect. The default branch ensures all cases are handled & improves clarity. (B): Correct. Use of multiple instanceof checks makes code redundant. (C): Incorrect. when clauses add conditional logic for matched cases which improves code readability. (D): Incorrect. Explicitly handling null improves clarity. [Code-Based | Single-Select] Q#13. Which of the following code snippets correctly demonstrates a record class in the permits clause of a sealed class? A) sealed class Vehicle permits Car, Bike { } record Car(String model) { } record Bike(String type) { } B) sealed class Vehicle permits Car, Bike { } record Car(String model) extends Vehicle { } record Bike(String type) { } C) sealed class Vehicle permits Car, Bike { } record Car(String model) extends Vehicle { } record Bike(String type) extends Vehicle { } D) sealed class Vehicle { } record Car(String model) extends Vehicle { } record Bike(String type) { } Answer: C Explanation: Option C is correct because both Car and Bike are record classes explicitly listed in the permits clause of the sealed class Vehicle and extend it as required. Option A is incorrect because record classes must explicitly extend the sealed class if they are mentioned in the permits clause. Option B is incorrect because the Bike class does not extend Vehicle. Option D is incorrect because the permits clause is missing from the sealed class declaration. [Scenario-Based | Single-Select] Q#14. A project involves creating a sealed hierarchy for geographic locations. The Region class is sealed, allowing City and Country as its permitted subclasses. The team proposes: sealed class Region permits City, Country {} final class City extends Region {} non-sealed class Country extends Region {} Later, the team attempts to declare the following: final class Continent extends Country {} What will happen during compilation? A) Compilation succeeds as Country is non-sealed. B) Compilation fails because Country is a subclass of Region. C) Compilation fails because Continent must be listed in Region’s permits D) Compilation fails because sealed classes cannot have multiple levels of inheritance. Answer: A) Compilation succeeds as Country is non-sealed. Explanation: (A): Correct. The non-sealed modifier on Country allows further subclassing without restrictions. (B): Incorrect. The non-sealed nature of Country makes this permissible. (C): Incorrect. Only subclasses of sealed classes must be explicitly listed in permits; Country being non-sealed does not have this restriction. (D): Incorrect. There is no rule preventing multiple levels of inheritance in a sealed hierarchy. [Code-Based | Single-Select] Q#15. What will the following code snippet print while running using Java 17? Object obj = "Java 17"; switch (obj) { case String s -> System.out.println(s.repeat(3)); case Integer i -> System.out.println(i * 3); default -> System.out.println("Unknown"); } A) Java 17 Java 17 Java 17 B) Java 17Java 17Java 17 C) Unknown D) Compile-time error Answer: B) Java 17Java 17Java 17 Explanation: (B): Correct. The type String matches obj, and s.repeat(3) repeats the string thrice. (A): Incorrect. The repeat method does not add spaces by default. (C): Incorrect. The default branch is not executed since obj matches String. (D): Incorrect. The syntax is valid and matches Java 17 features. [Concept-Based | Multi-Select] Q#16. Which of the following statements are true about type patterns in switch statements? A) They allow matching on the runtime type of objects. B) They eliminate the need for explicit type casting. C) They require the instanceof D) They are supported from Java 17 onwards. Answer: A), B), and D) Explanation: (A): Correct. Type patterns check the runtime type of the object in the switch. (B): Correct. Matched objects are automatically cast to the specified type. (C): Incorrect. The switch statement replaces the instanceof operator in this context. (D): Correct. Type patterns in switch are a Java 17 feature. [Code-Based | Single-Select] Q#17. What happens when this code is executed in Java 17? import java.util.HexFormat; public class HexFormatTest{ public static void main(String[] args) { HexFormat hex = HexFormat.of(); System.out.println(hex.formatHex(new byte[] {10, 20})); } } A) Prints 0a14 B) Prints 1014 C) Compile-time error D) Prints 1020 Answer: A) Prints 0a14 Explanation: (A): Correct. HexFormat.formatHex() formats the bytes as hexadecimal values (0a for 10, 14 for 20). (B): Incorrect. Incorrect Hexadecimal values. (C): Incorrect. No syntax issue exists. (D): Incorrect. Incorrect Hexadecimal values. [Code-Based | Multi-Select] Q#18. Consider the following code snippet: Object value = 10; String result = switch (value) { case Integer i when i >= 10 -> "Large Integer"; case Integer i -> "Small Integer"; case String s -> "String: " + s; default -> "Unknown"; }; Which of the following are correct outputs of the given code while running in Java 21? A) “Large Integer” if value is 24. B) “Small Integer” if value is 5. C) “String: Java” if value is “Java”. D) “Unknown” if value is null. Answer: A), B), C), and D) Explanation: (A): Correct. The value 24 matches the first case as it satisfies Integer i when i > 10. (B): Correct. The value 5 matches the second case as an Integer but fails the first condition. (C): Correct. The value “Java” matches the String s case. (D): Correct. A null value does not match any case, so the default case executes. [Scenario-Based | Single-Select] Q#19. You are designing a financial application that processes various payment methods (e.g., CreditCard, DebitCard, PayPal). You want to restrict new payment types to prevent unauthorized extensions while ensuring some payment methods can be extended further for specific providers (e.g., Visa, Mastercard for CreditCard). How would you define your hierarchy? A) Use sealed for Payment and final for all subclasses. B) Use sealed for Payment and non-sealed for CreditCard and PayPal. C) Use final for Payment and extend each method separately. D) Use abstract for Payment and leave it open for all subclasses. Answer: B) Use sealed for Payment and non-sealed for CreditCard and PayPal. Explanation: (B): Correct. Declaring Payment as sealed ensures a controlled hierarchy, while non-sealed subclasses like CreditCard allow further extension. (A): Incorrect. Marking all subclasses as final prevents extensibility for CreditCard providers like Visa or Mastercard. (C): Incorrect. Declaring Payment as final disallows any inheritance, which is not suitable here. (D): Incorrect. Using abstract provides no restriction, which defeats the purpose of controlling the hierarchy. [Code-Based | Single-Select] Q#20. What is the possible outputs of the following code snippet while running using Java 17? Object obj = List.of("One","Two", "Three"); switch (obj) { case String s -> System.out.println("String"); case List<?> l -> System.out.println("List"); default -> System.out.println("Other"); } A) String B) List C) Other D) Compile-time error Answer: B) List Explanation: (A): Incorrect. obj is not a String. (B): Correct. obj matches the type List<?>. (C): Incorrect. The default branch is not executed. (D): Incorrect. The syntax is correct. [Code-Based | Single-Select] Q#21. Which of the following URLs will isSameFile() return true for? URL url1 = new URL("https://example.com/page"); URL url2 = new URL("https://example.com/index"); URL url3 = new URL("https://example.com/page"); A) url1 and url2 B) url2 and url3 C) url1 and url3 D) None of the above Answer: C) url1 and url3 Explanation: (C): Correct. isSameFile() checks if URLs point to the same file. Both URLs match exactly. (A), (B): Incorrect. Mismatched URLs will not pass the isSameFile() check. (D): Incorrect as option A is correct. [Concept-Based | Multi-Select] Q#22. What is the benefit(s) of the new case syntax introduced in Java 17? A) It allows multiple case labels to share the same action. B) It eliminates the need for a default clause in a switch statement. C) It improves code readability and reduces redundancy in switch statements. D) It allows exception handling directly within a case. Answer: A) and C) Explanation: (A) is correct because the new syntax allows grouping multiple case labels together, reducing redundancy. (B) is incorrect as the default clause is still optional but not eliminated. (C) is correct since the syntax improves code readability & eliminates code redundancy. (D) is incorrect because it doesn’t allow exception handling within the case in the syntax. [Code-Based | Single-Select] Q#23. What is wrong with the following code snippet? switch (input) { case 1, 2, 3 -> System.out.println("Low range"); case 4, 5 -> System.out.println("Mid range"); case 6 -> { System.out.println("High range"); } case 7, 8, 9 -> throw new IllegalArgumentException("Out of range"); } A) The last case label group cannot throw an exception. B) The default case is missing. C) Mixing single-line and block statements for case labels is not allowed. D) There is nothing wrong with this code. Answer: B Explanation: (B) is correct because the default case is required for non-exhaustive cases. In Java, non-exhaustive cases in a switch statement occur when the provided case labels do not cover all possible values of the selector expression. In this example, cases for values >9 are not covered. (A) is incorrect as throwing exceptions is valid in a case label. (C) is incorrect as mixing single-line and block statements is allowed. (D) is incorrect because a missing default is an issue for exhaustive switches which will result in a compilation error. [Code-Based | Single-Select] Q#24. Which of the following case syntax will not have any compilation issue for multiple labels in a shopping cart discount logic? A) switch (category) { case "Electronics", "Appliances", "Furniture": System.out.println("10% Discount"); case "Clothing": System.out.println("15% Discount"); case "Groceries": System.out.println("5% Discount"); default: System.out.println("No Discount Available"); } B) switch (category) { case "Electronics", "Appliances", "Furniture" { System.out.println("10% Discount"); } case "Clothing", "Groceries" -> System.out.println("15% Discount"); default -> System.out.println("No Discount Available"); } C) switch (category) { case "Electronics", "Appliances", "Furniture" : { System.out.println("10% Discount"); } case "Clothing", "Groceries" -> System.out.println("15% Discount"); default -> System.out.println("No Discount Available"); } D) switch (category) { case {"Electronics", "Appliances", "Furniture"} -> System.out.println("10% Discount"); case "Clothing" -> System.out.println("15% Discount"); case "Groceries" -> System.out.println("5% Discount"); default -> System.out.println("No Discount Available"); } Answer: A Explanation: (A) is correct because it properly uses the case syntax with multiple labels. Although ‘->’ is introduced in place of ‘:’ in Java 17, but ‘:’ still compiles well even with Java 21 compiler. (B) is incorrect as the first case label is missing ‘->’ or ‘:’ before {}. (C) is incorrect because mixing of ‘->’ and ‘:’ is not allowed. We can use either of them in all case labels. (D) is incorrect because it uses {} to close first case label which is not allowed. References: https://docs.oracle.com/en/java/javase/17/language/ ♥ To Practice Java 17 coding Interview, kindly visit Java 17 Coding Interview Questions & Solutions. ♥ You may also attempt MCQs on other topics of Java & other related technologies by visiting our Quizzes & Practice Test section. Related
In this article, we will explore Java 17 Interview Questions & Answers with detailed explanation. We can also consider this article as a complete practice test for the concepts discussed. We have covered all types of questions such as concept-based, code-based and scenario-based. Some of the questions are based on features of Java 21 as well. On going through all the questions, you will have both conceptual & practical knowledge of the topics. These questions will make you aware about not only Java 17 features but other lower/higher version’s features also. You must go through the explanation part of each question which will help you to clear your additional concepts. ♠ If you want to explore concepts on Java 17 new features first, kindly visit detailed article on Java 17 new features. Java 17 Interview Questions & Answers Explained [Concept-Based | Single-Select] Q#1. Which of the following is a final feature in Java 17? A) Record classes B) Sealed classes C) Pattern Matching for instanceof D) Pattern Matching for switch Expressions and Statements Answer: B) Sealed classes Explanation: (A) Record classes: Record classes first previewed in Java SE 14 and became permanent in Java SE 16 release. (B) Sealed classes: Sealed classes first previewed in Java SE 15. This feature became permanent in Java SE 17 release. This means that it can be used in any program compiled for Java SE 17 without enabling preview features. (C) Pattern Matching for instanceof: This feature first previewed in Java SE 14 and became permanent in Java SE 16 release. (D) Pattern Matching for switch Expressions and Statements: This feature first introduced as a preview feature for Java SE 17 release under JEP 406, but not a final feature. [Code-Based | Single-Select] Q#2. What will the following code produce when executed in Java 17? public sealed interface Vehicle permits Car, Bike {} public final class Car implements Vehicle {} public final class Bike implements Vehicle {} class Truck implements Vehicle {} A) Compilation succeeds B) Compile-time error due to Truck not being listed in permits C) Runtime error due to an incomplete hierarchy D) Compile-time error due to sealed being unsupported Answer: B) Compile-time error due to Truck not being listed in permits Explanation: (A): Incorrect. Truck is not declared in the permits clause of Vehicle, violating sealed class rules. (B): Correct. All subclasses must be explicitly declared in the permits clause when using sealed types. (C): Incorrect. This issue is detected at compile time, not runtime. (D): Incorrect. Java 17 supports sealed classes and interfaces via JEP 409. [Concept-Based | Single-Select] Q#3. Which of the following is a valid approach to replace if-else using a switch statement in Java 17? A) switch (obj) { case String s -> System.out.println(s); default -> System.out.println("Not a String"); } B) switch (obj) { case Integer i -> System.out.println(i * 2); case Double d -> System.out.println(d + 1.5); default -> System.out.println("Not a Number"); } C) switch (obj) { case String s -> System.out.println("String"); case null -> System.out.println("Null"); default -> System.out.println("Not a String"); } D) All of the above Answer: D) All of the above Explanation: (A): Valid. Type pattern for String with default is correct. (B): Valid. Matches Integer and Double types separately with default. (C): Valid. Includes null handling in switch. (D): Correct. All options are valid. [Code-Based | Multi-Select] Q#4. Which of the following code snippets is correctly written in the context of sealed classes/interfaces in Java 17? A) public sealed class Shape permits Circle, Square {} B) public non-sealed class Circle extends Shape {} C) public sealed interface Animal non-permits Circle, Square {} D) public sealed class Vehicle permits {} Answer: A) and B) Explanation: (A): Correct syntax for sealed classes. Shape restricts its subclasses to Circle and Square. Correct. (B): A subclass (Circle) can be declared non-sealed, allowing further subclassing. Correct. (C): There is no ‘non-permits’ clause, but ‘permits’ introduced in Java 17. Incorrect. (D): The permits clause is empty, which is not a valid syntax. Incorrect. [Concept-Based | Multi-Select] Q#5. Which of the following statements are true about type patterns in switch? A) They improve readability by avoiding instanceof and casting. B) They allow any type of object in case label. C) They can handle null D) They support primitive types. Answer: A), B), and C) Explanation: (A): Correct. Type patterns eliminate redundant instanceof checks and casting. (B): Correct. Any type of object in case label is allowed. (C): Correct. case null can handle null values explicitly. (D): Incorrect. Primitive types are not supported in type patterns. Type patterns are introduced to handle patterns which involve reference types. Primitive types, being non-reference types, do not fit well in this pattern-matching mechanism. [Concept-Based | Multi-Select] Q#6. Which features are showcased in this Java 17 code snippet? public sealed class Shape permits Circle, Rectangle {} public final class Circle extends Shape {} public final class Rectangle extends Shape {} A) Strong encapsulation B) Sealed classes C) Restricted inheritance D) Pattern Matching for switch Answer: B) and C) Explanation: (A): Incorrect. Strong encapsulation refers to JDK internals, not sealed classes. (B): Correct. Sealed classes (JEP 409) restrict subclassing. (C): Correct. Restricted inheritance is a key feature of sealed classes. (D): Incorrect. There’s no usage of switch here. [Concept-Based | Single-Select] Q#7. Which of the following statements is false in the context of sealed classes in Java 17? A) A record class can be included in the permits clause of a sealed class or interface. B) All subclasses must be in the same module as the sealed class. C) All non-record subclasses must be either final, or sealed, or non-sealed. D) Subclasses must implement a common method. Answer: D) Subclasses must implement a common method Explanation: (A): Incorrect. A record class can be included in the permits clause of a sealed class or interface. (B): Incorrect. They must be in the same module as the sealed class (if the sealed class is in a named module) or in the same package (if the sealed class is in the unnamed module. (C): Incorrect. All non-record subclasses must have exactly one of the mentioned modifiers. If subclass is a record type class, it doesn’t require final modifier as it’s final by default. (D): Correct. Subclasses don’t require implementing a common method. [Scenario-Based | Multi-Select] Q#8. You are implementing a module to represent different types of memberships for a product. The hierarchy is as shown below in the code snippet. Which statements about this hierarchy are correct? sealed interface Membership permits Premium, Regular, Free {} sealed class Premium implements Membership permits Gold, Platinum {} final class Gold extends Premium {} final class Platinum extends Premium {} final class Regular implements Membership {} non-sealed class Free implements Membership {} A) Membership cannot have any additional subclasses without modifying its permits B) Premium can have further subclasses like Silver without modifying its own hierarchy. C) Gold and Platinum cannot have further subclasses. D) Free can have subclasses like TrialFree. Answer: A), C), and D) Explanation: (A): Correct. Membership is sealed, so it cannot have subclasses beyond those listed in its permits clause. (B): Incorrect. Premium is sealed, so additional subclasses like Silver would require updating its permits clause. (C): Correct. The final modifier on Gold and Platinum prohibits further subclassing. (D): Correct. The non-sealed modifier on Free allows further subclassing. [Concept-Based | Single-Select] Q#9. What is the main limitation of using record classes in the permits clause of a sealed class? A) Record classes cannot implement multiple sealed interfaces. B) Record classes cannot have any constructors. C) Record classes cannot have mutable fields. D) Record classes are implicitly final and cannot have further subclasses. Answer: D) Record classes are implicitly final and cannot have further subclasses. Explanation: (D): correct because record classes are implicitly final, meaning they cannot be subclassed further. (A): incorrect because record classes can implement multiple interfaces, as long as they follow the sealed class or interface hierarchy. (B): incorrect because record classes can define constructors if needed. (C): incorrect because immutability is a feature, not a limitation. [Code-Based | Single-Select] Q#10. What is the effect of the following code snippet in Java 17? public sealed interface Animal permits Dog, Cat {} public non-sealed class Dog implements Animal {} public final class Cat implements Animal {} A) Dog can have subclasses B) Cat can have subclasses C) Animal can have additional subclasses other than Dog & Cat D) Dog cannot have subclasses Answer: A) Dog can have subclasses Explanation: (A): Correct. non-sealed allows further subclassing. (B): Incorrect. final prevents subclassing for Cat. (C): A sealed class/interface can’t have subclasses other than which are declared in permits clause (D): A non-sealed class can have subclasses. [Code-Based | Multi-Select | Java 21] Q#11. Which of the following patterns are valid in a switch statement? A) case Double d when d > 0.5 B) case null C) case String s when s.startsWith("D") D) case Employee e when e.getEmpSalary()> 10000 Answer: A), B), C), and D) Explanation: (A): Correct. A when clause with a Double type pattern is valid. The when clause comes under a concept called ‘Guarded Pattern‘ introduced in Java 21. (B): Correct. case null is also supported. (C): Correct. String type pattern with a when clause is valid. (D): Correct. Any type pattern with a when clause is valid. [Concept-Based | Single-Select] Q#12. Which of the following is not a good practice in improving code readability while using type patterns in a switch? A) Using default as the last branch. B) Using instanceof C) Using when clauses for conditions. D) Handling all possible null cases. Answer: B) Using instanceof checks. Explanation: (A): Incorrect. The default branch ensures all cases are handled & improves clarity. (B): Correct. Use of multiple instanceof checks makes code redundant. (C): Incorrect. when clauses add conditional logic for matched cases which improves code readability. (D): Incorrect. Explicitly handling null improves clarity.
[Code-Based | Single-Select] Q#13. Which of the following code snippets correctly demonstrates a record class in the permits clause of a sealed class? A) sealed class Vehicle permits Car, Bike { } record Car(String model) { } record Bike(String type) { } B) sealed class Vehicle permits Car, Bike { } record Car(String model) extends Vehicle { } record Bike(String type) { } C) sealed class Vehicle permits Car, Bike { } record Car(String model) extends Vehicle { } record Bike(String type) extends Vehicle { } D) sealed class Vehicle { } record Car(String model) extends Vehicle { } record Bike(String type) { } Answer: C Explanation: Option C is correct because both Car and Bike are record classes explicitly listed in the permits clause of the sealed class Vehicle and extend it as required. Option A is incorrect because record classes must explicitly extend the sealed class if they are mentioned in the permits clause. Option B is incorrect because the Bike class does not extend Vehicle. Option D is incorrect because the permits clause is missing from the sealed class declaration. [Scenario-Based | Single-Select] Q#14. A project involves creating a sealed hierarchy for geographic locations. The Region class is sealed, allowing City and Country as its permitted subclasses. The team proposes: sealed class Region permits City, Country {} final class City extends Region {} non-sealed class Country extends Region {} Later, the team attempts to declare the following: final class Continent extends Country {} What will happen during compilation? A) Compilation succeeds as Country is non-sealed. B) Compilation fails because Country is a subclass of Region. C) Compilation fails because Continent must be listed in Region’s permits D) Compilation fails because sealed classes cannot have multiple levels of inheritance. Answer: A) Compilation succeeds as Country is non-sealed. Explanation: (A): Correct. The non-sealed modifier on Country allows further subclassing without restrictions. (B): Incorrect. The non-sealed nature of Country makes this permissible. (C): Incorrect. Only subclasses of sealed classes must be explicitly listed in permits; Country being non-sealed does not have this restriction. (D): Incorrect. There is no rule preventing multiple levels of inheritance in a sealed hierarchy. [Code-Based | Single-Select] Q#15. What will the following code snippet print while running using Java 17? Object obj = "Java 17"; switch (obj) { case String s -> System.out.println(s.repeat(3)); case Integer i -> System.out.println(i * 3); default -> System.out.println("Unknown"); } A) Java 17 Java 17 Java 17 B) Java 17Java 17Java 17 C) Unknown D) Compile-time error Answer: B) Java 17Java 17Java 17 Explanation: (B): Correct. The type String matches obj, and s.repeat(3) repeats the string thrice. (A): Incorrect. The repeat method does not add spaces by default. (C): Incorrect. The default branch is not executed since obj matches String. (D): Incorrect. The syntax is valid and matches Java 17 features. [Concept-Based | Multi-Select] Q#16. Which of the following statements are true about type patterns in switch statements? A) They allow matching on the runtime type of objects. B) They eliminate the need for explicit type casting. C) They require the instanceof D) They are supported from Java 17 onwards. Answer: A), B), and D) Explanation: (A): Correct. Type patterns check the runtime type of the object in the switch. (B): Correct. Matched objects are automatically cast to the specified type. (C): Incorrect. The switch statement replaces the instanceof operator in this context. (D): Correct. Type patterns in switch are a Java 17 feature. [Code-Based | Single-Select] Q#17. What happens when this code is executed in Java 17? import java.util.HexFormat; public class HexFormatTest{ public static void main(String[] args) { HexFormat hex = HexFormat.of(); System.out.println(hex.formatHex(new byte[] {10, 20})); } } A) Prints 0a14 B) Prints 1014 C) Compile-time error D) Prints 1020 Answer: A) Prints 0a14 Explanation: (A): Correct. HexFormat.formatHex() formats the bytes as hexadecimal values (0a for 10, 14 for 20). (B): Incorrect. Incorrect Hexadecimal values. (C): Incorrect. No syntax issue exists. (D): Incorrect. Incorrect Hexadecimal values. [Code-Based | Multi-Select] Q#18. Consider the following code snippet: Object value = 10; String result = switch (value) { case Integer i when i >= 10 -> "Large Integer"; case Integer i -> "Small Integer"; case String s -> "String: " + s; default -> "Unknown"; }; Which of the following are correct outputs of the given code while running in Java 21? A) “Large Integer” if value is 24. B) “Small Integer” if value is 5. C) “String: Java” if value is “Java”. D) “Unknown” if value is null. Answer: A), B), C), and D) Explanation: (A): Correct. The value 24 matches the first case as it satisfies Integer i when i > 10. (B): Correct. The value 5 matches the second case as an Integer but fails the first condition. (C): Correct. The value “Java” matches the String s case. (D): Correct. A null value does not match any case, so the default case executes. [Scenario-Based | Single-Select] Q#19. You are designing a financial application that processes various payment methods (e.g., CreditCard, DebitCard, PayPal). You want to restrict new payment types to prevent unauthorized extensions while ensuring some payment methods can be extended further for specific providers (e.g., Visa, Mastercard for CreditCard). How would you define your hierarchy? A) Use sealed for Payment and final for all subclasses. B) Use sealed for Payment and non-sealed for CreditCard and PayPal. C) Use final for Payment and extend each method separately. D) Use abstract for Payment and leave it open for all subclasses. Answer: B) Use sealed for Payment and non-sealed for CreditCard and PayPal. Explanation: (B): Correct. Declaring Payment as sealed ensures a controlled hierarchy, while non-sealed subclasses like CreditCard allow further extension. (A): Incorrect. Marking all subclasses as final prevents extensibility for CreditCard providers like Visa or Mastercard. (C): Incorrect. Declaring Payment as final disallows any inheritance, which is not suitable here. (D): Incorrect. Using abstract provides no restriction, which defeats the purpose of controlling the hierarchy. [Code-Based | Single-Select] Q#20. What is the possible outputs of the following code snippet while running using Java 17? Object obj = List.of("One","Two", "Three"); switch (obj) { case String s -> System.out.println("String"); case List<?> l -> System.out.println("List"); default -> System.out.println("Other"); } A) String B) List C) Other D) Compile-time error Answer: B) List Explanation: (A): Incorrect. obj is not a String. (B): Correct. obj matches the type List<?>. (C): Incorrect. The default branch is not executed. (D): Incorrect. The syntax is correct. [Code-Based | Single-Select] Q#21. Which of the following URLs will isSameFile() return true for? URL url1 = new URL("https://example.com/page"); URL url2 = new URL("https://example.com/index"); URL url3 = new URL("https://example.com/page"); A) url1 and url2 B) url2 and url3 C) url1 and url3 D) None of the above Answer: C) url1 and url3 Explanation: (C): Correct. isSameFile() checks if URLs point to the same file. Both URLs match exactly. (A), (B): Incorrect. Mismatched URLs will not pass the isSameFile() check. (D): Incorrect as option A is correct. [Concept-Based | Multi-Select] Q#22. What is the benefit(s) of the new case syntax introduced in Java 17? A) It allows multiple case labels to share the same action. B) It eliminates the need for a default clause in a switch statement. C) It improves code readability and reduces redundancy in switch statements. D) It allows exception handling directly within a case. Answer: A) and C) Explanation: (A) is correct because the new syntax allows grouping multiple case labels together, reducing redundancy. (B) is incorrect as the default clause is still optional but not eliminated. (C) is correct since the syntax improves code readability & eliminates code redundancy. (D) is incorrect because it doesn’t allow exception handling within the case in the syntax. [Code-Based | Single-Select] Q#23. What is wrong with the following code snippet? switch (input) { case 1, 2, 3 -> System.out.println("Low range"); case 4, 5 -> System.out.println("Mid range"); case 6 -> { System.out.println("High range"); } case 7, 8, 9 -> throw new IllegalArgumentException("Out of range"); } A) The last case label group cannot throw an exception. B) The default case is missing. C) Mixing single-line and block statements for case labels is not allowed. D) There is nothing wrong with this code. Answer: B Explanation: (B) is correct because the default case is required for non-exhaustive cases. In Java, non-exhaustive cases in a switch statement occur when the provided case labels do not cover all possible values of the selector expression. In this example, cases for values >9 are not covered. (A) is incorrect as throwing exceptions is valid in a case label. (C) is incorrect as mixing single-line and block statements is allowed. (D) is incorrect because a missing default is an issue for exhaustive switches which will result in a compilation error. [Code-Based | Single-Select] Q#24. Which of the following case syntax will not have any compilation issue for multiple labels in a shopping cart discount logic? A) switch (category) { case "Electronics", "Appliances", "Furniture": System.out.println("10% Discount"); case "Clothing": System.out.println("15% Discount"); case "Groceries": System.out.println("5% Discount"); default: System.out.println("No Discount Available"); } B) switch (category) { case "Electronics", "Appliances", "Furniture" { System.out.println("10% Discount"); } case "Clothing", "Groceries" -> System.out.println("15% Discount"); default -> System.out.println("No Discount Available"); } C) switch (category) { case "Electronics", "Appliances", "Furniture" : { System.out.println("10% Discount"); } case "Clothing", "Groceries" -> System.out.println("15% Discount"); default -> System.out.println("No Discount Available"); } D) switch (category) { case {"Electronics", "Appliances", "Furniture"} -> System.out.println("10% Discount"); case "Clothing" -> System.out.println("15% Discount"); case "Groceries" -> System.out.println("5% Discount"); default -> System.out.println("No Discount Available"); } Answer: A Explanation: (A) is correct because it properly uses the case syntax with multiple labels. Although ‘->’ is introduced in place of ‘:’ in Java 17, but ‘:’ still compiles well even with Java 21 compiler. (B) is incorrect as the first case label is missing ‘->’ or ‘:’ before {}. (C) is incorrect because mixing of ‘->’ and ‘:’ is not allowed. We can use either of them in all case labels. (D) is incorrect because it uses {} to close first case label which is not allowed. References: https://docs.oracle.com/en/java/javase/17/language/ ♥ To Practice Java 17 coding Interview, kindly visit Java 17 Coding Interview Questions & Solutions. ♥ You may also attempt MCQs on other topics of Java & other related technologies by visiting our Quizzes & Practice Test section.
[Code-Based | Single-Select] Q#13. Which of the following code snippets correctly demonstrates a record class in the permits clause of a sealed class? A) sealed class Vehicle permits Car, Bike { } record Car(String model) { } record Bike(String type) { } B) sealed class Vehicle permits Car, Bike { } record Car(String model) extends Vehicle { } record Bike(String type) { } C) sealed class Vehicle permits Car, Bike { } record Car(String model) extends Vehicle { } record Bike(String type) extends Vehicle { } D) sealed class Vehicle { } record Car(String model) extends Vehicle { } record Bike(String type) { } Answer: C Explanation: Option C is correct because both Car and Bike are record classes explicitly listed in the permits clause of the sealed class Vehicle and extend it as required. Option A is incorrect because record classes must explicitly extend the sealed class if they are mentioned in the permits clause. Option B is incorrect because the Bike class does not extend Vehicle. Option D is incorrect because the permits clause is missing from the sealed class declaration. [Scenario-Based | Single-Select] Q#14. A project involves creating a sealed hierarchy for geographic locations. The Region class is sealed, allowing City and Country as its permitted subclasses. The team proposes: sealed class Region permits City, Country {} final class City extends Region {} non-sealed class Country extends Region {} Later, the team attempts to declare the following: final class Continent extends Country {} What will happen during compilation? A) Compilation succeeds as Country is non-sealed. B) Compilation fails because Country is a subclass of Region. C) Compilation fails because Continent must be listed in Region’s permits D) Compilation fails because sealed classes cannot have multiple levels of inheritance. Answer: A) Compilation succeeds as Country is non-sealed. Explanation: (A): Correct. The non-sealed modifier on Country allows further subclassing without restrictions. (B): Incorrect. The non-sealed nature of Country makes this permissible. (C): Incorrect. Only subclasses of sealed classes must be explicitly listed in permits; Country being non-sealed does not have this restriction. (D): Incorrect. There is no rule preventing multiple levels of inheritance in a sealed hierarchy. [Code-Based | Single-Select] Q#15. What will the following code snippet print while running using Java 17? Object obj = "Java 17"; switch (obj) { case String s -> System.out.println(s.repeat(3)); case Integer i -> System.out.println(i * 3); default -> System.out.println("Unknown"); } A) Java 17 Java 17 Java 17 B) Java 17Java 17Java 17 C) Unknown D) Compile-time error Answer: B) Java 17Java 17Java 17 Explanation: (B): Correct. The type String matches obj, and s.repeat(3) repeats the string thrice. (A): Incorrect. The repeat method does not add spaces by default. (C): Incorrect. The default branch is not executed since obj matches String. (D): Incorrect. The syntax is valid and matches Java 17 features. [Concept-Based | Multi-Select] Q#16. Which of the following statements are true about type patterns in switch statements? A) They allow matching on the runtime type of objects. B) They eliminate the need for explicit type casting. C) They require the instanceof D) They are supported from Java 17 onwards. Answer: A), B), and D) Explanation: (A): Correct. Type patterns check the runtime type of the object in the switch. (B): Correct. Matched objects are automatically cast to the specified type. (C): Incorrect. The switch statement replaces the instanceof operator in this context. (D): Correct. Type patterns in switch are a Java 17 feature. [Code-Based | Single-Select] Q#17. What happens when this code is executed in Java 17? import java.util.HexFormat; public class HexFormatTest{ public static void main(String[] args) { HexFormat hex = HexFormat.of(); System.out.println(hex.formatHex(new byte[] {10, 20})); } } A) Prints 0a14 B) Prints 1014 C) Compile-time error D) Prints 1020 Answer: A) Prints 0a14 Explanation: (A): Correct. HexFormat.formatHex() formats the bytes as hexadecimal values (0a for 10, 14 for 20). (B): Incorrect. Incorrect Hexadecimal values. (C): Incorrect. No syntax issue exists. (D): Incorrect. Incorrect Hexadecimal values. [Code-Based | Multi-Select] Q#18. Consider the following code snippet: Object value = 10; String result = switch (value) { case Integer i when i >= 10 -> "Large Integer"; case Integer i -> "Small Integer"; case String s -> "String: " + s; default -> "Unknown"; }; Which of the following are correct outputs of the given code while running in Java 21? A) “Large Integer” if value is 24. B) “Small Integer” if value is 5. C) “String: Java” if value is “Java”. D) “Unknown” if value is null. Answer: A), B), C), and D) Explanation: (A): Correct. The value 24 matches the first case as it satisfies Integer i when i > 10. (B): Correct. The value 5 matches the second case as an Integer but fails the first condition. (C): Correct. The value “Java” matches the String s case. (D): Correct. A null value does not match any case, so the default case executes. [Scenario-Based | Single-Select] Q#19. You are designing a financial application that processes various payment methods (e.g., CreditCard, DebitCard, PayPal). You want to restrict new payment types to prevent unauthorized extensions while ensuring some payment methods can be extended further for specific providers (e.g., Visa, Mastercard for CreditCard). How would you define your hierarchy? A) Use sealed for Payment and final for all subclasses. B) Use sealed for Payment and non-sealed for CreditCard and PayPal. C) Use final for Payment and extend each method separately. D) Use abstract for Payment and leave it open for all subclasses. Answer: B) Use sealed for Payment and non-sealed for CreditCard and PayPal. Explanation: (B): Correct. Declaring Payment as sealed ensures a controlled hierarchy, while non-sealed subclasses like CreditCard allow further extension. (A): Incorrect. Marking all subclasses as final prevents extensibility for CreditCard providers like Visa or Mastercard. (C): Incorrect. Declaring Payment as final disallows any inheritance, which is not suitable here. (D): Incorrect. Using abstract provides no restriction, which defeats the purpose of controlling the hierarchy. [Code-Based | Single-Select] Q#20. What is the possible outputs of the following code snippet while running using Java 17? Object obj = List.of("One","Two", "Three"); switch (obj) { case String s -> System.out.println("String"); case List<?> l -> System.out.println("List"); default -> System.out.println("Other"); } A) String B) List C) Other D) Compile-time error Answer: B) List Explanation: (A): Incorrect. obj is not a String. (B): Correct. obj matches the type List<?>. (C): Incorrect. The default branch is not executed. (D): Incorrect. The syntax is correct. [Code-Based | Single-Select] Q#21. Which of the following URLs will isSameFile() return true for? URL url1 = new URL("https://example.com/page"); URL url2 = new URL("https://example.com/index"); URL url3 = new URL("https://example.com/page"); A) url1 and url2 B) url2 and url3 C) url1 and url3 D) None of the above Answer: C) url1 and url3 Explanation: (C): Correct. isSameFile() checks if URLs point to the same file. Both URLs match exactly. (A), (B): Incorrect. Mismatched URLs will not pass the isSameFile() check. (D): Incorrect as option A is correct. [Concept-Based | Multi-Select] Q#22. What is the benefit(s) of the new case syntax introduced in Java 17? A) It allows multiple case labels to share the same action. B) It eliminates the need for a default clause in a switch statement. C) It improves code readability and reduces redundancy in switch statements. D) It allows exception handling directly within a case. Answer: A) and C) Explanation: (A) is correct because the new syntax allows grouping multiple case labels together, reducing redundancy. (B) is incorrect as the default clause is still optional but not eliminated. (C) is correct since the syntax improves code readability & eliminates code redundancy. (D) is incorrect because it doesn’t allow exception handling within the case in the syntax. [Code-Based | Single-Select] Q#23. What is wrong with the following code snippet? switch (input) { case 1, 2, 3 -> System.out.println("Low range"); case 4, 5 -> System.out.println("Mid range"); case 6 -> { System.out.println("High range"); } case 7, 8, 9 -> throw new IllegalArgumentException("Out of range"); } A) The last case label group cannot throw an exception. B) The default case is missing. C) Mixing single-line and block statements for case labels is not allowed. D) There is nothing wrong with this code. Answer: B Explanation: (B) is correct because the default case is required for non-exhaustive cases. In Java, non-exhaustive cases in a switch statement occur when the provided case labels do not cover all possible values of the selector expression. In this example, cases for values >9 are not covered. (A) is incorrect as throwing exceptions is valid in a case label. (C) is incorrect as mixing single-line and block statements is allowed. (D) is incorrect because a missing default is an issue for exhaustive switches which will result in a compilation error. [Code-Based | Single-Select] Q#24. Which of the following case syntax will not have any compilation issue for multiple labels in a shopping cart discount logic? A) switch (category) { case "Electronics", "Appliances", "Furniture": System.out.println("10% Discount"); case "Clothing": System.out.println("15% Discount"); case "Groceries": System.out.println("5% Discount"); default: System.out.println("No Discount Available"); } B) switch (category) { case "Electronics", "Appliances", "Furniture" { System.out.println("10% Discount"); } case "Clothing", "Groceries" -> System.out.println("15% Discount"); default -> System.out.println("No Discount Available"); } C) switch (category) { case "Electronics", "Appliances", "Furniture" : { System.out.println("10% Discount"); } case "Clothing", "Groceries" -> System.out.println("15% Discount"); default -> System.out.println("No Discount Available"); } D) switch (category) { case {"Electronics", "Appliances", "Furniture"} -> System.out.println("10% Discount"); case "Clothing" -> System.out.println("15% Discount"); case "Groceries" -> System.out.println("5% Discount"); default -> System.out.println("No Discount Available"); } Answer: A Explanation: (A) is correct because it properly uses the case syntax with multiple labels. Although ‘->’ is introduced in place of ‘:’ in Java 17, but ‘:’ still compiles well even with Java 21 compiler. (B) is incorrect as the first case label is missing ‘->’ or ‘:’ before {}. (C) is incorrect because mixing of ‘->’ and ‘:’ is not allowed. We can use either of them in all case labels. (D) is incorrect because it uses {} to close first case label which is not allowed. References: https://docs.oracle.com/en/java/javase/17/language/ ♥ To Practice Java 17 coding Interview, kindly visit Java 17 Coding Interview Questions & Solutions. ♥ You may also attempt MCQs on other topics of Java & other related technologies by visiting our Quizzes & Practice Test section.