SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Java Questions For Selenium Interview

Part 8

36 : What is the difference between the Constructor and Method?
Answer : Main difference between the Constructor and Method is as bellow.

Constructor :
  1. Name of the constructor must be same as class name.
  2. Constructor must not have any return type.
  3. It is used to initialize the state of an object.
  4. It is not possible to call constructor directly. Constructors called implicitly when the new keyword creates an object.
Method :
  1. Method name can be any.
  2. Method must have return type.
  3. It is used to expose behavior of an object.
  4. Methods can be called directly.
Read more on Constructor and Method.

37 : What are the different types of types of constructors in java?
Answer : Mainly there are two types of constructors available in java.
  • Default Constructor : Constructor without parameter is called default constructor.
package JAVAExamples;

public class City {
 //Default Constructor
 City()
 {
  System.out.println("City is created");
 }  
 public static void main(String args[]){  
  City c=new City();  
 }  
}
  • Parameterized constructor : Constructor with parameter is called Parameterized constructor.
package JAVAExamples;

public class City {
 int id;
 String name;

 // parameterized Constructor
 City(int i, String n) {
  id = i;
  name = n;
 }

 void display() {
  System.out.println(id + " " + name);
 }

 public static void main(String args[]) {
  City c1 = new City(1, "New York");
  City c2 = new City(2, "London");
  c1.display();
  c2.display();
 }
}
Output :
1 New York
2 London

38 : Write a program for Fibonacci series in Java ?
Answer : Program for Fibonacci series is as bellow.

package JAVAExamples;

public class FibonacciSeries {
 public static void main(String args[]) {
  int x1 = 0, x2 = 1, x3, i, cnt = 15;
  // To print 0 and 1
  System.out.print(x1 + " " + x2);

  // loop starts from 2 as 0 and 1 are already printed.
  for (i = 2; i < cnt; ++i) {
   x3 = x1 + x2;
   System.out.print(" " + x3);
   x1 = x2;
   x2 = x3;
  }
 }
}

39 : Write a program to print below given pattern.

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

Answer : Program to print above pattern is as bellow.

package JAVAExamples;

public class Pattern {

 public static void main(String[] args) {
  for (int a = 1; a <= 5; a++) {
   for (int x = 1; x <= a; x++) {
    System.out.print(x+" ");
   }
   // To print new line.
   System.out.println();
  }
 }
}

40. What is the difference between “this” and “super” keywords in Java?
Answer : Difference is as bellow.
  1. “this” keyword is used to store current object reference while “super” keyword is used to store super class object in sub class..
  2. “this” is used to access methods of the current class while “super” is used to access methods of the base class.
  3. this() used to call constructors in the same class whereas super() is used to call super class constructor.
Read more on “super” keyword.

No comments:

Post a Comment