Java Flashcards

1
Q

Versions of java you worked with? What version of java do you currently use in your framework? Difference between JRE, JDK ?

A

Currently I am using Java 1.8 version, JRE, or Java Runtime Environment, is the essential component for running Java applications, comprising the Java Virtual Machine and supporting libraries. JDK, or Java Development Kit, is a superset of JRE and includes development tools like compilers and debuggers. JVM, or Java Virtual Machine, translates and executes Java bytecode, enabling the portability of Java code. The bytecode is generated by the Java compiler and transformed into class files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The difference between == and =

A

“=” is used to assign values, and “==” is a comparison operator used for equality checks in programming languages. It checks if two values are equal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a method? What is the main method? Why do we need one in java? Do we have to have a main method in java?

A

A method in Java is a set of instructions within a class that performs a specific task. The main method is a special method serving as the entry point for Java program execution, essential for the Java Virtual Machine (JVM) to start running the code. Having a main method is necessary in Java for creating an executable program; without it, the JVM doesn’t know where to begin execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain public static void main (String args[])?

A

public static void main(String args[]), shows the standard signature for the main method in Java, which is required for the JVM to recognize and initiate the execution of our program. public: it is an access specified which means it will be accessible by any Class. static: is a keyword to call this method directly using a class name without creating an object of it. void: it is a return type i.e. it does not return any value. main(): it is the name of the method that is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main executions occur. string args[]: it’s a command line argument passed to the main method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are Access Modifiers (Private, public, protected)? How did you use them?

A

Java provides access modifiers to set access levels for classes, variables, methods and constructors. public: Anyone, from anywhere, can access a class, constructor, inner class, method, or field variable marked as public. - protected: Classes in the same package or subclasses, even if in different packages, can access items marked as protected. - private: Only the class in which they are declared can access items marked as private. - Default (no modifier specified): When no access modifier is specified, it is said to have default access, which means it’s accessible only within the same package. In simpler terms, public is for everyone, protected is for certain friends (same package or subclasses), private is for yourself (only within the class), and default is for neighbors (within the same package). In our framework we follow the page object model design pattern, in page classes we store WebElements as public to give visibility to our test classes in a different package, Also in our framework, we build utility classes where we store methods to work with web elements, property files, and excel files and give public accessibility to those methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an instance variable and how do you use it? What is the difference between local and instance variables?

A

