[Go to site: main page, start]

0% found this document useful (0 votes)
2 views16 pages

Java Interview Questions

useful notes

Uploaded by

asnthemaster
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Java Interview Questions

useful notes

Uploaded by

asnthemaster
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

✅ 1. What is Java?

Q1. What is Java and why is it platform independent?​


A: Java is a high-level, object-oriented programming language
developed by Sun Microsystems. It’s platform independent because
Java code is compiled into bytecode, which can run on any machine
with a JVM (Java Virtual Machine).

✅ 2. Is Java purely object-oriented?


Q2. Is Java a pure object-oriented language? Justify.​
A: No. Java is not purely object-oriented because it supports
primitive data types like int, float, etc., which are not objects.

✅ 3. JDK, JRE, JVM


Q3. What is the difference between JDK, JRE, and JVM?​
A:
●​ JVM (Java Virtual Machine): Runs Java bytecode.​

●​ JRE (Java Runtime Environment): Provides JVM + runtime


libraries.​
●​ JDK (Java Development Kit): JRE + development tools
(compiler, debugger).​

✅ 4. Difference between static and object


Q4. What is the difference between static members and object
members?​
A: Static members belong to the class and are shared among all
instances; object members belong to each object separately.

✅ 5. Static keyword usage


Q5. Where can we use static keyword in Java?​
A:
●​ Static variables​

●​ Static methods​

●​ Static blocks​

●​ Static nested classes​


✅ 6. Arrays and array methods
Q6. How do you declare and initialize an array in Java?​
A:
int[] arr = {1, 2, 3, 4};

Q7. Name any two array utility methods from Arrays class.​
A: [Link](), [Link]()

✅ 7. String, String literal, String object


Q8. What is the difference between String literal and String
object?​
A: String literals are stored in String Constant Pool (SCP), while
String objects created using new keyword are stored in Heap
memory.

✅ 8. String Methods
Q9. Name 3 commonly used String methods.​
A: length(), substring(), equals()

✅ 9. SCP vs Heap
Q10. What is the difference between String Constant Pool and
Heap memory?​
A: SCP is a special area inside heap where string literals are stored
for reuse; Heap is the general memory area where all objects are
created.

✅ 10. Conditional and Looping Statements


Q11. What are the conditional statements in Java?​
A: if, if-else, else-if, switch
Q12. Name 3 looping statements in Java.​
A: for, while, do-while

✅ 11. Operators
Q13. What are the types of operators in Java?​
A: Arithmetic, relational, logical, assignment, bitwise, ternary,
unary

✅ 12. Methods and Constructors


Q14. What is constructor overloading?​
A: Defining multiple constructors with different parameter lists in
the same class.
Q15. What is a copy constructor?​
A: A constructor that creates a new object by copying data from
another object.

✅ 13. final vs finally vs finalize


Q16. Differentiate between final, finally, and finalize().​
A:
●​ final: Keyword to make a variable constant or prevent
inheritance/overriding.​

●​ finally: Block executed after try-catch (whether exception


occurs or not).​

●​ finalize(): Method called by Garbage Collector before destroying


object.​

✅ 14. OOPs Concepts


Q17. What are the four main OOPs concepts?​
A: Encapsulation, Inheritance, Polymorphism, Abstraction
✅ 15. Keywords in OOPs
Q18. Which keywords are used to implement inheritance in
Java?​
A: extends, super

✅ 16. Hybrid Inheritance


Q19. How do you achieve hybrid inheritance in Java?​
A: By combining classes and interfaces, since Java doesn’t support
multiple inheritance with classes.

✅ 17. Tightly vs Loosely Coupled


Q20. What is the difference between tightly coupled and loosely
coupled code?​
A: Tightly coupled code is highly dependent on specific
implementations; loosely coupled code uses abstraction/interfaces
allowing flexibility.

✅ 18. Exception Handling


Q21. What is exception handling?​
A: Mechanism to handle runtime errors using try-catch-finally
blocks.
Q22. Name two checked and two unchecked exceptions.​
A: Checked: IOException, SQLException; Unchecked:
NullPointerException, ArrayIndexOutOfBoundsException

✅ 19. Exception vs Error


Q23. What is the difference between Exception and Error in
Java?​
A: Exceptions can be handled in code; Errors are serious issues
not meant to be handled (e.g., OutOfMemoryError).

✅ 20. I/O Streams and File Handling


Q24. What is the difference between byte stream and character
stream?​
A: Byte stream reads/writes binary data; character stream
reads/writes text data.
Q25. Name two classes for file handling in Java.​
A: FileReader, FileWriter

✅ 21. Methods in Encapsulation


Q26. How does encapsulation work in Java?​
A: By declaring fields private and providing public getter and setter
methods.

