Java Interview Questions | Page 9

Question 81:

What is generics and its advantage?

Prior to Java 5 there was high risk of ClassCastException at run time working with collection classes.
For example :-

               ArrayList aList = new ArrayList();
               aList.add(new Integer(10));

              for(Object object : aList){
                            String output=(String) object; //type casting leads to ClassCastException at runtime

Above code compiles fine but throws ClassCastException at runtime because we are trying to cast Object in the list to String whereas the list contains element element that is of Integer type.

To avoid such condition Generics was added in Java 5. Generics provide compile-time check and remove risk of ClassCastException at runtime.
Lets see the above code snippet using Generics :-

                         List<String> aList = new ArrayList<String>();
                         aList.add(new Integer(5)); //compiler error

In this case if we try to add elements other than String in the list, the program gives compile time error.

Question 82:

What is difference between List<? extends T> and List <? super T> ?

If we wish to restrict a type to a specific type or to a subtype of that specific type, then we use the following notation:-

              <T extends UpperBoundType>

Similarly, If we wish to restrict a type to a specific type or to a supertype of that specific type, then we use the following notation:-

              <T super LowerBoundType>

Question 83:

Will the given snippet will compile successfully?If not, why?

 public class Example {
             public void print(Set<Boolean> strSet) { }
             public void print(Set<Integer> intSet) { }

In generics due to type erasure, Above class will have two overloaded methods that will have the same signature which contradicts to overloading.

Note:- The overloads would all share the same classfile representation and will generate a compile-time error.

Question 84:

Describe Java Reflection.

Reflection is considered as an advanced technique, which makes possible to examine and modify classes, interfaces, fields and methods at runtime.
The java.lang.Class class provides methods that can be used to get metadata, examine and change the run time behavior of a class.
The java.lang and java.lang.reflect packages provide classes for java reflection.

Question 85:

Give some examples where reflection is used?

  • IDEs using auto completion functionality to get method names and attribute names.
  • Tomcat web container to forward the request to correct module by parsing their web.xml files and request URI.
  • JUnit uses reflection to enumerate all methods in a class; assuming either testXXX named methods as test methods or methods annoted by @Test.
  • Spring; dependency injection.
  • Code Analyzer tools.

Question 86:

What are the drawbacks of reflection?

  • Performance Issue :- As Reflection resolves the types dynamically, it involves processing like scanning the classpath to find the class to load which causes slow performance of the system.
  • Security Issues :- Reflection can access information that is usually not allowed in non-reflexive code. For example:- Reflection allows you to access private methods and fields in a class.
  • High Maintenance :- Reflection code is hard to understand and debug, also any issues with the code can’t be found at compile time because the classes might not be available, making it less flexible and hard to maintain.
  • Security Restrictions :– Reflection requires runtime permissions that might not be available for system running under security manager. This can cause you application to fail at runtime because of security manager.
Question 87:

What is static import?

Static import is used to import the static methods and variables of any class.
Syntax :- import static fully_qualified_name_of_class;
For example:-

  • import static java.lang.Math.PI;
  • import static java.lang.System.out;
Question 88:

What is Marker interface?

Interface without any method or field i.e an empty interface is known as marker interface in java. It is used to convey to the JVM that the class implementing an interface of this category will have some special behavior.
For example :-

  • Serializable
  • Clonnable
  • Remote
Question 89:

 Describe strictfp keyword in Java.

 strictfp is a keyword in Java programming language which is used to ensure the precision of floating point calculation to IEEE's 754 standard to ensure portability.

Question 90:

Why multiple inheritance is not supported in Java?

Let us consider below scenario:- 

Multiple Inheritance

Assume B and C are overriding inherited method and their own implementation. Now D inherits both B & C using multiple inheritance. D should inherit the overridden method.The Question is which overridden method will be used? Will it be from B or C? Here we have an ambiguity. To exclude such situation multiple inheritance was not used in Java.

Share interview questions


comments powered by Disqus


Social Media