Q #1) What are the features in JAVA?
Ans: Features of Java:
- Oops concepts
- Object-oriented
- Inheritance
- Encapsulation
- Polymorphism
- Abstraction
- Platform independent: A single program works on different platforms without any modification.
- High Performance: JIT (Just In Time compiler) enables high performance in Java. JIT converts the bytecode into machine language and then JVM starts the execution.
- Multi-threaded: A flow of execution is known as a Thread. JVM creates a thread which is called main thread. The user can create multiple threads by extending the thread class or by implementing Runnable interface.
Q1. Explain JDK, JRE and JVM?
JDK | JRE | JVM |
It stands for Java Development Kit. | It stands for Java Runtime Environment. | It stands for Java Virtual Machine. |
It is the tool necessary to compile, document and package Java programs. | JRE refers to a runtime environment in which java bytecode can be executed. | It is an abstract machine. It is a specification that provides run-time environment in which java bytecode can be executed. |
Along with JRE, it includes an interpreter/loader, a compiler (javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in Java development. In short, it contains JRE + development tools. | It implements the JVM (Java Virtual Machine) and provides all the class libraries and other support files that JVM uses at runtime. So JRE is a software package that contains what is required to run a Java program. Basically, it’s an implementation of the JVM which physically exists. | JVM follows three notations: Specification(document that describes the implementation of the Java virtual machine), Implementation(program that meets the requirements of JVM specification) and Runtime Instance (instance of JVM is created whenever you write a java command on the command prompt and run class). |
JDK JVM JRE
JDK: JDK provides all the
tools,
executables
and binaries required to
compile, debug and execute
a Java Program.
JVM :The execution part is handled by JVM to provide machine independence.
(JRE) is the implementation of JVM. JRE consists of JVM and java binaries and other classes to execute any program successfully.
Types of relationships in Java
Inheritance
Student is A Person
eagle is A bird
elephant is an animal
Association
(has a)
a)Aggregation
When once class is used in another but can exist independently
Student has contactInfo
b)Composition
When once class is used in another but once cannot exist without other independently
Student has studentIdcard
https://www.youtube.com/watch?v=5pDg_7018CQ
Q7. What is association?
Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. These relationship can be one to one, One to many, many to one and many to many.
Q8. What do you mean by aggregation?
Aggregation is a specialized form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let’s take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy.
Q9. What is composition in Java?
Composition is again specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.
In case you are facing any challenges with these java interview questions, please comment your problems in the section below. Apart from this Java Interview Questions Blog, if you want to get trained from professionals on this technology, you can opt for a structured training from edureka!
What is Classloader in Java?
Java Classloader is the program that loads byte code program into memory when we want to access any class. We can create our own classloader
- by extending ClassLoader class and overriding loadClass(String name) method. Learn more at java classloader.
- by extending ClassLoader class and overriding loadClass(String name) method. Learn more at java classloader.
What are different types of classloaders?
There are three types of built-in Class Loaders in Java:
- Bootstrap Class Loader – It loads JDK internal classes, typically loads rt.jar and other core classes.
- Extensions Class Loader – It loads classes from the JDK extensions directory, usually $JAVA_HOME/lib/ext directory.
- System Class Loader – It loads classes from the current classpath that can be set while invoking a program using -cp or -classpath command line options.
What is diamond problem in Java?
https://www.journaldev.com/1775/multiple-inheritance-in-java
What is static keyword?
static keyword can be used with class level variables to make it global i.e all the objects will share the same variable.
static keyword can be used with methods also. A static method can access only static variables of class and invoke only static methods of the class.
What is static import?
If we have to use any static variable or method from other class, usually we import the class and then use the method/variable with class name.
import java.lang.Math;
//inside class
double test = Math.PI * 5;
We can do the same thing by importing the static method or variable only and then use it in the class as if it belongs to it.
import static java.lang.Math.PI;
//no need to refer class now
double test = PI * 5;
Use of static import can cause confusion, so it’s better to avoid it. Overuse of static import can make your program unreadable and unmaintainable.
What is static block?
Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader. It is used to initialize static variables of the class. Mostly it’s used to create static resources when class is loaded.
What is difference between Heap and Stack Memory?
Major difference between Heap and Stack memory are as follows:
- Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution.
- Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.
- Memory management in the stack is done in a LIFO manner whereas it’s more complex in Heap memory because it’s used globally.
Q10. What are the differences between Heap and Stack Memory?
The major difference between Heap and Stack memory are:
Features | Stack | Heap |
---|---|---|
Memory | Stack memory is used only by one thread of execution. | Heap memory is used by all the parts of the application. |
Access | Stack memory can’t be accessed by other threads. | Objects stored in the heap are globally accessible. |
Memory Management | Follows LIFO manner to free memory. | Memory management is based on generation associated to each object. |
Lifetime | Exists until the end of execution of the thread. | Heap memory lives from the start till the end of application execution. |
Usage | Stack memory only contains local primitive and reference variables to objects in heap space. | Whenever an object is created, it’s always stored in the Heap space. |
Java is Pass by Value or Pass by Reference?
This is a very confusing question, we know that object variables contain the reference to the Objects in heap space. When we invoke any method, a copy of these variables is passed and gets stored in the stack memory of the method. We can test any language whether it’s pass by reference or pass by value through a simple generic swap method, to learn more read Java is Pass by Value and Not Pass by Reference.
Can we use String with switch case?
One of the Java 7 feature was improvement of switch case of allow Strings. So if you are using Java 7 or higher version, you can use String in switch-case statements. Read more at Java switch-case String example.
What is instanceof keyword?
We can use instanceof keyword to check if an object belongs to a class or not. We should avoid it’s usage as much as possible. Sample usage is:
What is the use of System class?
Java System Class is one of the core classes. One of the easiest way to log information for debugging is System.out.print() method.
System class is final so that we can’t subclass and override its behavior through inheritance. System class doesn’t provide any public constructors, so we can’t instantiate this class and that’s why all of its methods are static.
Some of the utility methods of System class are for array copy, get the current time, reading environment variables. Read more at Java System Class.
command to run jar
What is Enum in Java?
Enum was introduced in Java 1.5 as a new type whose fields consists of fixed set of constants. For example, in Java we can create Direction as enum with fixed fields as EAST, WEST, NORTH, SOUTH.
enum is the keyword to create an enum type and similar to the class. Enum constants are implicitly static and final. Read more in detail at java enum.
What is Java Annotations?
Java Annotations provide information about the code and they have no direct effect on the code they annotate. Annotations are introduced in Java 5. Annotation is metadata about the program embedded in the program itself. It can be parsed by the annotation parsing tool or by the compiler. We can also specify annotation availability to either compile time only or till runtime also. Java Built-in annotations are @Override, @Deprecated and @SuppressWarnings. Read more at java annotations.What is Java Reflection API? Why it’s so important to have?
Java Reflection API provides the ability to inspect and modify the runtime behavior of java application. We can inspect a java class, interface, enum and get their methods and field details. Reflection API is an advanced topic and we should avoid it in normal programming. Reflection API usage can break the design pattern such as Singleton pattern by invoking the private constructor i.e violating the rules of access modifiers.Even though we don’t use Reflection API in normal programming, it’s very important to have. We can’t have any frameworks such as Spring, Hibernate or servers such as Tomcat, JBoss without Reflection API. They invoke the appropriate methods and instantiate classes through reflection API and use it a lot for other processing.Read Java Reflection Tutorial to get in-depth knowledge of reflection api.
What is composition in java?
Composition is the design technique to implement has-a relationship in classes. We can use Object composition for code reuse.Java composition is achieved by using instance variables that refer to other objects. The benefit of using composition is that we can control the visibility of other objects to client classes and reuse only what we need. Read more with example at Java Composition example.What is the benefit of Composition over Inheritance?
One of the best practices of Java programming is to “favor composition over inheritance”. Some of the possible reasons are:- Any change in the superclass might affect subclass even though we might not be using the superclass methods. For example, if we have a method test() in the subclass and suddenly somebody introduces a method test() in the superclass, we will get compilation errors in the subclass. The composition will never face this issue because we are using only what methods we need.
- Inheritance exposes all the superclass methods and variables to the client and if we have no control in designing superclass, it can lead to security holes. Composition allows us to provide restricted access to the methods and hence more secure.
- We can get runtime binding in composition where inheritance binds the classes at compile time. So composition provides flexibility in the invocation of methods.
What is Garbage Collection?
Garbage Collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. In Java, process of deallocating memory is handled automatically by the garbage collector.
We can run the garbage collector with code
Runtime.getRuntime().gc()
or use utility method System.gc()
. For a detailed analysis of Heap Memory and Garbage Collection, please read Java Garbage Collection.
Why should we use finalize ?
https://howtodoinjava.com/java/basics/why-not-to-use-finalize-method-in-java/
https://howtodoinjava.com/java/basics/why-not-to-use-finalize-method-in-java/
What is this keyword?
this keyword provides the reference to the current object and it’s mostly used to make sure that object variables are used, not the local variables having the same name.
What does super keyword do?
super keyword can be used to access super class method when you have overridden the method in the child class.
We can use super keyword to invoke superclass constructor in child class constructor but in this case, it should be the first statement in the constructor method.
No comments:
Post a Comment