Category : Java | Sub Category : Java Interview questions | By Prasad Bonam Last updated: 2020-10-20 10:09:18 Viewed : 917
Core Java Inteview Questions
JVM stands for Java Virtual Machine. It simulates a real computer and provides the runtime environment for running java applications.
In the first step, the java source code is converted to a byte code (binary code that can be understood by the JVM : instruction set of the JVM) by the java compiler (javac).
This byte code is converted by the JVM into a machine code (binary). Then, the machine code is run by the computer that has the JRE installed.
Actually, JVM interprets the byte code and runs the java program.
It uses the class libraries, and other files provided in JRE in order to accomplish this task.
JRE is an acronym of Java Runtime Environment.
Java Runtime Environment is an executable file that includes JVM, class libraries (util, math,etc), and other files.
JRE doesn`t include any development tool like compiler, debugger, etc.
JRE = JVM + Java standard classes (math, lang, util, etc) + runtime libraries
JREs are available for download in Oracle website. There are many versions for each hardware configuration (32 bits/ 64 bits) or operating system (windows, linux, Mac OS, etc).
JDK stands for Java Development Kit.
It is an executable or a set of tools created by sun Microsystems that is used for creating java application.
JDK=JRE+ java compiler (javac) + debugger + other development tools.
JIT compiler stands for Just In Time Compiler.
A JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it`s called) into a form that`s usually faster, typically the host CPU`s native instruction set.
A JIT has access to dynamic runtime information whereas a standard compiler doesn`t and can make better optimizations like inlining functions that are used frequently.
This is in contrast to a traditional compiler that compiles all the code to machine language before the program is first run (check this link for more details stackoverflow).
The extension of a source file in java is “.java”
The byte code is different from the machine code.
This is a binary code that the JVM can
understand and interpret. The byte code is written in the instruction set of
the JVM.
The JVM is like a real computer that has a CPU having an instruction set. This instruction set is used inside the byte code.
The extension of a compiled file in java is “.class”. This file contains
A platform can be software (Operating system, etc) or a hardware platform (hardware architecture, CPU family, etc).
The JVM is platform dependant whereas our java code is platform independent.
The Java executable file for desktop applications is an executable Jar (Java Archive).
This Jar file is an archive (a compressed file that contains .class files). You can unzip it like any rar or zip archive
We can break platform independency when we write a program that looks for specific OS related files or when using JNI (Java Native Interface).
The class loader is a piece of software packaged in the JRE. Its role is to dynamically load Java classes into the JVM.
JVM must at least include one class loader which is the primordial (or bootstrap) class loader.
The classes are loaded in java when needed. The first class loaded is the one that has a static main method.
The Java Virtual Machine must know where to find the project`s compiled classes.
It is not appropriate that the JVM looks through every folder on your machine in order to find your compiled classes.
So, we have to provide the JVM with the directories to use to look up for our compiled classes.
This is done by putting those directories in the classpath.
So, the classpath contains the paths used by the JVM (classloader) in order to find our compiled classes or the libraries used.
·
Numeric types attributes (int, float, double, etc) take zero as
a default value.
· Reference type attributes take null as a default value.
Those variables have whatever value when accessed before their initialization. We can`t predict their values before their initialization.
Only one public class per source file is allowed.
The allowed modifiers for a class are nothing or the public modifier.
When declaring an attribute in a class without specifying its visibility, it has the default visibility. The default visibility is the package visibility.
This keyword contains a reference to the current class.
A constructor is a method that initializes the attributes of an object when it is created. The constructor is called when using the new keyword.
A constructor is a method inside a java class that respects those conditions:
·
Has the same name as the class
· Has no return type
The default constructor is the constructor that has no arguments. This constructor is automatically generated by the compiler if we don`t write it explicitly.
When overloading constructor, the compiler will not auto-generate the default constructor for us. So, we should write the default constructor by ourselves.
This can cause many problems when using the well known frameworks like hibernate, spring, etc
No, the constructor is not inherited. We can call the constructor of the parent class explicitly using the super keyword.
Yes, a constructor can be
overloaded.
No,
overriding concept is related to inheritance. The constructor is not inherited.
No, a constructor cannot be final.
25.
What means a final class?
We cannot make child
classes from this class. We cannot inherit from a final class.
The method cannot be overridden.
The variable is
constant. We cannot change its value once it is initialized.
This is a variable
declared as final and not initialized when declaring it.
The blank final variable should be initialized in the constructor of the class if it is not static.
If it is static, we should initialize it in a static block.
If we don`t respect these rules, we will get an exception.
Yes
It is a
class that has the abstract keyword in its declaration. This class cannot be
instantiated. It is created to be inherited.
A class that has at least one abstract method is an abstract class.
It is a method that has no implementation and is marked with the abstract keyword.
Method overriding is used when creating a method that is provided in the parent class in a child class. This concept is tightly related to inheritance.
NO, we can`t override static methods because they belong to the class itself and not to the instance.
Yes, there is no problem.
Method overriding is used inside a single class. It is used when creating, in the same class, many methods that have the same name but differ by the list of arguments.
Method overloading enhances the readability of a program because we aren`t forced to create method of different names that make the same thing.
No, keeping the same list of parameters and changing only the return type will cause a compiler error.
For those who are familiar with C++, virtual methods are used to make dynamic binding. They are related to the polymorphism concept.
In JAVA, all the methods are virtuals.
Method Overloading |
Method Overriding |
Inside the same class |
In the child classes: related to
inheritance |
Different parameters list |
Same parameters list |
A class
B inherits from class A. The class B inherits some attributes and method from
the class A under certain conditions (public and protected members are
inherited).
In this
case, class A is called the parent class (super class) and class B is called the
child class.
So, the child class can use the inherited members without having to copy/paste them in the code of the class.
The inheritance allows the reuse of the code of the parent class inside child classes.
The
super keyword is used inside the child class to call a method present in the
parent class explicitly using the dot notation.
We can call a constructor present in the parent class using the super keyword.
No, because each one
among the two calls needs to be the first statement.
We can put inside an interface:
· abstract methods: we can omit the abstract keyword
Abstract
class |
interface |
abstract
classes has abstract methods and implemented methods. So, the child class
needs only to implement the abstract |
The implementing
class must implement all the methods of the interface or it will be |
methods to be non
abstract. |
abstract. |
abstract classes don`t
have this restriction. |
The interface can only
have final data. |
Multiple inheritance is not allowed for classes. |
An interface can
inherit from many interfaces |
The abstract keyword
must be used |
The abstract keyword
is not used even when declaring methods. |
A
static variable belongs to the class itself whereas instance variable belongs
to an instance created from this class.
The
static variable is shared between all the instances of a class. It is accessed
using the class name and the dot notation.
Example:
ClassName.staticVariableName
A
static method belongs to the class itself whereas instance method belongs to an
instance created from this class.
The
static method can be called without creating an instance of the class. It is
accessed using the class name and the dot notation.
Example:
ClassName.staticMethodName(params)
We can`t use not static
attributes inside a static method and vice versa.
We don`t need to create an object from the principal class in order to call the main method.
It is a
bloc of code (instructions present between curl braces) marked with the static
keyword.
It is used in a class to
initialize static attributes in the class loading phase.
We have composition when
we use an attribute of type ClassB inside a classA.