JOBS BY EDUCATION

JOBS BY LOCATION

Core Java interview Questions and answers

Core Java interview Questions and answers. These questions are frequently asked by the recruiters.Java questions can be asked from any core java topic. So we try our best to provide you the java interview questions and answers for experienced which should be in your to do list before facing java questions in  technical interview.

java-interview-questions

What is difference between JDK,JRE and JVM?

  • JVM: JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java bytecode can be executed. It is a specification. JVMs are available for many hardware and software platforms (so JVM is platform dependent).
  • JRE: JRE stands for Java Runtime Environment. It is the implementation of JVM.
  • JDK: JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.

How many types of memory areas are allocated by JVM?

Many types:

  • Class(Method) Area
  • Heap
  • Stack
  • Program Counter Register
  • Native Method Stack

What is JIT compiler?

Just-In-Time(JIT) compiler:It is used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

What is the base class of all classes?

java.lang.Object

Does Java support multiple inheritance?

Java doesn’t support multiple inheritance.

Is Java a pure object oriented language?

Java uses primitive data types and hence is not a pure object oriented language.

Are arrays primitive data types?

In Java, Arrays are objects.

What is difference between Path and Classpath?

Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.

What are local variables?

Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them.

What is platform?

A platform is basically the hardware or software environment in which a program runs. There are two types of platforms software-based and hardware-based. Java provides software-based platform.

What is the main difference between Java platform and other platforms?

The Java platform differs from most other platforms in the sense that it’s a software-based platform that runs on top of other hardware-based platforms.It has two components:

  • Runtime Environment
  • API(Application Programming Interface)

What gives Java its ‘write once and run anywhere’ nature?

The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform.

What is classloader?

The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.

Is Empty .java file name a valid source file name?

Yes, save your java file by .java only, compile it by javac .java and run by java yourclassname Let’s take a simple example:
//save by .java only
class A{
public static void main(String args[]){
System.out.println(“Hello java”);
}
}
//compile by javac .java
//run by java A
compile it by javac .java
run it by java A

Is delete,next,main,exit or null keyword in java?

No.

If I don’t provide any arguments on the command line, then the String array of Main method will be empty or null?

It is empty. But not null.

What if I write static public void instead of public static void?

Program compiles and runs properly.

What is the default value of the local variables?

The local variables are not initialized to any default value, neither primitives nor object references.

What is difference between object oriented programming language and object based programming language?

Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.

What will be the initial value of an object reference which is defined as an instance variable?

The object references are all initialized to null in Java.

What is constructor?

Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.

What is the purpose of default constructor?

The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.

Does constructor return any value?

yes, that is current instance (You cannot use return type yet it returns a value).more details…

Is constructor inherited?

No, constructor is not inherited.

Can you make a constructor final?

No, constructor can’t be final.

What is static variable?

  • static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
  • static variable gets memory only once in class area at the time of class loading.

What is static method?

A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.

Why main method is static?

because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation.

What is static block?

Is used to initialize the static data member.
It is executed before main method at the time of class loading.

Can we execute a program without main() method?

Ans) Yes, one of the way is static block.

What if the static modifier is removed from the signature of the main method?

Program compiles. But at runtime throws an error “NoSuchMethodError”.

What is difference between static (class) method and instance method?

static or class method instance method

  • A method i.e. declared as static is known as static method. A method i.e. not declared as static is known as instance method.
  • Object is not required to call static method. Object is required to call instance methods.
  • Non-static (instance) members cannot be accessed in static context (static method, static block and static nested class) directly. static and non-static variables both can be accessed in instance methods.
  • For example: public static int cube(int n){ return n*n*n;} For example: public void msg(){…}.

What is this in java?

It is a keyword that that refers to the current object.

What is Inheritance?

Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.

Which class is the superclass for every class.

Object class.

Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.
What is composition?

Holding the reference of the other class within some other class is known as composition.

What is difference between aggregation and composition?

Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike has an indicator (aggregation) but bike has an engine (compostion).

Why Java does not support pointers?

Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.

What is super in java?

It is a keyword that refers to the immediate parent class object.

Can you use this() and super() both in a constructor?

No. Because super() or this() must be the first statement.

What is object cloning?

The object cloning is used to create the exact copy of an object. 

What is method overloading?

If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program.

Why method overloading is not possible by changing the return type in java?

Because of ambiguity.

Can we overload main() method?

Yes, You can have many main() methods in a class by overloading the main method.

What is method overriding:

If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method.

 Can we override static method?

No, you can’t override the static method because they are the part of class not object.

