HomeAutomation

Java for Testers Interview Questions And Answers – Part 9

Java for Testers Interview Questions And Answers – Part 9

Java for Testers – Interview Questions and Answers Part-9

1) JVM is dependent or independent platform?

The JVM is not platform independent. Java Virtual Machine (JVM) provides the environment to execute the java file(. Class file).
So at the end it’s depends on your kernel , and kernel is differ from OS (Operating System) to OS.
The JVM is used to both translate the bytecode into the machine language for a particular computer, and actually execute the corresponding machine-language instructions as well. Without the JVM, you can’t run a Java application.

2) What is toString() method ?What happens when I use in the program?

Object class contains toString() method. We can use toString() method to get string representation of an object. Whenever we try to print the Object reference then internally toString() method is invoked.

3) What is the capacity of String Buffer?

Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.

4) What is dom concept?

The Document Object Model is a cross-platform and language-independent interface that treats an XML or HTML document as a tree structure wherein each node is an object representing a part of the document. The DOM represents a document with a logical tree.

5) What is genrics?

Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs.

6) What is synchronization?

Synchronization in java is the capability to control the access of multiple threads to any shared resource. Java Synchronization is better option where we want to allow only one thread to access the shared resource.

7) What is the difference between hashmap and hash set?

– HashMap stores a key-value pair but HashSet stores objects
– HashMap doesn’t allow duplicate keys and allows duplicate values but HashSet doesn’t allow duplicate values
– HashMap is faster than Hashset because values are associated with a unique key but HashSet is slower than HashMap because the member object is used for calculating hashcode value, which can be same for two objects
– HashMap contains a null key and multiple null values but HashSet contains a single null value
– In HashMap 2 objects are created during put operation, one for key and one for value but in HashSet only 1 object is created for add operation

8) What is the difference between set and linkedlist?

– List is an ordered collection it maintains the insertion order, which means upon displaying the list content it will display the elements in the same order in which they got inserted into the list.
Set is an unordered collection, it doesn’t maintain any order. There are few implementations of Set which maintains the order such as LinkedHashSet (It maintains the elements in insertion order).
– List allows duplicates while Set doesn’t allow duplicate elements. All the elements of a Set should be unique if you try to insert the duplicate element in Set it would replace the existing value.
– List allows any number of null values. Set can have only a single null value at most.

9) What is the difference between arraylist and vector list?

– ArrayList is not synchronized but Vector is synhronized
– ArrayList is fast because it is non-synchroized but Vector is slow because it is synchroized
– ArrayList is not a legacy class but Vector is a legacy class.

10) What is the difference between linkedhash set and hashset?

– HashSet does not maintain insertion order but LinkedHashSet maintains insertion order of objects
– HashSet performance is better than LinkedHashSet because LinkedHashSet maintains LinkedList internally to maintain the insertion order of elements

11) What are the types of assertion and what are assertion in java?

An assertion allows testing the correctness of any assumptions that have been made in the program. Assertion is achieved using the assert statement in Java. While executing assertion, it is believed to be true. If it fails, JVM throws an error named AssertionError. It is mainly used for testing purposes during development.

12) What is the default package in java ?

The Java standard libraries include java.lang package by default, which contains a number of components that are used very commonly in Java programs.

13) What are inner classes ..name them ?

Inner class means one class which is a member of another class. There are basically four types of inner classes in java.
– Nested Inner class
– Method Local inner classes
– Anonymous inner classes
– Static nested classes

14) In public static void main(String arr[])… what if i replace public with private ……….. remove static ……..replace void with string?

If we replace public with private then JVM will be unable to access/locate the main method.
If we remove static than JVM cannot invoke it without instatiating class.
If we replace void with String, it will give compilation error as an unexpected return value.

15) In hash map we have (key and value ) pair , can we store inside a value =(key, value ) again ?

Yes. We can store key/value inside a value.

16) What are variable scope in java (in class , in method , in static block)?

– Each variable declared inside a class with private access modifier but outside of any method, has class scope. As a result, these variables can be used everywhere in the class, but not outside of it.
– When a variable is declared inside a method, it has method scope and it will only be valid inside the same method.
– Variables declared inside a block are accessible only inside of that block.

