Professional Documents
Culture Documents
com---------
3. Inheritance - When one object acquires all the properties and behaviours of
parent object i.e. known as inheritance.
It provides code reusability. It is used to achieve runtime polymorphism.
6. Encapsulation - Binding (or wrapping) code and data together into a single unit
is known as encapsulation.
A java class is the example of encapsulation. Java bean is the fully encapsulated
class because all the data members are private here.
7. explain java in layman terms if I don't understand any technical stuffs like
private, class or object..
Ans- Java is a programming language which is plateform independent i.e. we can
compile code in one machine and can run in another machine with different OS.
We can develop different kind of applications using java.
8. why do we use interfaces? Why is interface concept present when something could
be achieved using classes or abstract classes?
Ans- to achieve 100% abstraction and standardization. By using classes and abstract
classes we can't achieve 100% abstraction bcoz loop holes are there.
9. tell the use of abstract classes over interfaces, and how are interfaces built
in terms of methods declared and variables within it..?
Ans- If the functionality you are creating will be useful across a wide range of
disparate objects, use an interface.
Abstract classes should be used primarily for objects that are closely related,
whereas interfaces are best suited for providing common functionality to unrelated
classes.
Interfaces can have only incomplete methods and all methods will be non-static and
public.
Variables declared inside interfaces will always be final, static and should be
initialized.
11. Giving an example, he asked me to chose which collection would I chose for that
particular scenario..
Like getwindowhandles returns set, but can I also make use of list or any other
collection? Similar questions..
Ans- No we can't use List because List allows duplicates whereas set doesn�t allow.
13. write a program to check if the given input is palindrome or not by taking
input from user at runtime.
Ans- class Palindrome{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a = n;
int palindrome =0;
int r = 0;
while(n>0){
r = n%10;
n = n/10;
palindrome = palindrome*10 + r;
}
if(palindrome==a){
System.out.println(a+� number is palindrome.�);
}else{
System.out.println(a+� number is not palindrome.�);
}
}
}
14. Difference between ArrayList and LinkedList?
ArrayList and LinkedList both implements List interface and maintains insertion
order. Both are non synchronized classes.
ArrayList:
ArrayList internally uses dynamic array to store the elements.
Manipulation with ArrayList is slow because it internally uses array. If any
element is removed from the array, all the bits are shifted in memory.
ArrayList class can act as a list only because it implements List only.
ArrayList is better for storing and accessing data.
//List<String> al=new ArrayList<String>();//creating arraylist
LinkedList:
LinkedList internally uses doubly linked list to store the elements.
Manipulation with LinkedList is faster than ArrayList because it uses doubly linked
list so no bit shifting is required in memory.
LinkedList class can act as a list and queue both because it implements List and
Deque interfaces.
LinkedList is better for manipulating data.
//List<String> al2=new LinkedList<String>();//creating linkedlist
Abstract class:
Abstract class can have abstract and non-abstract methods.
Abstract class doesn't support multiple inheritance.
Abstract class can have final, non-final, static and non-static variables.
Abstract class can have static methods, main method and constructor.
Abstract class can provide the implementation of interface.
The abstract keyword is used to declare abstract class.
Example:
public abstract class Shape
{
public abstract void draw();
}
Interface:
Interface can have only abstract methods.
Interface supports multiple inheritance.
Interface has only static and final variables.
Interface can't have static methods, main method or constructor.
Interface can't provide the implementation of abstract class.
The interface keyword is used to declare interface.
Example:
public interface Drawable
{
void draw();
}
class Testimmutablestring
{
public static void main(String args[])
{
String s="Sachin";
s.concat(" Tendulkar");//concat() method appends the string at the end
s=s.concat(" Tendulkar");
System.out.println(s);// will print Sachin Tendulkar
}
}
Comparable:
Comparable provides single sorting sequence. In other words, we can sort the
collection on the basis of single element such as id or name or price etc.
Comparable affects the original class i.e. actual class is modified.
Comparable provides compareTo() method to sort elements.
Comparable is found in java.lang package.
We can sort the list elements of Comparable type by Collections.sort(List) method.
Comparator:
Comparator provides multiple sorting sequence. In other words, we can sort the
collection on the basis of multiple elements such as id, name and price etc.
Comparator doesn't affect the original class i.e. actual class is not modified.
Comparator provides compare() method to sort elements.
Comparator is found in java.util package.
We can sort the list elements of Comparator type by
Collections.sort(List,Comparator) method.