Java is a programming language released by the sun microsystems in 1995. It is a general-purpose language based on the concepts of object-oriented programming and the runtime environment(JRE) that consists of JVM, which is the cornerstone of the Java platform.
In this article, you can go through the set of Core Java interview questions most frequently asked in the interview panel. This will help you crack the interview as the topmost industry experts curate these at HKR training.
Let us have a quick review of the Core Java interview questions.
Ans: Object-oriented programming works based on the concept of objects and the objects are the containers which store the data in the form of fields and code in the form of procedures which implements the logic for performing the operations. Java utilizes eight primitive data types such as boolean, byte, char, int, float, long, short, double and these data types are not the objects, hence java cannot be considered as a 100% object-oriented language.
Ans: Wrapper classes convert the Java primitives into the reference types which are the objects. Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class.
Ans:
Array List:
Vector:
Ans:
class Test
{
public static void main (String args[])
{
System.out.println(10 + 20 + "Java");
System.out.println("Java" + 10 + 20);
}
}
The program prints the following output.
30Java
Java1020
In the first scenario, 10 and 20 will be treated as numbers and the sum operation is performed which displays 30. Now, this sum 30 is treated as a string and concatenated with a string Java. Hence, the output becomes “30Java”.
In the second scenario, the string Javatpoint is concatenated with 10 as string “Java10” which then again concatenates with 20 as “Java1020”
Ans:
Finally:
It is used along with a try-catch block, the “finally” block ensures that a particular piece of code is always executed, even if the execution is thrown by the try-catch block.
Finalize:
It is a special method in the object class. The finalize() method is generally overridden to release system resources when garbage value is collected from the object.
Ans: There are many uses of this keyword.
Become a Java Certified professional by learning this HKR Java Training !
Ans: It is not possible by changing the return type of the program due to avoid the ambiguity. The following example explains the ambiguity.
class Sum{
static int addition(int a,int b)
{
return a+b;
}
static double addition(int a,int b)
{
return a+b;
}
}
class TestOverloading3{
public static void main(String[] args)
{
System.out.println(Sum.addition(10,10));//ambiguity
}
}
Output:
Compile Time Error: method addition(int, int) is already defined in class Sum
Ans: From java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is a subclass type. This is known as a covariant return type. The covariant return type specifies that the return type may vary in the same direction as the subclass.
class A{
A get()
{
return this;
}
}
class B1 extends A{
B1 get()
{
return this;
}
void message()
{
System.out.println("welcome to covariant return type");
}
public static void main(String args[])
{
new B1().get().message();
}
}
Output:
welcome to covariant return type
Ans: No. It’s because “this()” and “super()” has to be the first statement in the class constructor.
Example:
public class Test{
Test()
{
super();
this();
System.out.println("Test class object is created");
}
public static void main(String []args){
Test t = new Test();
}
}
Output:
Test.java:5: error: call to this must be first statement in constructor
Ans: The “super” keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever an instance of the subclass is created, an instance of the parent class is created implicitly which is then referred to by the super reference variable. The “super()” method is called in the class constructor implicitly by the compiler if there is no super or this.
Example:
class Fruit{
Fruit()
{
System.out.println("fruit is created");
}
}
class Apple extends Fruit{
Apple()
{
System.out.println("apple is created");
}
}
class TestSuper4{
public static void main(String args[])
{
Apple a=new Apple();
}
}
Output:
fruit is created
apple is created
Ans:
Method Overloading:
Method Overriding:
Ans: Apply these two steps for achieving encapsulation in java.
Declare the variable of the class as private.
Provide public set() and get() methods to modify the values of variables.
Example:
public class EncapsulationTest
{
private String empName;
private String empNum;
private int empAge;
public int getEmpAge()
{
return age;
}
public String getEmpName()
{
return name;
}
public String getEmpNum()
{
return empNum;
}
public void setEmpAge(int newAge)
{
age = newAge;
}
public void setEmpName(String newName)
{
name = newName;
}
public void setEmpNum(String newEmpNum)
{
empNum = newEmpNum;
}
}
Ans
Example:
class Bike {
void run()
{
System.out.println(“Bike is running”);
}
}
class Pulsar extends Bike {
void run()
{
System.out.println(“Pulsar is running safely with 100km”);
}
public static void main(String args[])
{
Bike b= new Bike(); //upcasting
b.run();
}
}
Ans:
Example:
public interface Switch{
public void on();
public void off();
}
Ans: There are various advantages of packages in java.
Ans: In Java, string objects are immutable which means once if the String object is created then its state cannot be changed. Java creates a new string object whenever you try to update the value of that object instead of updating the values of that particular object. String objects are generally cached in the String pool and hence Java String objects are immutable. Since String literals are usually shared between multiple clients, action from one client might affect the rest. It enhances security, caching, synchronization, and performance of the application.
Ans: Yes. It is possible to change the scope of the overridden method in the subclass. However, it should be noticed that the accessibility of the method cannot be decreased. The following measures must be taken care of while changing the accessibility of the method.
Ans: Yes, it is possible to modify the throws clause of the superclass method while overriding it in the subclass. However, there are certain rules that must be followed while overriding in case of exception handling.
Ans:
Example:
protected Object clone() throws CloneNotSupportedException
Ans: In order to reduce the complexity of the program and simplify the language, the concept of multiple inheritance is not supported in java. Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from a child class object, there will be ambiguity to call the method of A or B class.
Since the compile-time errors are better than runtime errors, Java renders compile-time error if you inherit two classes. So whether you have the same method or different, there will be a compile-time error.
Example:
class A{
void message()
{
System.out.println("Hello");
}
}
class B{
void message()
{
System.out.println("Welcome");
}
}
class C extends A,B
{
//suppose if it were
Public Static void main(String args[])
{
C obj=new C();
obj.message();
//Now which message() method would be invoked?
}
}
Output:
Compile Time Error
Ans:
Abstract class:
Interface:
Ans:
Checked Exception:
Unchecked Exception:
Ans:
Process:
Thread:
Ans:
Ans:
Applets:
Java Application:
Ans: An applet undergoes the following states during execution.
Ans: A Choice is displayed in a compact form. In order to see the list of all available choices, the user must pull down the form. Choice allows only one item to select. A list is displayed in such a way that several List items are visible to the user. A list allows selecting one or more List items.
Ans: A Scrollbar is a Component, but not a Container whereas the ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.
Ans: The PreparedStatements are precompiled and so its performance is much better. This statement objects can also be reused with different input values to their queries.
Ans: A CallableStatement is used for executing the stored procedures. The stored procedures are stored and offered by a database. It takes the input values from the user and will return a result. The usage of stored procedures is highly recommended because it offers security and modularity. The syntax of a method that prepares a “CallableStatement” is the following:
Syntax:
CallableStament.prepareCall();
Ans. The following are the core features of Java Programming:
Ans. A ClassLoader is a component of Java Virtual Machine (JVM) that helps in loading class files when a program is executed. Moreover, it is the first to load a workable file in Java language.
Top 30 frequently asked JAVA Interview Questions !
Ans.
C++
Java
Ans. While downloading the Java file, we get the following two important things:-
JDK -
It refers to the Java Development Kit (JDK).
This is a dedicated kit only useful for developing software.
Further, it is Platform Dependent as the compiler generates the byte code.
The JDK package includes many tools useful for debugging and Developing.
JRE -
It refers to Java Runtime Environment (JRE) in Java.
JRE is a group of software and libraries designed for executing Java Programs.
This is similar to JDK in platform dependency.
The package supports various files and libraries for a runtime environment in Java language.
Ans. The following are the different types of memory allocations in Java.:-
Ans. Yes, the program will run successfully even if we write such a keyword. This is due to the lack of specific rules for the order of specifiers in Java programming.
Ans. There is no default value stored in either Local Variables or any primitives and Object references.
Ans. Stack memory is useful to store the order of method execution and local variables. It is created at the time of the creation of threads. On the other hand, Heap memory is used to store the objects as dynamic memory for allocating the objects & JRE classes in the runtime.
Ans. An Association is a relationship without having ownership over the other. You can understand this by an example, such as a person can have multiple bank accounts in different banks. And also, a bank can have multiple customers, but no one can own another’s.
Ans. This is a type of constructor in Java that builds an object through another object of the same class.
Ans. The aggregation is a relation or association between two classes and it is also described as a “whole/part” and “has-a” relationship. It helps in reusing the code in Java. It contains the reference object to another class or object and is said to have ownership of that class.
Ans. An empty interface that doesn’t include any methods or constants in Java is a Marker interface. Popular examples of Market interfaces include Serializable, Cloneable, and Remote interfaces.
Ans. No, Java language is not considered a 100% OOP language. This is because it still uses eight or more primitive data types such as int, float double, etc.
Ans. It is a programming paradigm based on objects that aim to include the benefits of reusability & modularity. Further, it includes data and code where Data is in the form of fields and the regulation as procedures. Moreover, the object-oriented paradigm method allows us to build objects that perform together to generate better understandable software.
Ans. In Java programming, Wrapper Classes offer the process to convert objects into primitive and vice versa.
Ans. The ability to create a similar object which is exactly the same is called Object Cloning. Java programming offers a method clone() useful to create a clone for an existing object. This object contains the same functionality as the original object.
Ans. The package in Java is a collection of related classes, interfaces, important libraries, and JAR files. Moreover, they help in code reusability in Java.
Ans. Java Virtual Machine or JVM fully takes care of memory management in Java. The major aim of Java was to keep programming simple. Thus, memory access using pointers is not a recommended action in Java programming as it doesn’t allow any manipulation of the primary pointer through a reference variable. Thus, Java eliminates the concept of pointers.
Ans. The instance variables are declared within a class but outside a method or a constructor.
But a local variable is a variable that can be anywhere inside a method or a constructor. Also, it has limited scope to the code segment where the variable is declared.
Ans. A Java String Pool is a group of strings within Java's Heap memory. Whenever you try to build a new string object, then the JVM checks the presence of the object within the pool. If it is available, the same object reference is shared with the variable, otherwise it will create a new object.
Ans. The term JDK stands for Java Development Kit which includes the Package of JRE and Developer tools useful for developing various Java Apps and Applets. It has three different variants:-
Ans. JIT or Just-in-Time compiler is a runtime environment component that helps to improve the performance of Java apps. It helps to turn bytecode into machine code at runtime and then the same is executed directly.
Ans. Access Specifiers are predefined keywords used to set the access level to classes, methods, variables, etc. There are the following types of access specifiers in Java-
Ans.
JVM (Java Virtual Machine) includes a Just-in-Time (JIT) compiler tool that turns all the source code in Java into the low-level machine language (ML). It is very compatible to make it run faster than the constant application.
JRE or Java Runtime Environment includes class libraries and other JVM supporting files. But it doesn’t include any Java development tool like a compiler or debugger.
JDK or Java Development Kit includes some tools necessary to write Java Programs. Also, it uses JRE to execute these programs. Further, JRE contains a compiler, an applet viewer, and a Java app launcher.
Yes, A constructor in Java can return a value where it returns the class's existing instance completely. But you cannot accurately make a constructor return a value.
Ans. The term "this" is a specific keyword selected as a reference keyword and is useful to refer to the existing class properties. Such as method, variable, constructors, etc.
Ans. This is a process of building multiple methods using the same name but they have different parameters. There are two ways to overload a method such as:
Ans. Late binding is a dynamic binding process that occurs when the compiler doesn’t decide to call a method.
Ans. The life cycle of a thread has five different states such as:-
Ans. In Java programming, the Externalizable interface helps to customize the serialization process. This interface includes two different methods such as- readExternal and writeExternal.
Ans. JSP in Java refers to the Java Servlet Page is a text document that consists of two text types:-
Ans. JDBC refers to Java Database Connector which is an abstraction layer used to connect an existing database and a Java application.
Batch starts on 7th Jun 2023, Weekday batch
Batch starts on 11th Jun 2023, Weekend batch
Batch starts on 15th Jun 2023, Weekday batch