Java Programming Test 2

    Question 1JAVA - Type Casting

    What will happen in the following code?

    double d = 9.7; int x = (int) d; System.out.println(x);

    Question 2JAVA - Method Overloading

    Which statement about method overloading is true?

    Question 3JAVA - String Concatenation

    What will this code print?

    String s1 = "Hello"; String s2 = "World"; String s3 = s1 + s2; System.out.println(s3);

    Question 4JAVA - Array Length

    What will this code print?

    int arr[] = {10, 20, 30, 40}; System.out.println(arr.length);

    Question 5JAVA - Finally Block

    What will be the output?

    try { System.out.println("Try"); } finally { System.out.println("Finally"); }

    Question 6JAVA - Set Characteristics

    Which statement about `Set` is true?

    Question 7JAVA - Generic Method

    Which syntax is correct for a generic method?

    Question 8JAVA - Functional Interface

    Which annotation is used to mark a functional interface in Java?

    Question 9JAVA - Filter 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, 5); nums.stream().filter(n -> n % 2 == 0).forEach(System.out::print); } }

    Question 10JAVA - Runnable Example

    What will this code print?

    class MyTask implements Runnable { public void run() { System.out.println("Task executed"); } } public class Test { public static void main(String[] args) { Thread t = new Thread(new MyTask()); t.start(); } }

    Question 11JAVA - if-else Statement

    What will be the output of the code?

    int a = 5; if(a > 10) { System.out.println("Big"); } else { System.out.println("Small"); }

    Question 12JAVA - Method Overloading Example

    What will the following code print?

    class Test { void show(int x) { System.out.println("int: " + x); } void show(String s) { System.out.println("String: " + s); } } Test t = new Test(); t.show("Java");

    Question 13JAVA - String equals() vs ==

    What will be the output?

    String s1 = "Java"; String s2 = new String("Java"); System.out.println(s1 == s2); System.out.println(s1.equals(s2));

    Question 14JAVA - Index Out of Bounds

    What will happen in the code?

    int arr[] = new int[3]; System.out.println(arr[3]);

    Question 15JAVA - Multiple Catch Blocks

    Which catch block order is correct?

    Question 16JAVA - Map Basics

    Which statement is correct about `Map`?

    Question 17JAVA - Type Safety Example

    What will happen in this code?

    List list = new ArrayList(); list.add("Hello"); list.add(10); String s = (String) list.get(1); System.out.println(s);

    Question 18JAVA - Lambda Example

    What will this code print?

    interface Say { void speak(String msg); } public class Test { public static void main(String[] args) { Say s = (msg) -> System.out.println("Hello " + msg); s.speak("Java"); } }

    Question 19JAVA - Map Operation

    What will this code print?

    import java.util.*; public class Test { public static void main(String[] args) { List<String> words = Arrays.asList("a", "bb", "ccc"); words.stream().map(String::length).forEach(System.out::print); } }

    Question 20JAVA - Sleep Method

    What does Thread.sleep(1000) do?