HCLTech
50+ Interview Questions and Answers
Q1. 1. How to connect 2 DBs from spring boot application
To connect 2 DBs from a Spring Boot application, configure multiple data sources and use JdbcTemplate or EntityManager for each DB.
Configure multiple data sources in the application.properties file
Create separate configuration classes for each data source
Use JdbcTemplate or EntityManager to interact with each DB
Specify the appropriate data source in the repository or service classes
Q2. What is System.out.println?
System.out.println is a Java statement used to print output to the console.
System is a class in Java's core library.
out is a static member of the System class.
println is a method of the PrintStream class.
It is used to print output to the console.
It adds a newline character at the end of the output.
Q3. What is difference between sleep or wait method?
Sleep method pauses the thread for a specified time, while wait method pauses the thread until notified.
Sleep method is a static method of Thread class, while wait method is an instance method of Object class.
Sleep method does not release the lock on the object, while wait method releases the lock and waits for notification.
Sleep method can be interrupted by another thread, while wait method can only be interrupted by a call to notify or notifyAll method.
Example: Thread.sleep...read more
Q4. 1)Compare two string using Java 8 features without comparator and comparable 2) Given an array to print non duplicate in the array 3) Solid principles 4) Stereo type annotations 5) how to make list immutable 6)...
read moreThe interview questions cover a range of topics related to Java development, including Java 8 features, data structures, annotations, and database triggers.
Use Java 8 features like streams and lambda expressions to compare two strings without using comparator or comparable.
To print non-duplicate elements in an array, use a HashSet to store unique elements and then iterate through the array to check for duplicates.
Solid principles refer to a set of design principles for object...read more
Q5. 2. Difference between abstract method implementation and default method
Abstract method implementation is mandatory while default method is optional.
Abstract method has no implementation in the abstract class and must be implemented by the subclass.
Default method has a default implementation in the interface and can be overridden by the implementing class.
Abstract method is used to enforce a contract while default method is used to provide a default behavior.
Example: abstract method - public abstract void draw(); default method - default void dis...read more
Q6. What is static or final method?
Static or final method is a method that belongs to a class rather than an instance of the class.
Static method can be called without creating an instance of the class.
Final method cannot be overridden by a subclass.
Static and final methods can be accessed using the class name.
Example: Math.max() is a static method in the Math class.
Example: String.toUpperCase() is a final method in the String class.
Q7. What are the applications of java?
Java is used for developing desktop, web, mobile, and enterprise applications.
Desktop applications like media players, IDEs, and scientific applications
Web applications like e-commerce websites, social media platforms, and banking portals
Mobile applications for Android devices
Enterprise applications like customer relationship management systems and supply chain management systems
Q8. Write program to print odd and even number alternatively using 2 threads which contains odd number list and even number list
Program to print odd and even numbers alternatively using 2 threads with separate lists
Create two separate lists for odd and even numbers
Use two threads to print numbers alternatively from each list
Ensure synchronization between threads to avoid race conditions
Q9. Add string or integer value using map?
Yes, we can add string or integer value using map in Java.
We can use put() method to add values to a map.
For string values, we can use String as the value type.
For integer values, we can use Integer as the value type.
Example: Map
map = new HashMap<>(); map.put("key", "value"); Example: Map
map = new HashMap<>(); map.put("key", 123);
Q10. How to output in Java programming
Output in Java is done using the System.out.println() method.
Use System.out.println() to print output to the console
Use System.out.print() to print output without a newline character
Use System.out.printf() to format output with placeholders
Use FileWriter or PrintWriter to output to a file
Q11. Different types of oops concepts in java?
There are four main OOPs concepts in Java: Abstraction, Encapsulation, Inheritance, and Polymorphism.
Abstraction: Hiding implementation details and showing only necessary information.
Encapsulation: Wrapping data and methods into a single unit and restricting access to them.
Inheritance: Acquiring properties and behavior of a parent class by a child class.
Polymorphism: Ability of an object to take many forms and perform different actions based on the context.
Q12. Write a java program using java 8 , Print the name and count the each word in the name
Java program using Java 8 to print name and count each word in the name
Use Java 8 streams to split the name into words
Use Collectors.groupingBy to count the occurrences of each word
Print the name and the count of each word
Q13. Remove duplicate from a list of employee object based on empID using streams
Remove duplicates from a list of employee objects based on empID using streams
Use Java streams to group the employee objects by empID
Use a collector to collect the grouped objects and get the values as a list
Convert the list back to a stream and collect it to a new list
Q14. WHAT IS DIFFERENCE BETWEEN THROW AND THROWS?
throw is used to explicitly throw an exception in a method, while throws is used to declare the exceptions that a method may throw.
throw is used within a method to throw an exception explicitly.
throws is used in method signature to declare the exceptions that the method may throw.
throw is followed by an instance of Throwable class, while throws is followed by the exception class names separated by commas.
Example: throw new IllegalArgumentException();
Example: public void metho...read more
Q15. What is collection?
A collection is a group of objects that can be stored, manipulated, and retrieved as a single unit.
Collections are used to store and manage groups of related objects.
Java provides several built-in collection classes such as ArrayList, LinkedList, HashSet, etc.
Collections can be used to perform operations like sorting, searching, filtering, and more.
Collections can be generic or non-generic, depending on the type of objects they store.
Q16. Write a program for sum of numbers?
A program to calculate the sum of numbers.
Create a variable to store the sum.
Iterate through the numbers and add each number to the sum.
Return the sum.
Q17. Write code for controller, service and DAO layer for getEmployeeByID
Code snippets for controller, service, and DAO layers to retrieve employee by ID
Controller layer: Create a method to handle GET request for employee by ID
Service layer: Implement logic to retrieve employee by ID from DAO layer
DAO layer: Write query to fetch employee by ID from database
Q18. What is oops concept?
OOPs is a programming paradigm based on the concept of objects.
OOPs stands for Object-Oriented Programming.
It focuses on creating objects that contain both data and functions.
Encapsulation, Inheritance, Polymorphism, and Abstraction are the four main pillars of OOPs.
Java is an OOPs language.
Example: A car is an object that has properties like color, model, and functions like start, stop, and accelerate.
Q19. Abstraction vs interface?
Abstraction is a concept of hiding implementation details while interface is a contract that defines the behavior of a class.
Abstraction is achieved through abstract classes and methods
Interface is a collection of abstract methods and constants
Abstraction allows for flexibility in implementation
Interface allows for multiple inheritance
Abstraction is used for code reusability
Interface is used for achieving polymorphism
Q20. Overloading vs overriding?
Overloading is when multiple methods have the same name but different parameters. Overriding is when a subclass provides a different implementation of a method inherited from its superclass.
Overloading is resolved at compile-time based on the method signature.
Overriding is resolved at runtime based on the actual object type.
Overloading is used to provide different ways to call a method with different parameters.
Overriding is used to provide a specific implementation of a meth...read more
Q21. Explain how hashmap works internally ?
HashMap works internally using an array of buckets where key-value pairs are stored based on the hash code of the keys.
HashMap uses hashing to store key-value pairs in an array of buckets.
When a key-value pair is added, the key's hash code is used to determine the bucket where it will be stored.
If two keys have the same hash code, they are stored in the same bucket using a linked list or a balanced tree for collision resolution.
To retrieve a value, the key's hash code is used...read more
Q22. Write program to sort list based on multiple conditions
Program to sort list based on multiple conditions
Create a custom Comparator class to define the sorting logic
Implement the compare method to compare objects based on multiple conditions
Use Collections.sort() method to sort the list using the custom Comparator
Q23. What is controller advice annotation?
Controller advice annotation is used to handle exceptions globally in Spring MVC.
It is used to handle exceptions thrown by multiple controllers.
It can be used to add common model attributes to multiple controllers.
It can be used to customize the response of HTTP status codes.
Example: @ControllerAdvice
Example: @ExceptionHandler(Exception.class)
Q24. What is join in sql?
Join in SQL is used to combine rows from two or more tables based on a related column between them.
Join is used to retrieve data from multiple tables in a single query
Types of join include inner join, left join, right join, and full outer join
Join condition is specified using ON keyword
Example: SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column
Join can also be used with subqueries
Q25. What is polymorphism?
Polymorphism is the ability of an object to take on many forms.
Polymorphism allows objects of different classes to be treated as if they are of the same class.
It is achieved through method overriding and method overloading.
Example: A parent class Animal can have child classes like Dog, Cat, and Bird. All these child classes can have their own implementation of the method 'makeSound', which is overridden from the parent class.
Polymorphism helps in achieving code reusability an...read more
Q26. What is inheritence?
Inheritance is a mechanism in which one class acquires the properties and behaviors of another class.
It allows code reusability and saves time
The class that is inherited is called the superclass or parent class
The class that inherits from the superclass is called the subclass or child class
The subclass can access all the public and protected methods and fields of the superclass
Example: A Car class can inherit properties and behaviors from a Vehicle class
Q27. How to implement multithread in java
Multithreading in Java allows multiple threads to execute concurrently, improving performance and responsiveness.
Create a class that extends Thread or implements Runnable interface
Override the run() method with the code to be executed concurrently
Instantiate the class and call start() method to begin execution
Use synchronized keyword to handle thread synchronization and prevent race conditions
Q28. What is java?
Java is a high-level, object-oriented programming language used to develop applications for various platforms.
Java is platform-independent and can run on any device with a Java Virtual Machine (JVM)
It is known for its security features and is commonly used for developing web and mobile applications
Java is also used for developing enterprise-level applications and software tools
Examples of Java-based applications include Android apps, Minecraft, and Apache Hadoop
Q29. Why String immutable
String is immutable in Java to ensure security, thread safety, and optimization.
Immutable strings prevent modification, ensuring data integrity and security.
Immutable strings can be safely shared across multiple threads without synchronization issues.
Immutable strings allow for efficient memory allocation and string pooling.
Example: String s = "Hello"; s.concat(" World"); // returns a new string without modifying the original
Q30. Can hashmap contain null key?
Yes, hashmap can contain null key.
HashMap allows null key and null values.
If a null key is added, it will replace the existing null key if present.
Example: HashMap
map = new HashMap<>(); map.put(null, 1);
Q31. What is the javascript
JavaScript is a programming language commonly used for creating interactive effects within web browsers.
JavaScript is a high-level, interpreted programming language.
It is used to make web pages interactive and dynamic.
JavaScript code can be embedded directly into HTML pages.
Commonly used for client-side web development.
Examples include form validation, interactive maps, and dynamic content updates.
Q32. What is front developer j
Front-end developers are responsible for creating the user interface and user experience of a website or application.
Front-end developers use languages like HTML, CSS, and JavaScript to build the visual elements of a website.
They work closely with designers to implement the visual aspects of a website or application.
Front-end developers focus on creating a responsive and user-friendly interface for users to interact with.
Examples of front-end frameworks include React, Angular...read more
Q33. How to prevent hash coliseum
Preventing hash collisions in Java
Use a good hash function that distributes keys evenly
Implement a resizing strategy for hash tables
Use open addressing or separate chaining to handle collisions
Consider using a hash table library like Guava or Apache Commons
Avoid using mutable objects as keys in hash tables
Q34. What is functional interface
Functional interface is an interface with only one abstract method, used for lambda expressions.
Functional interface can have multiple default or static methods, but only one abstract method.
It is used for lambda expressions and functional programming in Java.
Examples of functional interfaces in Java are Runnable, Callable, and Comparator.
Q35. How to using java programming
Java programming can be used to develop various applications such as web, desktop, mobile, and enterprise applications.
Java can be used to create dynamic web applications using frameworks like Spring, Struts, and Hibernate.
Java can be used to develop desktop applications using JavaFX and Swing.
Java can be used to create mobile applications using Android Studio and Java ME.
Java can be used to develop enterprise applications using Java EE and EJB.
Java can also be used for scien...read more
Q36. What is synchronisation?
Synchronization is the process of controlling access to shared resources in a multi-threaded environment.
It ensures that only one thread can access a shared resource at a time.
It prevents race conditions and data inconsistencies.
It can be achieved using synchronized keyword, locks, and semaphores.
Example: Synchronizing a method or a block of code that accesses a shared variable.
Example: Using locks to ensure mutual exclusion between threads.
Q37. Explain diamond problem in java
Diamond problem occurs in multiple inheritance when a class inherits from two classes that have a common ancestor.
Occurs in Java when a class inherits from two interfaces that have a common method signature
Results in ambiguity as to which method implementation should be used
Can be resolved using default methods in interfaces introduced in Java 8
Q38. What is javascript
JavaScript is a high-level, interpreted programming language used for creating interactive websites.
JavaScript is commonly used for client-side web development.
It can be used to create dynamic and interactive web pages.
JavaScript can also be used for server-side development with Node.js.
Examples include form validation, interactive maps, and dynamic content updates.
Q39. What is Hasting?
Hasting is not a known term in the context of Java development.
Q40. What is cloning?
Cloning is creating an exact copy of an object.
Cloning is done using the clone() method.
The cloned object is a separate instance with the same state as the original.
Changes made to the cloned object do not affect the original object.
Cloning can be shallow or deep depending on the implementation.
Example: cloning an ArrayList to create a backup copy.
Q41. spring beans how works
Spring beans are Java objects managed by the Spring IoC container. They are defined in the Spring configuration file and are instantiated, assembled, and managed by the container.
Spring beans are defined in the Spring configuration file using XML or annotations.
The Spring IoC container is responsible for instantiating, assembling, and managing the beans.
Beans are singleton by default, but can be configured as prototype or other scopes.
Dependencies between beans are managed by...read more
Q42. What are Java basic
Java basics include syntax, data types, control structures, and object-oriented programming concepts.
Syntax: Java code is written in classes and methods.
Data types: Java has primitive and reference data types.
Control structures: Java has if-else, switch, for, while, and do-while loops.
Object-oriented programming: Java supports encapsulation, inheritance, and polymorphism.
Examples: int x = 5; String name = "John"; if (x > 0) { System.out.println("Positive"); } class Car { Stri...read more
Q43. Explain features of java 8
Java 8 introduced several new features including lambda expressions, streams, functional interfaces, and default methods.
Lambda expressions allow for more concise code and easier parallel programming.
Streams provide a new way to work with collections and perform operations in a functional style.
Functional interfaces enable the use of lambda expressions.
Default methods allow interfaces to have method implementations.
Optional class helps to avoid null pointer exceptions.
Q44. Short map value using Java 8
Use Java 8 to shorten map values
Use map() and collect() methods
Use lambda expressions to modify values
Example: map.replaceAll((k, v) -> v.substring(0, 5))
Example: map.values().stream().map(s -> s.substring(0, 5)).collect(Collectors.toList())
Q45. Internal workings of HashMap
HashMap is a data structure that stores key-value pairs and uses hashing to efficiently retrieve values.
HashMap uses an array of linked lists to store key-value pairs.
The key is hashed to determine the index in the array where the value will be stored.
If multiple keys hash to the same index, a linked list is used to handle collisions.
HashMap allows one null key and multiple null values.
Example: HashMap
map = new HashMap<>();
Q46. Linked list vs array list
Linked list is dynamic in size, allows for efficient insertion/deletion. Array list is fixed in size, allows for random access.
Linked list is efficient for insertion/deletion due to dynamic size.
Array list is efficient for random access due to fixed size.
Linked list uses pointers to connect elements, while array list uses indexes.
Example: Linked list is used in applications where frequent insertions/deletions are required. Array list is used when random access is needed.
Q47. Overriding vs Overloading
Overriding is when a subclass provides a specific implementation of a method in its superclass, while overloading is when multiple methods have the same name but different parameters.
Overriding involves changing the behavior of a method in a subclass, while overloading involves creating multiple methods with the same name but different parameters.
Overriding is used for runtime polymorphism, while overloading is used for compile-time polymorphism.
Example of overriding: supercl...read more
Q48. Explain the html
HTML is a markup language used for creating web pages.
HTML stands for HyperText Markup Language.
It uses tags to structure content on a web page.
Common tags include , ,
, ,,
Q49. What java
Java is a high-level, object-oriented programming language used to develop applications for various platforms.
Java is platform-independent and can run on any platform with a JVM.
It is used for developing web, mobile, desktop, and enterprise applications.
Java has a vast library of pre-built classes and APIs.
It supports multithreading, exception handling, and garbage collection.
Java is widely used in the industry for its security, scalability, and performance.
Examples of Java-b...read more
Q50. Why java
Java is a popular, versatile, and reliable programming language used for developing a wide range of applications.
Java is platform-independent, making it easy to write code once and run it on multiple platforms.
It has a vast library of pre-built classes and APIs, making development faster and easier.
Java is highly secure and provides built-in features for encryption and authentication.
It is widely used in enterprise applications, web development, mobile app development, and mo...read more
Q51. acid properties
ACID properties are a set of properties that guarantee database transactions are processed reliably.
ACID stands for Atomicity, Consistency, Isolation, Durability
Atomicity ensures that all operations in a transaction are completed successfully or none at all
Consistency ensures that the database remains in a consistent state before and after the transaction
Isolation ensures that transactions are executed independently of each other
Durability ensures that once a transaction is c...read more
Interview Process at null
Top Java Developer Interview Questions from Similar Companies
Reviews
Interviews
Salaries
Users/Month