50+ Java Interview Questions for Programmers with 2 to 3 Years Experience
Without wasting your time, here is a list of some Java interview questions for beginner programmers. This list is aimed at beginners and less experienced developers, such as those with 2 to 3 years of Java experience.
Hint: Bytecode and Java Virtual Machine
2) What is ClassLoader in Java?
(Answer)
Hint: The JVM loads part of the bytecode class. You can write it yourself.
3) Write a Java program that checks if the number is even or odd? (Answer)
4) What is the difference between ArrayList and HashSet in Java?
(Answers)
ueOwMAd5GBdw4blCOpEBpOdMOtcs-et6nPYA
5) What is double lock testing in singleton?
(Response)
Hint: Check if the setup started twice, the first time unlocked and the second time locked.
6) How to create a thread-like Singleton in Java? (Answer)
Hint: There are many ways, such as using an enum, or using two test classes, or using static classes.
7) When to use volatile variable in Java? (Answer)
Hint: When you want to tell the JVM that a variable can be modified by multiple threads and tell the JVM not to cache its value.
8) When are variables used in Java? (Answer)
Hint: When you want variables in a class to be non-serializable, the class uses the Serialized interface. So you can use it for changes that you don't want to keep their values. Watch the full Java MasterClass to learn about variables in Java.
9) What is the difference between variable and variable in Java?
(Answer)
Hint: completely different, one for serialization and one for concurrency.
10) What is the difference between Serializable and Externalizable in Java? (Answer)
Hint: Externalized gives you more control over the serialization process.
11) Can we override private methods in Java? (Answer)
Hint: No, because it doesn't exist in subclasses, which is an important requirement for overriding methods in Java.
12) Difference between Hashtable and HashMap in Java? (Answer)
Hint: There are many but most importantly the synchronous Hashtable is not HashMap. It's still old and slow compared to HashMap.
13) What is the difference between List and Set in Java? (Response)
Sets are unordered and duplicate items are not allowed.
14) Difference between ArrayList and Vector in Java (answer)
15) What is the difference between Hashtable and ConcurrentHashMap in Java? (Answer)
Hint: It's more scalable.
16) How to make ConcurrentHashMap? (Answer)
Hint: By dividing the map into sections and closing it only when the write is finished.
17) What are the two methods you would go through to use objects as keys in HashMap? (Answer)
Hint: equals and hashcode
18) What is the difference between wait and sleep in Java?
(Response)
Hint: The wait() method releases the lock or the clock, but does not sleep.
19) What is the difference between alert and alert all in Java? (Answer)
Hint: alert notifies you that a random phone is waiting in the lock, while alertAll notifies all the phone waiting on the monitor. If you are sure that only one thread is waiting, use alert, otherwise it is better to not alert at all. To learn more about threading fundamentals, check out Java Champion Heinz Kabutz's Threading Fundamentals mini-course.
20) Why are hashcode and equals() rewritten in Java? (Answer)
Hint: If you plan to store items in collection class like HashMap or ArrayList, follow the equation and hash code you should follow.
21) What does HashMap's load factor mean?
(answer)
Hint: HashMap resize threshold is usually 0.75; this means that the HashMap will resize itself if the HashMap is 75% full.
22) What is the difference between ArrayList and LinkedList in Java? (Answer)
Hint: Some are searchable while others are not, such as arrays and linked lists. Adding and deleting in linked lists is easy, but searching in an array is easy.
23) What is the difference between CountDownLatch and CyclicBarrier in Java? (Fixed)
24) When to use Runnable and Thread in Java? (Answer)
Hint: always
25) What does Enum type security mean in Java?
(Answer)
Hint: This means that you cannot assign instances of multiple Enum types to Enum variables. For example, if you have a variable such as the date DayOfWeek, you cannot assign a value to it from the DayOfMonth order.
26) How does autoboxing of integers work in Java?
(Answer)
Hint: Using the valueOf() method in Java.
27) What is the difference between PATH and Classpath in Java? (Answer)
Hint: PATH is used by the operating system, while Classpath is used by the JVM to find Java binaries such as JAR files or class files.
io-lPE67oMG1oBh204LvPm61t7kAcLFvp-B6
28) What is the difference between method overloading and overriding in Java? (Answer)
Hint: Overriding in subclasses, overloading occurs in the same class. Additionally, an override is a function when the overload is resolved at write time.
29) How to avoid subclassing classes in Java?
(Answer)
Hint: Make the constructor private
30) How do you restrict your class from your client? (Answer)
Hint: Make the constructor private or throw an exception from the constructor
31) What is the difference between StringBuilder and StringBuffer in Java? (Answer)
Hint: StringBuilder is out of sync but StringBuffer is synced.
32) What is the difference between polymorphism and inheritance in Java? (Response)
Hint: Inheritance allows for the reuse and creation of relationships between classes, which is essential for polymorphism, which ensures good behavior.
33) Can we override static methods in Java? (Answer)
Hint: No, because static calls are resolved at compile time while overrides are resolved at run time.
34) Can we access private methods in Java? (Answer)
Hint: Yes, in the same class but not outside of
35) What is the difference between interface and abstract class in Java?
(Answer)
Hint: As of Java 8, the distinction has become blurred. However, a Java class can still use multiple interfaces, but extend only one class.
36) What is the difference between DOM and SAX parser in Java? (Answer)
Hint: DOM loads all XML files into memory while SAX does not. It is an event-based parser that can be used to analyze large files, but the DOM is fast and should be preferred for small files.
37) What is the difference between associative and associated elements in Java? (Answer)
QSqKD-b97Dr36kViV1eTdvqNVNgdZRp52D7n
38) What is the difference between negative and non-negative function in Java? (Response)
Hint: While fail-safe does not throw ConcurrentModificationException, fail-safe throws ConcurrentModificationException when external changes are detected in the underlying collection during iteration.
39) Difference between iterator and enumeration in Java? (Answer)
Hint: Iterators also allow you to delete items during iteration when counting is not counted.
40) What is IdentityHashMap in Java? (Response)
Hint: Map using == equality operator instead of equals() method to achieve equality.
41) What is String Pool in Java?
(Answer)
Hint: The typesetting literal repository. Note, it was moved from persistent to concrete in JDK 7.
42) Can serializable classes in Java have non-serializable fields? (Response)
43) How is this different from super in Java?
(Response)
Hint: this refers to the current instance, and super refers to the instance of the superclass.
44) What is the difference between Comparator and Comparable in Java? (Answer)
Hint: Comparator defines different rules, while Comparable defines the natural order of objects, for example the alphabetical order of a string.
To learn more about Java sequencing, see the Complete Java Masters Course.
DOCGFtdTMhjj3faRAiQ69ZSTxf2pffyroFfv
45) Difference between java.util.Date and java.sql.
date in java? (Answer)
Hint: The first contains the date and time, and the second contains only the date.
46) Why wait and notify method specified in Object class in Java? (Response)
47) Why does Java not support multiple inheritance?
(Response)
Suggestion: Not supported due to poor knowledge of C++, but for Java 8, currently Java does not support multiple inheritance of Type.
48) Difference between checked and unchecked exceptions in Java? (Response)
Instructions: You should make an exception with the block if checked, if unchecked it's up to you; The calculation will not bother you.
49) What is the difference between error and exception in Java? (Answer)
Hint: I'm tired of typing, see answer
50) What is the difference between race and death in Java?
(Answer)