37 java interview questions

39
37 Java Interview Practice Questions

Upload: codementor

Post on 21-Apr-2017

9.299 views

Category:

Career


0 download

TRANSCRIPT

37 Java Interview Practice Questions

What is the difference between String, StringBuffer, and StringBuilder in Java?

Question 1:

How do you run a Java application on the command line and set the classpath with multiple jars?

Question 2:

What is the difference between final, finalize, and finally?

Question 3:

How does Garbage Collection prevent a Java application from going out of memory?

Question 4:

What i s the d i f fe rence be tween a ClassNotFoundException a n d NoClassDefFoundError?

Question 5:

Why isn't String's .length() accurate?

Question 6:

Given two double values d1, d2, why isn’t it reliable to test their equality using:

Question 7:

d1 == d2

What is the problem with this code:

Question 8:

final byte[] bytes = someString.getBytes();

What is the JIT?

Question 9:

final double d = 1 / 2;

System.out.println(d);

Question 10:Why does

Print 0? How can you make the code print 0.5 instead?

IntStream.range(0, 10).forEach(System.out::println);

Question 11:In this code:

what is the inferred type of the method reference System.out::println?

final Path path = Paths.get(...);

Files.lines(path).forEach(System.out::println);

Question 12:What is the problem with this code:

What will be the contents of the list after this operation and why?

Question 13:

final List<Integer> list = new ArrayList<>();

list.add(1);list.add(2);list.add(3);

list.remove(2);

Write a function to detect if two strings are anagrams (for example, SAVE and VASE)

Question 14:

What is the contract between equals and hashCode of an object?

Question 15:

Can an enum be extended?

Question 16:

How threadsafe is enum in Java?

Question 17:

How does a JVM handle storing local variables vs storing objects?

Question 18:

Identify the problem in the following code:Question 19:

public class Foo { public Foo() { doSomething(); }

public void doSomething() { System.out.println("do something acceptable"); }}

public class Bar extends Foo { public void doSomething() { System.out.println("yolo"); Zoom zoom = new Zoom(this); }}

When do you use volatile variables?

Question 20:

Why do you need to use synchronized methods or blocks?

Question 21:

What is the difference between HashMap and ConcurrentHashMap?

Question 22:

When do you need to override the equals and hashCode methods in Java?

Question 23:

What is a service?

Question 24:

What is a good usecase of calling System.gc()?

Question 25:

What is the marker interface in Java?

Question 26:

How are Annotations better than a Marker Interfaces?

Question 27:

What are checked and unchecked exceptions? When do you use them?

Question 28:

int a = 1L; Won’t compile and int b = 0; b += 1L; compiles fine. Why ?

Question 29:

Why aren’t you allowed to extend more than one class in Java but are allowed to implement multiple interfaces?

Question 30:

Question 31:

Test t = null; t.someMethod();

public static void someMethod() { ... }

Why doesn’t the following code generate a NullPointerException even when the instance is null?

Question 32:public class Test{ public static void main(String[] args) { Integer a = 1000, b = 1000; System.out.println(a == b);

Integer c = 100, d = 100; System.out.println(c == d); }}

why does the first case print false while the second case prints true?

Question 33:

String s1="home"; String s2="mohe";

How do you check whether or not the following two strings are anagrams?

How do you reverse String("Java Programming") without using Iterations and Recursions?

Question 34:

Give real world examples of when to use an ArrayList and when to use LinkedList

Question 35:

What is the difference between an Iterator and a ListIterator?

Question 36:

What is the advantage of a generic collection?

Question 37:

Did you know how to answer all 37 questions?

Feel free to check your answers hereor

schedule a mock interview withan experienced Java developer