Why we cannot override static method?

It is because the static method is the part of class and it is bound with class whereas instance method is bound with object and static gets memory in class area and instance gets memory in heap.

Can we override the overloaded method?

Yes.

What are instance variables?

Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.

How to define a constant variable in Java?

The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can’t be changed also.

           static final int MAX_LENGTH = 50; is an example for constant.

Should a main() method be compulsorily declared in all java classes?

No not required. main() method should be defined only if the source class is a java application.

What is the return type of the main() method?

Main() method doesn’t return anything hence declared void.

Why is the main() method declared static?

main() method is called by the JVM even before the instantiation of the class hence it is declared as static.

What is the argument of main() method?

main() method accepts an array of String object as argument.

Difference between method Overloading and Overriding.

Method Overloading Method Overriding

  • Method overloading increases the readability of the program. Method overriding provides the specific implementation of the method that is already provided by its super class.
  • method overlaoding is occurs within the class. Method overriding occurs in two classes that have IS-A relationship.
  • In this case, parameter must be different. In this case, parameter must be same.

Can you have virtual functions in Java?

Yes, all functions in Java are virtual by default.

What is covariant return type?

Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type. 

What is final variable?

If you make any variable as final, you cannot change the value of final variable(It will be constant).

What is final method?

Final methods can’t be overridden.

What is final class?

Final class can’t be inherited. 

What is blank final variable?

A final variable, not initialized at the time of declaration, is known as blank final variable.

Can we intialize blank final variable?

Yes, only in constructor if it is non-static. If it is static blank final variable, it can be initialized only in the static block.

Can you declare the main method as final?

Yes, such as, public static final void main(String[] args){}.

What is a package?

Package is a collection of related classes and interfaces. package declaration should be first statement in a java class.

Which package is imported by default?

java.lang package is imported by default even without a package declaration.

Can a class declared as private be accessed outside it’s package?

Not possible.

Can a class be declared as protected?

The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.

What is the access scope of a protected method?

A protected method can be accessed by the classes within the same package or by the subclasses of the class in any package.

What is the purpose of declaring a variable as final?

A final variable’s value can’t be changed. final variables should be initialized before using them.

What is the impact of declaring a method as final?

A method declared as final can’t be overridden. A sub-class can’t have the same method signature with a different implementation.

I don’t want my class to be inherited by any other class. What should i do?

You should declared your class as final. But you can’t define your class as final, if it is an abstract class. A class declared as final can’t be extended by any other class.

What value does read() return when it has reached the end of a file?

The read() method returns -1 when it has reached the end of a file.

Can a Byte object be cast to a double value?

No, an object cannot be cast to a primitive value.

What is the difference between a static and a non-static inner class?

A non-static inner class may have object instances that are associated with instances of the class’s outer class. A static inner class does not have any object instances.

What is an object’s lock and which object’s have locks?

An object’s lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object’s lock. All objects and classes have locks. A class’s lock is acquired on the class’s Class object.

What is the % operator?

It is referred to as the modulo or remainder operator. It returns the remainder of dividing the first operand by the second operand.

When can an object reference be cast to an interface reference?

An object reference be cast to an interface reference when the object implements the referenced interface.

Is null a keyword?

The null value is not a keyword.

Which characters may be used as the second character of an identifier, but not as the first character of an identifier?

  • The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.
  • Is the ternary operator written x : y ? z or x ? y : z ?
  • It is written x ? y : z.

How is rounding performed under integer division?

The fractional part of the result is truncated. This is known as rounding toward zero.

If a class is declared without any access modifiers, where may the class be accessed?

A class that is declared without any access modifiers is said to have package access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package.

Does a class inherit the constructors of its superclass?

A class does not inherit constructors from any of its superclasses.

What is the difference between a public and a non-public class?

A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package.

To what value is a variable of the boolean type automatically initialized?

The default value of the boolean type is false.

What is the difference between the prefix and postfix forms of the ++ operator?

The prefix form performs the increment operation and returns the value of the increment operation. The postfix form returns the current value all of the expression and then performs the increment operation on that value.

What restrictions are placed on method overriding?

Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides. The overriding method may not throw any exceptions that may not be thrown by the overridden method.

What is a Java package and how is it used?

A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces.

What modifiers may be used with a top-level class?

A top-level class may be public, abstract, or final.

Tags

You may also like...

0 thoughts on “Core Java interview Questions and answers”

Leave a Reply

Your email address will not be published. Required fields are marked *

Betway India Big Bash Image Banners

SUBSCRIBE FOR LATEST JOBS

Email *