[Go to site: main page, start]

0% found this document useful (0 votes)
5 views10 pages

Interview Java Questions

Java is a high-level, object-oriented programming language that is platform-independent due to its use of the Java Virtual Machine (JVM). Key concepts include OOP principles such as encapsulation, inheritance, and polymorphism, as well as differences between various Java components like JDK, JRE, and JVM. The document also covers exception handling, collections framework, multithreading, and advanced Java topics like garbage collection and functional interfaces.

Uploaded by

nandhitha5318b
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Interview Java Questions

Java is a high-level, object-oriented programming language that is platform-independent due to its use of the Java Virtual Machine (JVM). Key concepts include OOP principles such as encapsulation, inheritance, and polymorphism, as well as differences between various Java components like JDK, JRE, and JVM. The document also covers exception handling, collections framework, multithreading, and advanced Java topics like garbage collection and functional interfaces.

Uploaded by

nandhitha5318b
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Core Java

1. What is Java?

Java is a high-level, object-oriented, platform-independent programming language. It follows


the principle "Write Once, Run Anywhere" (WORA) using the JVM.

2. Why is Java platform independent?

Java source code is compiled into bytecode, which runs on the Java Virtual Machine
(JVM) available on different operating systems.

3. Difference between JDK, JRE, and JVM?


Componen Purpose
t

JVM Executes Java bytecode

JRE JVM + Libraries required to run Java programs

JDK JRE + Development tools (javac, debugger,


etc.)

4. What are the main OOP concepts?

●​ Encapsulation
●​ Inheritance
●​ Polymorphism
●​ Abstraction

5. What is Encapsulation?

Binding data and methods together and restricting direct access using access modifiers.

class Student {
private int id;

public void setId(int id){


[Link] = id;
}
}
6. Difference between Abstraction and Encapsulation?
Abstraction Encapsulation

Hides implementation Hides data

Achieved by abstract Achieved using access


classes/interfaces modifiers

7. What is Polymorphism?

One interface, multiple forms.

Compile Time

add(int a,int b)
add(double a,double b)

Run Time​
Method Overriding.

8. Difference between Method Overloading and Overriding?


Overloading Overriding

Same method name, different Same method


parameters signature

Compile-time polymorphism Runtime polymorphism

Same class Parent-child class

9. What is inheritance?

Acquiring properties of one class into another.

class Animal {}
class Dog extends Animal {}

10. Can Java support multiple inheritance?

Not through classes.

class A {}
class B {}
// class C extends A,B -> Not allowed

But possible using interfaces.

String Questions
11. Difference between String, StringBuilder, StringBuffer?
String StringBuilder StringBuffer

Immutable Mutable Mutable

Slow Fast Thread-safe

12. Why are Strings immutable?

●​ Security
●​ Thread safety
●​ String pool optimization

13. What is String Pool?

Special memory area in heap where string literals are stored.

String s1="Java";
String s2="Java";

Both refer to same object.

14. Difference between == and equals()?


String a="Java";
String b=new String("Java");

== → compares references

equals() → compares content


Exception Handling
15. What is Exception?

An unwanted event disrupting program execution.

16. Difference between Checked and Unchecked Exception?

Checked

●​ IOException
●​ SQLException

Unchecked

●​ ArithmeticException
●​ NullPointerException

17. Difference between throw and throws?


throw throws

Explicitly throws exception Declares


exception

Inside method Method signature

18. Can we have multiple catch blocks?

Yes.

try{
}
catch(IOException e){
}
catch(Exception e){
}

19. Difference between final, finally, finalize()?


final finally finalize()
Keyword Block Method

Constant Cleanup Garbage


code collection

Collections Framework
20. Difference between Array and ArrayList?
Array ArrayList

Fixed size Dynamic

Faster More flexible

21. Difference between List and Set?


List Set

Allows duplicates No duplicates

Ordered Unordered

22. Difference between HashMap and Hashtable?


HashMap Hashtable

Not synchronized Synchronized

Allows null Doesn't allow null

23. Difference between HashMap and TreeMap?


HashMap TreeMap

No ordering Sorted

O(1) O(log n)
average

24. What is Hashing?


Converting a key into a hash code to locate data quickly.

25. Difference between ArrayList and LinkedList?


ArrayList LinkedList

Dynamic array Doubly linked list

Fast retrieval Fast insertion/deletion

Multithreading
26. What is Thread?

Smallest unit of execution.

27. How to create a thread?

Method 1

class MyThread extends Thread {


public void run() {
[Link]("Running");
}
}

Method 2

class MyTask implements Runnable {


public void run() {
}
}

28. Difference between Thread and Runnable?

Runnable is preferred because Java supports only single inheritance.

29. What is Synchronization?


Ensures only one thread accesses critical resources at a time.

synchronized void display(){


}

30. What is Deadlock?

Two threads waiting for each other forever.

Advanced Java
31. What is JVM Memory Structure?

●​ Method Area
●​ Heap
●​ Stack
●​ PC Register
●​ Native Method Stack

32. Difference between Heap and Stack?


Heap Stack

Objects Local variables


stored

Shared Thread-specific

33. What is Garbage Collection?

Automatic memory cleanup of unused objects.

34. What are Wrapper Classes?

Convert primitive data types into objects.

int → Integer
double → Double
char → Character
35. What is Autoboxing?

Automatic conversion:

Integer num = 10;

Primitive int → Integer.

36. What is Interface?

Blueprint with abstract methods.

interface Animal {
void sound();
}

37. Difference between Abstract Class and Interface?


Abstract Class Interface

Can have Cannot


constructors

Partial abstraction Full abstraction

38. What is Functional Interface?

Interface containing exactly one abstract method.

@FunctionalInterface
interface Demo {
void show();
}

Examples:

●​ Runnable
●​ Comparator

39. What is Lambda Expression?

Short way of implementing functional interfaces.


(a,b) -> a+b

40. What is Stream API?

Used to process collections efficiently.

[Link]()
.filter(x -> x > 10)
.forEach([Link]::println);

Frequently Asked Coding Interview


Questions
Reverse a String
String str="hello";
String rev="";

for(int i=[Link]()-1;i>=0;i--){
rev+=[Link](i);
}

Check Palindrome
String s="madam";

String rev=new StringBuilder(s)


.reverse()
.toString();

[Link]([Link](rev));

Swap Without Third Variable


a=a+b;
b=a-b;
a=a-b;

Find Largest Element


int max=arr[0];
for(int i=1;i<[Link];i++){
if(arr[i]>max)
max=arr[i];
}

You might also like