Java Interview Question

1. What will be the output
final int i = 10;
i++;
System.out.println(i);
Answer: Get compilation Error. You cannot reinitialize the final value.

2. Will the code compile
final List<String> list = new ArrayList<String>();
list.add("e");
Answer: Yes, code will compile. list reference is final but we can add a member of it.

3.immutable Properties
◦The instance variable of the class is final i.e. we cannot change the value of it after creating an object.
◦The class is final so we cannot create the subclass.
◦There is no setter methods i.e. we have no option to change the value of the instance variable.

4. Will the code Compile
 class Base{
  private void get(){
   System.out.println("base A");
  }
 }

 class Derive extends Base{
  void get(){
   System.out.println("deri ");
  }
 }

Answer: Code will compile. Derive class get method consider as method. Hence It accept


4.1 Will the code Compile
 class Base{
  public boolean process(int a, int b){
   return true;
  }
 }

 class Derive extends Base{
  public String process(int a, int b){
    return "deri ";
  }
 }

Answer: Code will not compile. Return type of the process method is different between the base and derived class. Return type should the same.

5.Will the code compile & run
 class Base{
  protected void get(){
   System.out.println("base A");
  }
 }

 class Derive extends Base{
  @Override
  public void get(){
   System.out.println("deri ");
  }
 }

 public Test{
 public static void main(String[] args){
   Base d = new Derive();
   d.get();
  }
 }
Answer: Yes, It will compile & run

6.Java Annotations
Java Annotation is a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used
by java compiler and JVM.
Annotations in java are used to provide additional information, so it is an alternative option for XML and java marker interfaces.

7.Built-In Java Annotations used in other annotations
◦@Target
◦@Retention
◦@Inherited
◦@Documented


8.Will it compile?
class Base{
 public void get() throws Exception{
  System.out.println("base A");
 }
}
class Derive extends Base{
 @Override
 public void get() throws IOException{
  System.out.println("base A");
 }
}
Answer:
If the superclass method does not declare an exception ◦
 If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
◦If the superclass method declares an exception ◦
 If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent
exception.

9.Why we use @Override annotation?
@Override annotation assures that the subclass method is overriding the parent class method. If it is not so, compile time error occurs.
Sometimes, we does the silly mistake such as spelling mistakes etc. So, it is better to mark @Override annotation that provides assurity that method is overridden.

10. Write a custom annotation?
https://www.javatpoint.com/custom-annotation

@interface MyAnnotation{ 
 int value1() default 1; 
 String value2() default ""; 
 String value3() default "xyz"; 
 } 

How to apply Multi-Value Annotation
Let's see the code to apply the multi-value annotation.
@MyAnnotation(value1=11,value2="ram",value3="Chennai")

11.Built-in Annotations used in custom annotations in java
◦@Target
◦@Retention
◦@Inherited
◦@Documented

@Target tag is used to specify at which type, the annotation is used.
The java.lang.annotation.ElementType enum declares many constants to specify the type of element where annotation is to be applied such as TYPE, METHOD, FIELD etc.
Let's see the constants of ElementType enum:
TYPE class, interface or enumeration
FIELD  fields
METHOD methods
CONSTRUCTOR  constructors
LOCAL_VARIABLE local variables
ANNOTATION_TYPE annotation type
PARAMETER parameter

12. custom annotation: creating, applying and accessing annotation
@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
@interface MyAnnotation{ 
int value(); 

//Applying annotation 
class Hello{ 
@MyAnnotation(value=10) 
public void sayHello(){System.out.println("hello annotation");} 

 
//Accessing annotation 
class TestCustomAnnotation1{ 
public static void main(String args[])throws Exception{ 
 
Hello h=new Hello(); 
Method m=h.getClass().getMethod("sayHello"); 
 
MyAnnotation manno=m.getAnnotation(MyAnnotation.class); 
System.out.println("value is: "+manno.value()); 
}} 

13. @Retention
@Retention annotation is used to specify to what level annotation will be available.
RetentionPolicy.SOURCE refers to the source code, discarded during compilation. It will not be available in the compiled class. 
RetentionPolicy.CLASS  refers to the .class file, available to java compiler but not to JVM . It is included in the class file.
RetentionPolicy.RUNTIME refers to the runtime, available to java compiler and JVM .

14. Display the distinct value
    List<String> stringList = Arrays.asList("1","1","2","3","4");
    stringList.stream()
           .distinct()
           .forEach(System.out::println);

15. Jdk 1.7 features
Strings in Switch
diamond operator
List list = new ArrayList<>();
Multi-Catch Similar Exceptions
 try {
        throw new FileNotFoundException("FileNotFoundException");
    } catch (FileNotFoundException | IOException fnfo) {
        fnfo.printStackTrace();
}
Try with Resources
try (FileInputStream in = new FileInputStream("java7.txt")) {
System.out.println(in.read());
}
}
https://dzone.com/articles/new-java-7-language-features

Comments

Popular posts from this blog

Robotic Process Automation

WorkFusion

7 Rules of Success A.P.J Abdul Kalam