100% found this document useful (1 vote)
289 views5 pages

TCS Java Interview Questions Overview

The document lists the top 50 Java interview questions, covering fundamental concepts such as Java's features, JVM, JDK, and JRE, as well as object-oriented principles like inheritance, encapsulation, and polymorphism. It also addresses advanced topics including exception handling, multithreading, collections, and Java 8 features like lambda expressions and the Stream API. Each question is accompanied by a concise answer, making it a useful resource for Java interview preparation.

Uploaded by

anillohar7972
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
100% found this document useful (1 vote)
289 views5 pages

TCS Java Interview Questions Overview

The document lists the top 50 Java interview questions, covering fundamental concepts such as Java's features, JVM, JDK, and JRE, as well as object-oriented principles like inheritance, encapsulation, and polymorphism. It also addresses advanced topics including exception handling, multithreading, collections, and Java 8 features like lambda expressions and the Stream API. Each question is accompanied by a concise answer, making it a useful resource for Java interview preparation.

Uploaded by

anillohar7972
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

Top 50 Java Interview Questions - TCS

1. What is Java?

Java is a high-level, object-oriented programming language developed by Sun Microsystems. It is

platform-independent due to the Java Virtual Machine (JVM).

2. What are the features of Java?

Object-Oriented, Platform Independent, Secure, Robust, Multithreaded, Portable, and High Performance.

3. What is JVM?

JVM (Java Virtual Machine) is a part of JRE that executes Java bytecode and provides platform

independence.

4. What is JDK?

JDK (Java Development Kit) is a software development environment used to develop Java applications.

5. What is JRE?

JRE (Java Runtime Environment) provides libraries and JVM to run Java applications.

6. What is the difference between JDK, JRE, and JVM?

JDK = JRE + Development Tools; JRE = JVM + Libraries; JVM = Runs bytecode.

7. What are access modifiers?

They define access levels: public, private, protected, and default.

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

`equals()` compares content, `==` compares references.

9. What is inheritance in Java?

Inheritance allows one class to acquire properties and methods of another class using 'extends'.

10. What is encapsulation?

Encapsulation is wrapping data and methods into a single unit (class) and restricting access using access
modifiers.

11. What is polymorphism?

Polymorphism allows methods to behave differently based on the object (method overloading and overriding).

12. What is abstraction?

Abstraction is hiding implementation details and showing only essential features using abstract classes or

interfaces.

13. What is an interface?

An interface is a reference type with abstract methods that a class can implement.

14. What is the difference between abstract class and interface?

Abstract class can have method definitions; interface can't (prior to Java 8).

15. What is a constructor?

A constructor is a special method used to initialize objects.

16. What is method overloading?

Method overloading means defining multiple methods with the same name but different parameters.

17. What is method overriding?

Method overriding means redefining a parent class method in a child class.

18. What is the final keyword?

`final` can be used to mark a variable as constant, a method as non-overridable, or a class as

non-inheritable.

19. What is static keyword?

`static` means the variable or method belongs to the class, not the instance.

20. What is the difference between ArrayList and LinkedList?

ArrayList uses a dynamic array, faster in retrieval; LinkedList uses nodes, faster in insertion/deletion.
21. What is exception handling?

Exception handling is the process of handling runtime errors using try-catch blocks.

22. What is the difference between checked and unchecked exceptions?

Checked exceptions are checked at compile-time; unchecked are not.

23. What is a try-catch block?

Used to handle exceptions to prevent program crashes.

24. What is throw and throws?

`throw` is used to explicitly throw an exception, `throws` declares exceptions a method might throw.

25. What is multithreading?

Multithreading is executing multiple threads concurrently to improve performance.

26. How do you create a thread in Java?

By extending the Thread class or implementing the Runnable interface.

27. What is synchronization?

Synchronization ensures only one thread can access a resource at a time.

28. What is deadlock?

Deadlock is a situation where two or more threads are blocked forever, each waiting for the other.

29. What are collections in Java?

Collections are data structures like List, Set, and Map used to store and manipulate groups of objects.

30. What is the difference between HashMap and Hashtable?

HashMap is not synchronized; Hashtable is synchronized.

31. What is the difference between List and Set?

List allows duplicates and maintains order; Set does not allow duplicates.

32. What is garbage collection?


Garbage collection is automatic memory management that reclaims memory used by unreferenced objects.

33. What is the purpose of the 'this' keyword?

`this` refers to the current object instance.

34. What is super keyword?

`super` refers to the parent class and can be used to call parent methods or constructors.

35. What is the main method signature in Java?

`public static void main(String[] args)` is the entry point of Java applications.

36. What is the difference between pass by value and pass by reference?

Java passes everything by value (even object references).

37. What is a package in Java?

A package is a namespace that organizes classes and interfaces.

38. What is a singleton class?

A class that allows only one instance to be created throughout the application.

39. What is the use of transient keyword?

Marks a variable to be skipped during serialization.

40. What is serialization?

Serialization is converting an object into a byte stream for storage or transfer.

41. What is the use of instanceof?

`instanceof` checks whether an object is an instance of a specific class or subclass.

42. What is autoboxing and unboxing?

Autoboxing converts primitives to wrapper objects; unboxing does the reverse.

