Java Interview Imp Questions
Java Interview Imp Questions
**Answer:**
- **JRE (Java Runtime Environment)**: It is a part of the JDK and provides the
environment to run Java applications. It includes the JVM and libraries
necessary for executing Java programs.
**Answer:**
- **Inheritance**: Mechanism by which one class can inherit the fields and
methods of another class, promoting code reusability.
**Answer:**
- `==` checks for reference equality, meaning it checks whether two
references point to the same object in memory.
**Answer:**
**Answer:**
**Answer:**
An interface in Java is a reference type that is similar to a class but can only
contain abstract methods (without body) and static final variables
(constants). Interfaces are used to define a contract that classes must follow.
A class can implement multiple interfaces, allowing for multiple inheritance
of types.
**Answer:**
**Answer:**
**Answer:**
- **default** (no modifier): The member is accessible only within its own
package.
**Answer:**
a. Answer:
i. JDK (Java Development Kit): A software development
kit that includes JRE, tools for compiling Java programs, and
other development tools.
ii. JRE (Java Runtime Environment): A part of JDK that
provides the libraries, Java Virtual Machine (JVM), and other
components necessary to run Java applications.
iii. JVM (Java Virtual Machine): An abstract machine that
enables Java bytecode to be executed, providing the
environment for running Java applications.
iv.
4. What is a Class and an Object in Java?
a. Answer:
i. Class: A blueprint for creating objects, defining properties
(attributes) and behaviors (methods).
ii. Object: An instance of a class. It contains data and
methods defined in the class.
iii.
5. What is the difference between a constructor and a method?
a. Answer:
i. A constructor is a special method invoked when an object
is created, used to initialize the object's properties. It has
the same name as the class and does not have a return
type.
ii. A method is a block of code that performs a specific task
and can be called when needed. It has its own name and
can return a value.
6. What is the 'main' method in Java?
a. Answer: The main method is the entry point for any standalone
Java application. It has a specific signature: public static void
main(String[] args). The JVM looks for this method to start the
execution of the program.
b.
7. Explain the concept of inheritance in Java.
java
Copy code
this.name = name;
java
Copy code
void sound () {
System.out.println("Bark" );
java
Copy code
package com.example.myapp;
java
Copy code
java
Copy code
}
9. What are the differences between the 'sleep' and 'wait'
methods in Java?
a. Answer:
i. sleep(long millis): Causes the current thread to sleep for a
specified time. It releases the CPU but keeps the lock.
ii. wait(): Causes the current thread to wait until another
thread calls notify() or notifyAll(). It releases the lock on the
object.
1. What is Java?
Java is a high-level, class-based, object-oriented programming language
designed to have as few implementation dependencies as possible. It is a
platform-independent language, meaning it can run on any platform that has
a Java Virtual Machine (JVM).
18. Simple and Secure: It has simple syntax and provides security
features.
11. JRE (Java Runtime Environment): Provides libraries and the JVM
necessary to run Java applications.
12. JVM (Java Virtual Machine): It is the part of JRE that converts
bytecode into machine-specific code.
Overriding: A method in a subclass has the same name, return type, and
parameters as in the parent class, redefining its behavior.
LinkedList: Implements a doubly linked list and is faster for insertions and
deletions but slower for random access.
Thread: A class that can be extended to create a new thread. It also has the
option to implement the Runnable interface.
You said:
More questions with coding and all type
ChatGPT said:
ChatGPT
Sure! Here are more Java interview questions, including coding problems,
categorized by different topics:
Copy code
Copy code
Copy code
Copy code
3. Object-Oriented Programming
3.1. Create a Class and Object
Question: Create a class Dog with attributes and methods.
java
Copy code
4. Collections Framework
4.1. Use of ArrayList
Question: Write a method to demonstrate adding, removing, and iterating
over an ArrayList.
java
Copy code
import java.util.*;
System.out.println(frequencyMap);
Multithreading
System.out.println("Thread is running...");
Synchronization Example
Question: Write a program to demonstrate thread synchronization.
class Counter {
count++;
return count;
Counter counter;
MyThread(Counter counter) {
this.counter = counter;
}
public void run() {
counter.increment();
t1.start();
t2.start();
t1.join();
t2.join();
Advanced Java
6.1. Java Streams
Question: Write a program to filter a list of numbers using Java Streams.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
Lambda Expressions
Question: Write a program to demonstrate the use of lambda expressions.
import java.util.Arrays;
import java.util.List;
names.forEach(name -> {
System.out.println(name);
});
}
Sorting Algorithms
8.1. Implement Bubble Sort
Question: Write a method to implement bubble sort.
int n = arr.length;
arr[j + 1] = temp;
bubbleSort(arr);
}
Exception Handling
7.1. Custom Exception
Question: Create a custom exception in Java.
class AgeNotValidException extends Exception {
super(message);
try {
validateAge(16);
} catch (AgeNotValidException e) {