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:
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
- Java Interview Questions-Core Faq - 2
- Java Interview Questions-Core Faq - 3
- Features of Java Programming Language (2024)
- Difference between Java and JavaScript?
- What is the difference between JDK and JRE?
- What gives Java its 'write once and run anywhere' nature?
- What is JVM and is it platform independent?
- What is Just-In-Time (JIT) compiler?
- What is the garbage collector in Java?
- What is NullPointerException in Java
- Difference between Stack and Heap memory in Java
- How to set the maximum memory usage for JVM?
- What is numeric promotion?
- Generics in Java
- Static keyword in Java
- What are final variables in Java?
- How Do Annotations Work in Java?
- How do I use the ternary operator in Java?
- What is instanceof keyword in Java?
- How ClassLoader Works in Java?
- What are fail-safe and fail-fast Iterators in Java
- What are method references in Java?
- "Cannot Find Symbol" compile error
- Difference between system.gc() and runtime.gc()
- How to convert TimeStamp to Date in Java?
- Does garbage collection guarantee that a program will not run out of memory?
- How setting an Object to null help Garbage Collection?
- How do objects become eligible for garbage collection?
- How to calculate date difference in Java
- Difference between Path and Classpath in Java
- Is Java "pass-by-reference" or "pass-by-value"?
- Difference between static and nonstatic methods java
- Why Java does not support pointers?
- What is a package in Java?
- What are wrapper classes in Java?
- What is singleton class in Java?
- Difference between Java Local Variable, Instance Variable and a Class Variable?
- Can a top level class be private or protected in Java
- Are Polymorphism , Overloading and Overriding similar concepts?
- Locking Mechanism in Java
- Why Multiple Inheritance is Not Supported in Java
- Why Java is not a pure Object Oriented language?
- Static class in Java
- Difference between Abstract class and Interface in Java
- Why do I need to override the equals and hashCode methods in Java?
- Why does Java not support operator overloading?
- Anonymous Classes in Java
- Static Vs Dynamic class loading in Java
- Why am I getting a NoClassDefFoundError in Java?
- How to Generate Random Number in Java
- What's the meaning of System.out.println in Java?
- What is the purpose of Runtime and System class in Java?
- The finally Block in Java
- Difference between final, finally and finalize
- What is try-with-resources in java?
- What is a stacktrace?
- Why String is immutable in Java ?
- What are different ways to create a string object in Java?
- Difference between String and StringBuffer/StringBuilder in Java
- Difference between creating String as new() and literal | Java
- How do I convert String to Date object in Java?
- How do I create a Java string from the contents of a file?
- What actually causes a StackOverflow error in Java?
- Why is char[] preferred over String for storage of password in Java
- What is I/O Filter and how do I use it in Java?
- Serialization and Deserialization in Java
- Understanding transient variables in Java
- What is Externalizable in Java?
- What is the purpose of serialization/deserialization in Java?
- What is the Difference between byte stream and Character streams
- How to append text to an existing file in Java
- How to convert InputStream object to a String in Java
- What is the difference between Reader and InputStream in Java
- Introduction to Java threads
- Synchronization in Java
- Static synchronization Vs non static synchronization in Java
- Deadlock in Java with Examples
- What is Daemon thread in Java
- Implement Runnable vs Extend Thread in Java
- What is the volatile keyword in Java
- What are the basic interfaces of Java Collections Framework
- Difference between ArrayList and Vector | Java
- What is the difference between ArrayList and LinkedList?
- What is the difference between List and Set in Java
- Difference between HashSet and HashMap in Java
- Difference between HashMap and Hashtable in Java?
- How does the hashCode() method of java works?
- Difference between capacity() and size() of Vector in Java
- What is a Java ClassNotFoundException?
- How to fix java.lang.UnsupportedClassVersionError