java interview questions

Ace Your Java Interview: Top 20 Questions You Must Know

Top 20 java interview questions

In this blog we are going to see top 20 java interview questions. You can also see Cybersecurity interview questions , HTML interview questions, SOC analyst interview questions and Azure security interview questions.

1. PROGRAM TO CHECK UNIQUE NUMBER IN JAVA

Output:
Unique
Not Unique

2. PROGRAM TO FIND PERMUTATION OF A STRING

Output:
It is a permutation
It is a permutation
It is not a permutation

3. PROGRAM TO PUT HTML LINKS AROUND URLS STRINGS

4. PROGRAM TO CHECK PALINDROME PERMUTATIONS OF A STRING

Output:
True
False

5. PROGRAM TO COMPRESS STRING

Output :

a2b3c3

6. PROGRAM TO ROTATE MATRIX

7. PROGRAM TO CONVERT ALL 1 INTO ZERO MATRIX.

8. PROGRAM TO ROTATE A STRING.

Output:
True
False
False

9. PROGRAM TO ADD TWO NUMBERS WITHOUT USING PLUS ('+') SIGN

Output:

46

46

10. PROGRAM TO REMOVE DUPLICATES CHARACTER FROM A STRING

Output:
FOLLOW UP
FOLW UP

11. PROGRAM TO RETURN A CHARACTER FROM THE STRING.

Output:
abcd

e

12. PROGRAM TO REMOVE MIDDLE CHARATER FROM A STRING

Output:
abcde
abde

13. WRITE A PROGRAM FOR BUBBLE SORT IN JAVA

14. WRITE A PROGRAM FOR INSERTION SORT IN JAVA.

Output :

2, 4, 9, 6, 23, 12, 34, o, 1,
2, 4, 9, 6, 23, 12, 34, o, 1,
2, 4, 6, 9, 23, 12, 34, o, 1,
2, 4, 6, 9, 23, 12, 34, o, 1,
2, 4, 6, 9, 12, 23, 34, o, 1,
2, 4, 6, 9, 12, 23, 34, o, 1,
o, 2,4, 6, 9, 12, 23, 34, 1,
o, 1, 2, 4, 6, 9, 12, 23, 34,

15. WRITE A PROGRAM TO IMPLEMENT HASHCODE AND EQUALS.

The hashcode of a Java Object is simply a number, it is 32-bit signed int, that allows an object to be managed by a hash-based data structure. We know that hash code is an unique id number allocated to an object by JVM. But actually speaking, Hash code is not an unique number for an object. If two objects are equals then these two objects should return same hash
code. So we have to implement hashcode() method of a class in such way that if two objects are equals, i.e compared by equal() method of that class, then those two objects must return same hash code. If you are overriding hashCode you need to override equals method also.

The below example shows how to override equals and hashcode methods. The class Price overrides equals and hashcode. If you notice the hashcode implementation, it always generates unique hashcode for each object based on their state, i.e if the object state is same, then you will get same hashcode. A HashMap is used in the example to store Price objects as keys. It shows though we generate different objects, but if state is same, still we can use this as key.

Output :

In hashcode
In hashcode
In hashcode
In hashcode
Hashcode of the key: 1982479637
In hashcode
In equals
Value from map: Banana

16. HOW TO GET DISTINCT ELEMENTS FROM AN ARRAY BY AVOIDING DUPLICATE ELEMENTS?

Output :

527483

17. WRITE A PROGRAM TO FIND THE SUM OF THE FIRST 1000 PRIME NUMBERS.

Output:

3682913

18. WRITE A PROGRAM TO REMOVE DUPLICATES FROM SORTED ARRAY

Output:

2 3 6 8 9 10 12

19. FIND LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS.

Output:

[a2novice]
[uage_is]
[_jav, va_j]
[cab, abc, bca]

20. HOW TO SORT A STACK USING A TEMPORARY STACK?

Output:

input: [34, 3, 31, 98, 92, 23]
=============== debug logs================

Element taken out: 23

input: [34, 3, 31, 98, 92]
tmpStack: [23]
Element taken out: 92
input: [34, 3, 31, 98]
tmpStack: [23, 92]
Element taken out: 98
input: [34, 3, 31]
tmpStack: [23, 92, 98]
Element taken out: 31
input: [34, 3, 98, 92]
tmpStack: [23, 31]
Element taken out: 92
input: [34, 3, 98]
tmpStack: [23, 31, 92]
Element taken out: 98
input: [34, 3]
tmpStack: [23, 31, 92, 98]

Element taken out: 3

input: [34, 98, 92, 31, 23]
tmpStack: [3]
Element taken out: 23
input: [34, 98, 92, 31]
tmpStack: [3, 23]
Element taken out: 31
input: [34, 98, 92]
tmpStack: [3, 23, 31]
Element taken out: 92
input: [34, 98]
tmpStack: [3, 23, 31, 92]
Element taken out: 98
input: [34]
tmpStack: [3, 23, 31, 92, 98]
Element taken out: 34
input: [98, 92]
tmpStack: [3, 23, 31, 34]

 
 

Element taken out: 92
input: [98]
tmpStack: [3, 23, 31, 34, 92]
Element taken out: 98
input: []
tmpStack: [3, 23, 31, 34, 92, 98]
=============== debug logs ended ================
final sorted list: [3, 23, 31, 34, 92,98]

Scroll to Top
www.thecyberblogs.com