Interview Questions are a great source to test your knowledge about any specific field. But remember whenever you are adding java as your strength in your resume do mention is it is core java spring MVC or something else. Otherwise the interviewer can question you from any of the fields in java
Following are some Java (core) Interview questions and answers for Freshers.
1. What is the purpose of the Java Runtime Environment (JRE)?
JRE is a runtime environment in which Java bytecode is executed. For a runtime environment, JRE supports files and libraries.
2. What exactly is a Java Development Kit (JDK)?
JDK is a software development kit that includes the tools and libraries required to create Java applications.
3. Java is Platform-Independent. Why?
Any other programming language converts source code into executable code, which does not run on all platforms.
Using JVM, we can make byte code understandable on any platform, and this bytecode is platform-independent.
Meanwhile, JVMs are platform-specific, and Java is platform-independent because it is not platform-dependent. As a result, Java is platform agnostic.
4. What is the purpose of the Java Virtual Machine (JVM)?
JVM is an abstract machine that provides a runtime environment for executing Java bytecode. Learn the introduction to the Java virtual machine and architecture.
5. Java is not entirely object-oriented. Why?
Because java uses eight primitive data types that are not objects, such as boolean, byte, char, int, float, double, long, and short.
6. What are the constructors in java?
Constructor is a block of code used to initialise objects.
Syntax:
example:
Output:
26
7. What are wrapper classes in java?
Wrapper classes make it possible for primitive data types to be converted into objects and vice versa.
As an example:
8. What does a singleton class do?
Singleton classes can only have one object (a class instance) at a time. When we try to instantiate a singleton class after creating an object, the new variable also points to the first object created.
9. What are the OOP’s concepts in java?
Inheritance is a procedure of acquiring all the properties and behaviour of a parent class (super class) into child class (subclass).
Abstraction is defined as hiding internal implementation and showing only necessary information.
Polymorphism literally means many forms. Polymorphism is the ability of a method to perform different tasks. It can be achieved through methods.
Encapsulation is a procedure of binding data or variables and methods together.
10. What is the difference between == (double equals to) operator and equals method in java?
In general, equals() is a method and == is an operator. For reference comparison of heap objects, we use the == operator.
Because there is no concept of operator overloading in Java. Meanwhile, the == operator is used to determine whether or not the addresses of two objects are the same.
That is, the == operator determines whether or not both objects point to the same memory location.
The == operator can also be used to compare object types and primitive types such as boolean. While the String class's equals() method compares the contents of two objects.
11. Why is the main() method always static in java?
Because it is not necessary to invoke a static method object. If main() were a non-static method, the Java Virtual Machine would have to first create its object before calling the main() method, resulting in the extra memory allocation.
12. Why are strings immutable in java?
Strings are immutable in java because String objects are cached in string constant pool.
13. What are ArrayList and Arrays in java?
Array is an object which holds a fixed number of elements of similar type.
ArrayList is a collection framework component. Arraylist is a list interface implementation. ArrayList is a resizable array that grows and shrinks dynamically as elements are added and deleted. Java ArrayList is the best choice for frequent retrieval operations. Because the elements of an ArrayList are stored in sequential memory locations. ArrayList does not support primitive data types like int, double, char, and long. String and wrapper class objects can be stored in an ArrayList (Double, Integer). Duplicate elements are permitted in ArrayList. ArrayList maintains insertion order. Because of its functionality and flexibility, ArrayList is widely used. It is intended to hold a variety of object collections. ArrayList can contain an unlimited number of null values.
14. Difference between this and super keywords in java?
super keyword is the reference variable which is used to refer immediate parent class object.super keyword can be used to invoke immediate parent class method.super keyword can be used to invoke immediate parent class constructor.super keyword used to access methods of the base class.
This keyword is a reference variable to the current object. This keyword can be used to implicitly invoke the current class method. The this() keyword is used to call the current class's function Object() { [native code] }. This keyword can be used to pass an argument as part of a method call.
15. Difference between hashset and hashmap in java?
In HashMap,
We can store key-value pairs in HashMap.
For example, 1->"Hello," 2->"World" does not preserve insertion order. It is built on the Hash function. has the put() method Map interface is implemented. allows for multiple values Duplicate keys are not permitted. allows for a single null key and an unlimited number of null values
In HashSet,
Objects can be stored in HashSet. For example, the insertion order of HashSet:"Hello", "World" is not preserved. It is built around hashcode. has the add() method Set interface is implemented. duplicate elements are not permitted allows for a single null value
16.Does all properties of Immutable Objects need to be final in Java?
Not necessary, one can achieve the same functionality by making a member as non-final but private and also not modifying them except in constructor.
Don’t provide a setter method for them and if it is a mutable object make sure that you don’t leak any reference for that member.
17. What is the continue and break statement in java?
The main purpose of break statement is to terminate. Break statement first encountered in a loop, that loop will terminate and the control will shift to the next statement of the same loop.
Demo:
Output:
1
2
continue statement jumps to next iteration of a loop based on specific condition.
Demo:
Output:
2
4
6
8
10
18. What are access modifiers in java?
The scope of a variable, function Object() [native code], class, or method is limited or defined by access modifiers. Private access modifiers, protected access modifiers, public access modifiers, and default access modifiers are the four types of access modifiers in Java.
19. Difference between for and for each loop in java?
For loop,
In execution, there is no sequence. In the for loop, we can change the counter to our liking. JDK 1 was introduced from the beginning. There is no need to implement an interface. can gain access to the index As a result, it can replace elements in an array. The counter can be incremented and decremented.
Each loop,
Executes in order. The counter is raised by one. was introduced in JDK 5 and later. Containers must implement the Iterable interface in order to be looped over using for each loop. Because there is no access to the array index, it is not possible to replace the element at the given index. We can only iterate in increments, not decrements.
Demo on difference between foreach and for loop.
Output:
using for loop:
Kunal
DK
Saha
using for each loop:
Kunal
DK
Saha
Bonus:
While people are preparing for job interview as a fresher its always a great idea to explore a little more. Following are some Java (core) question for mid experience personal:
1. Can we make array volatile in Java?
This is one of the tricky Java multi-threading questions you will see in senior Java developer Interview. Yes, you can make an array volatile in Java but only the reference which is pointing to an array, not the whole array. What I mean, if one thread changes the reference variable to points to another array, that will provide a volatile guarantee, but if multiple threads are changing individual array elements they won't be having happens before guarantee provided by the volatile modifier.
2.What is busy spin? Why should you use it?
Busy spin is one of the technique to wait for events without releasing CPU. It's often done to avoid losing data in CPU cached which is lost if the thread is paused and resumed in some other core. So, if you are working on low latency system where your order processing thread currently doesn't have any order, instead of sleeping or calling wait(), you can just loop and then again check the queue for new messages. It's only beneficial if you need to wait for a very small amount of time e.g. in micro seconds or nano seconds. LMAX Disrupter framework, a high-performance inter-thread messaging library has a BusySpinWaitStrategy which is based on this concept and uses a busy spin loop for EventProcessors waiting on the barrier.
3.Can we create an Immutable object, which contains a mutable object?
Yes, its possible to create an Immutable object which may contain a mutable object, you just need to be a little bit careful not to share the reference of the mutable component, instead, you should return a copy of it if you have to. Most common example is an Object which contain the reference of java.util.Date object.
4.Can I store a double value in a long variable without casting?
No, you cannot store a double value into a long variable without casting because the range of double is more that long and you we need to type cast. It's not difficult to answer this question but many developer get it wrong due to confusion on which one is bigger between double and long in Java.
5.The difference between sleep and wait in Java?
Though both are used to pause currently running thread, sleep() is actually meant for short pause because it doesn't release lock, while wait() is meant for conditional wait and that's why it release lock which can then be acquired by another thread to change the condition on which it is waiting.
Conclusion:
Java is a simple high-level programming language that provides powerful tools and remarkable standards for application development. It was also one of the first programming languages to have excellent threading support for dealing with concurrency-based challenges. Java's simple syntax and built-in capabilities, together with the reliability it delivers to applications, are the primary reasons for the language's increasing popularity in the software sector.
Additional Links:
Java Cheatsheet : https://www.pephub.tech/blogs/learn-java-basics-to-advanced-concepts-java-cheatsheet-f8ba80e3
Java OPPs Cheatsheet : https://www.pephub.tech/blogs/java-oop-cheat-sheet-a-quick-guide-to-object-oriented-programming-in-java-1aefe19b
If you're looking to prepare for a job in the tech industry, Data Structures and Algorithms (DSA) are essential to master. Practising problems on plat......
Each and every programmer needs a strong grasp in DSA to write efficient and optimised codes. However, DSA has a reputation of being one of the most f......
Its that time round the calendar again, but different for you all as for the first time now. You all will be facing the on campus placement season.Wit......
WHAT IS WEB3?Web3 is the catch-all term for the vision of an upgraded version of the internet aimed at giving power back to the users. It is truly the......
Operating Systems is one of the core subjects for a computer science student. As such a lot of important questions are asked on this subject in interv......
Cognizant helps organizations remain ahead of the competition by modernizing technology, reinventing processes, and redefining customer experiences. I...
Getting started with cloud computing ?The words "cloud" and "web" will be interchangeable. What kind of business operates today without the internet? ...
SOFTWARE MAINTENANCE:Software Maintenance can be defined as the process of modifying a software product after its delivery. The main purpose of softwa...
A quick summary upto what we did this far from this series:Software EngineeringSoftware engineering is a discipline of engineering concerned wit...
Capgemini is a global leader in consulting, digital transformation, technology, and engineering services. In the rapidly evolving world of cloud, digi...
Technical Round :Candidates who pass the online test will be invited to the technical interview...
About Mindtree:Mindtree Ltd, located in Bangalore, India, is a prominent Indian multinational information technology and outsourcing company. The ...
TCS NQT Interview kicks off with the aptitude test. The test follows the following pattern : TCS uses the TCS-iON software for their online aptit...
Increased competition for fewer jobs in the current economy means that just being right for the role on paper, is not enough on its own anymore. You h...
About TCS NinjaTCS administers the NQT to select candidates for a variety of positions (National Qualifier Test). Tens of thousands of people apply fo...
Introduction:Understanding time complexity of a function is imperative for every programmer to execute a program in the most efficient manner. Time co...
Software MethodologyA software methodology (which is also known as software process) is a set of related development Phases that leads to the pr...
About TCS DigitalTCS selects applicants for a variety of jobs by conducting the NQT (National Qualifier Test). About 10,000-15,000 job applicants subm...
Software Engineering is a subject that requires when you are a software developer as well as when you are in your college preparing for a semester or ...
Are you getting ready for your SQL developer job interview?You've come to the correct place.This tutorial will assist you in brushing up on your SQL s...
Introduction: What is SQL?To understand SQL, we must first understand databases and database management systems (DBMS).Data is essentially a collectio...
TCS Recruitment ProcessInterview RoundsInterview round 1: TCS NQTTCS NQT (National Qualifier T...
TCS NQT Aptitude has three sections, namely the Numerical Ability, Verbal Ability, and Reasoning Abi...
About TCS NQTTCS NQTs, also known as TCS National Qualifier Tests or Tata Consultancy Services Natio...
Networking: Importance in real life!Using a computer for work, as well as for personal use, ha...
ER-DiagramWhat are ER Models ?An Entity Relationship Diagram (ERD) is a visual representation of dif...
What is actually DBMS?A Database Management System (DBMS) is system software used to manage the orga...
Nowadays students tend to go more for off campus placements rather than on campus placements.One of ...
C CHEATSHEETINTRODUCTION:C programming language was developed by Dennis Ritchie of Bell Labs in 1972...
C is a general purpose high-level language most popular amongst coders, it is the most compatible, e...
What is GenC and GenC Next?GenC stands for Generation Cognizant. It basically means the fresher hiri...
C++ programming language has templates which basically allows functions and classes to work with gen...
C++ was conceived in the late 1970s to overcome the limitations of C. It is an extension of th...
Interview Questions are a great source to test your knowledge about any specific field. But remember...
The most popular high-level, multipurpose programming language right now is Python.Python supports p...
IntroductionPython is a high-level programming language with dynamic binding and high-level inherent...
Object-Oriented Programming or better known as OOPs is one of the major pillars of Java that has lev...
Java is a high-level programming language known for its robustness, object-oriented nature, enhanced...
Placements are an important part of engineering life, and while everyone hopes to be placed in a reputable company, only a few are successful. This is primarily due to a...