Thursday, February 2, 2017

Basic and tricky java Interview Questions

Hi guys , Hope you all are doing good , Today I am going to share few tricky questions which i felt everyone who is preparing for java interview should know. So lets see

Q1) Difference between new operator and newInstance() method ?/
-    new is used when we know the type of class instance know at design time.
     if you dont know then we use newInstance method
    ex-
     Student std = new Student();
      Object o = Class.forName(arg[0]).newInstance();


Q2) classNotFoundException vs NoClassDefFoundException
       when we write like this
      Test test= new Test();
      and the Test.class file is not found then at runtime we will get NoClassDefException found.                (unchecked exception)
         Object o = Class.forName(args[0]).newInstance();
        will get the ClassNotFoundException (checked exception)



Q3) Diff b/w checked and unchecked exception
exception which are be checked by compiler  how to handle the expection. for the smooth execution of program.
ex- FileNotFoundException is checked exception , compiler ask to handle the exception while compile.
exception which are not checked by compiler
ArthematicException is unchecked exception.


control flow of try catch
if exception in araised in catch block it is abnormal exception

variable combination of try catch final block
- try catch
-try catch catch(catch should be in child to parent heirarchy)
try final
try catch final


------------------String---------------
Why String is always final in java - due to immutability ,security (thread safe) etc
Why we use character array over string to store password ?
-as string is immutable and it is persist till the garbage collector called . As data is stored for long time because string pool used the string data
- if we use JPasswordField then it will also return array[] for getPassword() and depricated getText() field

--------------String Pool-----------------
Why we use string pool ?
String pool is used to store the references of the strings
Ex-
String s=“prasad”;
String s1=“prasad”;
System.out.println(s==s1)  // output  true;

The class is loaded when JVM is invoked.
JVM will look for all the string literals in the program
First, it finds the variable s which refers to the  literal “prasad” and it will be created in the memory
A reference for the literal “prasad” will be placed in the string constant pool memory.
Then it finds another variable s2 which is referring to the same string literal “prasad“.
Now that JVM has already found a string literal “prasad“, both the variables s and s2 wil refer to the same object i.e. “prasad“.
----intern() --------------
intern method check whether the string is present in spring pool if yes then return the reference of the string otherwise create new string object
Ex-
String s ="prasad";
String s1="prasad".intern();
System.out.println(s==s1)  // output true
System.out.println(s.equals(s1));   // output true

String s ="prasad";
String s1="prasad1".intern();
System.out.println(s==s1)  // output false
System.out.println(s.equals(s1));   // output false

---------------------------

Difference btw encapculation & abstraction(how to achieve abstraction using encp)
- by decreasing the scope of behaviour(methods)
encapsulation - wrapping of data and associated func into a single unit is encp -(class)
Ex-
if we have one function which will call the other function so we can scope down the other function to achieve abstraction

Class A{

public void m1()
{
m4internal();
}
public void m2()
{
}
}
Class B{

protected void m4internal()

}

Hope these will be helpful for you , if you have any problem , do let me know

Thanks for Reading
-Noeik

0 comments:

Post a Comment