You are on page 1of 23

1. What is the difference between Implicit Wait and Explicit Wait ?

Answer: Explicit Wait will make the WebDriver wait for a specific web element for the
specified time. i.e. Using Explicit Wait, we can make the WebDriver wait for web element 'a' for
5 seconds and another web element 'b' for 10 seconds.

Where as Implicit Wait will make the WebDriver wait for all web elements for the same
specified time. Its generally not set for one or two web elements, instead it is once set will be
applicable for all elements.  i.e. Unlike Explicit Wait, Implicit Wait once set will be applicable to
all the web elements and hence the specified wait time will be applicable for all the web
elements.

2. What are the different Classes which implement WebDriver ?

Answer: First of all WebDriver is an Interface and there are different classes which implement
this Interface in Selenium. The Classes which implement the WebDriver interface in Selenium
are FirefoxDriver, ChromeDriver, InternetExplorerDriver, OperaDriver, SafariDriver,
RemoteWebDriver, EdgeDriver and EventFiringWebDriver Classes.

Example: In the statement WebDriver driver = new FirefoxDriver(); , WebDriver is an Interface


and FirefoxDriver is one of the Classes which is implementing WebDriver Interface.

3. How to read tool tip text using Selenium WebDriver ?

Answer: Tool Tip is a text that will be displayed when we hover our mouse cursor over the text
or web element on any web page. Selenium WebDriver can read the tool tip text using
getAttribute() method. Click here to read how Selenium WebDriver uses getAttribute() method
for reading a tool tip from a web page.

4. What are the different WebDriver exceptions, you have faced while working with
Selenium Projects ?

Answer: Exception is a problem which occurs during the execution of the program and there by
stops the program from further execution. Similarly, while running the Selenium programs,
different types of Exceptions related to WebDriver may arise. The list of WebDriver related
Exceptions that may commonly arise during Selenium programs execution are:
NoSuchElementException, ElementNotVisibleException, NoSuchFrameException,
NoAlertPresentException, NoSuchWindowException, SessionNotFoundException and
StaleElementReferenceException. Click here to find out more on these WebDriver exceptions.
5. Can Selenium automate Desktop Applications ?

Answer: No. Selenium can only automate Web Applications.

Examples: Web Applications like Facebook.com, Gmail.com etc can be automated by Selenium.
Where as the Desktop Applications like Paint, Skype, MS Office etc. cannot be automated by
Selenium.

6. What are the different components / tools in Selenium ?

Answer: Selenium comprises of 4 different components / tools. i.e. Selenium IDE, Selenium


RC, Selenium WebDriver and Selenium Grid.

7. What is the latest version of Selenium ?

Selenium got released into the market in three different versions so far and hence Selenium 3 is
the latest version.

8. What is the purpose of using Firebug ?

Answer: Firebug is a firefox add-on which helps us in locating HTML code for the inspected
GUI elements on the web pages.

9. What is Selenium IDE ?

Answer: Selenium is one of the components / tools of Selenium and is a Record & Playback
tool.

10. What is the main component of Selenium ?

Answer: Selenium WebDriver is the main component of Selenium.

Selenium is one of the components / tools of Selenium and is a Record & Playback tool.
11. What is the disadvantage of using Selenium IDE ?

Answer: Its not suitable for automating the complex applications, as the maintenance of
automated tests is more. i.e. If any functionality changes on the application, the automation
engineer has to re-record all the automated tests which got affected with this change again in
Selenium IDE

12. What are the different types of Locators ?

Answer: id, name, class name, link text, css selectors , xpath expressions and DOM are the
different types of locators which we use in Selenium for locating the GUI elements on the web
pages.

13. What is the priority in which we have to use the locators ?

Answer: We have to use the locators in the below specified priority:

 First Priority : id locator


 Second Priority : name locator
 Third Priority: Class Name locator
 Fourth Priority: link text locator
 Fifth Priority: CSS Selectors locator
 Sixth Priority: XPath locator
 Seventh Priority: DOM locator

id, name, class name, link text, css selectors , xpath expressions and DOM are the different types
of locators which we use in Selenium for locating the GUI elements on the web pages.

