Top Banner
37 Java Interview Practice Questions
39

37 Java Interview Questions

Apr 21, 2017

Download

Career

Codementor
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 37 Java Interview Questions

37 Java Interview Practice Questions

Page 2: 37 Java Interview Questions

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

Question 1:

Page 3: 37 Java Interview Questions

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

Question 2:

Page 4: 37 Java Interview Questions

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

Question 3:

Page 5: 37 Java Interview Questions

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

Question 4:

Page 6: 37 Java Interview Questions

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

Question 5:

Page 7: 37 Java Interview Questions

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

Question 6:

Page 8: 37 Java Interview Questions

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

Question 7:

d1 == d2

Page 9: 37 Java Interview Questions

What is the problem with this code:

Question 8:

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

Page 10: 37 Java Interview Questions

What is the JIT?

Question 9:

Page 11: 37 Java Interview Questions

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?

Page 12: 37 Java Interview Questions

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?

Page 13: 37 Java Interview Questions

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

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

Question 12:What is the problem with this code:

Page 14: 37 Java Interview Questions

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);

Page 15: 37 Java Interview Questions

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

Question 14:

Page 16: 37 Java Interview Questions

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

Question 15:

Page 17: 37 Java Interview Questions

Can an enum be extended?

Question 16:

Page 18: 37 Java Interview Questions

How threadsafe is enum in Java?

Question 17:

Page 19: 37 Java Interview Questions

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

Question 18:

Page 20: 37 Java Interview Questions

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); }}

Page 21: 37 Java Interview Questions

When do you use volatile variables?

Question 20:

Page 22: 37 Java Interview Questions

Why do you need to use synchronized methods or blocks?

Question 21:

Page 23: 37 Java Interview Questions

What is the difference between HashMap and ConcurrentHashMap?

Question 22:

Page 24: 37 Java Interview Questions

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

Question 23:

Page 25: 37 Java Interview Questions

What is a service?

Question 24:

Page 26: 37 Java Interview Questions

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

Question 25:

Page 27: 37 Java Interview Questions

What is the marker interface in Java?

Question 26:

Page 28: 37 Java Interview Questions

How are Annotations better than a Marker Interfaces?

Question 27:

Page 29: 37 Java Interview Questions

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

Question 28:

Page 30: 37 Java Interview Questions

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

Question 29:

Page 31: 37 Java Interview Questions

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

Question 30:

Page 32: 37 Java Interview Questions

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?

Page 33: 37 Java Interview Questions

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?

Page 34: 37 Java Interview Questions

Question 33:

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

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

Page 35: 37 Java Interview Questions

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

Question 34:

Page 36: 37 Java Interview Questions

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

Question 35:

Page 37: 37 Java Interview Questions

What is the difference between an Iterator and a ListIterator?

Question 36:

Page 38: 37 Java Interview Questions

What is the advantage of a generic collection?

Question 37:

Page 39: 37 Java Interview Questions

Did you know how to answer all 37 questions?

Feel free to check your answers hereor

schedule a mock interview withan experienced Java developer