Java Programming Test 3

    Question 1JAVA - Switch Statement

    What will this code print?

    int day = 2; switch(day) { case 1: System.out.println("Mon"); break; case 2: System.out.println("Tue"); break; default: System.out.println("Other"); }

    Question 2JAVA - Method Overriding

    Which of the following is true about method overriding?

    Question 3JAVA - String Length

    What will be printed?

    String str = "Java Programming"; System.out.println(str.length());

    Question 4JAVA - Enhanced For Loop

    What will be the output?

    int[] nums = {1, 2, 3}; for(int n : nums) { System.out.print(n + " "); }

    Question 5JAVA - Throw vs Throws

    Which statement is correct?

    Question 6JAVA - ArrayList Example

    What will this code print?

    import java.util.*; class Test { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("A"); System.out.println(list); } }

    Question 7JAVA - Wildcard ?

    What does the wildcard ? represent in generics?

    Question 8JAVA - Lambda with Thread

    What will happen?

    public class Test { public static void main(String[] args) { Thread t = new Thread(() -> System.out.println("Thread running")); t.start(); } }

    Question 9JAVA - Reduce Operation

    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); int sum = nums.stream().reduce(0, (a, b) -> a + b); System.out.println(sum); } }

    Question 10JAVA - Synchronization

    Why is synchronization used in multithreading?

    Question 11JAVA - While Loop

    How many times will this loop run?

    int i = 1; while(i < 4) { System.out.println(i); i++; }

    Question 12JAVA - Static Method

    Which statement is correct about static methods?

    Question 13JAVA - Substring Method

    What will this code print?

    String str = "Interview"; System.out.println(str.substring(0, 5));

    Question 14JAVA - Multi-Dimensional Array

    What will this code print?

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

    Question 15JAVA - Exception Propagation

    What will be the output?

    class Test { void m() { int x = 10/0; } void n() { m(); } void p() { try { n(); } catch(Exception e) { System.out.println("Handled"); } } } new Test().p();

    Question 16JAVA - HashSet Example

    What will this code print?

    import java.util.*; class Test { public static void main(String[] args) { Set<Integer> set = new HashSet<>(); set.add(1); set.add(2); set.add(1); System.out.println(set); } }

    Question 17JAVA - Upper Bounded Wildcard

    Which declaration is correct for a list of numbers (Integer, Double, etc.)?

    Question 18JAVA - ForEach with Lambda

    What will this code print?

    import java.util.*; public class Test { public static void main(String[] args) { List<String> list = Arrays.asList("A", "B", "C"); list.forEach(s -> System.out.print(s + " ")); } }

    Question 19JAVA - Collectors

    Which method collects stream results into a list?

    Question 20JAVA - Deadlock Example

    What is a deadlock?