Java Programming Test 5

    Question 1JAVA - Break Statement

    What will be the output?

    for(int i = 1; i <= 5; i++) { if(i == 3) break; System.out.print(i + " "); }

    Question 2JAVA - Main Method Signature

    Which is the correct main method declaration in Java?

    Question 3JAVA - String Interning

    What does the intern() method of String do?

    Question 4JAVA - Array Copy

    Which method is used for copying array elements efficiently?

    Question 5JAVA - Exception Hierarchy

    Which is the top-most superclass of all exceptions in Java?

    Question 6JAVA - Fail-Fast Iterator

    Which collections use fail-fast iterators?

    Question 7JAVA - Type Erasure

    How does Java implement generics internally?

    Question 8JAVA - Stream with Lambda

    What will this code print?

    import java.util.*; public class Test { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4); nums.stream().filter(n -> n % 2 == 0).forEach(System.out::print); } }

    Question 9JAVA - Distinct & Sorted

    What will this code print?

    import java.util.*; public class Test { public static void main(String[] args) { List<Integer> nums = Arrays.asList(5, 1, 2, 2, 3); nums.stream().distinct().sorted().forEach(System.out::print); } }

    Question 10JAVA - Volatile Keyword

    What does the volatile keyword ensure in Java?

    Question 11JAVA - Continue Statement

    What will be printed?

    for(int i = 1; i <= 4; i++) { if(i == 2) continue; System.out.print(i + " "); }

    Question 12JAVA - Pass by Value

    What will be the output?

    void change(int x) { x = 50; } int a = 10; change(a); System.out.println(a);

    Question 13JAVA - String isEmpty() vs isBlank()

    Which is true about isEmpty() vs isBlank()?

    Question 14JAVA - Array to String

    What will this code print?

    int[] arr = {1, 2, 3}; System.out.println(arr);

    Question 15JAVA - Catch Without Exception

    What will happen?

    try { System.out.println("Hello"); } catch(Exception e) { System.out.println("Catch"); } finally { System.out.println("Finally"); }

    Question 16JAVA - Queue Interface

    Which method of `Queue` retrieves and removes the head of the queue, or returns `null` if empty?

    Question 17JAVA - Multiple Type Parameters

    Which of the following correctly declares a generic class with two type parameters?

    Question 18JAVA - Comparator with Lambda

    Which lambda correctly sorts a list of strings by length?

    Question 19JAVA - AnyMatch Example

    What will this code return?

    import java.util.*; public class Test { public static void main(String[] args) { List<String> list = Arrays.asList("apple", "banana", "cherry"); boolean result = list.stream().anyMatch(s -> s.contains("a")); System.out.println(result); } }

    Question 20JAVA - ReentrantLock Example

    Which advantage does ReentrantLock have over synchronized?