#Java #interview #Questions #Core
Interview Questions
Java Core
- What are the differences between JRE, JVM, and JDK?
- What access modifiers exist?
-
What does the
final
keyword indicate? - What are the default values for uninitialized variables?
-
What do you know about the
main()
function? - What logical operations and operators do you know?
- What is the ternary conditional operator?
- What bitwise operations do you know?
-
Where and for what is the
abstract
modifier used? - Define the term “interface”. What are the default modifiers for fields and methods in interfaces?
- How does an abstract class differ from an interface? When should an abstract class be used, and when should an interface be used?
- Why are methods not defined in some interfaces at all?
-
Why can’t a method in an interface be declared as
final
? - What has a higher level of abstraction - a class, an abstract class, or an interface?
-
Can an object access a class member declared as
private
? If so, how? - What is the order of invocation for constructors and initialization blocks, considering the class hierarchy?
- Why are initialization blocks needed and what types are there?
-
To which constructs in Java is the
static
modifier applicable? - Why are static initialization blocks used in Java?
- What happens if an exception occurs in an initialization block?
- What exception is thrown when an error occurs in a class initialization block?
- Can a static method be overridden or overloaded?
- Can non-static methods overload static ones?
- Is it possible to narrow the access level or return type when overriding a method?
-
In overriding a method, is it possible to change: the access modifier, return type, argument type or their count, argument names or their order; remove, add, or change the order of elements in the
throws
section? - How to access overridden methods of a parent class?
- Can a method be declared as both abstract and static at the same time?
- What is the difference between an instance member of a class and a static member of a class?
- Where is the initialization of static/non-static fields allowed?
- What types of classes are there in Java?
- Tell me about nested classes. When are they used?
- What is a “static class”?
- What features exist in the use of nested classes: static and inner? What is the difference between them?
- What is a “local class”? What are its features?
- What are “anonymous classes”? Where are they used?
- How can a nested class access the field of its outer class?
-
What is the purpose of the
assert
operator? - What are Heap and Stack memory in Java? What is the difference between them?
- Is the statement true that primitive data types are always stored on the stack, and instances of reference data types are on the heap?
- How are variables passed to methods: by value or by reference?
- What is the purpose of a garbage collector?
- How does a garbage collector work?
- What types of garbage collectors are implemented in the HotSpot virtual machine?
- Describe the algorithm of any garbage collector implemented in the HotSpot virtual machine.
- What is a “string pool”?
-
What is
finalize()
? What is it needed for? -
What happens to the garbage collector if the method
finalize()
takes a significant amount of time, or an exception is thrown during execution? -
What are the differences between
final
,finally
, andfinalize()
? - Talk about type casting. What is narrowing and widening of type?
-
When could an application throw a
ClassCastException
? - What are literals?
- What is autoboxing in Java and what are the rules for wrapping primitive types in wrapper classes?
-
What are the features of the
String
class? -
Why is the
String
class immutable and final? -
Why is
char[]
preferred overString
for storing a password? -
Why is a string a popular key in
HashMap
in Java? -
What does the method
intern()
do in theString
class? -
Can strings be used in a
switch
statement? -
What is the main difference between
String
,StringBuffer
, andStringBuilder
? -
What is the
Object
class? What methods does it have? - Define the term “constructor”.
- What is a “default constructor”?
- What are the differences between a default constructor, a copy constructor, and a parameterized constructor?
- Where and how can you use a private constructor?
- Tell me about class loaders and dynamic class loading.
- What is Reflection?
-
What is the purpose of
equals()
. How does it differ from the==
operation? -
If you want to override
equals()
, what conditions must be met? -
What are the properties of the equivalence relation generated by
equals()
? -
Rules for overriding the method
Object.equals()
. -
What is the relationship between
hashCode()
andequals()
? -
If
equals()
is overridden, are there any other methods that should be overridden? -
What happens if you override
equals()
without overridinghashCode()
? What issues may arise? -
How are the methods
hashCode()
andequals()
implemented in theObject
class? -
What is the purpose of the
hashCode()
method? -
What are the rules for overriding the method
Object.hashCode()
? -
Are there any recommendations on which fields to use when calculating
hashCode()
? -
Can different objects have the same
hashCode()
? -
If a class
Point{int x, y;}
implements the methodequals(Object that) {(return this.x == that.x && this.y == that.y)}
but makes the hash codeint hashCode() {return x;}
, can such points be correctly stored and extracted from aHashSet
? -
Can different objects
(ref0 != ref1)
haveref0.equals(ref1) == true
? -
Can different references to the same object
(ref0 == ref1)
haveref0.equals(ref1) == false
? -
Can the
equals(Object that) {return this.hashCode() == that.hashCode()}
method be implemented like this? -
In
equals()
, it is required to check that the argumentequals(Object that)
is of the same type as the object itself. What is the difference betweenthis.getClass() == that.getClass()
andthat instanceof MyClass
? -
Is it possible to implement the
equals()
method for theMyClass
class like this:class MyClass {public boolean equals(MyClass that) {return this == that;}}
? -
There is a class
Point{int x, y;}
. Why is a hash code like31 * x + y
preferable tox + y
? - Tell me about object cloning.
- What is the difference between shallow and deep cloning?
- What cloning method is preferable?
-
Why is the
clone()
method declared in theObject
class and not in theCloneable
interface?