All in One Offer! | Access Unlimited Courses in any category starting at just $29. Offer Ends in:

Browse Library

  • Business Solutions
  • Become an Instructor
  • 0
    Shopping Cart

    Your Cart is empty. Keep shopping to find a course!

    Browse Courses

Top 20 Core Java Interview Questions:

Feb 01, 2023 at 05:23 AM By :- learnfly team

Core Java, as the name suggests, refers to the basic and essential conceptual understanding of Java programming, used for developing standalone applications. The essential features include:

  • OOP- Object Oriented Programming, is the strategy of building applications based on the objects.
  • Variables
  • Data types
  • Arrays
  • Loops, etc.

Java Programming is a hit among software developers, as this high-level object-oriented programming language is portable across platforms and operating systems, and has been used to develop applications, mobile apps, desktop software, and video games. As java, online programming is gaining attention, widely used at the enterprise level, and by millions of users worldwide, professionals are seeking to switch or increase their core java knowledge. This article understands the issue and answers the most frequently asked core java questions.

1. What are the differences between Java and JavaScript?

Languages:

While Java is an object-oriented programming language that has more verbose syntax, compared to Java Script, which is an object-based scripting language and has a concise syntax. Java is statically typed and mainly used for building standalone applications, whereas Java Script uses dynamic typing and is heavily used for creating web pages, therefore, mainly limited to web development.

Interaction with the Library:

A package manager is responsible to handle dependencies in a given project. Java has a wide range of collections of tools, libraries and frameworks, using package management services, like open-source software management by Maven, which is primarily used for java projects, and Gradle, which is an inbuild automation system. Java imports libraries and packages. However, Java Script uses script tags to include libraries and packages and uses package managers like npm- a default node packager for Node.js or yarn. Java libraries are often cross-platform compatible, while Java Script libraries need not necessarily be compatible across platforms, and users may find issues with accessibility.

Also, Java code needs to be complied with, whereas codes of Java Script are interpreted.

2. What components do you get when you download java file? How do these files differ from one another?

When we download Java file, we get the following components:

  • Java Development Kit- this is the core component, and is a bundle of software development tools, and includes java runtime environment, java compiler and, has a set of tools for debugging and developing.
  • Java Runtime Environment- this is also a set designed for executing java programs and has tools for running essential java applications and includes the java virtual machine and necessary libraries. JRE is platform dependent.
  • Java Standard Edition (SE): consists of core java developing languages, provides the core programming languages and has a set of libraries which has code for developing standalone applications.
  • Java Enterprise Edition EE): along with java SE, it also has additional libraries and tools to develop enterprise-level applications, like web services and JavaBeans.

The core difference is in the scope and purpose they serve. While JDK is dedicated entirely to software development, JRE is designed to execute java programs. JDK has everything required to develop and run java applications, while JRE has a limited scope, and supports files and libraries for a runtime environment.

3. What are the differences between heap and stack memory in Java?

Stack memory is the code, automatically allocated at the run time, and assigned for each individual application, while, heap memory, which is dynamically allocated, and not assigned to code, can be used for running the program as and when required. References and primitives are stored in stack, while objects are stored in heap. Heap is large and can grow and shrink whenever needed, in contrast to stack, which has limited growth, and the size is allocated at the start of a runtime. Due to the simple allocation and deallocation mechanism, stack memory is far more easily accessible as compared to heap memory.

4. Define wrapper classes.

Wrapper classes are classes in Java that wrap primitive data types (booleans, int) into objects(reference types). Wrapper classes provide an object-oriented interface for these primitive data types, wrapping them into objects, and object-based structures.

5. What is copy constructor in Java?

This constructor constructs an object by first taking an object from the same class, and then copying the values of another object belonging to the same class, thus initialising an object through another object of the same class.

6. What is object cloning?

Cloning suggests creating exact replica of the original object/subject. Object cloning is the process of recreating an object exactly the same as the original object, including the state and the behaviour. In Java, a class must implement a cloneable interface, and then the clone method () creates an object with the same functions as the original object. The entire object is duplicated in this cloning copying mechanism.

7. Explain Java String Pool.

