0% found this document useful (0 votes)
4 views3 pages

Java Code Snippets Interview Preparation

The document provides Java code snippets relevant for Technical Support Engineer interviews, covering key concepts such as string comparison, exception handling, static blocks, immutability, integer caching, method overriding, null pointer exceptions, concurrent modification exceptions, deadlocks, and lambda expressions. Each snippet includes code, output, and explanations to clarify the concepts. This serves as a practical reference for understanding Java fundamentals and common pitfalls.

Uploaded by

kaipumawa
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)
4 views3 pages

Java Code Snippets Interview Preparation

The document provides Java code snippets relevant for Technical Support Engineer interviews, covering key concepts such as string comparison, exception handling, static blocks, immutability, integer caching, method overriding, null pointer exceptions, concurrent modification exceptions, deadlocks, and lambda expressions. Each snippet includes code, output, and explanations to clarify the concepts. This serves as a practical reference for understanding Java fundamentals and common pitfalls.

Uploaded by

kaipumawa
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

Java Code Snippets for Technical Support Engineer

Interviews

1. String Comparison
String s1 = "Java";
String s2 = "Java";
String s3 = new String("Java");

[Link](s1 == s2);
[Link](s1 == s3);
[Link]([Link](s3));

Output:
true
false
true

Explanation: == compares references, equals() compares values.

2. Finally Block
try {
int x = 10 / 0;
}
catch(Exception e) {
[Link]("Exception");
}
finally {
[Link]("Finally");
}

Output:
Exception
Finally

Explanation: Finally block executes whether exception occurs or not.

3. Static Block
static {
[Link]("Static Block");
}

public static void main(String[] args) {


[Link]("Main Method");
}

Output:
Static Block
Main Method

Explanation: Static block executes before main method.

4. String Immutable
String s = "Java";
[Link](" World");
[Link](s);

Output:
Java

Explanation: Strings are immutable in Java.

5. Integer Caching
Integer a = 127;
Integer b = 127;

Integer x = 128;
Integer y = 128;

[Link](a == b);
[Link](x == y);

Output:
true
false

Explanation: Java caches Integer values from -128 to 127.

6. Method Overriding
class Parent {
void show() {
[Link]("Parent");
}
}

class Child extends Parent {


void show() {
[Link]("Child");
}
}

Output:
Child

Explanation: Method overriding demonstrates runtime polymorphism.

7. NullPointerException
String s = null;
[Link]([Link]());

Output:
NullPointerException

Explanation: Calling methods on null objects causes NPE.

8. ConcurrentModificationException
List<String> list = new ArrayList<>();
[Link]("A");
[Link]("B");

for(String s : list) {
[Link](s);
}

Output:
ConcurrentModificationException

Explanation: Modifying collection during iteration throws exception.

9. Deadlock Concept
Thread t1 locks A then waits for B.
Thread t2 locks B then waits for A.

Output:
Deadlock

Explanation: Both threads wait forever for each other's locks.

10. Lambda Expression


Test t = () -> [Link]("Lambda");
[Link]();

Output:
Lambda

Explanation: Lambda expressions provide concise implementation of functional interfaces.

You might also like