Forward from http://www.software-testing-tutorials-automation.com/2016/06/java-interview-questions-for-selenium.html (VPN needed to access)
- Static variables are preceded by static keyword. For non-static variable, there is not any preceding keyword.
- Memory is allocated for static variables at the time of class loading. Memory is allocated to non- static variables whenever an object is created.
- Memory is allocated only once to static variables on class loading. Memory is allocated multiple time whenever a new object is created to non-static variables.
- Static variable example : Collage name of students, Company name of employees..
- Method declared with static keyword is static method. If Method declared without static keyword then it is instance method.
- No need of object to call static methods. Object needed to call instance method.
- Can not access non static stuff inside static methods directly. Opposite to it, We can access static and non static stuff directly inside instance method.
Example :
17 : Can we use private member of parent class in sub class?
- System : is a final class in java.lang package.
- out : is a static member of system class. It is an instance of java.io.PrintStream. This stream is already open and ready to accept output data.
- println : is a method of java.io.PrintStream .It is an overloaded method.
public class fibonaccci {
public static void main(String[] args) {
int f1 = 0;
int f2 = 1;
int sum = 0;
for(int i=0; i<=21;){
System.out.println(f1);
sum = i+f2;
f2=i;
f1=sum;
i=f1;
}
}
}
public class SquareSum {
public static void main(String[] args) {
int sum = 0;
for (int i=5;i>=1;i--){
if(i%2!=0){
if(sum<(i*i)){
sum = (i*i)-sum;
}else{
sum = sum-(i*i);
}
}else{
sum = sum+(i*i);
}
}
System.out.println(sum);
}
}
24 : What is return type of testng @DataProvider annotation method?
public class swapNumbers {
public static void main(String[] args) {
int x = 10;
int y = 20;
System.out.println("Before swapping x = " + x + " and y = " + y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After swapping x = " + x + " and y = " + y);
}
}
Answer : Local variable is declared inside method or constructor and it is limited for that method or constructor only. View more detail on local variable in java.
Local Variable Example :
public class JavaVariable {
public void Calc() {
// Local Variables.
int sum;
int item1 = 5;
int item2 = 7;
sum = item1 + item2;
System.out.println("Sum is : " + sum);
}
public static void main(String args[]) {
JavaVariable j = new JavaVariable();
j.Calc();
}
}
27 : What is a Instance Variable in java?
Answer : Instance Variable is declared parallel to method or constructor in class. It is visible for all methods and constructors of that class. View more detail on instance variable in java.
Instance Variable Example :
public class JavaVariable {
//Instance Variable.
int sum;
public void Calc() {
// Local Variables.
int item1 = 5;
int item2 = 7;
sum = item1 + item2;
System.out.println("Sum is : " + sum);
}
public static void main(String args[]) {
JavaVariable j = new JavaVariable();
j.Calc();
}
28 : What is a Class Variable in java?
Answer : Class variable is declared with static keyword in class parallel to methods and constructor. Class variable is initialized only once at the start of execution and destroyed on end of program. Class variable is also known as static variable. View more detail on class variable in java.
Class Variable Example :
public class JavaVariable {
//Class Variable
static String sumType = "Basket";
//Instance Variable.
int sum;
public void Calc() {
// Local Variables.
int item1 = 5;
int item2 = 7;
sum = item1 + item2;
System.out.println(sumType + " Sum is : " + sum);
}
public static void main(String args[]) {
JavaVariable j = new JavaVariable();
j.Calc();
}
29 : What is the difference between instance variable and class variable?
Answer :
Instance Variable :
- It is unique to each instance of the class.
- Declared without static modifier.
- Memory allocation, loading and initialization is done at run time.
- It is shared by all instances of the class.
- Declared with static modifier.
- Memory allocation is done at compile time, loaded at load time and they are initialized at class initialization time.
Answer : Access modifiers allows us to set access levels for variables, methods, classes and constructors in java. We can control access levels using access modifiers in java.
package JAVAExamples;
public class StringReverse {
public static void main(String[] args) {
//String to reverse.
String str = "This Is String.";
String revstring = "";
for (int i = str.length() - 1; i >= 0; --i) {
//Start getting characters from end of the string.
revstring += str.charAt(i);
}
System.out.println(revstring);
}
}
Output : .gnirtS sI sihT
Answer : Main difference between the Constructor and Method is as bellow.
Constructor :
- Name of the constructor must be same as class name.
- Constructor must not have any return type.
- It is used to initialize the state of an object.
- It is not possible to call constructor directly. Constructors called implicitly when the new keyword creates an object.
- Method name can be any.
- Method must have return type.
- It is used to expose behavior of an object.
- Methods can be called directly.
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.
- “this” keyword is used to store current object reference while “super” keyword is used to store super class object in sub class..
- “this” is used to access methods of the current class while “super” is used to access methods of the base class.
- this() used to call constructors in the same class whereas super() is used to call super class constructor.
package JAVAExamples;
public class RemoveChar {
public static String removeChar(String str, char c) {
if (str == null)
return null;
return str.replaceAll(Character.toString(c), "");
}
public static void main(String[] args) {
System.out.println(removeChar("chicago", 'c'));
}
}
Answer : Polymorphism is ability using which we can create reference variables or methods which behaves differently in different programmatic context. Best example of polymorphism is human. We behaves differently with different people in different environment. Our behavior will be different when we meet to boss and meet to friend. Read more on Polymorphism in java.
47 : What is the advantages of Polymorphism?
Answer : Main advantage of polymorphism is code reusabilty. You can dynamically supply different implementations through polymorphism. So it will reduce your work volume in terms of handling and distinguishing various objects.
48 : What is a package?
Answer : A package is a namespace which allows developer to organizes a group of related classes and interfaces. Conceptually it is just like folder which contains different types of files. It is easy to keep things organised by keeping related classes and interfaces into packages.
49 : What is string in java?
Answer : In Java programming, String is object which is prepared by sequence of characters.java.lang package has String class to create and manipulate strings. Read more on string in java.
50 : What is StringBuffer in java?
Answer : StringBuffer help us to create mutable(modifiable) string in java. That means we can modify the string if we use StringBuffer.