Java Programming Test 4

    Question 1JAVA - For Loop Example

    What will be the output?

    for(int i = 0; i < 5; i += 2) { System.out.print(i + " "); }

    Question 2JAVA - Recursion Example

    What will this code print?

    int fact(int n) { if(n == 1) return 1; return n * fact(n - 1); } System.out.println(fact(4));

    Question 3JAVA - StringBuilder vs StringBuffer

    Which statement is correct?

    Question 4JAVA - Array Reference

    What will this code print?

    int[] a = {1, 2, 3}; int[] b = a; b[0] = 99; System.out.println(a[0]);

    Question 5JAVA - Custom Exception

    Which is correct about creating a custom exception?

    Question 6JAVA - HashMap Example

    What will this code print?

    import java.util.*; class Test { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "Java"); map.put(2, "Python"); map.put(1, "C++"); System.out.println(map); } }

    Question 7JAVA - Lower Bounded Wildcard

    Which syntax is used for a generic type that can accept Integer or its supertypes?

    Question 8JAVA - Method Reference

    Which is a valid method reference in Java?

    Question 9JAVA - Stream Count

    What will this code print?

    import java.util.*; public class Test { public static void main(String[] args) { List<String> names = Arrays.asList("John", "Jane", "Jack"); long count = names.stream().filter(s -> s.startsWith("J")).count(); System.out.println(count); } }

    Question 10JAVA - Executor Framework

    Which class provides factory methods for creating thread pools?

    Question 11JAVA - Do While Loop

    What will be the output?

    int n = 5; do { System.out.println(n); n++; } while(n < 5);

    Question 12JAVA - Varargs Method

    What will the following code print?

    void display(int... nums) { System.out.println(nums.length); } display(10, 20, 30);

    Question 13JAVA - String Reverse Example

    What will this code print?

    StringBuilder sb = new StringBuilder("Java"); System.out.println(sb.reverse());

    Question 14JAVA - Array Sorting

    Which class provides a built-in method to sort arrays in Java?

    Question 15JAVA - Try with Multiple Resources

    Which Java version introduced try-with-resources for automatic resource management?

    Question 16JAVA - Iterator

    Which method of `Iterator` is used to remove an element during iteration?

    Question 17JAVA - Generic Example Code

    What will this code print?

    class Box<T> { T value; Box(T value) { this.value = value; } T get() { return value; } } public class Test { public static void main(String[] args) { Box<String> b = new Box<>("Java"); System.out.println(b.get()); } }

    Question 18JAVA - Scope in Lambda

    Which statement about lambda expressions is true?

    Question 19JAVA - Parallel Stream

    Which method is used to create a parallel stream from a list?

    Question 20JAVA - Callable vs Runnable

    Which is true about Callable vs Runnable?