Friday, August 24, 2018

Java Questions For Selenium Interview

| Friday, August 24, 2018
Part 8

36 : What is the difference betwixt the Constructor together with Method?
Answer : Main departure betwixt the Constructor together with Method is equally bellow.

Constructor :
  1. Name of the constructor must live on same equally cast name.
  2. Constructor must non accept whatever supply type.
  3. It is used to initialize the set down of an object.
  4. It is non possible to telephone phone constructor directly. Constructors called implicitly when the novel keyword creates an object.
Method :
  1. Method elevate tin live on any.
  2. Method must accept supply type.
  3. It is used to let on demeanor of an object.
  4. Methods tin live on called directly.
Read to a greater extent than on Constructor together with Method.

37 : What are the unlike types of types of constructors inwards java?
Answer : Mainly in that place are ii types of constructors available inwards java.
  • Default Constructor : Constructor without parameter is called default constructor.
package JAVAExamples;  populace cast City {  //Default Constructor  City()  {   System.out.println("City is created");  }    populace static void main(String args[]){     City c=new City();    }   }
  • Parameterized constructor : Constructor amongst parameter is called Parameterized constructor.
package JAVAExamples;  populace cast City {  int id;  String name;   // parameterized Constructor  City(int i, String n) {   id = i;   elevate = n;  }   void display() {   System.out.println(id + " " + name);  }   populace static void main(String args[]) {   City c1 = novel City(1, "New York");   City c2 = novel City(2, "London");   c1.display();   c2.display();  } }
Output :
1 New York
2 London

38 : Write a programme for Fibonacci serial inwards Java ?
Answer : Program for Fibonacci serial is equally bellow.

package JAVAExamples;  populace cast FibonacciSeries {  populace static void main(String args[]) {   int x1 = 0, x2 = 1, x3, i, cnt = 15;   // To impress 0 together with 1   System.out.print(x1 + " " + x2);    // loop starts from 2 equally 0 together with 1 are already printed.   for (i = 2; i < cnt; ++i) {    x3 = x1 + x2;    System.out.print(" " + x3);    x1 = x2;    x2 = x3;   }  } }

39 : Write a programme to impress below given pattern.

1  1 2  1 2 iii  1 2 iii iv  1 2 iii iv 5

Answer : Program to impress to a higher house designing is equally bellow.

package JAVAExamples;  populace cast Pattern {   populace static void main(String[] args) {   for (int a = 1; a <= 5; a++) {    for (int x = 1; x <= a; x++) {     System.out.print(x+" ");    }    // To impress novel line.    System.out.println();   }  } }

40. What is the departure betwixt “this” together with “super” keywords inwards Java?
Answer : Difference is equally bellow.
  1. “this” keyword is used to shop electrical current object reference while “super” keyword is used to store super cast object inwards sub class..
  2. “this” is used to access methods of the electrical current class while “super” is used to access methods of the base of operations class.
  3. this() used to telephone phone constructors inwards the same cast whereas super() is used to telephone phone super cast constructor.
Read more on “super” keyword.

Related Posts