local variables is variables inside a method, constructor, or blocks, made when the method is used and gone when it’s done. Instance variables, declared in a class but not inside a method, are used by creating an object of the class. They are created when we make an object with ‘new’ and disappear when the object is destroyed. an example from a framework could be the use of an instance variable like @FindBy(xpath=”//img[contains(@src, ‘logo’)]”) public WebElement logo;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we access variables without creating an object instance of it? Difference between Instance Variable and static Variable? What is a static keyword in java? Where did you use static in your framework?

A

Static variables, declared with the static keyword outside methods, can be accessed across different classes without creating an object. On the other hand, instance variables, declared in a class, require creating an object to access them. Class variables (static) have a single shared copy among all objects of a class, while every object has its own copy of an instance variable. Changes to instance variables don’t reflect in other instances, but class variables have only one value across different objects. Static variables exist from program start to stop, whereas instance variables are created when an object is made with ‘new’ and disappear when the object is gone. The static keyword in Java signifies that the variable or method belongs to the class, shared among all instances. It allows accessing class variables and methods without an object reference. Static methods cannot call non-static members. In my framework, the static keyword is useful for common utility methods stored in a class, making them easily accessible without object creation. Example methods include waiting, frame switching, button clicking, and dropdown value selection. Static variables are also used in base classes and configuration readers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Super vs super()? this vs this()? Can super() and this() keywords be in the same constructor?

A

super is used to refer to the immediate parent class object, while super() calls the parent class constructor;this refers to the current instance of the class, and this() calls the current class constructor, and though both super() and this() can be in the same constructor, but only one of them can be utilized as the first statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a constructor? Use of constructor in class? Can you make the constructor static? What is the difference between constructor and method? Can we overload a constructor?

A

A constructor is a special method in a class that is automatically called when we create an object. It has the same name as the class and is used to set up the initial values of the object. Constructors don’t have a return type, and they ensure that the object starts with the right information. Unlike regular methods, constructors can’t be declared as static because their main job is to create instances of the class. Methods, on the other hand, are called explicitly in our code and perform actions or operations, we can have multiple versions of a constructor in the same class, a concept known as constructor overloading. This allows us to create objects in different ways, providing flexibility in how we initialize them. So constructors initialize object values and are automatically called when an object is created. They are distinct from methods, which are explicitly called and perform actions, and constructors can be overloaded for different ways of creating objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Difference between an abstract class and interface? Can we create an object for an abstract class? Interface? When to use abstract classes and interfaces in Java?

A

An interface is like a plan that tells classes what methods to have, and it also includes certain constant values. On the other hand, an abstract class, marked with the “abstract” keyword, can have both must-have methods and some that can be defined later. interface is only lists methods and constants that must be followed. But an abstract class is more flexible; it can have some methods already decided and others left for later. Interfaces can be thought of as a set of rules that multiple classes can follow at the same time, while abstract classes are like a starting point that other classes can build upon. In practical terms, when working with Selenium, WebDriver is like the rulebook, and FirefoxDriver is a class that follows those rules. It’s like saying, Here’s how a web driver should behave, and Firefox, you’re one way to do it. So, interfaces are strict rules, and abstract classes are a bit like a starting point for classes to follow rules and build upon.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain OOPS concepts? Is java 100% object oriented?

A

No, Java is not 100% object-oriented, since it has primitive data types, which are different from objects. Java’s Object-Oriented Programming (OOP) revolves around manipulating objects with a focus on four key concepts: inheritance for code reusability, polymorphism allowing objects to take on various forms, abstraction hiding implementation details and emphasizing functionality, and encapsulation binding code and data for security using private access and getter/setter methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is inheritance and benefits of it? Types of inheritance? How do you use it in your code?

A

In programming, inheritance lets a new class inherit features from an existing class, helping code reuse, customization through method changes, and starting a hierarchy for well-organized and easily maintainable code. Types of Inheritance: Single Inheritance - single base class and single derived class. Hierarchical Inheritance - when a class has more than one child classes (subclasses) Multilevel Inheritance - single base class, single derived class and multiple intermediate base classes. Multiple Inheritance - multiple classes and single derived class (Possible through interface only) Hybrid Inheritance - combination of both Single and Multiple Inheritance(Possible through interface only). In our current Cucumber framework, we have BaseClass where we initialize the WebDriver interface. After we extend the Base Class in other classes such as PageInitializer and to the Common methods where we have functions to work with Web Browser.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is polymorphism? Types of polymorphism?

A

Polymorphism is the ability of an object on many forms. Polymorphism allows us to perform a task in multiple ways. A combination of overloading and overriding is known as Polymorphism. There are two types of Polymorphism in Java 1. Compile time polymorphism (Static binding) – Method overloading 2. Runtime polymorphism (Dynamic binding) – Method overriding

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Method overloading & overriding? How do you use it in your framework? Any example or practical usage of Run time polymorphism?

A

Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters (remember that method parameters accept values passed into the method). Overloading: Same method name with different arguments in the same class. Method overriding: Declaring a method in child class which is already present in the parent class, overriding means to override the functionality of an existing method. With method overriding a child class can give its own specific implementation to an inherited method without modifying the parent class method. Assume we have multiple child classes. In case one of the child classes wants to use the parent class method and the other class wants to use their own implementation then we can use overriding features.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can we override/overload the main method? Explain the reason? Can you override the static method? Can we overload and override private methods?

A

We cannot override a static method, so we cannot override the main method. However, you can overload the main method in Java. But the program doesn’t execute the overloaded main method when you run your program; you have to call the overloaded main method from the actual main method. We don’t use it in my framework. Static methods are bound with class, it is not possible to override static methods. In java not possible to override private methods because these methods are specific to classes, not visible in child classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can we achieve 100% abstraction in JAVA?

A

In JAVA abstraction can be achieved with the help of Abstract Classes and Interfaces. Using abstract class, we can achieve 0 to 100% or partial abstraction. Using interfaces, we can achieve 100% or full abstraction.

16
Q

What is encapsulation?

A

It is the technique of making the fields in a class private and providing access to the fields through public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. Therefore, encapsulation is referred as data hiding, main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. Encapsulation gives maintainability, flexibility, and extensibility to our code.

17
Q

What are the primitives and wrapper classes?

A

Primitives are data types in Java. There are a total of 8 primitive data types in Java: byte, short, int, long, float, double, char, boolean. Every primitive data type has a class dedicated to it and these are known as wrapper classes. These classes wrap the primitive data type into an object of that class.

18
Q

What is collection in Java and what type of collections have you used?

A

Java provides Collection Framework which defines several classes and interfaces to, Maps are not part of collection but built based on the collection concepts represent a group of objects as a single unit. Mostly in my current project we use List and Map.

19
Q

What is array and Arraylist (List)? Difference between them?

A

● Arrays are fixed in size but ArrayLists are dynamic in size. ● Array can contain both primitives and objects but ArrayList can contain only object elements. ● To find the size on an Array we use ArrayName.length and for arrayList we use Array-ListName.size() ● Array uses assignment operators to store elements but ArrayList uses add() to insert elements. ● Array can be multi dimensional , while ArrayList is always single dimensional.

20
Q

Difference between ArrayList vs LinkedList?

A

ArrayList and LinkedList, both implement List interface and provide capability to store and get objects as in ordered collections. Both are non-synchronized classes and both allow duplicate elements. ArrayList; ● ArrayList internally uses a dynamic array to store the elements. ● Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the bits are shifted in memory. ● ArrayList is better for storing and accessing data. LinkedList ● LinkedList internally uses a doubly linked list to store the elements (consist of value + pointer to previous node and pointer to the next node) ● Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory. ● LinkedList is better for manipulating data. ArrayList vs Vector? Both implement List Interface and maintains insertion order ArrayList is not synchronized, so it is fast. Vector - is synchronized, so it is slow.

21
Q

Difference between HashSet vs HashMap ?

A

HashSet 1. HashSet class implements Set interface 2. In HashSet, we store objects(elements or values). 3. HashSet does not allow duplicate elements that mean you cannot store duplicate values in HashSet. 4. HashSet permits to have a single null value. 5. HashSet is not synchronized. HashMap 1. HashMap class implements the Map interface 2. HashMap is used for storing Key, Value paired objects. 3. HashMap does not allow duplicate keys however it allows having duplicate values. 4. HashMap permits a single null key and any number of null values. 5. HashMap is not synchronized. ArrayList vs HashSet? Both ArrayList and HashSet are non synchronized collection class , Both ArrayList and HashSet can be traversed using Iterator. ArrayList ● ArrayList implements List interface ● ArrayList allows duplicate values ● ArrayList maintains the order of the object in which they are inserted ● In ArrayList we can add any number of null values ● ArrayList is index based. HashSet ● HashSet implements Set interface ● HashSet doesn’t allow duplicates values ● HashSet is an unordered collection and doesn’t maintain any order ● HashSet allow one null value ● HashSet is completely object based

22
Q

What is Map? How did you use it in your framework?

A

Java Map Interface. Map contains values based on key, i.e. key and value pairs. Each key and value pair are known as an entry. Map is a collection of entry objects. A Map contains unique keys. A Map is useful if we have to search, update or delete elements based on a key. The Map interface is implemented by different Java classes, such as HashMap, HashTable, LinkedHashMap and TreeMap. HashMap: it makes no guarantees concerning the order of iteration, HashMap doesn’t maintain the insertion order of elements. LinkedHashMap: It orders its elements based on the order in which they were inserted into the set (insertion-order). TreeMap: It stores its elements in red-black tree, orders its elements based on their values; it’s significantly slower than HashMap. HashTable: it also stores the data in a key-value pair, but the HashTable stores only non-null objects i.e any key or value can’t be Null. HashTable is similar to HashMap except it is synchronized or we say thread safe.

23
Q

Difference between HashTable vs HashMap ?

A

Both HashMap and Hashtable implement Map Interface. HashMap: ● HashMap is non-synchronized, so it is not-thread safe, HashMap is fast, HashMap allows one null key and multiple null values. Hashtable: Hashtable is synchronized, so it is thread-safe, Hashtable is slow, Hashtable doesn’t allow any null key or value.

24
Q

How can you handle exceptions? Types of exceptions you faced in your project? What is the parent of all exceptions?

A

Exception is a problem that can occur during the normal flow of execution. Depending on the situation, we can use try catch finally blocks. In try block: Code that might throw some exceptions , In catch block we define exception type to be caught and what to do if an exception happens in TRY block code Types of Exception: Checked Exceptions are the exceptions that are checked at compile time. For example checked exceptions are; ClassNotFoundException here class not found, InstantiationEx-ception here is Attempt to create an object of an abstract class or interface FileNotFoundException is attempt to open file that doesn’t exist or open file to write but have only read permission, On the other hand Unchecked Exceptions are the exceptions that are not checked at compile time, they are Runtime Exceptions. Exception faced as part of java perspective for example; ArithmeticException - Arithmetic error, such as divide-by-zero, ArrayIndexOutOfBoundsException - Array index is out-of-bounds, NullPointerException - Invalid use of a null reference, IllegalArgumentException - Illegal argument used to invoke a method.

25
Q

What is the difference between final, finally, and finalize?

A

The final keyword is used to apply restrictions on class, methods, and variables and used to declare constant values. The variable declared as final should be initialized only once and cannot be changed, used to prevent inheritance. Java classes declared as final cannot be extended, used to prevent method overriding. Methods declared as final cannot be overridden. Finally block is always executed when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. Finalize() method is a protected method of java.lang.object class and it is inherited by every class we create in Java, it is used to perform some clean up operations on an object before it is removed from memory.

26
Q

What is the difference between String and StringBuffer? String and StringBuilder? What is mutable and immutable? StringBuffer vs StringBuilder?

A

The most important difference between String and StringBuffer in java is that String object is immutable whereas the StringBuffer object is mutable. Once a String Object is created we cannot change it and every time we change the value of a String there is actually a new String Object getting created. For example we cannot reverse a string directly, only through using the StringBuffer class. There are 2 ways to make String mutable: 1. by using StringBuffer 2. by using StringBuilder. The StringBuffer and StringBuilder Class are mutable means we can change the value of it without creating a new Object. Objects of StringBuilder and StringBuffer Classes live inside heap memory. immutability vs. mutability ✓ String is an immutable class. It means once we are creating String objects it is not possible to perform modifications on existing objects. (String object is fixed object) ✓ StringBuffer and StringBuilder are mutable classes. It means once we are creating StringBuffer/ StringBuilder objects on that existing object it is possible to perform modifications. StringBuffer vs StringBuilder? Both Classes are mutable, except StringBuffer is thread-safe (synchronized) and StringBuilder is not thread-safe (non synchronized) which makes StringBuilder faster compared to StringBuffer.

26
Q

How many catch blocks can we have? Which catch block will get executed if you get an ArithmeticException?

A

Throws is used to declare an exception, it works similar to the try-catch block. is used in method declaration, is followed by exception class names, we can declare multiple exception with throws, throws declare at method it might throws Exception used to hand over the responsibility of handling the exception occurred in the method to the caller method. Throw is used in the method body to throw an exception, it is followed by an instance variable, we cannot declare multiple exceptions with throw, The throw keyword is used to handover the instance of the exception created by the programmer to the JVM manually, throw keyword is mainly used to throw custom exceptions.

27
Q

What is a singleton and have you used the singleton concept in your project ?

A

In my current project, I do not use the concept of singleton class. But I know what the singleton concept is in Java. It means there is only one copy of that class. If you try to create another one, it just refers back to the first one. This ensures that when changes are made inside the class, they affect everyone using it. It is commonly used for tasks like logging and managing driver objects.

28
Q

What is the Garbage Collection in Java?

A

Java automates the ID and removal of unused objects in the heap memory. In the heap, memory is assigned for objects when they are created, and it remains assigned until there are references to those objects. The Garbage Collection process is responsible for retrieving memory when objects are no longer needed, opening up space in the heap. This automatic management gives effective memory usage throughout the program’s execution.