Automation Using Selenium Webdriver

Sunday, 20 November 2016

Remove duplicates from arraylist without using collections

1.Write a Java program to remove duplicate elements from an arraylist without using collections (without using set)



  • package arrayListRemoveduplicateElements;
  • import java.util.ArrayList;
  •  
  • public class RemoveDuplicates {
  • public static void main(String[] args){
  •     
  •     ArrayList<Object> al = new ArrayList<Object>();
  •     
  •     al.add("java");
  •     al.add('a');
  •     al.add('b');
  •     al.add('a');
  •     al.add("java");
  •     al.add(10.3);
  •     al.add('c');
  •     al.add(14);
  •     al.add("java");
  •     al.add(12);
  •     
  • System.out.println("Before Remove Duplicate elements:"+al);
  •  
  • for(int i=0;i<al.size();i++){
  •  
  •  for(int j=i+1;j<al.size();j++){
  •             if(al.get(i).equals(al.get(j))){
  •                 al.remove(j);
  •                 j--;
  •             }
  •     }
  •  
  •  }
  •  
  •     System.out.println("After Removing duplicate elements:"+al);
  •  
  • }
  •  
  • }


  • Output:
    1. Before Remove Duplicate elements:[java, a, b, a, java, 10.3, c, 14, java, 12]
    2. After Removing duplicate elements:[java, a, b, 10.3, c, 14, 12]



    2. Write a Java program to remove duplicate elements from an array using Collections (Linkedhashset)

    1. package arrayListRemoveduplicateElements;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashSet;
    5. import java.util.List;
    6.  
    7. public class RemoveDuplicates {
    8.  
    9. public static void main(String[] args){
    10.     
    11.     List<String>  arraylist = new ArrayList<String>();
    12.     
    13.     arraylist.add("instanceofjava");
    14.     arraylist.add("Interview Questions");
    15.     arraylist.add("Interview Programs");
    16.     arraylist.add("java");
    17.     arraylist.add("Collections Interview Questions");
    18.     arraylist.add("instanceofjava");
    19.     arraylist.add("Java Experience Interview Questions");
    20.     
    21.     
    22.     System.out.println("Before Removing duplicate elements:"+arraylist);
    23.     
    24.     HashSet<String> hashset = new HashSet<String>();
    25.     
    26.     /* Adding ArrayList elements to the HashSet
    27.      * in order to remove the duplicate elements and 
    28.      * to preserve the insertion order.
    29.      */
    30.     hashset.addAll(arraylist);
    31.  
    32.     // Removing ArrayList elements
    33.     arraylist.clear();
    34.  
    35.     // Adding LinkedHashSet elements to the ArrayList
    36.     arraylist.addAll(hashset );
    37.  
    38.     System.out.println("After Removing duplicate elements:"+arraylist);
    39.  
    40. }
    41.  
    42. }



    Output:
      



    1. Before Removing duplicate elements:[instanceofjava, Interview Questions, Interview
    2. Programs, java, Collections Interview Questions, instanceofjava, Java Experience Interview
    3. Questions]
    4. After Removing duplicate elements:[java, Collections Interview Questions, Java Experience
    5. Interview Questions, Interview Questions, instanceofjava, Interview Programs]





    Thursday, 17 November 2016

    Top 10 Oops Concepts Interview Questions

    What are the oops concepts in java

    Oop is an approach that provides a way of modularizing a program by creating partitioned memory area
    for both data and methods that can be used as template for creating copies of such modules on demand.
    The four oops concepts are
    Encapsulation
    Polymorphism
    Inheritance
    Abstraction

     What is encapsulation

    • The process of binding the data with related methods known as encapsulation.
    • Class is the base for encapsulation.

    Need for Encapsulation

    encapsulation

     

    • Any program of  any programming language contains only two parts.
      i.Data
      ii.Logic
    • Out of these two highest priority would be always given to data part.
    • Functions /logics would e giving output based on the data.
    • There is no proper security for the data part in the structured programming language programs like c - program.
    • There was no proper separation between the data and functions of different domains because of which complexity of the programming increases and data insecurity arises
    • The concept of proper binding is missing in structured programming languages  

    Reason for Data Insecurity in C-Programing language:

    • It is the  GLOBAL VARIABLES which leads to the data insecurity in c- programs 
    • GLOBAL VARIABLES would be available for both related and unrelated functions also.
    • Whenever an unrelated function accesses the data in a wrong way data corruption arises

    Sample c -program:

    1. #include <stdio.h>
    2.  
    3. /* global variable declaration */
    4. int a = 20;
    5.  
    6. int main ()
    7. {
    8.  
    9. /* local variable declaration in main function */
    10.   int a = 10;
    11.   int b = 20;
    12.   int c = 0;
    13. int mul=0;
    14.  
    15.  c = sum( a, b);
    16.  
    17.  printf ("a+b = %d\n",  c);
    18.  
    19. mul= sum( a, b);
    20.  
    21. printf ("a*b= %d\n",  mul); 
    22. return 0;
    23.  
    24. /* function to add two integers */
    25. int sum(int a, int b)
    26. {   
    27.     return a + b;
    28. }
    29.  
    30. /* function to multiply two integers */
    31. int mul(int a, int b)
    32. {
    33.     return a * b;
    34. }

      What is Encapsulation?

      • The concept of binding the data along with its related and corresponding functions is known as Encapsulation.

      Binding:

      • The concept of restricting the availability of the data only to its related areas and related functions it is known as binding.
      • Because of binding we can restrict the availability of dta to the unrelated functions
      • So that unrelated functions cannot access the data 
      • Class is the base for encapsulation

      Example program:

      1. package com.instanceofjava;
      2.  
      3. public class Student {
      4.  
      5. // variables
      6.    int rno;
      7.     String name;
      8.     String clas;
      9.     String Address;
      10.     int rank;
      11.  
      12. //setter and getter methods of variables 
      13.  
      14.  public int getRno() {
      15.        return rno;
      16.     }
      17.  
      18. public void setRno(int rno) {
      19.        this.rno = rno;
      20. }
      21.  
      22. public String getName() {
      23.         return name;
      24. }
      25.  
      26.  public void setName(String name) {
      27.         this.name = name;
      28. }
      29.  
      30. public String getClas() {
      31.        return clas;
      32. }
      33.  
      34. public void setClas(String clas) {
      35.         this.clas = clas;
      36. }
      37.  
      38. public String getAddress() {
      39.        return Address;
      40. }
      41.  
      42. public void setAddress(String address) {
      43.       Address = address;
      44. }
      45.  
      46. public int getRank() {
      47.     return rank;
      48.  }
      49.  
      50. public void setRank(int rank) {
      51.         this.rank = rank;
      52.  }
      53.  
      54.     //constructor
      55.    Student(int rno,String name,String clas,String Address,int rank){
      56.  
      57.         this.rno=rno;
      58.         this.name=name;
      59.         this.clas=clas;
      60.  
      61.         this.Address=Address;
      62.         this.rank=rank;
      63.  
      64.     }
      65.  
      66.  public static void main(String args[]){
      67.  
      68.     Student student= new Student(1, "sai", "class IV", "hyderabad", 1); 
      69.  
      70.     System.out.println("Name: "+student.getName());
      71.     System.out.println("Class: "+student.getClas());
      72.     System.out.println("Rno: "+student.getRno());
      73.  
      74.     System.out.println("Rank: "+student.getRank());
      75.     System.out.println("Address: "+student.getAddress());
      76.  
      77.   }
      78. }

         Output:

        1. Name: sai 
        2. Class: class IV 
        3. Rno: 1 
        4. Rank: 1 
        5. Address: hyderabad

        output:

        3.What is class ? 

         

        • A class is a specification or blue print or template of an object.
        • Class is a logical construct , an object has physical reality.
        • Class is a structure.
        • Class is a user defined data type in java
        • Class will acts as base for encapsulation.
        • Class contains variables and methods.


        1. package com.instanceofjava;
        2.  
        3. class Demo{
        4.  
        5. int a,b;
        6. void show(){
        7. }
        8.  
        9. }

        4. What is an object?

        • Object is instance of class.
        • Object is dynamic memory allocation of class.
        • Object is an encapsulated form of all non static variables and non static methods of a particular class.
        • The process of creating objects out of class is known as instantiation.
        1. package com.instanceofjava;
        2.  
        3. class Test{
        4.  
        5. int a,b;
        6. void print(){
        7. System.out.println("a="+a);
        8. System.out.println("b="+b);
        9. }
        10.  
        11. public static void main(String [] args){
        12.    
        13.    Test obj= new Test();
        14.   obj.a=10;
        15.   obj.b=20;
        16.   obj.print();
        17. }
        18. }

        Output:

        1. a=10
        2. b=20

        5. What are the Object Characteristics?

        •  The three key characteristics of Object are
        • State
        • Behavior
        • Identity

        State:

        • Instance variables value is called object state.
        • An object state will be changed if instance variables value is changed.

        Behavior:

        • Behavior of an object is defined by instance methods.
        • Behavior of an object is depends on the messages passed to it.
        • So an object behavior depends on the instance methods.

        Identity:

        • Identity is the hashcode of an object, it is a 32 bit integer number created randomly and assigned to an object by default by JVM.
        • Developer can also generate hashcode of an object based on the state of that object by overriding hashcode() method of java.lang.Object class.
        • Then if state is changed , automatically hashcode will be changed.

        6.What is Inheritance?

        • As the name suggests , inheritance means to take something that already made.
        • One of the most important feature of Object oriented Programming. It is the concept that is used for re usability purpose.
        • Getting the properties from one class object to another class object.

        7. How inheritance implemented in java?

        • Inheritance can be implemented in JAVA using below two keywords.
          1.extends
          2.implements
        • extends is used for developing inheritance between two classes or two interfaces, and implements keyword is used to develop inheritance between interface and class.

        1. package com.instanceofjava;
        2. class A{
        3.  
        4. }


        1. package com.instanceofjava;
        2. class B extends A{
        3.  
        4. }

        8. What are the types of inheritances?

        • There are two types of inheritance
          1.Multilevel Inheritance
          2.Multiple Inheritance

        Multilevel Inheritance:

        • Getting the properties from one class object to another class object level wise with some priority is known as multilevel inheritance.



        1. package com.instanceofjava;
        2.  
        3. class A{
        4.  
        5. }
        6.  
        7. class B extends A{
        8.   
        9. }
        10.   
        11. class C extends B{
        12.  
        13. }


        Multiple Inheritance:

        Why Java does not supports multiple inheritance?

        Inheritance:

        • The concept of getting properties of one class object to another class object is known as inheritance.
        • Here properties means variable and methods.

        Types of Inheritance:

        1. Multiple inheritance.
        2. Multilevel inheritance.

         Multiple inheritance:

        • The concept of Getting the properties from multiple class objects to sub class object with same priorities is known as multiple inheritance.
        • Java Doesn't Support multiple Inheritance.

        Diamond problem:

        • In multiple inheritance there is every chance of multiple properties of multiple objects with  the same name available to the sub class object with same priorities leads for the ambiguity.

        1. //Multiple inheritance program
        2. Class A{
        3. }
        4. Class B extends A{
        5. public void show(){
        6. }
        7. }
        8. Class C extends A{
        9. public void show(){
        10. }
        11. }
        12. Class D extends B,C{  // not supported by java leads to syntax error.
        13. }





        •  We have two classes B and c which are inheriting A class properties.
        • Here Class D inheriting B class and C class So properties present in those classes will be available in java.
        • But both classes are in same level with same priority.
        • If we want to use show() method that leads to ambiguity
        • This is called diamond problem.
        • Because of multiple inheritance there is chance of the root object getting created more than once.
        • Always the root object i.e object of object class hast to be created only once.
        • Because of above mentioned reasons multiple inheritance would not be supported by java.
        • Thus in java a class can not extend more than one class simultaneously. At most a class can extend only one class.
        So these are the reasons that java does not supports multiple inheritance.Even though having this much reasons some people say and we also gets some doubts when we are using interfaces. lets me clear this one also. our immediate question should be.

        Is Java supports multiple inheritance using interfaces?

        • Before that we need to know about interfaces.
        • Interfaces having fully abstract functionality.
        • Means methods in interfaces by default public abstract methods so we need to implement these methods in classes which are extending this interface.
            
        1. /Multiple inheritance program
        2. interface A{
        3. public void  show();
        4. }
        5. interface B{
        6. public void  display();
        7. }
        8. Class C Implements A,B{
        9. }

        What is polymorphism

        • Defining multiple methods with same name,
         Static polymorphism:

        • Defining multiple methods with same name with different parameters.
        • Is also known as method overloading.


        1. package com.instanceofjava;
        2. class Demo{
        3.   
        4. void add(){
        5. }
        6.   
        7. void add(int a, int b){
        8. }
        9.  
        10. void add(float a, float b){
        11.   
        12. }
        13. public static void main(String [] args){
        14.  Demo obj= new Demo();
        15.  
        16. obj.add();
        17. obj.add(1,2);
        18. obj.add(1.2f,1.4f);

        19. }

        20. }


         Dynamic Polymorphism:

        • Defining multiple methods with same signature in super class and sub class.
        • The sub most object method will be executed always.
         what is the difference between method overloading and method overriding?

        • Method overloading is nothing but defining multiple fuctionalities with same name with 
          different parameters is known as method overloading
        • while compile time itself we know which method is execute
        • Overloading gives better performance compared to overriding. The reason is that the 
          binding of overridden methods is being done at runtime.
        • Argument list should be different while doing method overloading.
        • Polymorphism not applies to overloading. Return type of overloaded methods should be same. 
          static binding is used for overloaded methods
        public class A{
        public void show(int a){
        S.o.p("india");
        }
        public void show(int a,int b){
        S.o.p("thota");
        }

        public void show(float a){
        S.o.p("jagan");
        }
        public static void main(String args[]){

        A a=new A();
        a.show(10);
        a.show(1,2);
        a.show(1.2);
        }
        }
        • Method overriding is nothing but defining multiple finctionalities with same name with 
          same definition in super class and sub class is known as Method overriding.
          While Run time only now which method is Executed.
        • Overriding gives slower performance compared to overloading.The reason is that the 
          binding of overridden methods is being done at run time.
        • Argument list should be same while doing method overriding.
          Polymorphism applies to overriding. 
        • In method overriding the return type of overriding method can be different from overridden 
          method.
        • Dynamic binding used for method overriding methods
        public class A{
        public void display(){
        S.o.p("India");
        }
        }
        Public class B extends A{

        public void display(){
        S.o.p("Jagan");
        }
        public staic void main(String args[]){
        A a =new B();
        a.display();
        }
        }

        10. Similarities and differences between this and super keywords?

         this:
        • This is a keyword used to store current object reference.
        • It must be used explicitly if non -static variable and local variables name is same.
        • System.out.print(this); works fine
        super:
        • Super is a keyword used to store super class non -static members reference in sub class object.
        • used to separate super class and sub class members if both have same name.
        • System.out.println(super); compilation Error

        Abstract Class:

        Abstract Class and Interfaces

        • Abstract class means hiding the implementation  and showing the function definition to the user is known as Abstract class
        • Abstract classes having Abstract methods and normal methods (non abstract methods) will be there.
        • Abstract classes having methods will be anything means public ,private,protected.
          In Abstract classes  variables will be anything( public, private, protected)
          For Abstract classes we not able to create object directly.But Indirectily we can create object using sub calss object.
        • A Java abstract class should be extended using keyword “extends”.
        • A Java abstract class can have instance methods that implements a default behavior.
          If you know requirement and partially implementation you can go for Abstract classes.
          abstract  class  can extend from a class or from an abstract class.
        • Abstract class can extend only one class or one abstract class at a time. soAbstract classes can't support multiple inheritance.
        • In comparison with java Interfaces, java Abstract classes are fast.
          If you add new method to abstract class, you can provide default implementation of it. So you don’t need to change your current code.
        • Abstract classes can have constructors .
          We can run an abstract class if it has main() method.
        • Example Program:


          abstract class A{

          abstract void display();
          public void show(){
          S.o.p("Jagan");
          }

          Public class B extends A{
          void display();
          }
          Abstract class C Extends B{
          //Escaping Here
          }
          public static void main(String args()){
          A a= new B();
          a.display();
          a.show();

          }

          Interface :

          • Interface nothing but some set of rules.
          • Interfaces having only Abstract methods.it is purely Achive Abstraction.
          • In Interfaces by default the methods will be public abstract methods.
          • In Interfaces by default the variables will be static final .
          • For Interfaces we can't create object directly or Indirectly but we can give sub class object reference to interface .
          • Java interface should be implemented using keyword “implements”. 
          • methods of a Java interface are implicitly abstract and cannot have implementations.
          • If u don't know Any requirement and any implementation you can go for Interfaces.
          • Interface can extend only from an interface
          • Interface can extend any number of interfaces at a time. so interfaces can support multiple inheritance(syntactical not implementation of multiple inheritance).
          • In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
          • if  you add new method to interface, you have to change the classes which are implementing that interface
          • Interface having no constructor.
            we can’t run an interface because they can’t have main method implementation.
          Example Program:

          public interface Payment
          {
              void makePayment();//by default it is a abstract method
          }
          public class PayPal implements Payment
          {
              public void makePayment()
              {
                  //some logic for paypal payment
                  //like paypal uses username and password for payment
              }
          }
          public class CreditCard implements Payment
          {
              public void makePayment()
              {
                  //some logic for CreditCard payment
                  //like CreditCard uses card number, date of expiry etc...
              }