14. What is the difference between Absolute XPath and Relative XPath Expressions ?

Answer: Complete path has to be specified with Absolute XPath expressions, where as in


Relative XPath Expressions we can provide direct path to the UI elements.

Hence because of simple changes in the applications functionality, the complete path in absolute
XPath may change. Since Relative XPath is the direct path, its path won't change with the simple
changes in the application.
15. What is the disadvantage of Absolute XPath and why is Relative XPath recommended
over it ?

Answer: For simple changes on the application like moving the button from one place on the
page to other place, the absolute XPath expression changes. As Relative XPath expressions won't
change with these simple UI changes on the pages, hence Relative XPath is recommended over
Absolute XPath.

16. What is the concept that makes XPath Expressions powerful out of all the
locators ?
Answer: XPath AXES is the concept which makes the XPath Expressions powerful out of all the
locators. i.e.  By using XPath AXES we can traverse both forward and backwards in the HTML
code of the web pages.

17. Why CSS Selectors have higher priority over XPath Expressions ?
Answer: The below are two reasons why CSS Selectors are preferred over XPath Expressions:

 When compared to XPath Expressions, CSS Selectors locate the UI elements faster.
 Selenium may not be able to locate few UI elements using XPath Expressions, while
executing the Automation scripts on Internet Explorer Browser.

18. What is the name of the Add-on which can automatically generate the XPath
Expressions and CSS Selectors ?

Answer: Firepath is the Add-on which can auto generate the XPath Expressions and CSS
Selectors, just by inspecting the GUI elements on the web pages.

19. Write a Java Program to print the even numbers between 1 and 100 using for loop ?

Answer: The below for loop can print the even numbers between 1 to 100:

