Java Interview Question Part - 1



Q1. What’s the purpose of Static methods and static variables?
Ans: When there is a requirement to share a method or a variable between multiple objects of a 
class instead of creating separate copies for each object, we use static keyword to make a
method or variable shared for all objects.

Q2. What is a singleton class ? Give a practical example of it's usage ?
Ans: A singleton class in java can have only one instance and hence all it's methods an variable belong to just one instance.Singleton class concept is useful for the situation when ther is need to limit the number of objects for a class.
The best example of the singleton is printer only one person can take print out at a time.

Q3.what is different between double and float variable in java?
Ans: In java float take 4 byte in memory while Double takes 8 bytes in memory. Float is single floating point decimal number while Double is double precision decimal number.

Q4.What is ternary operater ? 
 Ans: Ternary operater ,also called conditional operator is used to decide which value to assign to a variable base on the Boolean value evalution.

 String staus;
 int rank=3;
 staus =(ran == 1) ? "Pass " :"Fail";
 System.out.println(status);

Q5.Can we declare a class as Abstract without having any abstract method?
Ans: Yes we can create an abstract class by using abstract keywork before class name evenif it doesn't have any abstract method.However ,if a class has even one abstract method ,it must declered as abstact otherwise it will throw error.

Q6 What is Lambda Expression?
Lambda Expression is an anonymous function which accepts a set of inputs paramaters and return results,

Lambda Expression is block of code without any name  and it executed on demand.

Q7 What is Syntex of the Lambda Expression?
 () -> System.out.println("Hello Javaweb-world");
  -> is denoted as Lambda Arror Operator.It seperates paramaters list and body.

Q8.what is function interface ?
Ans: An interface with exactily one abstract method is called Functional interface.we can use lambda expressions to instantiate them and avoid using bulk anonymous class implementation. 

Q9. What is Garbage Collection?
Ans: Garbage Connection means to automatic memory management in java. Garbag collection is to keep as much of heap available free for the program as possible. JVM removes object on the heap which no longer have references from the heap.

Q10. Explain Garbage Collection with an example?
Ans: let see in the example:
    Public void getDateCal(){
       Calender cal=new GregorianCalendar(2017,10,30);
       System.out.println(cal);
    }
In above example Object of the class GergorianCalender is created on the heap by the first line of function with referance variable "cal".
End of the function execution,this referance variable cal is no longer valid.Hence JVM recognizes this

and remove the object from the heap.this is called Garbage Collection. 

No comments: