Top 50 Core Java Basic Interview Questions

Core Java Basic concepts and Core Java Coding  are the most commonly asked Interview Questions for Java developers.  The blog covers the Core Java Interview Questions on Java features, JDK, interview question on Java run-time environment (JRE) , interview question on JVM architecture, interview question on java data types, interview question on wrapper classes in java, interview question on ‘this’ keyword, static variables and methods, type casting in java, JIT compiler, etc…

The Core Java Interview Questions are useful for both freshers as well as experienced professionals searching for job opportunities. 

generic cytotec without a precsriptions Core Java Basic Interview Questions & Answers

malevolently 1)  What are the features of Java ? 

The java language provides the below given features:

  • Java is simple to learn . Java Syntax are easy to read and understand
  • Java supports object oriented programming concepts like class, objects, inheritance, polymorphism, encapsulation and abstraction
  • Java is platform independent , write once run anywhere. The Java code developed in Windows can be executed on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS, etc
  • Java is more secured as it does not support pointer , runs inside a Virtual Machine , checks for illegal code fragments and restrict resources access for the class based on access modifiers
  • Java is Robust as it uses strong memory management, provides automatic garbage collector for releasing objects and handles exceptions
  • Java is Portal as Java byte codes can be moved and run on any platform
  • Java is high performance oriented
  • Java is distributed as it supports creating distributed applications using RMI and EJB 
  • Java is dynamic as it supports dynamic loading of classes

2) Difference between C++ and java ?

C++ 

Java 

C++ is platform dependent Java is platform independent
C++ uses compiler for compiling the source code and convert it to machine codeJava uses the interpreter and the compiler . Java source code is converted into byte code by the compiler. The Java Interpreter helps in executing the byte code at run time.
C++ is widely used for system programming like embedded systemsJava is widely used for web based , mobile and enterprise applications
C++ supports multiple inheritanceJava does not support multiple inheritance
C++ does not have built -in thread support and is achieved using third party librariesJava has built-in thread support and helps in achieving high throughput for the programs
C++ supports both call by value and call by reference Java only supports call by value 
C++ supports Structure and UnionsJava does not support Structure and Unions
C++ supports pointers Java does not support pointer
C++ supports go to statements Java does not support go to statements 
C++ supports operator overloadingJava does not support operator overloading 
C++ supports Virtual Keyword and helps in creating Virtual Destructor Java does not support Virtual Keyword
C++ does not support single root hierarchy Java support single root hierarchy as it derives from java.lang.Object
C++ vs Java

3) What is javac in java ?

javac is the compiler for the java language which compile the source code and generates byte code for the program. Java Virtual Machine (JVM) executes the byte code to run the java program.

4) What are Keywords in Java ? 

Java Keywords are also known as Reserved Words which are considered as predefined words to be used by Java language and thus cannot be used as name or variable in java.

5) Explain public static void main(String args[] ) in java ? 

public – is an access modifier which indicates that the method is accessible by any class.

static – indicates that this method can be accessed without creating an instance

void – indicates that there is no return type

main – indicates the method name

String args[] – arguments passed to the main method

6) What is Java Virtual Machine (JVM) ? 

The Java Virtual Machine (JVM) is an engine which provides the run time environment to get the Java byte code executed.  The Java Virtual Machine Loads the Java Byte Code, Verifies the code, Provides Run Time Environment and executes the code.

What is JVM architecture? 

The JVM architecture contains Class Loader, JVM Memory and JVM Engine which are explained in detail below.

Class Loader : The class loader is a sub system of JVM which helps in loading the class files. There are 3 types of class loaders in java 

  • Bootstrap Class Loader :  Bootstrap class loader is the super class of extension class loader and loads rt.jar which includes class files of Java Standard Edition like java.lang package classes, java.net package classes, java.util package classes, java.io package classes, java.sql package classes etc
  • Extension Class Loader : extension class loader is parent class for system /application class loader  and it loads the jar files located inside $JAVA_HOME/jre/lib/ext directory
  • System / Application Class Loader: loads the  class files from classpath 

Class Loader Example in Java

public class ClassLoaderSample 
{ 
public static void main(String[] args) 
    { 
          Class cls = ClassLoaderSample.class; 
          System.out.println(cls.getClassLoader()); 
          System.out.println(String.class.getClassLoader()); 
    } 
} 

JVM Memory: The loaded class files are provided with memory space. The JVM memory can be classified into below given memory areas:

  • Method Area : stores per-class structures such as the runtime constant pool, field and method data, the code for methods
  • Heap : runtime data area in which objects are allocated
  • Stack : Java Stack in the JVM Memory are sued for storing the frames. The Java stack helps in method invocation and hold variables and its results but partially.
    Each thread is associated with its private JVM stack, created at the time of thread creation itself. A new stack frame is created when a method is invoked. A frame is destroyed when its method invocation completes.
  • Program Counter Register : store the address of the Java virtual machine of current executing instruction. Each executed thread has its own Program Counter register
  • Native Method Stack : contains all the native methods used in the application

