Java Interview Questions - Core Faq - 1

Java is Pass by Value or Pass by Reference?

Java is pass-by-value. When you pass a primitive data type or an object reference as an argument to a method, a copy of the value/reference is passed, and modifications to the parameter within the method do not affect the original variable.

Is null a keyword?

Yes, in Java, null is a keyword that represents the absence of a value or a reference that does not point to any object.

Are true and false keywords?

No, true and false are not keywords in Java. They are boolean literals, which are used to represent true and false values in boolean expressions.

What is a JAR file?

A JAR (Java ARchive) file is a compressed file format that packages Java class files, associated metadata, and resources into a single archive. It simplifies the distribution of Java libraries and applications.

How to run a JAR file through the command prompt?

You can run a JAR file through the command prompt using the java -jar command followed by the name of the JAR file. For example, to run "myapp.jar," you would use the command: java -jar myapp.jar.

How to get the path of a running JAR file?

You can get the path of a running JAR file using the following code:

String path = YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getPath();

What is type casting?

Type casting is the process of converting a variable of one data type to another data type, either implicitly or explicitly, to perform operations or assignments that require matching data types.

What is Upcasting?

Upcasting is the casting of a subclass object to its superclass type. It happens implicitly and is safe since a subclass inherits all the properties of its superclass.

What is Downcasting?

Downcasting is the casting of a superclass object to its subclass type. It requires an explicit cast and may cause a runtime exception if the object is not actually an instance of the subclass.

Can a double value be cast to a byte?

Yes, a double value can be cast to a byte, but it may result in loss of precision and range. The value will be truncated to fit into the byte's range, which is -128 to 127.

Why not use Double or Float to represent currency?

Using Double or Float to represent currency is not recommended due to precision issues with floating-point arithmetic, which can lead to rounding errors. Instead, it's better to use BigDecimal for precise currency calculations.

What is the default value of Boolean?

The default value of a Boolean in Java is false.

What is the default value of local variables?

Local variables in Java do not have default values. They must be explicitly initialized before being used, or the compiler will raise an error.

What are the advantages of a Java enum?

Java enums provide a type-safe way to represent a fixed set of constants. They improve code clarity, prevent invalid values, and support additional methods, making code more maintainable and less error-prone.

Can Enum extend any class in Java?

No, an enum in Java cannot extend any class because enums implicitly extend the java.lang.Enum class, and Java does not support multiple inheritance.

What is the importance of the main method in Java?

The main method is the entry point of a Java program. It is crucial because it is where the program starts execution, and it allows you to run the application and interact with its functionalities.

Why is the main() method declared static?

The main() method is declared static in Java because it belongs to the class rather than an instance of the class. Being static, it can be called without creating an object, allowing the JVM to run the program without creating instances.

Can we execute a program without main() method?

No, we cannot execute a Java program without a main() method. The JVM looks for the main() method to start the execution, and if it is not present, the program won't run.

Should a main() method be compulsorily declared in all Java classes?

No, a main() method is not compulsory in all Java classes. It is only required in the class that serves as the entry point of the program.

Does the order of public and static declaration matter in main()?

No, the order of the public and static declarations in the main() method doesn't matter. The JVM looks for the method signature, which is public static void main(String[] args), regardless of the order of modifiers.

Can the main() method in Java return any data?

No, the main() method in Java is declared with a void return type, meaning it does not return any data explicitly. Its purpose is to start the program's execution and not to provide a return value.

How can we execute any code even before the main method in Java?

We can execute code before the main method in Java by using static blocks or static initialization of classes. These blocks are executed when the class is loaded, which happens before the main method is called.

Can I override a static method in Java?

No, static methods cannot be overridden in Java. They are associated with the class itself, and the method resolution is determined at compile-time based on the reference type, not the object's actual type.

What are the restrictions imposed on a static method or a static block of code?

In Java, a static method cannot access instance (non-static) variables directly, as it operates at the class level, not on individual objects. Additionally, a static block can only contain static members initialization and cannot refer to instance variables.

Can we call a non-static method from inside a static method?

Yes, we can call a non-static method from inside a static method, but we need to create an instance of the class containing the non-static method before calling it.

Can you override a private or static method in Java?

No, private and static methods cannot be overridden in Java. Private methods are not accessible outside the class, and static methods are associated with the class itself, not with individual instances.

What is the dot (.) operator in Java?

The dot (.) operator in Java is used to access members (fields and methods) of a class or object. It is used to specify the scope and navigate the hierarchy of classes.

What is the % operator?

The % operator in Java is the modulus operator, which returns the remainder of the division between two numbers. For example, 10 % 3 would return 1, as 10 divided by 3 equals 3 with a remainder of 1.

Can a for statement loop indefinitely?

Yes, a for statement can loop indefinitely if the loop condition always evaluates to true or if there is no termination condition, like for(;;). It is essential to include a proper termination condition to prevent infinite loops.

How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

Unicode characters are represented using 16 bits, ASCII uses 7 bits (extended ASCII can use 8 bits), UTF-16 uses 16 bits, and UTF-8 uses a variable number of bits, typically between 8 and 32 bits, to represent characters.

Which non-Unicode letter characters may be used as the first character of an identifier?

In Java, the first character of an identifier must be a letter (Unicode character) or a dollar sign ($) or an underscore (_). The subsequent characters can be letters, digits, dollar signs, or underscores.

What is hashCode?

hashCode is a method in the Object class, which is used to compute a hash code (an integer) for an object. It is often used in hash-based data structures like HashMap and HashSet to efficiently store and retrieve objects.

What is the difference between super() and this()?

super() is used to call a constructor from the parent class, while this() is used to call a constructor from the same class. super() is typically used to invoke the parent class's constructor during inheritance, and this() is used for constructor chaining within the same class.

Last Updated : 28 July 2023