Menu Close

core java interview questions

Is Java Standalone or Web application?

  • JDK or any Programming language software is STANDALONE.
  • We need to install which is compatible to OS.

What is Platform dependency?

  • Platform means Operating System.
  • C and C++ languages are platform dependent.
  • Platform dependent language allows the developer to develop standalone applications only.

What is Platform in-dependency?

  • Platform independent languages allow developing web applications.
  • Examples Java, .Net , Python ….

What is API(Application Program Interface ?

  • API generally termed as Library.
  • It is a collection of pre-defined implementation can be referred in the application development.
  • Java API is a collection of interfaces and their implementations.

What is JIT(Just In Time interpreter)?

  • A program that converts JVM understandable code into a OS understandable instructions.

What is Variable?

  • Variable is a named memory location.
  • An identity of memory location by which we can store and process information.

What is Block?

  • Set of Instructions without identity.
  • We cannot access the block explicitly because of no identity.
  • JVM access the blocks implicitly.

What is Method?

  • Method is a block of instructions with Identity.
  • Method performs a task with its logic
  • Every method must be called explicitly (using its identity).

What Java class name starts with Capital letter?

  • Java programmer need to follow few convention rules in the application development to recognize the various elements of application such as package, interface, class, variable, method and constant variable.

What is Signature of method?

  • Signature is a set of arguments in a method or constructor.
  • Prototype is different from Signature.
  • Prototype includes modifiers, return type…

What is main() signature?

  • It is String array.
  • It is used to collect command line arguments.
  • We can pass arguments at command line while invoking java application.

Why we need to set path for JDK?

  • Operating system should start Computer (javac) and JVM(java) to compile and run java application.
  • OS can recognize executable files (javac, java…) only if we set the path to executable files folder in OS environment variables.

What is the use of main() method in Java?

  • It is static method.
  • Every java application execution starts from main method.
  • JVM invokes main method implicitly
  • The main method is mandatory for every java standalone applications.

What is static block?

  • Block of instructions having no identity.
  • JVM invokes implicitly at the time of class loading.
  • It executes before main method execution in application.

Can we define more than one static block in a single class?

  • Yes allowed.
  • All these blocks execute in the defined order.

What is Local variable?

  • The declaration of a variable inside block or method.
  • Local variables can be accessed directly.
  • We can access the variable only inside the block in which it has declared.
  • Local variables must be initialized before its use.

What is Static variable?

  • A variable declare inside a class and outside to all the methods and blocks.
  • We can initialize static variable directly.
  • Static variable gets memory allocation inside the method area.
  • Static variable can be accessed using class name.

Among Static block and Variable which one executes first?

  • Static variables initializes with default values when program execution starts.
  • Static block and Variable having equal priority, hence these 2 members execute in the defined order.

How to access static & local variables in Java?

  • Local variables can be accessed directly.
  • Static variables can be accessed using class name or object reference variable.

Where we can create object for class?

  • To access restricted members (non-static) we need to create object.
  • We need to create object using any one of 4 static members (static block, static variable, static user method or static main method.

What is Non-Static variable?

  • Declare a variable inside the class without static keyword.
  • Non-static variable must be initialized using constructor.
  • It gets memory allocation inside the object and implicitly initializes with default values.

What is constructor?

  • It is a special java method having the same name of class.
  • Return type is not allowed in the definition of constructor.
  • It used to initialize object.
  • We must call explicitly in the process of object creation.

What is Non-static block?

  • Block of instructions having no identity.
  • We cannot call explicitly in java application.
  • JVM invokes non-static block implicitly.
  • Non-static block executes as soon as object has created.

What is Default constructor?

  • While compiling java source program, compiler is looking for constructor definition.
  • If not present, compiler will write zero arguments constructor with empty definition is called “Default constructor”

What is “this” keyword in Java?

  • It is a keyword.
  • It is a pre-defined non-static variable
  • It holds object reference.
  • It must be used inside the non-static context only.

Does constructor returns any value?

  • Yes constructor returns address of newly created object.
  • But constructor doesn’t allow any return type it its definition.

How can we connect class in Java app?

  • Java application is a collection of class files.
  • We can connect class file by accessing one class members from another class.

Why character occupies 2 bytes in Java?

  • Java mostly used to develop web applications.
  • Web applications need to represent more than 1 language character set at a time.
  • 2 bytes memory(65536) required to represent these many languages called UNICODE char system.

What is character System?

  • Representing all the characters of one language using constant integer values is called character system.
  • One of the examples is ASCII

What is Type casting?

  • It is the conversion of data from one type to another type at the time of execution.
  • Java supports both Primitive casting and Object casting.

What are Implicit & Explicit Castings?

  • Implicit: JVM converts the data automatically from one type to another type.
  • Explicit: Programmer needs to convert manually from one type to another type.

Is Java fully Object Oriented Programming language?

  • No, java is not fully Object oriented.
  • Reasons are:
  • Supporting primitive data types
  • Not supporting all the OOP concepts

What is Boxing in java?

  • Conversion primitive data into Object type is called Boxing.
  • Boxing can be performed by using pre-defined functionality of wrapper classes.

What is Un boxing?

  • Conversion of Object data into primitive data.
  • We can perform un boxing using wrapper class methods.

What is Auto-Boxing in java?

  • Implicit Conversion primitive data into Object type data.
  • Auto boxing since jdk 1.5

How to convert byte –> Byte?

  • Using static method of Byte class as follows
  • byte b = 100 ;
  • Byte obj = Byte.valueOf(b);

How to create Byte object ?

  • We can instantiate Byte object using Byte class constructor.
  • byte b = 100 ;
  • Byte obj = new Byte(b);

What is the use of parseXxx() methods in Java?

  • Parsing means Conversion of data.
  • parseXxx() methods available in Wrapper classes used to converts String into different primitive types.

Why parseXxx() methods throws Exception ?

  • Data conversions (String->Primitive) will be done at runtime in java application.
  • Invalid Data conversions raise Runtime Errors.
  • RuntimeError is called Exception in Java

What is ‘’ describes in Java?

  • “in” is the pre-defined object reference variable of type.
  • “in” is available in System class as static variable.
  • “in” represents pre-defined keyboard object.

What is ‘System.out’ describes ?

  • “out” is the static variable available in System class.
  • “out” is object reference variable of class type.
  • “out” represents Standard output Stream(monitor).

What are command line arguments in Java?

  • Passing input values (arguments) to the program from command prompt while invoking the application.
  • main(String args[]) collects these arguments and store into an array ot type String.

Why arguments array of type String ?

  • main(int[ ]) —> collects only integers.
  • main(float[ ]) —> collect only float values.
  • but main(String[ ]) —> collects any type of input data values from command line.

How many access modifiers java supports?

  1. private
  2. <package> or <default>
  3. public
  4. protected

What Access modifier describes in Java?

  • Access modifier describes what are the limitations(boundaries) of object members or class members to be accessed in java application.

Where we can apply access modifiers?

  • We can apply access modifiers to
    • Class
    • Variable
    • Method
    • Constructor

Why can’t we apply access modifiers to blocks?

  • Access modifiers can be applied only to members whom we can access.
  • Blocks we cannot access explicitly as they have no identities.

What are the limitations of private modifier?

  • Private members can be accessed only with in the class.
  • These members are not visible to outside world to access.

What is default access modifier?

  • In java application, default access modifier is package level.
  • If we do not apply any modifier, we can access that member in all the classes of same package.

Why a class cannot be private?

  • Class represents Object in Java application.
  • If complete object is private, that is not visible to outer objects to communicate.
  • Hence complete object cannot be defined as private.

What are the limitations of protected member?

  • Protected members can be accessed within the package and only from sub classes of outer package.
  • We define protected members only in Parent-Child relation.

Why a class cannot be protected?

  • Protected members can be accessed only in Parent-Child relation.
  • If class is protected, it is visible only in hierarchy. Outer objects cannot communicate.

What is Java package?

  • Package is a java folder or directory.
  • Package is a set of related classes, abstract classes and interfaces.

What is the default package in Java?

  • In java “lang” package is the default package.
  • “lang” package classes are directly available into every java application.
  • Using “lang” package classes we can implement basic programming in java.

How to access one class members from another class of same package?

  • In java default access permissions are package level.
  • We can access one class members directly from another class if both the classes are belong to same package.

What is the default access modifier of constructor if class is public?

  • Compiler supplies the default constructor if we don’t define any constructor.
  • if class is package level –> default constructor is also package level.
  • if class in public –> default constructor is public

What is use of “import” keyword?

  • “import” is used to connect the class files of different packages in java application.
  • import statement doesn’t load the class directly into memory.
  • It creates internal pointer to package by which classes will be loaded at runtime.

Can we define more than 1 public class in a single java source file?

  • If class is not public, we must save the class with any name. If class is “public”, we must save with the same name of class name.
  • Hence we cannot place 2 class files in a single java source file of public type.

Does “import” statement loads the class directly?

  • “import” statement doesn’t load the class directly.
  • It creates internal pointer to the package or class by which classes will be loaded while application is running.
  • Unused classes will not be loaded into JVM.

What is “Fully Qualified Name” ?

  • Writing complete package name while using any class in java application.
  • We can replace “import” statement with “Fully Qualified Name”
  • java.util.Random r = new java.util.Random();

What is the advantage of Fully Qualified Name?

  • The main advantage of packages is “Avoid collisions between class names”.
  • We can define 2 class files with the same identity using unique packages only.
  • We can access duplicate classes of different packages only by using “Fully Qualified Name”.

Can we define user class with identity System?

  • Yes allowed.
  • Pre-defined System class file belongs to java.lang package.
  • User-defined System class belongs to any user package.
  • We can access these classes using “Fully Qualified Name”

What is “Class Loader Sub System”?

  • It is responsible for loading the class files into JVM when we use.
  • It gives first priority to Current Working Directory to load the class and then searches in API.

Can we access sub package class by importing super package?

  • Not allowed.
  • We must import sub packages explicitly to access class members of sub package.

How can we create sub packages?

  • We can create sub packages using “package” keyword as follows.
  • Syntax: package outer_package.inner_package
  • Example: package maths.operators.arithmetic ;

Is java fully Object Oriented Programming Language?

  • No
  • Java allows primitive data types and doesn’t support all the OOP features (Multiple inheritance).

What is Encapsulation?

  • Writing Data(variables) and Code(methods) into a single unit.
  • Encapsulation is the concept of protecting Object information.

What is Inheritance?

  • Inheritance is the concept of extending (updating) existing object functionality into a new Object.
  • It is also called “Is-A relation”.

What is Abstraction?

  • Hiding un-necessary details of object and shows only essential features in the process of communication.
  • Abstraction explains what object can do, instead how it does it.

What is Polymorphism?

  • Defining an object(class) that shows different behavior(methods) with one identity.
  • Java supports Compile time and Runtime polymorphism.

What is Abstraction?

  • Hiding un-necessary details of object and shows only essential features in the process of communication.
  • Abstraction explains what object can do, instead how it does it.

Can we access static members using Object reference variable?

  • Yes allowed.
  • Using object reference variable we can access both static members and non-static members in java application.

Can we access static members using “this” keyword?

  • “this” is a pre-defined non static variable which holds object address.
  • Using “this” we can access both static & non-static members in java application.
  • But “this” must be used only in non-static context

Can we define both static & non-static members with same name?

  • Not allowed.
  • With one identity either we can define static members or non-static members.

What is this() method?

  • Used to access current class constructor explicitly.
  • this() must be used only in another constructor of same class.
  • Call to this() must be the first statement in constructor.

Why we use this() method?

  • Used to initialize the object via multiple constructors.
  • Process of connecting constructors in Object creation process is called “Constructor Chaining”.

What is the advantage of Inheritance?

  • The main advantage of inheritance is “code re-usability”.
  • It is the concept of updating object functionality into a new Object.

Does java supports all types of Inheritance?

  • No.
  • Java supports Single, Multi-level and Hierarchical inheritances.
  • It doesn’t support Multiple & Hybrid inheritances.

What is the super class of all java classes ?

  • Object class is the super class of all.
  • It is available in lang package.
  • Every class implicitly inherits Object class to get pre-defined Object behavior.

How to access Parent object functionality from Child class?

  • In Is-A relation, always we need to create object for Child class only.
  • By using Child object address, we can access both Parent and Child objects functionality.

How Parent class gets instantiate in Inheritance?

  • In the process of Child Object creation, JVM instantiates Parent object first.
  • JVM invokes Parent class constructor implicitly to instantiate Parent class.

How to analyze whether JVM instantiating class or not?

  • By defining a default constructor inside the class.
  • In the process of object creation either by JVM(implicitly) or by Programmer(explicitly), constructor calling is mandatory.

What is hierarchical inheritance?

  • Sharing properties to more than one class which are extended.
  • Allowing many classes to update the functionality of existing object.

What is the use of “super” keyword?

  • A pre-defined non static variable.
  • User to access Parent class functionality from Child class.
  • It must be used only in non-static context.

What is super() method ?

  • Used to access Parent class constructor explicitly from the Child class.
  • It must be used only in Child class constructor.

What is the use of super() method?

  • Used to initialize Parent class Object in the process of Child object creation.
  • We cannot access Parent class constructor explicitly while instantiating Child. Hence we use super() method to access Parent’s constructor from Child’s constructor.

How to access super() method ?

  • Must be called only from Child class constructor.
  • Call to super() must be the first statement in the constructor.
  • Hence we cannot connect more than one constructor.

Can we print address of Parent class?

  • Not possible.
  • It Parent-Child object creation process, Parent will not get separate memory allocation.
  • Using Child object address only, we can access both Parent & Child objects functionality.

What is “final” in Java ?

  • “final” is a modifier.
  • It can be applied to class, variables and method.
  • “final” modifier restricts updating permissions of class, method and variable.

Where we user “final” modifier in java ?

  • We can restrict updating permissions of class, method and variable using “final” modifier.
  • “final” class cannot be extended.
  • “final” method cannot be overridden.
  • “final” variable cannot be modified.

What is Method overriding?

  • Defining a method in Child class with the same prototype of Parent.
  • Method overriding is used to update the Parent object functionality in Child class.

Can we override constructor?

  • Not allowed.
  • Constructor name and class name should be same.
  • Constructor definition belongs to single class.
  • What is an abstract class?
  • The partial definition of Object is called abstract class.
  • An abstract class can be defined using the abstract modifier.

What type of method allowed in Abstract class?

  • Abstract class allows Concrete & Abstract methods.

What are Concrete & Abstract methods?

  • Concrete methods having the definition (body).
  • Abstract methods don’t have definitions. These are called specifications of Object.

Can we Instantiate abstract class?

  • No, the abstract class is partially defined hence we can instantiate only classes directly.

Can we define main() inside an abstract class?

  • Yes allowed. JVM invokes the main method directly using its class name.

Can we define the abstract class with no abstract methods?

  • Yes, but it is not recommended because the meaning of abstract class is the partial definition of Object.

How can we instantiate the abstract class?

  • By instantiating its extended (child) class.

Can we place instance variables inside an abstract class?

  • Yes, and we can provide initialization in the process of Child object creation using super() method.

How can we initialize abstract class object?

  • Using the super() method.

Can an abstract class be final?

  • No, the abstract class must be extended whereas final class cannot be extended.

Can an abstract method be final?

  • No, the abstract method must be overridden in the extended class.

Why can abstract method not be static?

  • An abstract method is specific functionality
  • static method is common functionality
  • Hence we cannot combine both abstract & static.

Can a final class extend abstract class?

  • Yes, but it has to implement all the specifications (abstract methods) of an abstract class.

What is the Syntactical error?

  • While writing programs using any programming language, we need to follow some language rules.
  • Violation of these rules produces syntax errors.

What is the Logical error?

  • No error will be generated at the time of compilation and execution. But intended output will not be generated.
  • That means the program is logically incorrect.

What is Exception?

  • Exception is a runtime error.
  • The exception occurs if we violate JVM rules in the application.

Why do we need to handle the exception?

  • Exception Handling is to avoid following problems
  • Abnormal termination of program
  • Informal information to end user.
  • Improper shutdown of resources.

What is Default exception handler?

  • It is a pre-defined program that handles the exception if we don’t handle in the application.

What is the super class of all the Exception classes?

  • java.lang.Throwable class is the super class of all the Exception classes in the hierarchy.

What are the keywords used to handle exceptions?

  • try, catch, throw, throws and finally.

What happens when exception rises in a block or method?

  • Block or Method creates an Exception object with the complete information of runtime error and throws.

How to collect exception object to be handled?

  • By defining Exception type variables inside the catch block, we can collect exception object and handles.

When catch block executes in the application?

  • Only if the exception has risen in the try block.

What is the main advantage of catch block?

  • The catch block is used to avoid abnormal termination of the program.
  • Catch block is used to provide formal information to end user.

Can we collect Exception object into its Parent type variable in Hierarchy?

  • Yes, it is the concept of Runtime binding.

Why we can’t handle errors?

  • An exception occurs while the application is running and these can be handled.
  • Errors not allowed running the application, hence we cannot handle.

What is Unchecked Exception?

  • Sub class of RuntimeException class is Unchecked.
  • Compile will not check whether we handle the exception or not in the application.

What is Checked Exception?

  • Not the sub class of RuntimeException in the hierarchy.
  • Handling is mandatory.
  • The checked exception occurs when java application connected to outer resources.

Can a try block having more than 1 catch block?

  • Yes, and it is used to handle different types of exceptions occurs in different lines of code in the try block.

When we use try with the multi catch?

  • When one transaction is a collection of steps and each step there is a chance of getting an exception.

Can we handle the same exception more than one-time using single try block?

  • No, compiler raises an error message.

Can we handle FileNotFoundException followed by IOException?

  • No, once we handle the top level exception, we cannot handle its child in the same try block.

What is finally block?

  • It is used to place resource releasing code (closing statements) such as File, Database or Server in exception handling process.

When finally block executes?

  • Finally-block executes whether or not an exception has raised in the try block.
  • It executes in case of abnormal termination also.

Can we define only try-finally blocks?

  • Yes, it is allowed.
  • But in case of an exception, the program terminates abnormally after execution of finally block code.

What is the use of “throw” keyword?

  • A programmer can define user exceptions.
  • Pre-defined exception will be raised automatically by JVM.
  • It is used to throw an exception object explicitly.
  • We use throw keyword most used to throw user defined exception class object.

What is re-throwing of Exception?

  • After collecting the object in the catch block, we can re-throw the same exception for further handling.

Can we throw pre-defined exception class object explicitly?

  • Yes, we can create the object for pre defined exception class and it can be thrown.

What is Multi tasking?

  • Performing more than one task simultaneously.

What is the difference between Multi-tasking and Multi-Threading?

  • Task is a program(process)
  • Thread is a sub program(sub process)
  • N process spaces required to execute N tasks
  • N threads executes in a single process space.

How can we create Thread?

  • We can create thread in 2 ways
  • By extending Thread class.
  • By implementing Runnable interface.

When we use Runnable interface to create Thread?

  • To apply the Thread functionality only to Child class in Parent-Child relation.

What are the specifications of Runnable interface?

  • Runnable interface providing only one specification called run().

What is Runnable state?

  • The state in which the Thread waits in the Queue to be started.

What is waiting state in thread life cycle?

  • Once the thread has been started, for any reason logic execution stops until specified time has reached to resume.

How to execute thread logic?

  • By invoking the start() method, we can execute the defined thread logic.

What is Single threaded application?

  • When we invoke Java application, JVM create and Start a default thread called main-thread.
  • If the application is executing only with default thread is called Single threaded application.

What is Multi threaded application?

  • Defining and Executing custom thread along with main thread is called Multi threaded application.

Can we call run() method directly?

  • Yes we can call but  the logic execute from the current thread space sequentially.

What is the use of sleep() method?

  • It is used to stop current thread execute for specified number of milliseconds.

How to check thread properties such as Name & Priority?

  • Using pre-defined methods getName() and getPriority() of Thread class

What is the use of join() method?

  • Used to stop current thread execution until joined(this) thread execution completes.

Can we call start() method concurrently on thread?

  • No, it results IllegalThreadStateException.

What is Runnable object ?

  • The class Object which is implemented from Runnable interface.

Can we start() Runnable object directly?

  • No, Runnable object is not a Thread hence we cannot start directly.
  • We need to construct the Thread object with Runnable object to start.

What is the use of Thread priority?

  • Using priorities of threads only, operating system can schedule the threads from Runnable state.

What is Thread synchronization?

  • It is the concept of allowing threads sequentially when multiple threads are trying to access the resource.

Can we synchronize a class completely?

  • No, we cannot synchronize the Object completely.

What is Non-Daemon thread?

  • Every thread by default is Non daemon thread. Non daemon executes foreground logic of application.

What is Daemon Thread?

  • It is called service thread. It runs behind the application. It provides service to non daemon thread.
  • Daemon thread stops forcefully by JVM when non daemon threads execution completed.

How to create User Daemon thread?

  • By invoking a setDaemon() method of Thread class as soon as Thread object has created.

Can we change Non-Daemon behavior of main thread to Daemon ?

  • No, we cannot change the behavior of thread once it has been started.

What is Garbage Collection?

  • The mechanism of deleting un referenced objects to reuse the memory in the application.

What is Garbage collector thread?

  • A daemon thread that runs behind the application and removes un used objects in the memory.

What is the use of finalize() method ?

  • A method invokes by GC thread before destruction of Thread.
  • It is working like destructor in C++
  • Finalize() method contains resource destruction logic of Object.

Who will create and start Garbage Collector thread ?

  • JVM create and starts Garbage collector thread when required.

What is Factory class?

  • A class cannot be instantiated directly from another class.
  • We can get Factory class object with request to factory method.
  • Factory class can be instantiated many times.

What is Singleton class?

  • A class can be instantiated only once with request to factory method.
  • Object cannot be duplicated but can be shared.

What is Runtime Class in java?

  • A pre defined Singleton class belongs to lang package.
  • Runtime class represents JVM.

How to instantiate Runtime class?

  • By requesting factory method of Runtime class.
  • getRuntime() is the method that returns Runtime class on request.

What is Inner class?

  • Defining a class inside another class.
  • It is also called Nested class.

What are the advantages of Inner class?

  • Inner classes having two benefits.
  • Setting object dependencies
  • Avoid collisions between class names.

How to instantiate non-static inner class?

  • With the instance of Outer class, we can instantiate non static inner class.

Can an inner class be final?

  • Yes an inner class can be final.

What is Anonymous inner class?

  • Defining a class inside block or method or constructor.
  • Local inner class scope belongs the method in which it has defined.

Can we access private members of Inner class from Outer class?

  • Yes we can access the private functionality of Inner class from Outer class.

Can we apply access modifiers to Local inner class?

  • No, we cannot apply. Local inner class is scope inside that same block hence access modifiers need not be applied.

What is anonymous inner class?

  • Defining a class without identity.
  • It is one kind of local inner class because the definition inside the method