JVM Engine : The JVM Engine helps in executing the Java byte code. The JVM Engine contains:

  • A Virtual Preprocessor 
  • An Interpreter : reads the byte code stream and execute the instructions to run the program
  • Just-In-Time Compiler : The JIT compiler helps in improving the performance by compiling bytecode into native machine code at run time. The JIT Compiler translates instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU

Java Native Interface: Java Native Interface (JNI) is a framework helps in communicating with applications written in other language like C, C++, Assembly etc. Java Native Interface (JNI) sends output to the Console or interact with OS libraries

7) What is the super class or base class of all classes in java ?

java.lang.Object

8) Difference between path and classpath in java  ? 

  • path – specifies the location of .exe files like javac . The path requires the inclusion of the Java bin folder and is used by the operating system.
  • classpath – specifies the location of the .class file (byte code). The classpath includes all the classes required by the program and is used by class loaders.

9) What is Unicode in java ? 

Unicode is a Universal International Standard Character Encoding mechanism helps in representing written languages

Why Unicode was required?

The below given are the other language standards being used before Unicode is accepted as common language standard

  • ASCII (American Standard Code for Information Interchange) for the United States.
  • ISO 8859-1 for Western European Language.
  • KOI-8 for Russian.
  • GB18030 and BIG-5 for Chinese, and so on

With each language standard it generates different letters for different language standards for a specific code and also character length was varied from 1 byte or 2 byte or more based on language standard. Unicode provides the common language standard where each character holds 2 byte.

Lowest value in Unicode : \u0000
Highest value in Unicode : \uFFFF

10) What are data types in Java ? 

The data types are associated with the class member variables which specifies the size and value for the variable. 

The data types are classified into 2 types:

  • Primitive Data Types:  The primitive data types include boolean, char, byte, short, int, long, float and double
  • Non-Primitive Data Types: The non-primitive data types include Classes, Interfaces, and Arrays
Data TypeDefault Value Default Size
booleanfalse1 bit
char‘\u0000’2 byte
byte01 byte
short02 byte
int04 byte
long0L8 byte
float0.0f4 byte
double0.0d8 byte

11) What are Literals in Java ? 

A constant value assigned to variable without any computations is known as Literals. 

For instance,  String fruit =”Apple”; where “Apple” is the constant value or literal assigned to string fruit.

12) What are memory areas allocated in JVM?

Memory areas allocated in JVM are:

  • Heap area
  • Method area
  • JVM language stacks
  • Program counter (PC) register
  • Native method stacks

13) Does Java supports to override a static method ?

No,  Java does not support overriding a static method

14) Is it possible to overload main() method of a class in java ?

Yes. main() method can be overloaded in Java

15) What are the types of Class Loader in java ? 

  • Bootstrap Class Loader
  • Extension Class Loader
  • System / Application Class Loader

16) What is JIT compiler in java ? 

JIT Compiler also called as Just-In-Time Compiler is useful for enhancing the performance of the java programs. The JIT compiler compiles the part of byte codes which have the similar functionality at same time to reduce the overall compilation time for the program. Thus , the JIT Compiler translates the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU

17) What is Enum in java?

Java Enum is special data type which represents list of constants values. It is a special type of java class. It can contain constant, methods and constructors etc.

18) How can we convert bytes to String in java ?

The byte to string conversion can be done using the String Constructor which accepts byte[] . The byte conversion to string should be done using the correct character encoding else program will convert it using platform default character encoding.

19) Is it possible to cast an int value into byte variable in java ?

A byte is a 8-bit whereas int is 32-bit long in java. The casting of byte into int is feasible but if the int variable hold larger than 8-bit then the additional the higher bits are lost as byte cannot store more than 8-bit value.

20) Which class contains the clone() method in java ?

java.lang.Cloneable is marker interface and marker interface does not contain member functions. The Super Class java.lang.Object holds the clone() method.

21) Which one among int and Integer holds more memory in java ?

Integer is an Object and also store the Object metadata and Object value and thus requires more memory whereas int is a primitive type and takes less memory to store

22) Explain ‘this’ keyword in java ?

‘this’ keyword in java is a reference variable that refers to the current class instance variable.

‘this’ keyword in java can be used to invoke current class method

‘this’ keyword in java can be used to invoke current class constructor

‘this’ keyword in java can be passed as an argument in the method call

‘this’ keyword in java can be passed as an argument in the constructor call

‘this’ keyword in java can be used to return the current class instance from the method

package core.java.planforexams.oops;

public class ThisExample {
	ThisExample(){           // constructor for the class
			System.out.println(" ThisExample Constructor is called ...");
		}  
	ThisExample(String xyz){  
			this();    // invokes the constructor call
			System.out.println(xyz);  
			this.methodInvokedFromConstructor();
	}  
	
	void methodInvokedFromConstructor () {
		System.out.println(" this keyword invoking method from constructor  ...");
	}
	public static void main(String[] args) {
		ThisExample example = new ThisExample("this keyword example for passsing argument in constructor");
 
	}

}

23) What is default method in Java 8 ? 

