Cognizant
20+ Interview Questions and Answers
Q1. What array list and linkedlist difference,how hashmap internally working,what is synchronised and it's type,what is difference between abstract and interface,class loading mechanism in Java, you hat is single t...
read moreJava Developer interview questions covering array list, linkedlist, hashmap, synchronization, abstract vs interface, singleton class, Spring framework, database configuration, and Java 8 features.
ArrayList and LinkedList differ in terms of implementation, performance, and usage
HashMap uses hashing to store and retrieve key-value pairs
Synchronized is used to ensure thread safety and prevent race conditions
Abstract classes cannot be instantiated and can have both abstract and n...read more
Q2. write program fibonacci series, write program using boolean, write class cast exceptn, singleton design pattern(like you have to jvm one have jdk 1.7 and other have jdk 1.8 how many instance created if one then...
read moreThis interview question covers various topics including programming, design patterns, data structures, and exception handling in Java.
Write a program to generate the Fibonacci series
Write a program using boolean variables
Explain the ClassCastException and how to handle it
Discuss the Singleton design pattern and its implementation with different JDK versions
Explain abstract classes and interfaces in your current project
Explain hashing in HashMap and the number of buckets
Differ...read more
Q3. Difference between List and Set? which one to prefer in which requirements and why ?
List allows duplicates and maintains insertion order, Set doesn't allow duplicates and doesn't maintain order.
Use List when duplicates are allowed and order is important
Use Set when duplicates are not allowed and order is not important
List examples: ArrayList, LinkedList
Set examples: HashSet, TreeSet
Q4. 1. What is jvm 2.Explain Oops 3.Sql queries 4. Rest Api 5. coding problem (Sort arraylist) 6. Known Sorting Algorithm
Interview questions for Java Developer
JVM is a virtual machine that executes Java bytecode
OOPS is a programming paradigm based on objects and classes
SQL queries are used to retrieve data from a database
REST API is a web service that uses HTTP requests to access and manipulate data
Sorting an ArrayList can be done using Collections.sort() method
Known sorting algorithms include Bubble Sort, Quick Sort, and Merge Sort
Q5. how to create immutable classes?
Immutable classes cannot be modified after creation. They ensure thread safety and prevent bugs.
Make the class final so it cannot be extended
Make all fields private and final
Do not provide any setters, only getters
If a field is mutable, return a copy of it in the getter
Consider using a builder pattern for complex objects
Q6. How to implement Spring security with JWT token?
Implementing Spring security with JWT token involves configuring security filters and authentication providers.
Add dependencies for Spring Security and JWT
Configure security filters to validate JWT tokens
Implement authentication provider to authenticate users
Generate and sign JWT tokens for successful authentication
Example: https://www.baeldung.com/spring-security-jwt
Q7. how to create singleton class ?
Singleton class is a class that can only have one instance at a time.
Create a private constructor to prevent instantiation from outside the class.
Create a private static instance of the class.
Create a public static method to get the instance of the class.
Ensure thread safety by using synchronized keyword or static initializer.
Example: public class Singleton { private static Singleton instance = null; private Singleton() {} public static synchronized Singleton getInstance() { ...read more
Q8. Find second largest number using java 8 features Write a java program to reverse a string few questions about springboot and core java
Use Java 8 features to find the second largest number and reverse a string.
Use streams and collectors to find the second largest number in an array.
Use StringBuilder to reverse a string in Java 8.
For Spring Boot questions, focus on dependency injection, auto-configuration, and RESTful services.
For core Java questions, focus on OOP concepts, collections, and exception handling.
Q9. What is idempotency? Solid ? Design patterns used in projects
Idempotency is the property of an operation where multiple identical requests have the same effect as a single request.
Idempotency ensures that even if the same request is made multiple times, the result will be the same.
In RESTful APIs, using HTTP methods like GET and PUT can help achieve idempotency.
Design patterns like Command Pattern and State Pattern can be used to ensure idempotency in projects.
Q10. How to sort an array list
To sort an array list, use the Collections.sort() method.
Import the java.util.Collections class
Call the sort() method and pass the array list as parameter
Ensure the objects in the array list implement the Comparable interface or pass a Comparator object as second parameter
Q11. internal working of hashmap?
HashMap is a data structure that stores key-value pairs and uses hashing to retrieve values.
HashMap uses an array of buckets to store key-value pairs
Each bucket contains a linked list of entries
Hashing is used to determine the index of the bucket where the entry should be stored
If multiple entries have the same hash code, they are stored in the same bucket as a linked list
HashMap allows null keys and values
HashMap is not thread-safe and requires synchronization for concurrent...read more
Q12. Given a string , print the letter and the number of times it occurs
Print each letter in a string along with the number of times it occurs
Iterate through the string and count the occurrences of each letter
Use a HashMap to store the letter as key and count as value
Print the letter and count for each unique letter
Q13. How HashMap works internally?
HashMap is a data structure that stores key-value pairs and uses hashing to retrieve values quickly.
HashMap uses hashing to store and retrieve values based on their keys.
It uses an array of buckets to store the key-value pairs.
The hash code of the key is used to determine the index of the bucket where the pair will be stored.
If two keys have the same hash code, they are stored in the same bucket as a linked list.
HashMap allows null keys and values, but only one null key.
The i...read more
Q14. How do you get data from database in spring boot?
Data can be retrieved from a database in Spring Boot using JPA repositories or JDBC templates.
Use JPA repositories to create custom queries and retrieve data from the database.
Use JDBC templates to execute SQL queries and retrieve data from the database.
Configure the data source in the application.properties file.
Use annotations like @Repository, @Entity, @Table, etc. to map Java objects to database tables.
Q15. Explain runtime polymorphism and compile time polymorphism
Runtime polymorphism occurs when a method is overridden in a subclass, while compile time polymorphism occurs when method overloading is used.
Runtime polymorphism is achieved through method overriding, where a subclass provides a specific implementation of a method that is already provided by its parent class.
Compile time polymorphism is achieved through method overloading, where multiple methods with the same name but different parameters are defined in the same class.
Runtim...read more
Q16. How to make class immutable?
To make a class immutable in Java, you can follow certain guidelines to ensure that its state cannot be changed once it is created.
Make the class final so that it cannot be extended.
Make all fields private and final to prevent modification.
Do not provide setter methods for the fields.
If the fields are mutable objects, return copies of them instead of the original objects in getter methods.
Q17. What is java and python
Java and Python are programming languages used for developing software applications.
Java is an object-oriented language used for developing desktop, web, and mobile applications.
Python is a high-level language used for web development, data analysis, artificial intelligence, and scientific computing.
Java is compiled and runs on a virtual machine, while Python is interpreted.
Java is statically typed, while Python is dynamically typed.
Java has a strong emphasis on type safety a...read more
Q18. What is Static Method?
Static method is a method that belongs to the class itself, not to any specific instance of the class.
Static methods can be called without creating an instance of the class.
They are commonly used for utility methods that do not require access to instance variables.
Static methods are declared using the 'static' keyword.
Example: Math.max() is a static method in the Math class that returns the maximum of two numbers.
Q19. Explain oops concepts
OOPs concepts are the fundamental principles of object-oriented programming.
Encapsulation: Hiding the implementation details of an object and exposing only the necessary information.
Inheritance: Creating new classes from existing ones, inheriting their properties and methods.
Polymorphism: The ability of objects to take on many forms, allowing them to behave differently in different situations.
Abstraction: Focusing on the essential features of an object and ignoring the non-es...read more
Q20. Long term goal explain
To become a technical lead and contribute to the development of innovative solutions.
I aim to continuously improve my technical skills and knowledge.
I want to gain experience in leading a team and managing projects.
I aspire to work on challenging projects that require creative problem-solving.
I hope to make a positive impact on the company and its customers through my work.
Example: I want to lead a team in developing a cutting-edge AI-powered application that revolutionizes t...read more
More about working at Cognizant
Interview Process at null
Top Java Developer Interview Questions from Similar Companies
Reviews
Interviews
Salaries
Users/Month