for(int i=1;i<=100;i++) {

if(i%2==0) {
System.out.println(i);

20. Write a Java Program to find the sum of first 100 numbers using for loop ?

Answer: The below for loop can find the sum of first 100 numbers:

int sum = 0;

for(int i=1;i<=100;i++) {

sum = sum + i;

System.out.println("The sum of first 100 numbers is : "+sum);

21. Write a for loop which prints numbers from 1 to 100. But if the number is divisible by
5, then it should print 'divisible by 5 followed by that number' ?

Answer: The below for loop solves the below question:

for(int i=1;i<=100;i++) {

if(i%5==0) {

System.out.println("Divisible by 5 : "+i);

}else {

System.out.println(i);

}
22. Does Java support multiple inheritance ?

Answer: The answer is No and Yes. i.e. No in case of Classes and Yes in case of Interfaces.

Multiple inheritance is not supported in case of Classes:

In case of Classes, a single class cannot inherit multiple classes due to the ambiguity problem.

Lets say there are three Classes i.e. ClassA, ClassB and ClassC. Assume that ClassA and ClassB
have a method having same name say mOne() . In this case, if ClassC extends both ClassA and
ClassC, then there is an ambiguity of inheriting mOne() from either ClassA or ClassB. i.e. Java
cannot decide whether the mOne() can be inherited from ClassA or ClassB.

Hence multiple inheritance is not supported for Classes in Java.

Multiple inheritance is supported in case of Interfaces:

In case of Interfaces, a single class can inherit multiple interfaces as there is no ambiguity
problem.

Lets say there are two interfaces i.e. InterfaceA, InterfaceB and a class ClassC. Though
InterfaceA and InterfaceB have a method with same name say mOne(), ClassC can still inherit
multiple interfaces, as mOne() method is not implemented in any of the interfaces.

Hence multiple inheritance is supported by Interfaces in Java.

23. What is the parent or base class of all the classes in Java ?

Answer: All the classes in Java are children or grand children of Object Class.

24. What is the difference between instance variables and local variables in Java ?

Answer: Variables declared inside the methods are known as local variables, where as the
variables which are declared directly inside the class but outside the methods are known as
Instance variables.

25. Is Java a pure or 100% Object Oriented Programming language ?

Answer: No. Java uses primitive data types, hence it is not a 100% or pure Object Oriented
Programming language.
26. What is the difference between Primitive and Non-Primitive Data types in Java ?

Answer: Predefined or default data types in Java are known as Primitive Data types.

Examples for Primitive Data types are :  byte, short, int, long, float, double, char and boolean

Programmer defined data types in Java are known as Non-Primitive Data types. objects can be
created for non-primitive data types.

Examples for Non-Primitive Data types are : Strings, Arrays and Classes.

27. Why Strings are immutable in Java ?

Answer: In Java Strings are by default immutable (i.e. once created they cannot be changed).

For example: If you assign a value to the String say "Arun Motoori" as shown below:

String name = "Arun Motoori";

And, in the next statement, I will change the value of the String to other value say "Arun Kumar"
as shown below:

name = "Arun Kumar";

After performing the above two steps, though it looks like the value has been updated. But in real
a new object will be created to store new value "Arun Kumar" and the old object storing "Arun
Motoori" is not deleted. i.e. Instead of updating or changing the value stored in old object, a new
object will be created for every new value assigned to the same variable of String.

So in Strings, on assigning a value to the variable, a new object will be created to store the value
and the value stored in the object cannot be changed or mutated. Hence Strings are immutable in
Java.

But, StringBuffer in Java are mutable or changeable.

28. What is the difference between String and StringBuffer classes in Java ?

Answer: Strings in Java are immutable (i.e. cannot be changed once created), where as
StringBuffer is mutable.

Note: Refer to the Question 27 fore more details.


29. Write a program for accessing all the elements of the below two Dimensional
array using for loop.

        int[][] a = {{5,2,9},{4,6,8}};

Answer:  The below program accesses all the elements of a two dimensional array using
for loop:

for(int i=0;i<a.length;i++) {
        
        for(int j=0;j<a[i].length;j++) {
            
            System.out.print(a[i][j]+" ");
            
        }
        
        System.out.println();
        
    }

30. How to assign different types of values say integer, character, String, decimal
and boolean into the same array ?

Answer:  By creating the arrays as Object Class type, we can assign different types of
values say integer, character, String, decimal and boolean into the same array.

31. What is the disadvantage of Arrays ?

Answer:  Arrays are fixed in size. i.e. After creating an Arrays, we cannot change its size at
later point of the program. 

32. Practical Assignment : Go to Java 8 API page over Web and find the String predefined
class and its predefined methods say equals(), substring(), length(), trim() etc. .

Answer:  The below are the steps to find the String predefined class and its
predefined methdos in Java 8 API:
 Search "Java 8 API" in google search
 Click on the  https://docs.oracle.com/javase/8/docs/api/ from search results
 Search for 'String' predefined class in the displayed Java 8 API Library page
 And in the page the page of 'String' predefined class page, scroll down a bit and
find its predefined methods.

33. What is the difference between equals() and ==  operator in Java ?

Answer: equals() method is used to compare two Strings in Java, where as == operator is


a relational operator which is used to compare two data type values of same type. 

34. What is the purpose of using Wrapper classes in Java ?

Answer:  Wrapper classes represent respective data types values as objects in Java.  


Example: 'Integer' wrapper class represents 'int' data type value as an object in J.

35. What is the purpose of using Constructors in Java ?

Answer:  Constructors simplify the initialization of variables in Java. 


Reason: Instead of initializing the variables for each and every object, we can create a
constructor to initialize these variables, as the constructor is automatically called when the
object is created.  

36. How constructors are different from methods in Java ?

Answer: The below are few differences between constructors and methods in Java:

 Constructors must have the same name of their Class name


 Constructors are automatically called when the Objects are created for the Class
 Constructors won't have any return type
37. What is the purpose of using "this" keyword in Java ?

Answer: The purpose of using "this" keyword in Constructors and methods, is to


differentiate the instance variables from the parameterized variables having same names.

38. What is Overloading in Java ?

Answer:  Duplicate methods/constructor names are allowed inside the same class, as


long as their parameters count or parameters declaration are different. 

When this Overloading technique is applied to methods is known as method overloading and
when applied to constructors is known as constructor overloading. 

39. What is the purpose of using Packages in Java ?

Answer: Packages in Java are used to group / organize the related Classes, Interfaces and
other Files.

40. What is the keyword used by a Java Class to inherit the Properties say
Variables and methods of another Class ?

Answer: "implements" is the keyword used by a Java Class to inherit the Properties of


another Class. 

41. How to access the variables and methods of another Class in Java ?


Answer: In order to access the variables and methods from another class in Java, we have to
create an Object for the Class having those variables and methods.

42. What is Overriding in Java ?


Answer: When a method in the Child class (i.e. sub-class) is duplicate of a method in Parent
class (i.e. super-class) , then the method in the sub-class is said to override the method in
super-class. 
43. Is Overriding applicable for Constructors in Java ?

Answer: No,
Overriding can be implemented only for methods and cannot be
implemented for Constructors. 

Reason: As the name of the constructors needs to be same as the name of the Classes,
Overriding is not applicable for constructors. 

44. What are different modifiers in Java ?

Answer: Modifiers
in Java can be categorized into two types i.e. Access modifiers and
Non-Access modifiers.

 Different Access modifiers we have in Java are - public, private, protected and
default
 Different Non-Access modifiers we have in Java are - static, final and abstract

45. What is the difference between default and protected access modifiers in


Java ?

Answer: Both default and protected access modifiers specified properties act as public
to all the Classes inside the same package.

But coming to the Classes outside the package, default and protected properties act as
private.

The difference between default and protected is that, protected specified properties
can be inherited outside the package, where as default specified properties cannot be
inherited outside the package. 

46. What is the difference between static variables and instance variables in Java ?
Answer: static variables belong to the class memory, hence needs to be called using the
Class name. Where as instance variables belong to the object memory, hence needs to be
called using object reference.

47. What are the differences between static methods and non-static methods in
Java ?

Answer: The below are the differences between static methods and non-static methods
in Java:

 static methods belong to class memory, hence needs to be called using the
Class name. Where as non-static methods belong to object memory, hence
needs to be called using object reference.
 static methods can only access static variables and static methods. Where as
non-static methods can access both static and non-static variables and methods. 

48. What happens when we specify the final non-access modifier with variables


and methods in Java ?

Answer: Thevariables and methods will be have as below on providing them with final
non-access modifier:

 variable values cannot be changed at later point of the program. 


 methods cannot be overridden 

49. What is the difference between abstract classes and interfaces in Java  ?

Answer:  abstract classes contains abstract and non-abstract methods, where as all the
methods inside the interfaces are by default abstract type.

50. What is the keyword used for inheriting the interfaces in Java  ?

Answer:  "implements" is the keyword used for inheriting the interfaces in Java.


51. How to handle exceptions in Java  ? 
Answer: Exceptions in Java are handled using try .. catch blocks in Java.

The difference between default and protected is that, protected specified properties
can be inherited outside the package, where as default specified properties cannot be
inherited outside the package. 

52. What is the difference between checked and unchecked exceptions in Java  ? 


Answer: checked exceptions are the exceptions which are checked by the compiler, where as
unchecked exceptions are the exceptions which are ignored by the compiler and are
observed during execution of the program.

53. Assignment: Create a sample text file having ten lines of text and read the text
from the file using file handling mechanism with the help of while loop ?

Answer:   Please find the below program for reading the text form the file using while loop:

File file = new File("Sample.txt");


FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

String str;

while((str=br.readLine())!=null){

    System.out.println(str);

br.close();

54. What is the disadvantage of Arrays and how to overcome in Java  ? 

Answer: Arrays are fixed in size, where as collections overcome this disadvantage as they
are growable in size.
55. What is the difference between ArrayList and HashSet in Java  ? 
Answer: ArrayList stores the objects in an organized manner using index values, where as
HashSet stores the objects in a random order and don't have index values. 

56. What is the relation ship between Iterator interface and iterator() method in
Java  ? 

Answer: iterator() method is the predefined method of Collection interface and the return
type of iterator() method is Iterator interface.

57. What are the predefined methods of HashMap, which are used for adding the
value and retrieving the values  in Java  ? 

Answer:  HashMap uses put() method for storing key value pairs, and uses get(key) for
retrieving the values. 

58. What will the below Java code print  ? 


String x = "Latest version"
String y = "of Selenium "
int z = 3
System.out.println("We are learning Selenium"+" and the "+x+" "+y+" is "+z);

Answer:  We are learning Selenium and the Latest version of Selenium is 3

59. Write a program in Java to print the below output  ?

1
12
123
1234
12345
123456
1234567

Answer:   The below program prints the required output:

for(int i=1;i<=7;i++) {
            
    for(int j=1;j<=i;j++) {
        
        System.out.print(j+" ");
        
    }
    
    System.out.println();
            
}

60. Write a program in Java to create an integer array and print the elements of
the array in reverse ?

Answer: The below program prints the required output:

int[] a = {9,3,6,8,4,7};
        
for(int i=a.length-1;i>=0;i--) {
    
    System.out.print(a[i]+" ");
    
}

61. Write a program in Java to print alternative elements in a String array  ?

Answer: The below program prints the required output:

String[] a = {"One","Two","Three","Four"};
        
for(int i=0;i<a.length;i++) {
    
    System.out.println(a[i]);
    
    i++;
    
}
62. Write a program in Java to find the greatest number in an integer array  ?

Answer:  The below program prints the required output:

int[] a = {9,3,6,4,8,5};
        
int temp = a[0];

for(int i=0;i<a.length;i++) {
    
    if(temp<a[i]) {
        
        temp = a[i];                
    }
    
}

System.out.println("The greatest value in the array is "+temp);

63. Write a program in Java to find the least number in an integer array   ?

Answer:  The below program prints the required output:

int[] a = {9,3,6,4,8,5};
        
int temp = a[0];

for(int i=0;i<a.length;i++) {
    
    if(temp>a[i]) {
        
        temp = a[i];                
    }
    
}
System.out.println("The least value in the array is "+temp);

64. Is Java Case sensitive ?


Answer: Yes, Java is case sensitive. 

Examples:

 Java compiler accepts 'public' keyword, but complier error will be displayed on
providing 'Public' keyword.
 Java compiler accepts 'System' keyword, but complier error will be displayed on
using 'system' keyword with print statements

65. Which keyword is used for defining / creating a Class in Java ?

Answer: 'class' keyword is used in Java for defining / creating a class in Java.

Example: In the below code, Demo is the class name and class is the keyword which
is defining / creating it.

public class Demo{

66. What is the method from which Java programs starts execution ?


Answer: Java programs starts execution from main() method

Without main() method, we won't be able to run any Java programs. 

67. Give some examples for getting compiler errors in Java ?

Answer: The below are few examples which give compiler errors in Java:
 When we don't end any Java statement with ; symbol
 Java is case sensitive, hence we get compiler errors when we provide wrong
cases
 When starting brace '{' count, don't match the ending brace count '}'

68. What is the difference between print and println statements in Java ?


Answer: 'print' statements prints the provided text and stays in the same line, where as
'println' statements prints the provided text and moves to the new line before executing any
following statements.  

69. What are the different types of comments in Java ?


Answer: There are two types of commenting mechanisms in Java. 

 Single line comments



o Starts with //
o Used to comment only single line
 Multi line comments

o Starts with /* and ends with */  
o Used to comment multiple lines 

70. What are the different things required for storing data in Java ?
Answer:  The below are the different things required for storing data in Java programs:

 Data types
 Variables
 Literals
 Assignment operator =

71. What is the difference between Instance variables and local variables ?


Answer: Variables declared inside the methods are known as local variables, where as the
variables which are declared directly inside the class but outside the methods are known as
Instance variables.

72. What are the different data types in Java and what is their purpose ?
Answer: The below are the different data types in Java:

 byte
 short
 int
 long
 float
 double
 boolean
 char

Purpose: On declaring a variable with int data type, we can store integer values into the
variables and on declaring a variable with boolean data type, we can store true/false into
the variables. Hence data types define the type of values we can store into their defined
variables.

73. Is String a primitive data type ?


Answer: No, String is not a primitive data type. Instead String is a predefined class in
Java. 

74. What are the different types of operators in Java ?


Answer:  The below are the different types of operators in Java :

 Arithmetic operators - Examples: +, -, *, /,%, ++,--


 Relational operators - Examples: ==,!=,<,>, <=,>=
 Logical operators - &&, ||, !
 Assignment operators - =, +=, -=,*=, /=, %=
 Conditional operators - ?:

75. What are the different flow control structures in Java ?


Answer: The below are the different flow control structures in Java:
 Selection statements - if, if .. else, if .. else if .. else and switch
 Iterative statements - while, do .. while, for and for-each 
 Transfer statements - break, continue, return, try, catch and finally

76. What is the difference between while and do .. while loop in Java ?


Answer: do-while loop works similar to while loop, but the block of code inside the do-
while loop will be executed at-least once even after the loop condition is false

77. What is the difference between break and continue statements in Java ?


Answer: break and continue are the transfer statements in Java. 

 The purpose of the break; statement is to come out of the statements based on
some condition.
 The purpose of the continue statement is to skip the current iteration of a loop based on
some condition and continue with the next iteration.

78. Can we call the same method multiple times in Java ?

Answer: Yes, the same method can be called any number of times in Java. 

79. What is the keyword to be used in Java while declaring methods, when the
method don't have anything to return ?

Answer: void is the keyword , we have to use when the method don't return any value
to its calling method. 

80. What are the different types of Arrays in Java ?

Answer:  The below are the different types of Arrays in Java:

 Single dimensional arrays


 Two dimensional arrays
 Three dimensional arrays

81. What is the predefined variable of Arrays, which can be used to find the size of
the arrays ?

Answer: 'length' is a predefined variables of Arrays, which is used find the size of the
arrays.

82. Provide an example for using for loop with single dimensional arrays ?
Answer: Please find the below example:

int[] a = {5,3,9};

for(int i=0;i<a.length;i++) {
  
     System.out.println(a[i]);
  
}

83. Provide an example for using for-each loop with single dimensional arrays ?
Answer: Please find the below example:

int[] a = {5,3,9};

for(int temp:a) {
  
     System.out.println(temp);
  
}

84. Write a Java Program to print the even numbers between 1 and 100 using for
loop ?

Answer:  The below for loop can print the even numbers between 1 to 100:

for(int i=1;i<=100;i++) {
   if(i%2==0) {

System.out.println(i);

   }

85. Write a Java Program to find the sum of first 100 numbers using for loop ?
Answer:   The below for loop can find the sum of first 100 numbers:

int sum = 0;

for(int i=1;i<=100;i++) {

   sum = sum + i;

System.out.println("The sum of first 100 numbers is : "+sum);

86. Write a for loop which prints numbers from 1 to 100. But if the number is
divisible by 5, then it should print 'divisible by 5 followed by that number' ?

Answer:  The below for loop solves the below question:

for(int i=1;i<=100;i++) {

if(i%5==0) {

System.out.println("Divisible by 5 : "+i);

}else {

System.out.println(i);

87. What is the super interface of WebDriver ?


Answer:  SearchContext is the super interface of WebDriver and WebElement interfaces in
Selenium. 

You can also check it at http://seleniumhq.github.io/selenium/docs/api/java/index.html

88. Is the FirefoxDriver a Class or an interface ?


Answer:  FirefoxDriver is a predefined Class in Selenium.

89. What is the difference between driver.get("url") and


driver.navigate().to("url") ?

Answer:  Both driver.get() and driver.navigate().to() do exactly the same. They both take
you to the page of the provided URL. driver.get() is easier than driver.navigate().to(), as it
takes very less time to type. In addition to this, driver.navigate() also has the capability to
navigate back, navigate forward() and refresh the page using back(), forward() and
refresh(). 

90. How can we create a Data Driven Framework using TestNG ?


Answer:  TestNG provides an annotation known as @DataProvider and also dataProvider
attriubute of @Test annotation. Using which, we will be able to achieve the Parametrization
of tests. Hence the role of TestNG in Data Driven framework is to parameterize the tests
using the above specified annotations and their attributes. Along with TestNG, we also need
to use POI API for reading the test data from the Excel files. 

You might also like