0% found this document useful (0 votes)
36 views

Core Java and Selenium Int Qs and As

The document discusses various Java concepts: 1. It defines immutable classes as classes whose contents cannot be changed once created, with String being an example. 2. It explains that String is immutable in Java to prevent issues from shared cached String objects being modified. 3. It describes assertions as statements that test assumptions, and can throw errors if the assertion is false. 4. It notes the default packages in Java include the unnamed package, java.lang, and the current package.

Uploaded by

san
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Core Java and Selenium Int Qs and As

The document discusses various Java concepts: 1. It defines immutable classes as classes whose contents cannot be changed once created, with String being an example. 2. It explains that String is immutable in Java to prevent issues from shared cached String objects being modified. 3. It describes assertions as statements that test assumptions, and can throw errors if the assertion is false. 4. It notes the default packages in Java include the unnamed package, java.lang, and the current package.

Uploaded by

san
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Core Java and Selenium Int Qs and As

1. What is an immutable class?


A: Immutable class is a class which once created, its contents cannot be
changed. Immutable
Objects are the objects whose state cannot be changed once constructed.
e.g. String class
2. Why String is Immutable or Final in Java
A: String is Immutable in Java because String objects are cached in String pool.
Since cached String literal is shared between multiple clients there is always a
risk, where one client's action would affect all other client. For example, if one
client changes value of String "Test" to "TEST", all other client will also see that
value as explained in first example. Since caching of String objects was
important from performance reason this risk was avoided by making String class
Immutable.
3. What are the types of assertion and what are assertion in java
A: An assertion is a statement in the JavaTM programming language that enables
you to test youre
assumptions about your program. For example, if you write a method that
calculates the speed of a
particle, you might assert that the calculated speed is less than the speed of
light.
Each assertion contains a Boolean expression that you believe will be true when
the assertion
executes. If it is not true, the system will throw an error. By verifying that the
Boolean expression is
indeed true, the assertion confirms your assumptions about the behavior of your
program,
increasing your confidence that the program is free of errors.
4. What is the default package in java

www.qascripts.com

Page 1

Core Java and Selenium Int Qs and As


A: There are three packages are imported by default for each source file. First,
the package with no name. Second, the java.lang package. And third, the current
package (the package in which the current file is defined).
5. What is Singleton class
A: In object-oriented programming , a singleton class is a class that can have
only one object(an instance of the class) at a time.
6. Difference between singleton and static class
A: 1. Singleton object stores in Heap but, static object stores in stack
2. We can clone the object of Singleton but, we cannot clone the static class
object
3. Singleton class follow the OOP (object oriented principles) but not static
class
4. We can implement interface with Singleton class but not with Static class.
7. What is difference between .equals() , (==)
A: Both equals() and "==" operator in Java is used to compare objects to check
equality but main difference between equals method and == operator is that
former is method and later is operator. Since Java doesnt support operator
overloading, == behaves identical for every object but equals() is method, which
can be overridden in Java and logic to compare objects can be changed based
upon business rules. Another notable difference between == and equals method
is that former is used to compare both primitive and objects while later is only
used for objects comparison.
8. CompareTo() vs. equals()
A: 1. Equals will take any Object as a parameter, but compareTo will only take
Strings.
2. Equals only tells you whether they're equal or not, but compareTo gives
information on how the Strings compare lexicographically.
9. What is Synchronization?
A: Synchronization in java is the capability of control the access of multiple
threads to any shared
resource.

www.qascripts.com

Page 2

Core Java and Selenium Int Qs and As

There are two types of synchronization


1. Process Synchronization
2. Thread Synchronization
10.When to use HashTable and HashMap
The basic difference between a Hashtable and an HashMap is that, Hashtable is
synchronized while HashMap is not. Thus whenever there is a possibility of
multiple threads accessing the same instance, one should use Hashtable. While
if not multiple threads are going to access the same instance then use HashMap.

Difference between HashTable and HashMap

One of the major differences between HashMap and Hashtable is that


HashMap is non-synchronized whereas Hashtable is synchronized, which
means Hashtable is thread-safe and can be shared between multiple
threads but HashMap cannot be shared between multiple threads without
proper synchronization. Java 5 introduced ConcurrentHashMap which is an
alternative of Hashtable and provides better scalability than Hashtable in
Java.Synchronized means only one thread can modify a hash table at one
point of time. Basically, it means that any thread before performing an
update on a hashtable will have to acquire a lock on the object while
others will wait for lock to be released.

The HashMap class is roughly equivalent to Hashtable, except that it


permits nulls. (HashMap allows null values as key and value whereas
Hashtable doesnt allow nulls).

One more notable difference between Hashtable and HashMap is that


because of thread-safety and synchronization Hashtable is much slower
than HashMap if used in Single threaded environment. So if you dont
need synchronization and HashMap is only used by one thread, it
outperform Hashtable in Java.

www.qascripts.com

Page 3

Core Java and Selenium Int Qs and As

HashMap does not guarantee that the order of the map will remain
constant over time

11.Main Difference Between Static And Non Static Methods In Java


A: We can call static methods directly while we cannot call non static methods
directly. You need to create and instantiate an object of class for calling non
static methods. VIEW THIS POST to learn about object In Java.
Non static stuff (methods, variables) cannot be accessible Inside static methods
Means we can access only static stuff Inside static methods. Opposite to It, Non
static method do not have any such restrictions. We can access static and nonstatic both kind of stuffs inside non static methods.
Static method is associated with the class while non-static method Is associated
with an object.

www.qascripts.com

Page 4

You might also like