43. What is a lambda expression?

A lambda is an anonymous function used to simplify code, introduced in Java 8.


44. What is the Stream API?

Stream API is used to process collections in a functional style (introduced in Java 8).

45. What is Optional in Java 8?

Optional is a container object to avoid null pointer exceptions.

46. What is the default method in interface?

A default method has a body and can be overridden in implementing classes (Java 8+).

47. What are functional interfaces?

Interfaces with a single abstract method. Used in lambda expressions (e.g., Runnable).

48. What is method reference?

Method reference is a shorthand for calling methods using `::` operator.

49. What is the difference between Comparable and Comparator?

Comparable is natural ordering; Comparator is custom ordering.

50. What are annotations in Java?

Annotations provide metadata to code and are used by compilers and frameworks.

Common questions

Powered by AI

Garbage collection in Java is a form of automatic memory management that reclaims memory occupied by objects that are no longer in use by the application, thus preventing memory leaks and optimizing resource utilization . A common misconception amongst developers is that garbage collection ensures zero memory leaks automatically. While it manages most routine deallocation tasks, developers must be cautious about holding references to objects longer than necessary, which can lead to memory leaks as the garbage collector will not reclaim objects that are still reachable.

Access modifiers in Java (public, private, protected, and default) play a significant role in enforcing encapsulation by controlling the visibility and accessibility of classes, methods, and variables . They help define clear interfaces and shield certain parts of code from unauthorized access or modifications. However, misuse can lead to several issues; for instance, overly restrictive access can hinder code reusability and flexibility, while overly permissive access can expose sensitive code components, leading to security vulnerabilities. Choosing inappropriate access levels might also complicate debugging and maintenance efforts.

Java is termed a "write once, run anywhere" language primarily because its applications are compiled into an intermediate bytecode that can be executed on any platform equipped with a Java Virtual Machine (JVM). This design abstracts the program from platform-specific details. However, limitations in fully achieving this promise arise from differences in JVM implementations across different platforms, such as variations in performance characteristics and feature support. Also, native platform-dependent libraries and APIs used within Java applications can tether them to specific environments, thus compromising cross-platform execution.

Method overloading in Java involves defining multiple methods with the same name but different parameter lists within the same class, which allows methods to perform similar operations with varying inputs . Method overriding, on the other hand, involves redefining a method from a parent class in a subclass, allowing subclasses to provide specific implementations of methods that are already defined in superclass . Overloading is useful when similar functionality is to be applied to different data types or numbers of inputs, while overriding is essential for implementing specific behaviors in subclasses. For instance, a bank application might use method overloading to calculate interest for different account types, while method overriding could allow each account subclass to provide a specific authentication process.

Lambda expressions in Java provide a means to implement functional programming by enabling concise and readable expressions for implementing methods defined by functional interfaces . They allow for clearer and more concise code, especially when dealing with iterations and functional operations on collections through the Stream API. These features reduce boilerplate code and enable more readable and maintainable software. However, limitations include potential overhead in understanding for developers unfamiliar with functional paradigms, and the complexity in debugging lambda expressions compared to traditional approaches due to their anonymous nature.

The Stream API in Java enhances data processing by enabling functional-style operations on streams of elements, which allows for writing clean, concise, and efficient code. It includes operations like filtering, mapping, and reducing that are lazily executed, optimizing performance by processing elements only if necessary . However, the use of the Stream API may be inappropriate in scenarios where explicit control over iteration is required, or when handling external resources or input/output operations where exception handling needs to be precise, as streams are primarily designed for internal iteration and functional operations.

The Java Virtual Machine (JVM) is crucial for enabling platform independence in Java applications because it allows Java bytecode to run on any device that has a JVM installed. This decouples Java programs from the underlying hardware and operating systems, allowing the same Java code to execute on different platforms without modification. The JVM interprets the Java bytecode into native machine code, thus providing the abstraction necessary for platform neutrality .

Abstraction in Java is a process of hiding the implementation details of a class and showing only the essential features to the user. This is achieved using abstract classes and interfaces . Abstraction is important in object-oriented programming as it reduces complexity and allows developers to focus on the interactions at a higher level without needing to deal with implementation specifics. It also enhances code maintainability by enforcing a clean separation between an object's behavior and its implementation.

Encapsulation in Java involves wrapping data (variables) and the methods that operate on them into a single entity or class, while restricting access to the internal state from outside the class . This enforces information hiding by allowing internal class implementation details to remain hidden from other classes, thus preventing unauthorized access and modification. This contributes to software maintenance by providing clear interfaces and protecting internal details, making updates easier and reducing dependency issues. Furthermore, by restricting data manipulation, it enhances security against unintended or malicious access.

Synchronized blocks in Java prevent concurrency issues by ensuring that only one thread can access the critical section of code that manipulates a shared resource at a time. This is achieved by locking the object on which the synchronized block is defined . However, improper use of synchronization can lead to common pitfalls such as deadlocks, where two or more threads are waiting indefinitely for each other to release locks, and reduced performance due to unnecessary serialization of thread execution. Another issue is fragmenting the synchronized code block, which can inadvertently allow access to non-synchronized chunks, causing inconsistent state or data corruption.

You might also like