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.
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();
Batch starts on 1st Jun 2022, Weekday batch
Batch starts on 5th Jun 2022, Weekend batch
Batch starts on 9th Jun 2022, Weekday batch