Java Quizz Dive into our tech quiz zone and put your technical skills to the test! Our quizzes cover a wide array of technical topics, perfect for sharpening your knowledge and challenging your understanding. Compete with others, see your rankings, and boost your technical proficiency. Start quizzing today! 1 / 56 1. Consider the following program: public class StrEqual { public static void main(String[] args) { String s1 = "hello"; String s2 = new String("hello"); String s3 = "hello"; if (s1 == s2) { System.out.println("s1 and s2 equal"); } else { System.out.println("s1 and s2 not equal"); } if (s1 == s3) { System.out.println("s1 and s3 equal"); } else { System.out.println("s1 and s3 not equal"); } } } Which one of the following options provides the output of this program when executed? s1 and s2 not equal s1 and s3 equal s1 and s2 not equal s1 and s3 not equal s1 and s2 equal s1 and s3 equal s1 and s2 equal s1 and s3 not equal 2 / 56 2. Which of the following state(s) is/are NOT legitimate thread state(s)? (Select all that apply.) WAITING TERMINATED NEW RUNNABLE EXECUTING 3 / 56 3. Which of the following is correct about Statement class of JDBC? Both of the above. none of the above. Statement encapsulates an SQL statement which is passed to the database to be planned and executed. Statement encapsulates an SQL statement which is passed to the database to be parsed and compiled. 4 / 56 4. What will be the output of the below statements? public class Test { public static void main(String[] args) { String s1 = null; System.out.println(s1); //line 2 System.out.println(s1.toString()); //line 3 } } None null NullPointerException NullPointerException NullPointerException null null 5 / 56 5. Consider the following program and predict the output: class MyThread extends Thread { public MyThread(String name) { this.setName(name); start(); System.out.println("in constructor " + getName()); } @Override public void start() { System.out.println("in start " + getName()); } public void run() { System.out.println("in run " + getName()); } } public class ThreadTest { public static void main(String[] args) { new MyThread("oops"); } } in start oops in constructor oops in run oops in start oops in run oops in constructor oops in constructor oops in start oops in run oops in start oops in constructor oops 6 / 56 6. Lambda expressions can be used with the Java Collections API primarily in ... Sorting Filtering Iterating All of the above 7 / 56 7. Which method is used to transform each element of a Stream using a provided function? reduce map filter flatMap 8 / 56 8. Consider the following program and predict the output: class MyThread extends Thread { @Override public void run() { System.out.println("In run method; thread name is: " + Thread.currentThread().getName()); } } public class ThreadTest { public static void main(String args[]) { Thread myThread = new MyThread(); myThread.run(); // #1 System.out.println("In main method; thread name is: " + Thread.currentThread().getName()); } } The program results in a compiler error at statement #1. The program results in a runtime exception. The program prints: In the run method; the thread name is: thread-0 In the main method; the thread name is: main The program prints the following: In run method; thread name is: main In main method; thread name is: main 9 / 56 9. Consider the following program and choose the right option: class MyThread extends Thread { public void run() { System.out.println("Running"); } } public class ThreadTest { public static void main(String args[]) throws InterruptedException { Runnable r = new MyThread(); // #1 Thread myThread = new Thread(r); // #2 myThread.start(); } } The program will result in a compilation error at statement #1. The program will compile with no errors but does not print any output in the console. The program will result in a compilation error at statement #2. The program will compile with no errors and will print “Running” in the console. 10 / 56 10. You cannot use ... inside lambda expressions. break and continue return statement this keyword arithmetic operations 11 / 56 11. What is the output of the following program? public class Question { T t; public static void main(String[] args) { Question q = new Question(); // 1 q.t = new Float(1); // 2 System.out.println(q.t); } } An exception occurs at runtime 1.0 Compilation fails on the line marked as // 1 Compilation fails on the line marked as // 2 12 / 56 12. In which of the following type of ResultSet, the cursor can only move forward in the result set? ResultSet.TYPE_SCROLL_SENSITIVE None of the above. ResultSet.TYPE_FORWARD_ONLY ResultSet.TYPE_SCROLL_INSENSITIVE 13 / 56 13. What is the main purpose of the Stream.sorted method? To remove duplicate elements from the stream To reverse the order of elements in the stream To randomly shuffle the elements of the stream To sort the elements of the stream in their natural order or by a provided comparator 14 / 56 14. Which of the following is correct about setFetchSize(int)? None of the above. Both of the above. setFetchSize(int) affects how the database returns the ResultSet data. setFetchSize(int) defines the number of rows that will be read from the database when the ResultSet needs more rows. 15 / 56 15. What is the main advantage of multithreading in Java? Reduced memory usage. Simplified program structure. Improved program performance. Elimination of runtime errors. 16 / 56 16. What is the purpose of the Stream.flatMap method? To merge multiple streams into one To filter elements based on a flat structure To map each element to a new value To replace each element with a stream and then flatten the streams into a single stream 17 / 56 17. Which method is used to start the execution of a thread? start() execute() begin() run() 18 / 56 18. Which of the following correctly declares an array of integers in Java? int arr[]; All of the above int arr = new int[10]; int[] arr; 19 / 56 19. What is the output of the following code? int[] arr = {1, 2, 3, 4, 5}; System.out.println(arr[2]); 3 2 4 1 20 / 56 20. Consider this code segment? DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE", Locale.US); System.out.println(formatter.format(LocalDateTime.now())); Which of the following outputs matches the string pattern "EEEE" given in this code segment? Friday Sept September F 21 / 56 21. Which of the following keywords is used to manually throw an exception? catch try throws throw 22 / 56 22. Which method is used to start a thread in Java? run() start() init() execute() 23 / 56 23. How can synchronization be achieved in Java threads? Using the synchronized keyword. Using the static keyword. Using the volatile keyword. Using the final keyword. 24 / 56 24. Which of the following is a checked exception? ArrayIndexOutOfBoundsException ArithmeticException NullPointerException IOException 25 / 56 25. Select all the classes that extend the String class StringBuffer StringBuilder None StringWriter 26 / 56 26. What is the default priority of a thread in Java? 10 1 5 0 27 / 56 27. Consider the following program and choose the correct answer: class MyThread extends Thread { public MyThread(String name) { this.setName(name); } @Override public void run() { try { sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } play(); } private void play() { System.out.print(getName()); System.out.print(getName()); } } public class ThreadTest { public static void main(String args[]) throws InterruptedException { Thread tableThread = new MyThread("Table"); Thread tennisThread = new MyThread("Tennis"); tableThread.start(); tennisThread.start(); } } This program will always print the following: Table Table Tennis Tennis. This program will always print the following: Tennis Tennis Table Table. The output of this program cannot be predicted; it depends on thread scheduling. This program will throw an IllegalStateException. 28 / 56 28. What is the output of the following program? public class Question { public static void main(String[] args) { Question q = new Question(); List l = new ArrayList(); l.add(20); l.add(30); q.m1(l); } private void m1(List l) { m2(l); // 1 } private void m2(List l) { l.set(1, l.get(0)); // 2 System.out.println(l); } } Compilation fails on the line marked as // 2 Compilation fails on the line marked as // 1 [20, 20] An exception occurs at runtime 29 / 56 29. What is the purpose of the join() method in Java threads? To schedule the execution of a thread. To wait for a thread to finish its execution. To notify a thread to stop execution. To interrupt the execution of a thread. 30 / 56 30. Which method is used to transform each element of a Stream using a provided function? reduce map filter flatMap 31 / 56 31. Which of these collectors is used for grouping elements of a Stream? Collectors.toSet() Collectors.groupingBy() Collectors.joining() Collectors.toList() 32 / 56 32. What is the maximum number of threads that can be created in a Java program? There is no fixed limit. It depends on the amount of available memory. It is limited to 100 threads. It depends on the number of CPU cores. 33 / 56 33. Consider the following program and predict the output: public class Test { public static void main(String[] args) { String s = new String("5"); System.out.println(1 + 10 + s + 1 + 10); } } 27 115110 1105110 11511 34 / 56 34. Given List list = new ArrayList(); // 1 list.add(new Integer(2)); // 2 list.add(new Object()); // 3 Which line will generate a compile-time error? Line marked as // 3 Line marked as // 1 Line marked as // 2 No compile-time error is generated 35 / 56 35. Which of the following is correct about PreparedStatement? Prepared statements are more secure because they use bind variables, which can prevent SQL injection attack. Both of the above. PreparedStatement allows mapping different requests with same prepared statement but different arguments to execute the same execution plan. None of the above. 36 / 56 36. Which of the following are valid ChronoField values for LocalDate? HOUR_OF_DAY DAY_OF_MONTH DAY_OF_WEEK Both A & C MILLI_OF_SECOND 37 / 56 37. Consider the following program and predict the output: class MyThread implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName()); } } public class ThreadTest { public static void main(String arg[]) { Thread thread = new Thread(new MyThread()); thread.run(); thread.run(); thread.start(); } } main main Thread-0 Thread-0 main Thread-1 Thread-0 Thread-1 Thread-2 main Thread-0 Thread-1 38 / 56 38. What type of operation is Stream.filter? Terminal Intermediate Mutable Source 39 / 56 39. What is the purpose of the wait() method in Java threads? To notify other threads to resume execution. To release the lock held by the thread. To terminate a thread. To pause the execution of a thread. 40 / 56 40. Which of the following type of JDBC driver, uses database native protocol? Native-protocol, pure Java driver JDBC-ODBC Bridge plus ODBC driver JDBC-Net, pure Java driver Native-API, partly Java driver 41 / 56 41. What is the result of applying the Stream.reduce operation? An Optional describing the reduced value, if any A boolean indicating if any stream elements match a given predicate A list of elements that were reduced A new stream with elements reduced according to a provided accumulator function 42 / 56 42. Which of these is a terminal operation in the Stream API? forEach flatMap filter map 43 / 56 43. Which of the following is a valid lambda expression? All of the above x -> x*x (x, y) -> return x + y; (int x, int y) -> x + y 44 / 56 44. Which method is used to compare two strings, ignoring case considerations? compareTo() equalsIgnoreCase() compareToIgnoreCase() equals() 45 / 56 45. What does the Stream.peek method do? Consumes and removes an element from the stream Performs an action on each element of the stream without modifying it Filters elements based on a predicate Reduces the stream to a single summary element 46 / 56 46. What is the output of the following code? try { int a = 10 / 0; } catch (ArithmeticException e) { System.out.println("Exception caught"); } finally { System.out.println("Finally block"); } Finally block Exception caught Exception caught Finally block 47 / 56 47. Which one of the following options correctly describes the behavior of this program? When executed, the program prints “Worker ”, and after that the program hangs (i.e., does not terminate). When executed, the program prints “Worker ” and then terminates. When executed, the program throws an IllegalMonitorStateException. The program does not compile and fails with multiple compiler errors. When executed, the program prints the following: “Worker Master ”. 48 / 56 48. Which of the following are valid ChronoUnit values for LocalTime? Both B & D NANOS DAY HALF_DAYS YEAR 49 / 56 49. Which of the following methods can be used to make a thread pause execution for a specific amount of time? sleep() pause() yield() wait() 50 / 56 50. Consider the following program and predict its output: public class Test { public static void main(String[] args) { String str = null; switch (str) { // #1 case "null": System.out.println("null string"); // #2 break; } } } This program prints the following: null string. This program results in throwing a NullPointerException. This program results in a compiler error in statement #2. This program results in a compiler error in statement #1. 51 / 56 51. What will be the output of the below statements? public class Test { public static void main(String[] args) { String s1 = "abc"; StringBuffer s2 = new StringBuffer(s1); System.out.println(s1.equals(s2)); } } ClassCastException at runtime Compile-time error true false 52 / 56 52. What will be the output of the following code? String str = "Hello"; str.concat(" World"); System.out.println(str); Compilation Error Hello World World Hello 53 / 56 53. Which of the following statements correctly describes the behavior of this program? This program will throw a NullPointerException. This program will result in a compiler error. This program will print null in the console. This program will print 10 in the console. 54 / 56 54. Which one of the following classes is best suited for storing timestamp values of application events in a file? java.time.Instant class java.time.ZoneId class java.time.Period class java.time.ZoneOffset class java.time.Duration class 55 / 56 55. What does the following lambda expression represent? () -> {} A lambda that throws an exception A lambda that does nothing and returns void None of the above A lambda that accepts two parameters and does nothing 56 / 56 56. Which of the following statements are true? Lambda expressions don't return values Curly brackets are required whenever the return keyword is used in a lambda expression A return keyword is always required in a lambda expression A return keyword is always optional in a lambda expression Your score is 0%