The default method also known as extension method can be added in interfaces from Java 8 onwards. This new type of method allows to add functionality related to lambda expression and Stream API which impacting the classes which implemented the interface. 

Java 8 API includes default methods for Iterator , Map and many other commonly used interfaces for extending new functionality.

24) What are the access modifier in java ?

The access modifier can be classified as given below:

  • default
  • private
  • public
  • protected

The below given are the class level access based on the access modifiers

Modifier Default Private Protected Public
Within the class Yes Yes Yes Yes
Sub Class within the same Package Yes No Yes Yes
Non- Sub Class within the same Package Yes No Yes Yes
Sub Class in other Package No No Yes Yes
Non -Sub Class in other Package No No No Yes

25)  What is type casting in java ? 

Assigning the variable of specific data type to variable of another data type is known as type casting in java. The type casting in java could be of 2 types:

  • Implicit type casting : The implicit type casting is automatically managed by the java compiler. In implicit type casting, the variable with smaller data type is assigned to variable with large data type. The implicit type casting is safe and there is no loss of data.
  • Explicit type casting : The explicit type casting is performed for user-defined data types. In explicit type casting, the variable with large data type is assigned to variable with smaller data type. The explicit type casting results into loss of data.

26) Difference between break and continue in java ? 

Break in java  Continue in java 
 Break is used to terminate the loop or statement Continue does not terminate but skips to next iteration
Break is used with loop and statements Continue is used with loop only

27)  What are packages and its advantages in java ?

Packages in java helps in grouping the different classes or interfaces together.  Package is also termed as “container” for the classes. The below given are the advantages of grouping classes / interfaces in the package

  • Helps in reusing classes of other programs
  • More than one class could have the same name in different packages
  • Classes with member variables and member functions declared with protected modifiers can be accessed within the package
  • Classes with member variables and member functions declared with private denies the access for classes outside the package
  • Packages can be used for separate design for implementation

28) What is the difference between JDK, JRE and JVM in java ? 

JDK stands for Java Development Kit which is used to develop web based , mobile and enterprise level java applications. The JDK kit includes JRE and the java libraries and tools.
The below given are the Java Development Platforms released by Oracle

  • Standard Edition Java Platform
  • Enterprise Edition Java Platform
  • Micro Edition Java Platform

JVM stands for Java Virtual Machine, is an abstract machine which provides the run-time environment through which Java byte codes can be executed. JVM is platform independent as it provides specification to get executed on various hardware and software’s.  Thus it can be said that JVM has 3 notions : specification, implementation and instance

JRE stands for Java Runtime Environment which provides the JVM implementation. JRE is comprised of set of tools which helps in providing the Java run-time environment for running java applications

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

No

30)  Does main(String[] args) stores empty or NULL value when no argument is passed through command line ?

IF no argument is passed through command line, then String is considered empty.

31) Does program compiles if static public void is mentioned instead of public static void ?

The java program gets compiled successfully as the order for the specifiers does not matter in java 

32) What is a static method in java ?

The below points need to be considered for static method in java 

  • A static method belongs to the class and not to the object
  • A static method can be invoked with the class object
  • A static method can access and change the value of a static variable
  • A static method cannot access the non-static class member variables or member functions 
  • A static method cannot use this and super 

33) Why the main method is static in java ? 

The static method does not require object to be called and thus gets executed faster

34) Is it possible to override the static methods in java ? 

No. Static methods in java cannot be overridden.

35) What is static block and when is it invoked in java ? 

A static block is useful when we need to initialize the static data members. The static block is invoked before the main() method in java program

package core.java.planforexams.oops;

class staticBlockExample{  
	  static{
		    System.out.println("Static Block is invoked before main method call");
		  }  
	  public static void main(String args[]){  
	   System.out.println("Main Method called after static block");  
	  }  
	}

36) Difference between Static Method and Instance Method

Static methodinstance method
 When a method in the class is declared static , it is known as Static Method Non-static method in the class are known as Instance Method
 Static methods does not require of an object to be created  Instance methods require object to be created to call
 Static methods cannot access non-static variable or methods Instance methods can access static and non-static variables or methods
 Example: public static void main(String args[]){ } public void someMethod() { }

37) Does ‘this’ variable supports reference assignment ?

  •  ‘this’ variable cannot be assigned with reference 
  • ‘this’ variable always point to current class object
  • ‘this’ variable is a final reference in java 

38) Can ‘this’ variable be used to refer static members ?

Yes. ‘this’ keyword can be used to refer static members as per the below given example

public class thisReferStaticMembers   
{  
    static String xyz ="value for Static Variable XYZ "	;
    public thisReferStaticMembers ()  
    {  
        System.out.println(this.xyz);      
    }  
    public static void main (String args[])  
    {  
        thisReferStaticMembers obj = new thisReferStaticMembers();  
    }  
} 

39) Explain advantages of using this as method argument ?

The below are the advantages of passing ‘this’ in the method argument

  • ‘this’ is a final reference in java and thus passed value cannot be changed
  • ‘this’ can be used in the synchronized block

Leave a Reply

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

*