Add fresher-qa module: 50 Java interview questions for freshers, one runnable demo each, asserted transcripts, javac error captures
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5DfjBGLBr7F5km2AN
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
=== Q01 Why is Java platform independent? ===
|
||||
magic = CAFEBABE
|
||||
class file major version = 69
|
||||
=== Q02 JDK vs JRE vs JVM ===
|
||||
running on Java 25
|
||||
JVM: OpenJDK 64-Bit Server VM
|
||||
compiler available: true
|
||||
=== Q03 Primitive types and their sizes ===
|
||||
byte 1 byte, -128 to 127
|
||||
short 2 bytes, -32768 to 32767
|
||||
int 4 bytes, -2147483648 to 2147483647
|
||||
long 8 bytes, -9223372036854775808 to 9223372036854775807
|
||||
float 4 bytes, double 8 bytes, char 2 bytes
|
||||
field defaults: 0, false, null
|
||||
=== Q04 Type casting and overflow ===
|
||||
7 / 2 = 3, (double) 7 / 2 = 3.5
|
||||
(byte) 130 = -126
|
||||
Integer.MAX_VALUE + 1 = -2147483648
|
||||
c + 1 = 66, (char) (c + 1) = B
|
||||
0.1 + 0.2 = 0.30000000000000004
|
||||
Math.addExact: integer overflow
|
||||
=== Q05 Autoboxing and the Integer cache ===
|
||||
127 == 127 -> true
|
||||
128 == 128 -> false
|
||||
128 equals 128 -> true
|
||||
Long 127 equals Integer 127 -> false
|
||||
=== Q06 Is Java pass by value or pass by reference? ===
|
||||
after reassign: original
|
||||
after mutate: original + mutated
|
||||
=== Q07 static variables, static blocks and initialisation order ===
|
||||
static block runs once, when the class loads
|
||||
instance block runs before each constructor body
|
||||
constructor #1
|
||||
instance block runs before each constructor body
|
||||
constructor #2
|
||||
=== Q08 final, finally and finalize ===
|
||||
final array contents changed: 99
|
||||
in try
|
||||
finally always runs
|
||||
Object.finalize forRemoval = true, since 9
|
||||
=== Q09 Stack vs heap ===
|
||||
heap array length = 1000000
|
||||
StackOverflowError after many frames: true
|
||||
@@ -0,0 +1,36 @@
|
||||
=== Q10 The four pillars of OOP ===
|
||||
Circle area = 3.14
|
||||
Rectangle area = 6.00
|
||||
rejected: amount must be positive, balance still 500.0
|
||||
=== Q11 Inheritance and constructor chaining ===
|
||||
Animal constructor
|
||||
Dog constructor
|
||||
Animal constructor
|
||||
Puppy constructor for Bruno
|
||||
=== Q12 Method overloading vs overriding ===
|
||||
FancyPrinter.show(Object)
|
||||
show(String)
|
||||
FancyPrinter.show(Object)
|
||||
=== Q13 Abstract class vs interface ===
|
||||
Scooter has 2 wheels, charging, plug Type 2
|
||||
=== Q14 Polymorphism: what is and is not dynamic ===
|
||||
Child.who() | Parent.field | Parent.static | Child.static
|
||||
=== Q15 Can you override static, private or final methods? ===
|
||||
Base.secret | Base.locked
|
||||
=== Q16 How do you create an immutable class? ===
|
||||
members: [Asha, Ravi]
|
||||
add blocked: UnsupportedOperationException
|
||||
=== Q17 Composition vs inheritance (is-a vs has-a) ===
|
||||
stack = [99, 1, 2], pop gives 2
|
||||
=== Q18 Access modifiers ===
|
||||
a -> public
|
||||
b -> protected
|
||||
c -> (package-private)
|
||||
d -> private
|
||||
=== Q19 Object class methods: toString, equals, hashCode ===
|
||||
default toString looks like ClassName@hex: true
|
||||
p1.equals(p2) = true
|
||||
set contains an equal point: false
|
||||
=== Q20 Constructors: default, chained and private ===
|
||||
default config: dev:8080
|
||||
same Registry instance: true
|
||||
@@ -0,0 +1,31 @@
|
||||
=== Q21 == vs equals() on Strings ===
|
||||
a == b -> true
|
||||
a == c -> false
|
||||
a.equals(c) -> true
|
||||
=== Q22 Why is String immutable? ===
|
||||
s = java
|
||||
upper = JAVA
|
||||
same object returned when nothing changes: true
|
||||
=== Q23 String vs StringBuilder vs StringBuffer ===
|
||||
builder result: cba-
|
||||
append is synchronized: StringBuffer=true, StringBuilder=false
|
||||
=== Q24 The string pool, compile-time constants and intern() ===
|
||||
"hel" + "lo" == "hello" -> true
|
||||
"hel" + lo == "hello" -> false
|
||||
"hel" + finalLo == "hello" -> true
|
||||
("hel" + lo).intern() == "hello" -> true
|
||||
=== Q25 Reverse a string and check for a palindrome ===
|
||||
weivretni
|
||||
true
|
||||
false
|
||||
=== Q26 Useful String methods and the split() surprises ===
|
||||
trim().length() = 6
|
||||
strip().length() = 2
|
||||
" ".isBlank() = true, "ab".repeat(3) = ababab
|
||||
"a,b,,".split(",").length = 2
|
||||
"a,b,,".split(",", -1).length = 4
|
||||
String.join: 2026-11-11
|
||||
=== Q27 Count characters, find the first non-repeating one, check anagrams ===
|
||||
frequency: {s=3, w=1, i=1}
|
||||
first non-repeating: w
|
||||
listen/silent anagrams: true
|
||||
@@ -0,0 +1,49 @@
|
||||
=== Q28 The Collections Framework hierarchy ===
|
||||
ArrayList extends AbstractList implements [List, RandomAccess, Cloneable, Serializable]
|
||||
LinkedList extends AbstractSequentialList implements [List, Deque, Cloneable, Serializable]
|
||||
HashSet extends AbstractSet implements [Set, Cloneable, Serializable]
|
||||
TreeSet extends AbstractSet implements [NavigableSet, Cloneable, Serializable]
|
||||
ArrayDeque extends AbstractCollection implements [Deque, Cloneable, Serializable]
|
||||
HashMap extends AbstractMap implements [Map, Cloneable, Serializable]
|
||||
TreeMap extends AbstractMap implements [NavigableMap, Cloneable, Serializable]
|
||||
=== Q29 ArrayList vs LinkedList ===
|
||||
ArrayList is RandomAccess: true
|
||||
LinkedList is RandomAccess: false
|
||||
LinkedList is also a Deque: true
|
||||
after add(0, 5): [5, 10, 20, 30], get(2) = 20
|
||||
=== Q30 How HashMap works internally ===
|
||||
Aa: hashCode=2112, bucket (16 slots)=0
|
||||
BB: hashCode=2112, bucket (16 slots)=0
|
||||
Ankur: hashCode=63412443, bucket (16 slots)=12
|
||||
both survive a collision: first, second, size 2
|
||||
=== Q31 HashMap vs Hashtable vs ConcurrentHashMap vs TreeMap ===
|
||||
HashMap: null key accepted
|
||||
Hashtable: null key throws NullPointerException
|
||||
ConcurrentHashMap: null key throws NullPointerException
|
||||
TreeMap: null key throws NullPointerException
|
||||
=== Q32 HashSet vs LinkedHashSet vs TreeSet ===
|
||||
HashSet [banana, apple, cherry, pear, mango]
|
||||
LinkedHashSet [pear, apple, mango, banana, cherry]
|
||||
TreeSet [apple, banana, cherry, mango, pear]
|
||||
=== Q33 Comparable vs Comparator ===
|
||||
natural (Comparable): [Student[name=Asha, marks=88], Student[name=Meena, marks=88], Student[name=Ravi, marks=90]]
|
||||
marks desc, then name: [Student[name=Ravi, marks=90], Student[name=Asha, marks=88], Student[name=Meena, marks=88]]
|
||||
=== Q34 Fail-fast iterators and ConcurrentModificationException ===
|
||||
removing 2 inside for-each -> ConcurrentModificationException
|
||||
removing the second-to-last element threw nothing, list = [1, 2, 4]
|
||||
removeIf even -> [1, 3, 5]
|
||||
=== Q35 Arrays.asList() and List.of() pitfalls ===
|
||||
write-through to the array: changed
|
||||
Arrays.asList().add -> UnsupportedOperationException
|
||||
Arrays.asList(int[]) has 1 element
|
||||
List.of(.., null) -> NullPointerException
|
||||
=== Q36 Queue, Deque and PriorityQueue ===
|
||||
PriorityQueue polls: 1 2 3 4 5
|
||||
ArrayDeque as a stack pops: cba
|
||||
=== Q37 Mutable keys and the equals()/hashCode() contract in a HashMap ===
|
||||
get with the mutated key: null
|
||||
get with a fresh "A1" key: null
|
||||
size is still 1
|
||||
=== Q38 unmodifiableList() view vs List.copyOf() snapshot ===
|
||||
view sees the change: [a, b, c]
|
||||
copy does not: [a, b]
|
||||
@@ -0,0 +1,21 @@
|
||||
=== Q39 Checked vs unchecked exceptions ===
|
||||
checked: NoSuchFileException (is an IOException: true)
|
||||
unchecked: ArithmeticException: / by zero
|
||||
NoSuchFileException is checked: true
|
||||
=== Q40 try, catch, finally and return ===
|
||||
finally runs before the method actually returns
|
||||
returned 1
|
||||
primitiveSnapshot = 10
|
||||
finallyOverrides = 7
|
||||
=== Q41 throw vs throws, and custom exceptions ===
|
||||
withdrawal refused <- cause: balance 100 < 500
|
||||
=== Q42 try-with-resources and suppressed exceptions ===
|
||||
open first
|
||||
open second
|
||||
close second
|
||||
close first
|
||||
caught: body failed
|
||||
suppressed: close failed: second
|
||||
=== Q43 Error vs Exception, and multi-catch ===
|
||||
Error, not Exception: Requested array size exceeds VM limit
|
||||
multi-catch caught ClassCastException
|
||||
@@ -0,0 +1,27 @@
|
||||
=== Q44 Lambdas and functional interfaces ===
|
||||
180.0 true 6 hello 5
|
||||
=== Q45 Streams: map, filter, collect, laziness ===
|
||||
[ASHA, ARJUN]
|
||||
{4=2, 5=2}
|
||||
nothing visited yet: no terminal operation
|
||||
visited asha
|
||||
first = asha
|
||||
reuse -> stream has already been operated upon or closed
|
||||
=== Q46 Optional ===
|
||||
expensive() called
|
||||
orElse : value
|
||||
orElseGet : value
|
||||
nothing here
|
||||
=== Q47 Records ===
|
||||
Point[x=1, y=2] equals Point[x=1, y=2]: true, x() = 1
|
||||
rejected: negative coordinate
|
||||
=== Q48 var, switch expressions and text blocks ===
|
||||
{"name": "asha",
|
||||
"grade": "A"}
|
||||
=== Q49 Method references and default methods ===
|
||||
abc
|
||||
walk and swim
|
||||
=== Q50 Threads: start() vs run(), and virtual threads ===
|
||||
running on: main
|
||||
running on: worker-1
|
||||
virtual: true
|
||||
@@ -0,0 +1,38 @@
|
||||
=== javac FinalOverride.java ===
|
||||
FinalOverride.java:6: error: locked() in Derived cannot override locked() in Base
|
||||
String locked() { return "Derived.locked"; }
|
||||
^
|
||||
overridden method is final
|
||||
1 error
|
||||
|
||||
=== javac CheckedNotHandled.java ===
|
||||
CheckedNotHandled.java:6: error: unreported exception IOException; must be caught or declared to be thrown
|
||||
String s = Files.readString(Path.of("missing.txt"));
|
||||
^
|
||||
1 error
|
||||
|
||||
=== javac LossyCast.java ===
|
||||
LossyCast.java:3: error: incompatible types: possible lossy conversion from double to int
|
||||
int x = 3.5;
|
||||
^
|
||||
LossyCast.java:4: error: incompatible types: possible lossy conversion from int to byte
|
||||
byte b = 200;
|
||||
^
|
||||
2 errors
|
||||
|
||||
=== javac MissingReturnAndUnreachable.java ===
|
||||
MissingReturnAndUnreachable.java:4: error: missing return statement
|
||||
}
|
||||
^
|
||||
1 error
|
||||
|
||||
=== javac --release 21 PreSuper.java ===
|
||||
PreSuper.java:8: error: flexible constructors is not supported in -source 21
|
||||
super();
|
||||
^
|
||||
(use -source 25 or higher to enable flexible constructors)
|
||||
1 error
|
||||
|
||||
=== javac --release 25 PreSuper.java ===
|
||||
compiled without errors
|
||||
|
||||
Reference in New Issue
Block a user