In these kinds of posts, the questions and the answers remain almost the same, the difference lies in the explanation you like.
Will try to make it as short as possible and try to cover as much content as I can.
These are based on the interview process, so each question might have a follow-up question.
Let’s get going.
Q1. So Why do you choose Java?
A1. Some features Java has:
- Object-Oriented
- Platform Independent
- Multithreading Support
- Secure, Robust, Distributed, Dynamic, Architecture neutral, Portable, High performance
Q2. How java is Platform Independent?
A2. Java is both a compiler and interpreter language. When you compile a Java program it creates a .class file which is a collection of byte code, these byte code are not machine instructions instead they are instructions that JVM can understand. Since every Java program runs on a JVM, the same byte code can be run on any platform. The key is byte code is not machine instruction they are platform-independent instruction to JVM. On the other hand, JVM or Java virtual machine is platform-dependent because it converts byte code into machine level instruction which is platform-specific and that's why you have a different version of JDK and JRE
Does this also answer - Why write once run anywhere?
.java -> javac compiler -> .class (Run anywhere) -> JVM (Platform dependent conversion) -> Machine Instructions
Q3. What does the Java Platform consist of?
A3. The Java platform has two components:
- The Java Virtual Machine - We have explained in the previous question.
- The Java Application Programming Interface (API)
The platform provides these features and tools:
Development Tools: The development tools for compiling, running, monitoring, debugging, and documenting your applications.
Application Programming Interface (API): A wide range of useful classes related to objects, networking, security, XML generation, database access etc.
Deployment Technologies, User Interface Toolkits, Integration Libraries
Q4. Can you explain the OOPS concepts?
A4. There are 4 OOPS Concept
- Polymorphism
- Inheritance
- Abstraction
- Encapsulation
Will explain each in the below questions.
Q5. What is Polymorphism?
A5. In General - Something that can take many forms.
For Programmers - The same word has a different characteristic in a different context or single action in different ways.
There are 2 types of polymorphism
- Static Polymorphism aka Compile Time Polymorphism
- Achieved by function overloading
- Function Overloading - The function name will be the same but all the other things in the signature can change.
- Return type of methods does not contribute towards different method signature.
- Dynamic Polymorphism aka Run Time Polymorphism
- Achieved by function overriding
- Which Function to call will be determined at runtime
- It uses dynamic method dispatch mechanism to achieve the polymorphism
- The signature will be same.
Example:
class A {
public void method() {
System.out.println("Inside A's method");
}
}
class B extends A {
public void method() {
System.out.println("Inside B's method");
}
}
class C extends A {
public void method() {
System.out.println("Inside C's method");
}
}
class RunTimePolymorphism {
public static void main(String args[]) {
A a = new A();
A b = new B();
A c = new C();
a.method(); //Inside A's method
b.method(); //Inside B's method
c.method(); //Inside C's method
}
}
}
Q6. What is Inheritance?
A6. To inherit the properties from the class.
The class from which we inherit the properties - Parent Class
The class to which we inherit the properties - Child Class
* No private properties are inherited.
Q7. What are the types of Inheritance?
A7. There are 4 types:
- Single Inheritance: Single Child will be extending the parent class.
- Multi-Level Inheritance: The ChildClass is extending ParentClass, the ParentClass is extending GrandParentClass.
- Hierarchical Inheritance: ParentClass has several ChildClasses.
- Hybrid Inheritance: Combination of 2 or more inheritances.
Note: The derived class cannot have a more restrictive access specifier than the parent class.
Q8. What all Access Specifiers are there in Java?
A8. There are 4 types and listed below in restrictive order:
- Public - Acess is available for the whole world.
- Protected - Acess is available for that package and subclasses.
- Default - Acess is available for that package.
- Private - Acess is available for that particular class.
Q9. What is Encapsulation?
A9. Mechanism of wrapping data and code together.
It prevents access of data based on access specifiers.
We try to achieve data hiding.
Implement at the execution level.
Q10. What is Abstraction?
A10. Display only the relevant information to the end-user rather than giving all the unnecessary information.
We try to achieve information hiding.
Implement at the design level.
Q11. What is an Object?
A11. Every object has 2 characteristics. (State and Behaviour).
In terms of programming languages, the state becomes fields or variables, and the behavior is expressed through methods. Methods can be used to change the state of the object. This ensures that the internal implementation is also not revealed thus achieving data encapsulation.
Q12. What is Class?
A12. As we know that there are several objects which share the same state and behavior. The blueprint which they follow is called Class.
Q13. What is an Interface?
A13. The interface gives the class more formal nature, it makes a contract between the class and the outer world, that you need to have all the given methods implemented at the time of compilation for successful compile.
This contract is enforced by the compiler.
Q14. What is a Package?
A14. The package is a namespace to organize all the files which have similar behavior.
Q15. What is Classpath?
A15.
- Classpath in Java is an environment variable used by JVM to locate class files(.class files) in Java during class loading.
- You can override the value of Classpath by using –cp or –classpath while running your application.
- If two classes with the same name exist in Java Classpath then the class which comes earlier in Classpath will be picked by Java Virtual Machine.
- Default CLASSPATH in Java points to the current directory denoted by "." and it will look for any class only in the current directory.
- When you use the -jar command-line option to run your program as an executable JAR, then the Java CLASSPATH environment variable will be ignored, and also the -cp and -classpath switches will be ignored and, In this case, you can set your java classpath in the META-INF/MANIFEST.MF file by using the Class-Path attribute.
- ClassNotFoundException is an Exception and will be thrown when a Java program dynamically tries to load a particular Class at Runtime and don’t find that on Java classpath due to the result of Class.forName() or loadClass() method invocation.
- NoClassDefFoundError comes when a particular class was present in Java Classpath during compile time but not available during runtime on Classpath in Java.
Q16. What is Classloader?
A16. Java class loaders are used to load classes at runtime. ClassLoader in Java works on three principles: delegation, visibility, and uniqueness.
- Delegation principle forward request of class loading to parent class loader and only loads the class if the parent is not able to find or load the class.
- Visibility principle allows child class loader to see all the classes loaded by parent ClassLoader, but parent class loader can not see classes loaded by a child.
- Uniqueness principle allows one to load a class exactly once, which is basically achieved by delegation and ensures that child ClassLoader doesn't reload the class already loaded by a parent.
Below are the 3 types of Classloaders and the location where they will search for files.
- Bootstrap ClassLoader - JRE/lib/rt.jar
- Extension ClassLoader - JRE/lib/ext or any directory denoted by java.ext.dirs
- Application ClassLoader - CLASSPATH environment variable, -classpath or -cp option, Class-Path attribute of Manifest inside JAR file.
Q17. JVM Memory Components
A17. To understand this, I follow a basic question -> What all components Java have? Classes, Objects, Methods and Native Methods.
- Class Area: Class Area stores all the details which a class have such as variables, method - it’s data and code, the runtime constants. (1 per JVM)
- Heap: After classes there are objects. So we need a memory to store the object which are getting created. This is the memory block in which the memory is allocated to the objects. (1 per JVM)
- Stack: Java Stack stores stack frames. It holds local variables and partial results, and plays a part in method invocation and return. Each thread has a private JVM stack, created at the same time as the thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes. (1 per thread)
- I believe everyone has seen stackoverflow exception, that happened when the JVM have no space for a new stack frame to be created, it will throw a StackOverflowError.
- Program Counter Register: PC (program counter) register contains the address of the Java virtual machine instruction currently being executed. (1 per thread)
- Native Method Stack: It contains all the native methods used in the application.
Q18. Is ++ operator thread-safe in Java?
A18. No, it's not thread-safe operator because it involve multiple instructions like reading a value, incriminating it and storing it back into memory which can be overlapped between multiple threads.
The reason I added this question is, If you ever face an issue where the code is written for multi-threaded env and the counter values is not what you are expecting. Then this could be 1 of the reasons behind it.
Q19.Output 3*0.1 == 0.3? true or false?
A19. False because some floating point numbers can not be represented exactly.
This takes me to the last question in this post.
Q20.Which datatype should be used while handling details where precision is required, for eg: Handling Amount?
A20. BigDecimal, because float and double stores approximate values.
#HappyCoding
Top comments (0)