- Manual Testing Interview Questions (01-40)
- Manual Testing Interview Questions (41-80)
- Selenium With Java Interview Questions - (01-35)
- Selenium With Java Interview Questions - (36-70)
- Java Programs for QA Automation Interview (01-15)
- Java Programs for QA Automation Interview (16-30)
- TestNG Interview Questions for QA (01-20)
- TestNG Interview Questions for QA (21-40)
- Maven Interview Questions for QA Automation Interview (01-15)
- Maven Interview Questions for QA Automation Interview (16-30)
- Most Frequently Asked API Testing Interview Questions (01-20)
- Most Frequently Asked API Testing Interview Questions (21-40)
1. What is Java and explain features of java.
- Java is high-level, object-oriented, secure, platform independent, robust, high performance and portable programming language.
- James Gosling - 1991.
- It is also known as Platform as it provides its own JRE and API.
Simple:
- Java is a straightforward, clean, and easy-to-understand language. It is simple because:
- Java syntax is based on C++, so it is easier for programmers to learn it after C++.
- Explicit pointers and operator overloading, among other features, have been removed from Java.
- Automatic Garbage Collection in Java.
Object-oriented:
- Java is an object-oriented programming language. Everything in Java is an object.
- Object-oriented software is made up of a variety of distinct types of objects that include both data and behavior.
Platform Independent:
- Java comes with its own platform for executing its code.
- Java doesn't depend upon any operating system to be executed.
Portable:
- Java is Write Once and Run Anywhere.
- Java program (.java) converted into byte code(.class) which can easily run on any machine.
Secured:
- Because it does not use explicit pointers, Java is safe.
- Java also provides the concept of bytecode and exception handling which make it more secured.
Robust:
- Java is a powerful programming language because it makes extensive use of memory management.
- The concepts like automatic garbage collection, exception handling make it more robust.
Architecture-neutral:
- Java is architecture neutral because it has no implementation-dependent features, such as the size of primitive types.
- The int data type occupies two bytes in 32-bit architecture and four bytes in 64-bit architecture in C programming. In Java, however, it takes up 4 bytes of memory on both 32 and 64-bit platforms.
Interpreted:
- Java uses a JIT interpreter along with the compiler for program execution.
High-performance:
- Java bytecode is faster than other traditional interpreted programming languages since it is "near" native code.
Multi-threaded:
- The primary advantage of multi-threading is that each thread consumes no memory. It shares a common memory area.
Dynamic:
- Java is a dynamic language. It supports the dynamic loading of classes. This means that classes are loaded as needed.
Distributed:
- Java is distributed because it makes it easy for users to construct distributed Java applications. Distributed applications are built using RMI and EJB.
- This Java feature allows us to access files from any system connected to the internet by calling methods.
2. Is java pure object oriented?
- Java is not a pure Object Oriented Language because it supports primitive data types such as byte, boolean, char, double, float, int, long, short.
- These primitive data types are not object oriented. This is the reason why Java is not a 100% object-oriented language.
- No, only methods can be declared as protected.
- Yes. Source file can contain any number of class declarations but only one class can be declared as public.
- JRE stands for “Java Runtime Environment”. The Java Virtual Machine (JVM), Java platform classes, and support libraries are all part of it.
- Only previously written apps can be run with JRE. We are unable to create new apps or make changes to existing ones.
- As the name suggests, JRE only provides a Runtime Environment.
6. What is JDK and why does it need to be installed?
- JDK stands for Java Development Kit. It's a JRE superset.
- We can use JDK to create, compile, and execute (run) new programmes as well as modify old ones.
- JDK contains JRE as well as development tools (environment to develop, debug and monitor Java programs).
7. What is the JVM and why is it necessary?
- JVM stands for Java Virtual Machine. JVM drives the java code.
- We can run Java byte code by translating it to the current OS machine language using JVM.
- It turns Java into a portable language (write once, run anywhere)
8. Explain public static void main (String[] args)
- The main() is where every Java program begins. JVM always looks for this method signature to start running an application.
- public static void main(string[] args) can also be written as public static void main(String args[]). Don’t get confused.
- public is an access modifier. The scope of public access modifiers is everywhere.
- It has no restrictions. Declared public data members, methods, and classes can be accessible from anywhere.
- If you make a main() method public then it is allowed to be executed by any program globally.
- If you make a main() method non-public then it is not allowed to be executed by any program.
- To access the main method, the JVM cannot build a class object at runtime. By declaring the main() method as static, JVM can invoke it without instantiating the class.
- JVM will not be able to call the main method to run the program if it is not declared as static.
- The term "void" refers to a method that returns no value.
- Every method in Java specifies the return type.
- In Java, the main method does not return anything.
- Java program starts with the main method and terminates once the main method is finished executing.
- If we make the main method return a value, JVM will be unable to do anything with it. As a result, no value must be returned.
- main is the name of Java main method. When you compile and run a Java program, it is the first thing that runs.
- JVM looks for the main method to start the execution of a Java program.
- String[] args is a parameter that accepts inputs. The name of the String array is ‘args‘ but it can be of anything based on users choice.
- The type of args is always String[] because Java's main method accepts only a single argument of the type String array.
- We can change the array name to whatever and it will still work. i.e. String[] xyz or String[] abc
9. How many types of memory are allocated by JVM?
- Heap - whenever an object is created.
- Class area
- Static
- Native method stack.
- Program counter register
10. What is a platform?
- Platform is a set of hardware and software environments on which pieces of software are executed.
- Java is a software based platform.
11. JIT compiler
At compile Time At Runtime
Source code . java → compiler → Bytecode → JIT compiler → Native Machine Code
- The Java Runtime Environment (JRE) is responsible for executing source code and it contains a JIT compiler that does the performance optimization.
- It converts the bytecode into native machine code at runtime.
- The bytecode - Java compiler converts java source code file into byte code which is intermediate language between source code and machine code.
- The bytecode is not platform specific and can be executed on any machine.
13. Is the empty .java file name valid?
- Yes.
Class A {
// code
}
To compile: javac .java
To run: java A
14. Which OOPs Concepts you used in your current project?
1. Inheritance
We establish a Base Class in a standard Page Object Model to initialise the WebDriver interface, Data Source, Excel Reader, Property File or Config File, WebDriver waits, and so on. We extend the Base Class in our Test Class and Utility Class.We do it by using the extends keyword and achieve the Inheritance. This makes the class more reusable, as we won't have to repeat the startup code.
2. Encapsulation
We know that in a POM Project, we construct a distinct class for each page. All of these classes are excellent examples of encapsulation, in which the data of one class is kept separate from that of another. We declare the data members using @FindBy and initialise them using a constructor with initElement() to utilize them in the test methods.
3. Polymorphism
Polymorphism is based on the idea of a single interface that supports numerous methods. WebDriver is a browser interface that supports several methods such as ChromeDriver(), IEDriver(), SafariDriver(), and FirefoxDriver().
Method Overloading In Selenium
- Methods Overloading is the process of employing two methods with the same name but different parameters in the same class.
- We now use Implicit Wait in Selenium to have the page wait for a set amount of time.
- Because we may supply different Timestamps or TimeUnits like SECONDS, MINUTES, and so on, this is the ideal example of Method Overloading.
Method Overriding In Selenium
- In the WebDriver interface, we use two different methods for navigating or accessing any website i.e. driver.get() and driver.navigate().to().
- These two methods are examples of Method Overriding.
16. Compile-time Polymorphism vs Runtime Polymorphism
17. Method Overloading vs Method Overriding
- Local variables are not initialized to any value, not primitive or object references.
19. What is the initial value of Object reference which is defined as an instance variable?
- All object references are initialized to null.
- Private: If a variable is designated private, it can only be accessed by other members of the class. Outside of the class, this variable will be unavailable. As a result, outside members are unable to access private members.
- Public: Methods/variables with public modifiers can be accessed by all the other classes in the project.
- Default: If a variable/method is defined without any access modifier keyword, then that will have a default modifier access.
- Protected: If a variable is declared as protected, then it can be accessed within the same package classes and subclass of any other packages.
21. When to use interfaces and when to use abstract classes?
Abstract classes:
- The abstract class is a good choice if you want common base class implementation to all derived classes.
- It is a good choice if you want to use non-public members because in an interface all members must be public.
- If we want to add a new method in future then abstract class is a good choice because if we add a new method to the interface then all the classes that implemented this interface need to implement a new method.
- Abstract classes have an advantage of better forward compatibility. Once a client uses the interface, we can still add new behavior without breaking existing code.
Interfaces:
- Interfaces are a good choice when we think API’s will not change for a while.
- If we are using a small concise bit of functionality, use interfaces. If we are designing large functional units, use abstract classes.
- We want to use things similar to multiple inheritance.
22. Abstract Class vs Interface
23. What is the purpose of static methods and variables in Java?
- The methods or variables that are defined as static are shared among all the objects of the class.
- The static is a part of a class and not the objects.
- The static variables are defined in the class area and we don't need to create an object of the class to access such variables.
- Packages avoid name clashes.
- Packages provide easier access control.
- We can have classes accessed only within the package.
25. What is a Constructor?
- Constructor is a special type of method that has the same name as the class name and is used to initialize the state of an object.
- Every time an object is created using a new keyword, default constructor is called.
- It must not return any explicit value.
26. How many types of constructor are used in java?
- Default Constructor and parameterized constructor.
The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.
class Student{
int id;
String name;
Student_Info(){
System.out.println(id+” ”+name);
}
Public static void main(String[] args){
Student s1 = new Student();
Student s2 = new Student();
s1.Student_Info();
s2.Student_Info();
}
}
Output:
0 null
0 null
28. Does the constructor return any value?
- Instance of a class
29. Can constructor be inherited?
- No
30. Can constructor be overridden?
- No
31. Can you make the constructor final?
- No, constructor can never be declared as final because it is never inherited.
- If you try to do so it will throw a compile-time error.
32. Can constructor be overloaded?
Yes, by changing the number of arguments or by changing data types of arguments.
33. Can we use this and super in a constructor?
- No
- The term "static variable" refers to the property that all objects share.
- Static methods belong to class rather than objects.
- It can be called using class names. Static variables can be accessed and changed by static methods.
36. What are the restrictions applied to java static methods
- Static methods cannot use non-static variables or call non-static methods directly.
- Because this and super are non-static, they can't be utilised in a static context.
37. Why is the main method static?
- Because objects are not required to call the static method.
- If we make java main method non-static then JVM will have to create an object first and then call main method which will lead to extra memory allocation.
38. Can we override the static method?
- No
39. Can we overload the static method?
- Yes.
class Test1 {
static void display(int a){
System.out.println(a);
}
}
class Test2{
static void display(int a,int b){
System.out.println(a+" "+b);
}
public static void main(String[] args){
Test1.display(10);
Test2.display(10,20);
}
}
Output:
10
10 20
40. Can we make the constructor static?
- Constructor is invoked when an object is created. If we try to make the constructor static, it will throw a compiler error.
41. Can we override the overloaded methods?
- Yes
42. Can we override the private methods?
- No. Because the scope of private methods is limited to the class and we cannot access them outside of scope.
43. Can we change the scope of the overridden method in subclass?
Yes, We can change the scope of a method in a subclass. But, we cannot decrease the accessibility.
- Private: Private can be changed to public, private, protected.
- default: default can be changed to public.
- protected: Protected can be changed to public and default.
- public: public will remain public.
44. Can we execute a java program without the main() method?
- It is possible before JDK 1.7. Since JDK 1.7 it is not possible.
45. Can we make abstract methods static?
- No.
- In Java, if we declare an abstract method as static then it will become part of a class and we can call it directly which is unnecessary.
- Calling undefined method is completely useless therefore it is not allowed.
- Yes
- Object class
- Because pointers are unsafe and complex to understand.
- Stop value change.
- Stop method overriding.
- Stop inheritance.
Next: Java Interview Questions for QA (51-100)
Greetings, reader! Your input is highly important to us. Please share your thoughts in the comments section below.
Contact:
Email: piyushagrawal.automation@gmail.com
Follow on LinkedIn: Piyush Agrawal - LinkedIn
Follow on YouTube: Piyush Agrawal - Youtube
No comments:
Post a Comment