Selenium Java Interview Questions
Caught Exception and Uncaught exception
Caught Exception:
When the programmer is not liable for error, Error come in run time, after execution.
Ex: int i =5/0;
Uncaught Exception:
When programmer is liable for error. Error comes at the time of programming.
Ex:
Thread.sleep(5000L);
---------------------------------------------------------------------------------------------------------
Difference between "Add throws declaration" and Surround with Try/Catch
Add throws declaration: If we don't want to report the error than we use this function.
Surround with Try/Catch block: If we want to report the error in selenium report than we use this function.
---------------------------------------------------------------------------------------------------------
Difference between Throws and Throw
Throws: Throws come in front of function and it tells Java that this function is capable of throwing an exception.
Ex: Add throws declaration" or Surround with Try/Catch
Throw: Throw clause is used to deliberately through an exception.
---------------------------------------------------------------------------------------------------------
Difference between final and finally
final: Stats that you cannot change the value of variable.
Ex: final int s=100; // Cannot change the value
//s =101; //it will give error
finally: finally is used with try/catch block. Finally stats that (finally is a block) it will executed for sure weather try block or catch block is executed or not.
Ex: try{
//establish DB connection
//fire query
//get results
//close connection
}catch{Exception e}
{
//error
System.out.println("error");
}finally{
//close the connection if established
System.out.println("is finally");
}
---------------------------------------------------------------------------------------------------------
Difference between Error and Exception:
Error: Out of memory error, ex: Assertion error thrown, when running the eclipse sometimes when run big program when error comes eclipse gone out of memory error.
Exception: run time exception, null pointer exception; divide by zero exception, array index out of bond etc.
---------------------------------------------------------------------------------------------------------
String to Integer Conversion:
String x = 100;
int 1 = Integer.parseInt (x);
int is the primitive data type but Integer is an in build class just like a String class which represent int.
---------------------------------------------------------------------------------------------------------
Integer to String Conversion:
String z = String.valueof(i);
---------------------------------------------------------------------------------------------------------
String to Boolean conversion:
boolean b = Boolean.valueof("true");
https://mail.google.com/mail/u/0/images/cleardot.gif
---------------------------------------------------------------------------------------------------------
Collection API: Array List and Hash table are collection API.
ArrayList is similar to Array but it is not an array, it is inbuilt class and it is dynamically growing in size.
Ex: It is used where we need to read/get bulk data from site, get data from popup etc.
ArrayList <String> list = new ArrayList <String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list.size());
for(int i =0;i<list.size();i++)
{
System.out.println(list.get(i));
}
---------------------------------------------------------------------------------------------------------
Hah table and array list both are dynamically growing data structure. whereas Array List grow with index and Hash table grows with Kay and Value pair.
---------------------------------------------------------------------------------------------------------
HashTable: Hashtable is inbuilt class we can create object of the class. Hashtable contains key value pair (key and value). Key should be unique and corresponding to each key there should be a value.
Ex: We used Hash table to store test data.
Hashtable <String, String> table = new Hashtable <String, String>();
table.put("Name", "Sunil");
table.put("Company", "InfoBeans");
table.put("Place","Indore");
System.out.println("------------ Hash Table ----------");
System.out.println(table.get("Name"));
Caught Exception and Uncaught exception
Caught Exception:
When the programmer is not liable for error, Error come in run time, after execution.
Ex: int i =5/0;
Uncaught Exception:
When programmer is liable for error. Error comes at the time of programming.
Ex:
Thread.sleep(5000L);
---------------------------------------------------------------------------------------------------------
Difference between "Add throws declaration" and Surround with Try/Catch
Add throws declaration: If we don't want to report the error than we use this function.
Surround with Try/Catch block: If we want to report the error in selenium report than we use this function.
---------------------------------------------------------------------------------------------------------
Difference between Throws and Throw
Throws: Throws come in front of function and it tells Java that this function is capable of throwing an exception.
Ex: Add throws declaration" or Surround with Try/Catch
Throw: Throw clause is used to deliberately through an exception.
---------------------------------------------------------------------------------------------------------
Difference between final and finally
final: Stats that you cannot change the value of variable.
Ex: final int s=100; // Cannot change the value
//s =101; //it will give error
finally: finally is used with try/catch block. Finally stats that (finally is a block) it will executed for sure weather try block or catch block is executed or not.
Ex: try{
//establish DB connection
//fire query
//get results
//close connection
}catch{Exception e}
{
//error
System.out.println("error");
}finally{
//close the connection if established
System.out.println("is finally");
}
---------------------------------------------------------------------------------------------------------
Difference between Error and Exception:
Error: Out of memory error, ex: Assertion error thrown, when running the eclipse sometimes when run big program when error comes eclipse gone out of memory error.
Exception: run time exception, null pointer exception; divide by zero exception, array index out of bond etc.
---------------------------------------------------------------------------------------------------------
String to Integer Conversion:
String x = 100;
int 1 = Integer.parseInt (x);
int is the primitive data type but Integer is an in build class just like a String class which represent int.
---------------------------------------------------------------------------------------------------------
Integer to String Conversion:
String z = String.valueof(i);
---------------------------------------------------------------------------------------------------------
String to Boolean conversion:
boolean b = Boolean.valueof("true");
https://mail.google.com/mail/u/0/images/cleardot.gif
---------------------------------------------------------------------------------------------------------
Collection API: Array List and Hash table are collection API.
ArrayList is similar to Array but it is not an array, it is inbuilt class and it is dynamically growing in size.
Ex: It is used where we need to read/get bulk data from site, get data from popup etc.
ArrayList <String> list = new ArrayList <String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list.size());
for(int i =0;i<list.size();i++)
{
System.out.println(list.get(i));
}
---------------------------------------------------------------------------------------------------------
Hah table and array list both are dynamically growing data structure. whereas Array List grow with index and Hash table grows with Kay and Value pair.
---------------------------------------------------------------------------------------------------------
HashTable: Hashtable is inbuilt class we can create object of the class. Hashtable contains key value pair (key and value). Key should be unique and corresponding to each key there should be a value.
Ex: We used Hash table to store test data.
Hashtable <String, String> table = new Hashtable <String, String>();
table.put("Name", "Sunil");
table.put("Company", "InfoBeans");
table.put("Place","Indore");
System.out.println("------------ Hash Table ----------");
System.out.println(table.get("Name"));