java interview questions

Top 20 java interview questions

1. PROGRAM TO CHECK UNIQUE NUMBER IN JAVA

carbon30

Output:
Unique
Not Unique

2. PROGRAM TO FIND PERMUTATION OF A STRING

carbon29

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

3. PROGRAM TO PUT HTML LINKS AROUND URLS STRINGS

carbon28

4. PROGRAM TO CHECK PALINDROME PERMUTATIONS OF A STRING

carbon27

Output:
True
False

5. PROGRAM TO COMPRESS STRING

carbon26

Output :

a2b3c3

6. PROGRAM TO ROTATE MATRIX

carbon25

7. PROGRAM TO CONVERT ALL 1 INTO ZERO MATRIX.

carbon24

8. PROGRAM TO ROTATE A STRING.

carbon23

Output:
True
False
False

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

carbon22

Output:

46

46

10. PROGRAM TO REMOVE DUPLICATES CHARACTER FROM A STRING

carbon21

Output:
FOLLOW UP
FOLW UP

11. PROGRAM TO RETURN A CHARACTER FROM THE STRING.

carbon20

Output:
abcd

e

12. PROGRAM TO REMOVE MIDDLE CHARATER FROM A STRING

carbon19

Output:
abcde
abde

13. WRITE A PROGRAM FOR BUBBLE SORT IN JAVA

carbon18

14. WRITE A PROGRAM FOR INSERTION SORT IN JAVA.

carbon17

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.

carbon16

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?

carbon15

Output :

527483

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

carbon14

Output:

3682913

18. WRITE A PROGRAM TO REMOVE DUPLICATES FROM SORTED ARRAY

carbon13

Output:

2 3 6 8 9 10 12

19. FIND LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS.

carbon12

Output:

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

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

carbon11

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]

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *