What is JVM? Why is Java called the Platform Independent Programming Language?
Answer: A Java virtual machine (JVM) is a process virtual machine that can execute Java bytecode. Each Java source file is compiled into a bytecode file, which is executed by the JVM. Java was designed to allow application programs to be built that could be run on any platform, without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible because it is aware of the specific instruction lengths and other particularities of the underlying hardware platform.
What is the Difference between JDK and JRE?
Answer: The Java Runtime Environment (JRE) is the Java Virtual Machine (JVM) where your Java programs are being executed. It also includes browser plugins for applet execution. The Java Development Kit (JDK) is the full-featured Software Development Kit for Java, including the JRE, the compilers, and tools (like Javadoc, and Java Debugger), for a user to develop, compile and execute Java applications.
What does the “static” keyword mean? Can you override the private or static method in Java?
Answer: The static keyword denotes that a member variable or method can be accessed, without requiring an instantiation of the class to which it belongs. A user cannot override static methods in Java, because method overriding is based upon dynamic binding at runtime and static methods are statically blinded at compile time. A static method is not associated with any instance of a class, so the concept is not applicable.
Can you access a non-static variable in a static context?
Answer: A static variable in Java belongs to its class and its value remains the same for all its instances. A static variable is initialized when the class is loaded by the JVM. If your code tries to access a non-static variable, without any instance, the compiler will complain, because those variables are not created yet and are not associated with any instance.
What are the Data Types supported by Java? What are Autoboxing and Unboxing?
Answer: The eight primitive data types supported by the Java programming language are:
- byte
- short
- int
- long
- float
- double
- Boolean
- char
Autoboxing is the automatic conversion made by the Java compiler between the primitive types and their corresponding object wrapper classes. For example, the compiler converts an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this operation is called unboxing.
What is Function Overriding and Overloading in Java?
Method overloading in Java occurs when two or more methods in the same class have the same name, but different parameters. On the other hand, method overriding is defined as the case when a child class redefines the same method as a parent class. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides.
What is a Constructor, Constructor Overloading in Java, and Copy-Constructor?
A constructor gets invoked when a new object is created. Every class has a constructor. In case the programmer does not provide a constructor for a class, the Java compiler (Javac) creates a default constructor for that class. The constructor overloading is similar to method overloading in Java. Different constructors can be created for a single class. Each constructor must have its unique parameter list. Finally, Java does support copy constructors like C++, but the difference lies in the fact that Java doesn’t create a default copy constructor if you don’t write your own
Does Java support multiple Inheritance?
No, Java does not support multiple inheritances. Each class can extend only to one class but can implement more than one interface.
What is the difference between an Interface and an Abstract class?
Java provides and supports the creation both of abstract classes and interfaces. Both implementations share some common characteristics, but they differ in the following features:
1 . All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract.
- A class may implement a few numbers of Interfaces but can extend only one abstract class.
- For a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract.
- Abstract classes can implement interfaces without even providing the implementation of interface methods.
- Variables declared in a Java interface by default final. An abstract class may contain non-final variables.
- Members of a Java interface are public by default. A member of an abstract class can either be private, prote cited, or public.
- An interface is abstract and cannot be instantiated. An abstract class also cannot be instantiated but can be invoked if it contains the main method.
What are pass-by-reference and pass-by values?
When an object is passed by value, this means that a copy of the object is passed. Thus, even if changes are made to that object, it doesn’t affect the original value. When an object is passed by reference, this means that the actual object is not passed, rather a reference of the object is passed. Thus, any changes made by the external method, are also reflected in all places.
What are the basic interfaces of Java Collections Framework?
Java collection framework provides a well-designed set of interfaces and classes that support operations on a collection of objects. The most basic interfaces that reside in the Java Collections Framework are:
- Collection, which represents a group of objects known as its elements.
- Set, which is a collection that cannot contain duplicate elements.
- List, which is an ordered collection and can contain duplicate elements.
- Map, which is an object that maps keys to values and cannot contain duplicate keys.
Why doesn’t Collection extend Cloneable and Serializable interfaces?
The Collection interface specifies groups of objects known as elements. Each concrete implementation of a Collection can choose its way of how maintaining and ordering its elements. Some collections allow duplicate keys, while some other collections don’t. The semantics and the implications of either cloning or serialization come into play when dealing with actual implementations. Thus, the concrete implementations of collections should decide how they can be cloned or serialized.
What is an Iterator?
The Iterator interface provides a few numbers of methods that can iterate over any Collection. Each Java Collection contains the iterator method that returns an Iterator instance. Iterators can remove elements from the underlying collection during the iteration.
What differences exist between Iterator and List-Iterator?
The differences between these elements are listed below:
- An Iterator can be used to traverse the Set and List collections, while the List-Iterator can be used to iterate only over Lists.
- The Iterator can traverse a collection only in the n forward direction, while the List-Iterator can traverse a List in both directions.
- The List-Iterator implements the Iterator interface and contains extra functionality, such as adding an element, replacing an element, getting the index position for previous and next elements, etc.
What is the difference between fail-fast and fail-safe?
The Iterator’s fail-safe property works with the clone of the underlying collection and thus, it is not affected by any modification in the collection. All the collection classes are in java. util package is fail-fast, while the collection classes are in java. util. concurrent is fail-safe. Fail-fast iterators throw a Concurrent Modification Exception, while fail-safe iterator never throws such an exception.
How does HashMap work in Java?
A HashMap in Java stores key-value pairs. The HashMap requires a hash function and uses hash-Code and equals methods, to put and retrieve elements to and from the collection respectively. When the put method is invoked, the HashMap calculates the hash value of the key and stores the pair in the appropriate index inside the collection. If the key exists, its value is updated with the new value. Some important characteristics of a HashMap are its capacity, its load factor, and the threshold resizing.
What is the importance of hash-Code () and equals () methods?
In Java, a HashMap uses the hash-Code and equals methods to determine the index of the key-value pair and to detect duplicates. More specifically, the hash-Code method is used to determine where the specified key will be stored. Since different keys may produce the same hash value, the equals method is used, to determine whether the specified key exists in the collection or not. Therefore, the implementation of both methods is crucial to the accuracy and efficiency of the HashMap.
What differences exist between HashMap and Hash table?
Both the HashMap and Hash table classes implement the Map interface and thus, have very similar characteristics. However, they differ in the following features:
- A HashMap allows the existence of null keys and values, while a Hash table doesn’t allow either null keys or null values.
- A Hash-table is synchronized, while a HashMap is not. Thus, HashMap is preferred in single-threaded environments, while a Hash table is suitable for multi-threaded environments.
- A HashMap provides its set of keys, and a Java application can iterate over them. Thus, a HashMap is fail-fast. On the other hand, the Hash table provides an Enumeration of its keys.
- The Hash table class is considered a legacy class.
GIPHY App Key not set. Please check settings