Java Technology

Java Technology Like & Share this page For Top most interview Question of java.........................

06/03/2026

Write a program to find 9th higest salary.

SELECT empSal
FROM (
SELECT empSal,
DENSE_RANK() OVER (ORDER BY empSal DESC) AS rnk
FROM employee
)
WHERE rnk = 9;

06/03/2026

package java8;

import java.util.Arrays;
import java.util.Comparator;

public class ArraysOfString {

public static void main(String[] args) {
String[] arr = {"aa", "a", "bb", "ccc"};
Arrays.stream(arr)
.sorted(Comparator.comparing(String::length))
.forEach(System.out::println);

}
}

o/p : a aa bb ccc

06/03/2026

Wap to check string is anagram?
package java8;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class AnagramLambda {


public static void main(String[] args) {

String s1 = "geeks";
String s2 = "kseeg";

Map map1 = s1.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

Map map2 = s2.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));


System.out.println(map1.equals(map2) ? "Anagram" : "Not Anagram");



boolean isAnagram =
Arrays.equals(
s1.chars().sorted().toArray(),
s2.chars().sorted().toArray()
);

System.out.println(isAnagram ? "Anagram" : "Not Anagram");


}
}

27/09/2025

Sql Query
========================================

1. Write a query to find the second highest salary in the employees table.
SELECT MAX(SALARY)
FROM HR.EMPLOYEES
WHERE SALARY NOT IN (SELECT MAX(SALARY) FROM HR.EMPLOYEES);
2. Write a query to find the second highest salary using a < condition.
SELECT MAX(SALARY)
FROM HR.EMPLOYEES
WHERE SALARY < (SELECT MAX(SALARY) FROM HR.EMPLOYEES);
________________________________________
Aggregate Functions by Department
3. Write a query to display the maximum salary in each department.
SELECT MAX(SALARY), DEPARTMENT_ID
FROM HR.EMPLOYEES
GROUP BY DEPARTMENT_ID;
4. Write a query to display the minimum salary in each department.
SELECT MIN(SALARY), DEPARTMENT_ID
FROM HR.EMPLOYEES
GROUP BY DEPARTMENT_ID;
5. Write a query to display the number of employees in each department.
SELECT COUNT(*), DEPARTMENT_ID
FROM HR.EMPLOYEES
GROUP BY DEPARTMENT_ID;
________________________________________
Odd and Even Rows
6. Write a query to display all employees with odd-numbered rows using ROWNUM.
SELECT *
FROM (
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, ROWNUM RN
FROM HR.EMPLOYEES ORDER BY RN
)
WHERE MOD(RN,2) != 0;
7. Write a query to display all employees with even-numbered rows using ROWNUM.
SELECT *
FROM (
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, ROWNUM RN
FROM HR.EMPLOYEES ORDER BY RN
)
WHERE MOD(RN,2) = 0;
________________________________________
Grouping by Names
8. Write a query to display the count of each first name, ordered by frequency.
SELECT FIRST_NAME, COUNT(*)
FROM HR.EMPLOYEES
GROUP BY FIRST_NAME
ORDER BY COUNT(*);
9. Write a query to display only those first names that occur more than once.
SELECT FIRST_NAME, COUNT(*)
FROM HR.EMPLOYEES
GROUP BY FIRST_NAME
HAVING COUNT(*) > 1;
________________________________________
Pattern Matching with LIKE
10. Display the first names that start with ‘D’.
SELECT FIRST_NAME FROM HR.EMPLOYEES WHERE FIRST_NAME LIKE 'D%';
11. Display the first names that end with ‘n’.
SELECT FIRST_NAME FROM HR.EMPLOYEES WHERE FIRST_NAME LIKE '%n';
12. Display the first names that contain the letter ‘r’.
SELECT FIRST_NAME FROM HR.EMPLOYEES WHERE FIRST_NAME LIKE '%r%';
13. Display the first names that do not contain the letter ‘r’.
SELECT FIRST_NAME FROM HR.EMPLOYEES WHERE FIRST_NAME NOT LIKE '%r%';
14. Display the first names that are exactly 4 characters long.
SELECT FIRST_NAME FROM HR.EMPLOYEES WHERE FIRST_NAME LIKE '____';
15. Display the first names where the second character is ‘o’.
SELECT FIRST_NAME FROM HR.EMPLOYEES WHERE FIRST_NAME LIKE '_o%';
16. Display the first names where the fourth character is ‘m’.
SELECT FIRST_NAME FROM HR.EMPLOYEES WHERE FIRST_NAME LIKE '___m%';
17. Display the first names that contain the substring ‘ll’.
SELECT FIRST_NAME FROM HR.EMPLOYEES WHERE FIRST_NAME LIKE '%ll%';
18. Display the first names that start with ‘R’ and end with ‘l’.
SELECT FIRST_NAME FROM HR.EMPLOYEES WHERE FIRST_NAME LIKE 'R%l';
________________________________________
Filtering by Hire Date
19. Write a query to display employees hired on 17-June-2013, by formatting the hire date and using LIKE.
SELECT FIRST_NAME, HIRE_DATE
FROM HR.EMPLOYEES
WHERE TO_CHAR(HIRE_DATE, 'MM/DD/YYYY, HH:MI:SS AM') LIKE '06/17/2013%';
20. Write a query to display employees hired exactly on 17-June-2013 using TO_DATE.
SELECT FIRST_NAME, HIRE_DATE
FROM HR.EMPLOYEES
WHERE HIRE_DATE = TO_DATE('2013-06-17', 'YYYY-MM-DD');

