1. What is the difference between JDK, JRE, and JVM?
JDK (Java Development Kit) is a software development kit that includes tools for developing Java applications, JRE (Java Runtime Environment) is a software package that provides the runtime environment for executing Java programs, and JVM (Java Virtual Machine) is an abstract computing machine that provides the runtime environment in which Java bytecode can be executed.
2. Explain the principle of OOP (Object-Oriented Programming) and its core concepts.
OOP is a programming paradigm based on the concept of objects, which can contain data and code. Its core concepts include encapsulation, inheritance, and polymorphism, facilitating modular and reusable code design.
3. What is the difference between abstract class and interface? When would you use one over the other?
An abstract class can have method implementations and member variables, while an interface cannot contain method implementations or member variables, only method declarations. Abstract classes are used when there is a need for a default behavior or when there are common characteristics among classes, while interfaces are used when multiple unrelated classes need to share common method signatures.
4. What are the access modifiers in Java? Explain each one.
Java access modifiers control the visibility and accessibility of classes, methods, and variables. "Public" allows unrestricted access, "protected" allows access within the same package or subclasses, "default" allows access within the same package only, and "private" restricts access to within the same class.
5. What is method overloading and method overriding? Provide examples
Method overloading involves defining multiple methods in the same class with the same name but different parameters, while method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.Example:javaCopy code// Method overloading void calculate(int x) {...} void calculate(int x, int y) {...} // Method overriding class Parent { void display() {...} } class Child extends Parent { @Override void display() {...} }
6. Explain the significance of the "static" keyword in Java
: In Java, the "static" keyword signifies that a variable or method belongs to the class itself rather than instances of the class, enabling access without needing to instantiate the class.
7. What are the different types of inheritance in Java? Provide examples.
In Java, the different types of inheritance include single inheritance, where a class inherits from only one superclass, and multiple inheritance, which is achieved through interfaces where a class can implement multiple interfaces.Example of single inheritanceclass Animal { void eat() { System.out.println("Animal eats"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } } Example of multiple inheritance using interfaces:javaCopy codE interface A { void methodA(); } interface B { void methodB(); } class MyClass implements A, B { public void methodA() { System.out.println("Method A"); } public void methodB() { System.out.println("Method B"); } }
8. What is the purpose of the "final" keyword in Java? How is it used?
The "final" keyword in Java is used to restrict the modification of classes, methods, and variables, making them immutable or unextendable.
9. How does exception handling work in Java? Explain the try-catch-finally block.
: In Java, exception handling allows code to handle unexpected errors gracefully. The try-catch-finally block is used to handle exceptions: code within the try block is executed, and if an exception occurs, it's caught by the catch block, and finally block is executed regardless of whether an exception occurred or not, typically used for cleanup tasks.
10. What is multithreading? How can you achieve it in Java?
Multithreading is the ability of a CPU or a single core in a multi-core processor to provide multiple threads of execution concurrently. In Java, you can achieve multithreading by extending the Thread class or implementing the Runnable interface and overriding the run() method, then starting the thread using the start() method.