Common Java Interview Questions And Answered
Table Content
Table of Contents
Introduction
Welcome to “Common Java Interview Questions And Answered”! In this space, we explore the intricate realm of Java programming, aiming to arm you with the knowledge and insights necessary to ace your interviews. Whether you’re a seasoned developer seeking to sharpen your skills or a newcomer embarking on your journey in Java, this blog is tailored to cater to your needs. Stay tuned as we unravel Java’s mysteries, decode its concepts, and equip you with the tools to thrive in the dynamic world of software development. Let’s dive deep into Java together and unlock the doors to endless possibilities!
1. What is Java?
Java is an object-oriented, high-level, general-purpose programming language originally designed by James Gosling and further developed by the Oracle Corporation. It is one of the most popular programming languages in the world.
2. What are the features of Java?
The following are the various features of the Java programming language:
- High Performance: Using a JIT (Just-In-Time) compiler allows high performance in Java. The JIT compiler converts the Java bytecode into machine language code, which then gets executed by the JVM.
- Multi-threading: A thread is a flow of execution. The JVM creates a thread which is called the main thread. Java allows the creation of several threads using either extending the thread class or implementing the Runnable interface.
- OOPs Concepts: Java follows various OOPs concepts, namely abstraction, encapsulation, inheritance, object-oriented, and polymorphism
- Platform Independency: Java makes use of the Java Virtual Machine or JVM, which allows a single Java program to operate on multiple platforms without any modifications.
3. What is the static keyword?
The static keyword is used with a class-level variable to make it global so all the objects will be able to share the same variable. It can also be used with methods. A static method can access only static variables of the class and invoke only a static method of the class.
The interview generally asks this question in the Java interview questions for freshers. Even if you are a fresher, you should have a good knowledge of the keywords in Java.
4. What are the Memory Allocations available in Java?
Java has five significant types of memory allocations.
- Class Memory
- Heap Memory
- Stack Memory
- Program Counter-Memory
- Native Method Stack Memory
5. What do you mean by aggregation?
Aggregation refers to the relationship between two classes best described as a “whole/part” and “has-a” relationship. This kind is the most specialized version of an association relationship. It contains the reference to another class and is said to own that class.
6. What is a classloader?
Classloader is the part of JRE(Java Runtime Environment), during the execution of the bytecode or created .class file classloader is responsible for dynamically loading the java classes and interfaces to JVM(Java Virtual Machine). Because of classloaders Java run time system does not need to know about files and file systems. To know more about the topic refer to ClassLoader in Java.
7. Difference between JVM, JRE, and JDK.
JVM: JVM also known as Java Virtual Machine is a part of JRE. JVM is a type of interpreter responsible for converting bytecode into machine-readable code. JVM itself is platform dependent but it interprets the bytecode which is the platform-independent reason why Java is platform-independent. JRE: JRE stands for Java Runtime Environment, it is an installation package that provides an environment to run the Java program or application on any machine. JDK: JDK stands for Java Development Kit which provides the environment to develop and execute Java programs. JDK is a package that includes two things Development Tools to provide an environment to develop your Java programs and, JRE to execute Java programs or applications.
8. Can we override the private methods?
It is not possible to override the private methods in Java. Method overriding is where the method in the subclass is implemented instead of the method from the parent class. The private methods are accessible only within the class in which it is declared. Since this method is not visible to other classes and cannot be accessed, it cannot be overridden.
9. What are the different types of inheritance in Java?
Inheritance is the method by which the Child class can inherit the features of the Super or Parent class. In Java, Inheritance is of four types:
Single Inheritance: When a child or subclass extends only one superclass, it is known to be a single inheritance. Single-parent class properties are passed down to the child class.
Multilevel Inheritance: When a child or subclass extends any other subclass, a hierarchy of inheritance is created, known as multilevel inheritance. In other words, one subclass becomes the parent class of another.
Hierarchical Inheritance: When multiple subclasses derive from the same parent class is known as Hierarchical Inheritance. In other words, a class that has a single parent has many subclasses.
Multiple Inheritance: When a child class inherits from multiple parent classes is known as Multiple Inheritance. In Java, it only supports multiple inheritance of interfaces, not classes.
10. Can we make the main() thread a daemon thread?
This technique designates whether the active thread is a user thread or a daemon thread. For instance, tU.setDaemon(true) would convert a user thread named tU into a daemon thread. On the other side, executing tD.setDaemon(false) would convert a Daemon thread, tD, into a user thread.
11. What is a Memory Leak? Discuss some common causes of it.
A memory leak is the slow degradation of system performance over time brought on by the fragmentation of a computer’s RAM as a result of shoddy application design or programming that fails to release memory chunks when they are no longer required. These memory leaks frequently result from session items in excess, insertion into Collection objects without deletion, infinite caches, excessive page switching on the operating system, listener methods that are not called, and bespoke data structures that are poorly written
12. What are Brief Access Specifiers and Types of Access Specifiers?
Access Specifiers are predefined keywords used to help JVM understand the scope of a variable, method, and class. We have four access specifiers.
- Public Access Specifier
- Private Access Specifier
- Protected Access Specifier
- Default Access Specifier
13. What is a Priority Queue?
A priority queue, like a regular queue, is an abstract data type, but it has a priority associated with each element contained by it.
The element with the high priority is served before the element with low priority in a priority queue. Elements in a priority queue are ordered either according to the comparator or naturally. The order of the elements in a priority queue represents their relative priority.
14. What is the use of a System class in Java?
This question is among the most common Java interview questions for freshers. Java System class is one of the core classes. One of the easiest ways to log information for debugging is System.out.print() method. System class is final so we can’t subclass and override its behavior through inheritance.
System class doesn’t provide any public constructors, so we can’t instantiate this class and that’s why all of its methods are static. Some of the utility methods of System class are for array copy, get the current time, and reading environment variables.
The element with the high priority is served before the element with low priority in a priority queue. Elements in a priority queue are ordered either according to the comparator or naturally. The order of the elements in a priority queue represents their relative priority.
15. What Is JIT?
JIT stands for “just in time”. It’s a component of the JRE that runs in the runtime and increases the performance of the application. Specifically, it’s a compiler that runs just after the program’s start.
This is different from the regular Java compiler which compiles the code long before the application is started. JIT can speed up the application in different ways.
For example, the JIT compiler is responsible for compiling bytecode into native instructions on the fly to improve performance. Also, it can optimize the code to the targeted CPU and operating system.
Additionally, it has access to many runtime statistics which may be used for recompilation for optimal performance. With this, it can also do some global code optimizations or rearrange code for better cache utilization.
16. What Is a JAR?
JAR is a shortcut for the Java archive. It’s an archive file packaged using the ZIP file format. We can use it to include the class files and auxiliary resources that are necessary for applications. It has many features:
- Security – we can digitally sign JAR files
- Compression – while using a JAR, we can compress files for efficient storage
- Portability – we can use the same JAR file across multiple platforms
- Versioning – JAR files can hold metadata about the files they contain
- Sealing – we can seal a package within a JAR file. This means that all classes from one package must be included in the same JAR file
17. Does Java use pointers?
No, Java doesn’t use pointers. It has tough security. Instead of pointers, references are used in Java as they are safer and more secure when compared to a pointer.
18. What Is the Difference Between Overloading and Overriding?
When there are two methods of the same name but different properties, it is overloading. Overriding occurs when there are two methods of the same name and properties, one is in the child class and the other is in the parent class. Check out this post for a more in-depth analysis.
19. What is the difference between JavaScript and Java?
Some of the key differences between JavaScript and Java are:
- Java is Object Oriented Programming Language. But, JavaScript is an Object Oriented Scripting language.
- Java code runs in a virtual machine or browser (Applets) whereas JavaScript code runs on a browser.
- We have to compile Java source code to byte code before JVM can understand and execute it. JavaScript code is text based and we don’t need to compile it.
- We use JavaScript to perform browser-specific tasks. We use Java to create standalone utility apps, web applications, and web services.
- JavaScript is lightweight whereas we have to install Java and configure it to run
20. What are the advantages of packages in Java?
- Packages help developers avoid name clashes.
- Packages provide easier access control.
- Packages can also contain hidden classes that are not visible to the outer classes and are only used within the package.
- Packages create a standardized hierarchical structure, making it easier to locate related classes.
Enrolling in Anubhav for Java training provides several compelling advantages:
- 1. Expert Instructors: Benefit from the guidance of experienced instructors who are Java experts. They offer comprehensive instruction and practical insights, ensuring a deep understanding of Java concepts and best practices.
- 2. Comprehensive Curriculum: Anubhav’s Java training covers all essential topics, from basic syntax to advanced concepts like multithreading and design patterns. This ensures students are well-prepared for real-world Java development.
- 3. Hands-on Learning: The training emphasizes hands-on experience with coding exercises, projects, and real-world examples. This interactive approach helps solidify understanding and develop practical programming skills.
- 4. Flexibility and Convenience: Anubhav offers flexible training options, including online and in-person classes. Students can choose the format that best fits their schedule and preferences, ensuring a convenient learning experience.
- 5. Affordable Pricing: Despite offering top-quality training, Anubhav’s Java programs are priced competitively. Students receive excellent value for their investment, gaining valuable skills that can advance their careers.
- In summary, Anubhav’s Java training program provides a comprehensive and flexible learning experience led by expert instructors, making it an excellent choice for mastering Java programming.