✅ 22. Multithreading
Q27. What are two ways to create a thread in Java?​
A: By extending Thread class or implementing Runnable interface.
Q28. What is synchronization?​
A: Prevents multiple threads from accessing shared resources
simultaneously.

✅ 23. Multithreading vs Multitasking vs Multiprocessing


Q29. Differentiate between multithreading, multitasking, and
multiprocessing.​
A:
●​ Multithreading: multiple threads in a process.​

●​ Multitasking: running multiple processes.​

●​ Multiprocessing: multiple CPUs handling tasks.​


✅ 24. StringBuilder vs StringBuffer vs String
Q30. Difference between String, StringBuffer, and
StringBuilder?​
A:
●​ String: immutable​

●​ StringBuffer: mutable, synchronized​

●​ StringBuilder: mutable, not synchronized​

✅ 25. JDBC Connectivity


Q31. What is JDBC?​
A: Java Database Connectivity – API to connect Java with
databases.

✅ 26. Interfaces in JDBC


Q32. Name four interfaces in JDBC.​
A: Connection, Statement, ResultSet, PreparedStatement
✅ 27. Steps for JDBC Connectivity
Q33. What are the 5 steps for JDBC connection?​
A:
1.​ Load driver​

2.​ Establish connection​

3.​ Create statement​

4.​ Execute query​

5.​ Close connection​

Q34. What is method overloading and overriding?​


A: Overloading: same method name, different parameters;
Overriding: subclass redefines superclass method.

Q35. What is abstraction?​


A: Hiding implementation details, exposing only functionality.
Q36. Can an interface have constructors?​
A: No, interfaces cannot have constructors.

Q37. Can an abstract class have constructors?​


A: Yes, abstract classes can have constructors.

Q38. Can we instantiate an abstract class?​


A: No.

Q39. What is a marker interface?​


A: An interface with no methods, used to mark a class (e.g.,
Serializable).

Q40. What is polymorphism?​


A: Ability of an object to take many forms (method
overloading/overriding).

Q41. What is the purpose of this keyword?​


A: Refers to the current object.
Q42. Can we override static methods?​
A: No, static methods cannot be overridden (they can be hidden).

Q43. What is the default value of object reference in Java?​


A: null

Q44. What is garbage collection?​


A: Automatic memory management that deallocates unreachable
objects.

Q45. What is a transient variable?​


A: A variable not serialized during object serialization.

Q46. What is the difference between == and .equals()?​


A: == checks reference equality; .equals() checks object content
equality.

Q47. Can a class be both final and abstract?​


A: No.
Q48. What is method hiding?​
A: When a static method in subclass hides static method in
superclass.

Q49. Can constructor be static?​


A: No.

Q50. What is the use of instanceof operator?​


A: To check if an object is an instance of a specific class or
subclass.

✅ 1. Collections (Java Collections Framework)


Q51. What is Java Collections Framework?​
A: It’s a set of classes and interfaces that implement commonly
reusable data structures like List, Set, Map, and Queue.

Q52. What is the difference between List and Set?​


A:
●​ List: Allows duplicates, maintains insertion order (e.g.,
ArrayList, LinkedList).​

●​ Set: Doesn’t allow duplicates, no guaranteed order (e.g.,


HashSet, LinkedHashSet).​

Q53. What is the difference between HashMap and HashTable?​


A:
●​ HashMap: Not synchronized, allows one null key.​

●​ HashTable: Synchronized, doesn’t allow null keys.​

Q54. What is the difference between Iterator and ListIterator?​


A:
●​ Iterator: One-directional traversal.​

●​ ListIterator: Bi-directional traversal (forward and


backward).​
Q55. What is the difference between ArrayList and LinkedList?​
A:
●​ ArrayList: Better for random access, slower for insert/delete.​

●​ LinkedList: Better for frequent insert/delete operations.​

✅ 2. Wrapper Classes and Utility Methods


Q56. What are wrapper classes in Java?​
A: Wrapper classes provide object representations of primitive
types (e.g., Integer, Double, Character).

Q57. Why do we need wrapper classes?​


A: To use primitive types as objects (e.g., in collections like
ArrayList) since collections work with objects, not primitives.

Q58. How do you convert a String to an int in Java?​


A: Using [Link](String) method:
int num = [Link]("123");
Q59. What is autoboxing and unboxing in Java?​
A:
●​ Autoboxing: Automatic conversion from primitive to wrapper
(e.g., int → Integer).​

●​ Unboxing: Automatic conversion from wrapper to primitive


(e.g., Integer → int).​

Q60. Name two utility methods from Integer and Double


wrapper classes.​
A:
●​ [Link](), [Link]()​

●​ [Link](), [Link]()

You might also like