Java String Pool is a collection of strings in Java’s heap memory, where strings are stored. If the new string object is present in the memory, then, JVM checks at the pool if the object is present, and then shared, otherwise the object is newly created. The string pool eliminates duplicate copies in the heap memory, i.e when a string is created, it is stored in the string pool, and a reference to a similar string will refer to the string that already existed in the string pool. So if you had a string “hello world” and have another string with the same variable, they will refer to the same object in the string pool, rather than two separate objects.

8. What is the final keyword in Java?

The final keyword in java indicates an entity that cannot be changed, thus maintaining the consistency and integrity of the object. due to this, further changes cannot be made once initialised. This is the final term in java and is a predefined word, used while declaring values to variables. Once the value is defined, it remains the same throughout the running environment, i.e, the program execution.

9. Define package in Java

A collective bundle of classes and interface names, a package is a mechanism in Java for organising similar classes and interfaces. You have to import classes and interfaces from another package using an import statement if you want objects from another package in Java programming. Packages are fundamental in java programming as they enable managing and structuring complex programs.

10. What is a marker interface?

When an interface is empty in java programming, it is called a marker interface. A marker interface has no methods or flags, but is used to indicate some property or behaviour of a class, but doesn't define any behaviour or characteristic of the class. This is an excellent way to communicate information about a class, without actually writing a code.

11. What are the default values stored in Local Variables?

There is no default value assigned to local variables in Java. Local variable has to be explicitly initialised with a value, otherwise, it yields compile time error, and unpredictable and incorrect behavior in program.
If you are wondering how to initialise a local variable, here is an example:

public void myMethod() {
int myInt; // local variable declared
myInt = 10; // local variable initialized
// do something with myInt
}

 

12. Why is the main method declared static in Java?

A java compiler can call java’s main function before or after creating a class object. Users can call the main method directly from the class as declaring the main method static. The static declaration is a performance-oriented optimisation, helping to ensure that the main method can be easily and reliably called from other parts of the program. The main method needs to be declared static for it to be successfully executed by Java Virtual Machine (JVM).

13. So what happens, if the main method is not declared static?

Though the program may be compiled correctly if the main method is not declared static, then there is ambiguity and you will get a compile error reading “non-static main method cannot be referenced from a static content.”

14. What happens if an exception is not caught in the code?

Unless caught by an exception handler, if the exception is not caught in the code, an exception will propagate in the code, by moving up the call stack, and if not caught, the program will terminate.

15. Why does the Java array index start with 0?

The starting index of the Java array is 0 because it is based on a zero-indexed numbering system because which makes it easier to calculate the memory location within an array. In this, the first element is the 0th element, the second element is the 1st element, and so on and so forth. The distance from the array beginning is known as offset, and this offset can be determined by multiplying the index by the size of each element. Since there is no distance, because the first element is at the beginning, it makes the offset 0.

16. Why is synchronisation necessary in multi-threaded programming?

So that multiple threads have access to the same resources without any interference. If multiple threads are attempting to access the same resources, this would result in an incorrect outcome. In java, synchronisation is possible by synchronising blocks. These blocks ensure that a shared resource is accessible to one thread at a given time to maintain the integrity of the data.

17. Explain the term ‘double brace initialisation’ in Java.

This is a technique used for anonymous classes, that implement the map interface. There are two braces- the first brace constructs a class without a name, that extends the class, and the second set added initial values to the map.

18. Write a java program to check if the given 2 strings are anagrams.

Here is a java program to check if strings are anagram:
import java.util.Arrays;

public class AnagramChecker {
public static boolean areAnagrams(String s1, String s2) {
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
return Arrays.equals(c1, c2);
}
public static void main(String[] args) {
String s1 = "listen";
String s2 = "silent";
if (areAnagrams(s1, s2)) {
System.out.println("The strings are anagrams.");
} else {
System.out.println("The strings are not anagrams.");
}
}
}

 

19. Write a code to find HCF of number 12 and 36.

Here is the code for the same:

public class HCF {
public static int findHCF(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void main(String[] args) {
int a = 12;
int b = 36;
int hcf = findHCF(a, b);
System.out.println("The HCF of " + a + " and " + b + " is: " + hcf);
}
}

 

Define late binding.

Late binding is a method in which, the method call is determined at running time rather than compile time. The method’s code segment is anonymous at runtime.

Students learning on Learnfly works with Fortune 500 companies around the globe.

Sign Up & Start Learning
By signing up, you agree to our Terms of Use and Privacy Policy
Reset Password
Enter your email address and we'll send you a link to reset your password.