17) Write a Java program so that when ever you create a object, you get to know how many object u have created?

// Java program Find Out the Number of Objects Created of a Class
class FindCreatedObjects {

static int noOfObjects = 0;
{
noOfObjects += 1;
}
public Test()
{
}
public Test(int n)
{
}
public Test(String s)
{
}

public static void main(String args[])
{
Test t1 = new Test();
Test t2 = new Test(5);
Test t3 = new Test("Automation");

System.out.println(Test.noOfObjects);
}
}

 

18) What is difference between .equals() , (==) and compare-to(); ?

– Double equals operator is used to compare two or more than two objects, If they are referring to the same object then return true, otherwise return false.
– String equals() method compares the two given strings based on the data / content of the string. If all the contents of both the strings are same then it returns true. If all characters are not matched then it returns false.
– The java compare two string is based on the Unicode value of each character in the strings. If two strings are different, then they have different characters at some index that is a valid index for both strings, or their lengths are different, or both.

19) What is the difference between hash code and equals?

– equals: a method provided by java.lang.Object that indicates whether some other object passed as an argument is “equal to” the current instance. Two objects are equal if and only if they are stored in the same memory address.
– hashcode(): a method provided by java.lang.Object that returns an integer representation of the object memory address. By default, this method returns a random integer that is unique for each instance. This integer might change between several executions of the application and won’t stay the same.

20) Do you have any idea on Enumeration?

Enumeration means a list of named constant. In Java, enumeration defines a class type. An Enumeration can have constructors, methods and instance variables. It is created using enum keyword. Each enumeration constant is public, static and final by default. Even though enumeration defines a class type and have constructors, you do not instantiate an enum using new. Enumeration variables are used and declared in much a same way as you do a primitive variable.

21) What is priority queue in collection, what is its use and how you have use in your project?

A PriorityQueue is used when the objects are supposed to be processed based on the priority. The PriorityQueue is based on the priority heap. The elements of the priority queue are ordered according to the natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.

22) What is the difference between MAP & Set ?

– While a Map holds two objects per Entry e.g. a key and a value and It may contain duplicate values but keys are always unique. Set doesn’t allow duplicates.
– Set just allow one null element as there is no duplicate permitted while in Map you can have null values and at most one null key.

23) How to call the super class method in subclass?

If you override a method in a subclass, but still need to call the method defined in the superclass, you can do so using the super reference, like this:
public class Car extends Vehicle {

public void setLicensePlate(String license) {
super.setLicensePlate(license);
}

}

 

24) What is the base class for all java classes? And mention its methods?

The Object class is the parent class of all the classes in java by default. Following are different methods of Object class:
– getClass(): returns the Class class object of this object. The Class class can further be used to get the metadata of this class.
– hasCode(): returns the Class class object of this object. The Class class can further be used to get the metadata of this class.
– equals(Object obj): compares the given object to this object.
– toString(): returns the string representation of this object.

25) What is hashcode method? Explain pragmatically by implementing hashcode method?

The default implementation of hashCode() in Object class returns distinct integers for different objects.

// Java puzzle to illustrate use of hashcode() and equals() method
public class HashCodeExample {
private final String first, last;
public Name(String first, String last)
{
this.first = first;
this.last = last;
}
public boolean equals(Object o)
{
if (!(o instanceof Name))
return false;
Name n = (Name)o;
return n.first.equals(first) && n.last.equals(last);
}
public static void main(String[] args)
{
Set<Name> s = new HashSet<Name>();
s.add(new Name("Bijan", "Patel"));
System.out.println(
s.contains(new Name("Bijan", "Patel")));
}
}

 

Next Steps:

> More interview questions and answers on Java, continue to the next post (Click on Next Post link below)

> Check complete Java interview questions and answers here (Click here)

Please leave your questions/comments/feedback below.

Happy Learning 🙂

About Me > Bijan Patel

Connect to me on Linked In (Click here)

Visit me on my site (Click here)

On a mission to contribute to the Software Testing Community in all possible ways.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp