1. What is a thread in Java? Answer: A thread in Java is the smallest unit of execution within a process. It allows concurrent execution of tasks, providing a way to improve program performance. 2. Explain the difference between the Thread class and the Runnable interface. Answer: The Thread class is a class in Java that directly extends the Thread class, while the Runnable interface is an interface that can be implemented by a class to define a thread. Using Runnable is generally preferred as it allows for better object-oriented design and avoids the single inheritance limitation. 3. How can you create a thread in Java? Provide examples. Answer: Threads can be created by extending the Thread class or implementing the Runnable interface. java // Extending Thread class class MyThread extends Thread { public void run () { // Code to be executed in the new thread } } // Implementing Runnable interface class MyRunnable implements Runnable { ...
To find the second-highest salary from a list of employees using Java 8 streams, you can follow these steps: Create a list of employees with their salaries. Use Java 8 streams to sort the employees by salary in descending order. Skip the first element (which is the employee with the highest salary). Get the first element of the remaining stream (which is the employee with the second-highest salary). Example code: java import java.util.ArrayList; import java.util.List; class Employee { private String name; private double salary; public Employee (String name, double salary) { this .name = name; this .salary = salary; } public double getSalary () { return salary; } } public class SecondHighestSalary { public static void main (String[] args) { List<Employee> employees = new ArrayList <>(); employees.add( new Employee ( "John" , 60000.0 )); employees.add( new Employe...