Top 8 Tricky Java Questions ๐Ÿš€

Jaspreet Singh Sodhi
3 min readMay 25, 2023

--

  1. Why java is not 100% object oriented Language ?

Because java uses primitive data types such as. โ€” byte , char , bool ,int float, long ,short , & in order to make them Object Oriented what java does they made wrapper classes over them to make object of that classes

2. Why Java donโ€™t uses pointers ?

Since in java JVM is responsible for implicit memory locations there is no points of using pointers as it will contradict things & will make the things unsafe.

3. What is JIT Compiler in Java ?

JIT Complier ~ Just in time compiler is a part of JVM & it is used to convert byte codes into machine understandable codes. So how things work here is

Conversion of .java source code files to ~ .class (byte codes) by the Javac compiler this is being done on compile time

Now what happen at runtime, these .class files are loaded by JVM & with the help of an interpreter these .class files are converted to machine understandable codes.

But when JVM enables this JIT compiler there it compiles the .class files into more efficient native code thus , further JVM uses those native codes to directly execute the logic instead of compiling , which alternatively increases efficiency.

4. Why String is immutable in Java ?

String is immutable in java because string pools requires the string to be immutable.

String pool is a storage space in the Java heap memory where string literals are stored.

Also string is being is shared on different areas like file system , database connections then it can be user id or passwords so it is quite unsafe if things are not immutable & can be changed by any third party person or software.

5. Can you override a private or static method in java ?

You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding.

& in case of private -

Similarly, you cannot override a private method in sub class because itโ€™s not accessible there. What you can do is create another private method with the same name in the child class.

6. Does finally Every time executes in Java ?

Not in following cases-

โ€œSystem.exit()โ€ function

system crash

7. What is Object class in java & methods under it ?

Object class is present in java.lang package. Object class acts as a root of the inheritance hierarchy in any Java Program.

Important Methods ~

equals : This method is used for checking the equality of contents between two objects as per the specified business logic , while == checks for references

clone ~ Creates and returns a copy of this object.

finalise ~ this method runs when there is no references to any object , & it is about to be collected by garbage collector .

public final Class getClass() : Returns the runtime class of an object.

public int hashCode(): Returns a hash code value for the object.

public String toString(): Returns a string representation of the object.

8. How can we Making the class immutable in Java ?

~ Make the class final

~ make all feilds private so that direct access is not allowed

~ dont provide setter methods for variables

~ make all mutable feilds final so that its value can be assigned only once

~ initialise all the feilds via constructor performing deep copy

~ perform cloning of objects in the getter methods to return a copy

~ rather than returning the actual object reference.

Hope you liked it & it was helpful some where!

Thank you !

--

--

Jaspreet Singh Sodhi
Jaspreet Singh Sodhi

Written by Jaspreet Singh Sodhi

Full Stack Software Engineer | Curating Top-Notch Content @jaspreet.dev on Instagram โœจ

No responses yet