open this link for practice.

07/09/2025

try-catch-finally with return in Java

Case 1: return in try, but finally executes

public class Test {
public static int testMethod() {
try {
System.out.println("Inside try");
return 1;
} catch (Exception e) {
System.out.println("Inside catch");
return 2;
} finally {
System.out.println("Inside finally");
}
}

public static void main(String[] args) {
System.out.println("Result = " + testMethod());
}
}
//output
Inside try
Inside finally
Result = 1

Case 2: return in finally (overrides try/catch)

public class Test {
public static int testMethod() {
try {
System.out.println("Inside try");
return 1;
} catch (Exception e) {
System.out.println("Inside catch");
return 2;
} finally {
System.out.println("Inside finally");
return 3;
}
}

public static void main(String[] args) {
System.out.println("Result = " + testMethod());
}
}
//output
Inside try
Inside finally
Result = 3

Note : If you return inside finally, it overrides any previous return.

Case 3: Exception occurs in try

public class Test {
public static int testMethod() {
try {
int x = 10 / 0; // Exception
return 1;
} catch (Exception e) {
System.out.println("Inside catch");
return 2;
} finally {
System.out.println("Inside finally");
}
}

public static void main(String[] args) {
System.out.println("Result = " + testMethod());
}
}

//Output:
Inside catch
Inside finally
Result = 2

Catch handled the exception, finally executed, then return value from catch.

Golden Rules to remember (interview points):
1. finally always executes (except System.exit or JVM crash).
2. If finally has a return statement, it overrides try/catch returns.
3. Avoid return inside finally → makes debugging very confusing.

Case 4: Using System.exit() inside try

public class Test {
public static int testMethod() {
try {
System.out.println("Inside try");
System.exit(0); // Terminates JVM immediately
return 1;
} catch (Exception e) {
System.out.println("Inside catch");
return 2;
} finally {
System.out.println("Inside finally");
return 3;
}
}

public static void main(String[] args) {
System.out.println("Result = " + testMethod());
}
}
//output
Inside try
After printing "Inside try", the JVM shuts down.
• finally is NOT executed.
• The return statement is never reached.

Case 5: Using System.exit() inside catch
public class Test {
public static int testMethod() {
try {
int x = 10 / 0; // ArithmeticException
return 1;
} catch (Exception e) {
System.out.println("Inside catch");
System.exit(0); // JVM shuts down here
return 2;
} finally {
System.out.println("Inside finally");
return 3;
}
}

public static void main(String[] args) {
System.out.println("Result = " + testMethod());
}
}

//output
Inside catch

Again, finally is skipped because JVM exits immediately.

Key Interview Notes:
• Normally, finally always executes.
• But two exceptions:
1. If JVM terminates abnormally (e.g., crash, kill -9).
2. If you explicitly call System.exit() → JVM shuts down, so finally is skipped.

All important points about try-catch-finally with return in Java
• If return is inside try, the expression is evaluated, but finally always executes before returning.
• If return is inside catch, same rule — finally executes before returning.
• If finally contains no return, then the value from try/catch is returned after finally finishes.
• If finally itself has a return, that overrides and discards any previous return from try/catch.
• If finally modifies a variable (but does not return), the evaluated return value from try/catch remains unchanged.
• If System.exit(0) (or JVM crash) happens in try/catch, then finally does not execute.
• So rule of thumb: finally block always executes (except JVM termination), and its return (if present) takes highest priority.

Address

G-6/365 FF Sec-16 Rohini New Delhi
Delhi
110085

Alerts

Be the first to know and let us send you an email when Java Technology posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Contact The School

Send a message to Java Technology:

Shortcuts

Share

Category