Java SDET Interview questions and Answers




Difference between abstraction and interface.

1 Abstract class 

--------------

1)An abstract class can have method body (non-abstract methods).

2)An abstract class can have instance variables.

3)An abstract class can have constructor

4)An abstract class can have static methods. 

5)You can extends one abstract class.


Interface

---------

1)Interface have only abstract methods.

2)An interface cannot have instance variables.

3)Interface cannot have constructor.

4)Interface cannot have static methods.

5)You can implement multiple interfaces



What is Encapsulation?


Encapsulation-
-encapsulation is java process of wrapping code and data into a single unit.
-we can create a fully encapsulated class in java by making all the data members of the class private.
-we can use setter and getter methods to set and get the data in encapsulation
-By providing only setter and getter method, we can make the class read only and write only.
-encapsulation provides us control over the data.

What do you understand by OOPS Concept


Oops concept-

1.Inheritance-Inheritance in java is mechanism in which child class acquires all properties of parent class. Here properties are variables, functions, etc. Inheritance provides code reusability. When we inherit properties from existing class, we can reuse methods and fields of parent class and we can add methods and fields also. The class which inherits the properties of other class is called as child class/derived class. The class whose properties are inherited is called as parent class/base class/super class.

2.Polymorphism- When one task is performed by different ways is called as polymorphism. Polymorphism in java is a concept by which we can perform single action in different ways.
Types-
1.Method overloading
2.Method overriding


3. Encapsulation-
-encapsulation is java process of wrapping code and data into a single unit.
-we can create a fully encapsulated class in java by making all the data members of the class private.
-we can use setter and getter methods to set and get the data in encapsulation
-By providing only setter and getter method, we can make the class read only and write only.
-encapsulation provides us control over the data.


4.Abstraction- is a process of hiding the implementation details and showing only functionality to user. It shows only important things to user and hides the internal details. E.g. sending sms, we just type the test and send the message. We don’t know the internal processing of message delivery. 

What is Hash Map ? Can we store objects in hash map and how to retrieve them?

 HashMap is a hash table based implementation of Java's Map interface. A map is an object that maps keys to values. A map cannot contain duplicate keys

--> In Java HashMap object provides key/value pairs for storing information in memory. You can store any data type combination as long as the keys are unique.

--> When you want to retrieve the object, you call the get() method and again pass the key object.


Explain overriding and method over loading .

Method overloading:
=================
--> Overloading deals with multiple methods in the same class with the same name but different signatures 
--> Overloading lets you define a similar operation in different ways for different data.

Method overriding:
================
--> Overriding deals with two methods, One in the parent class and one in a child class that have the same signature
--> Overriding lets you define a similar operation in different ways for different object types.

Explain overriding and method over loading .

Constructor :
===========
Constructor has same name as the class name
Constructor is used to initilize the data members and start up tasks
Constructor is automatically called when an object is created 
There is no return data type in constructor 
There is always a default constructor provided by compiler

Interface:
=========
Interface contains only abstract methods
Access modifiers for methods in interfaces must be public
Variables defined must be public,static,final
To implement an interface, we use implements keyword


Explain Set and Map in Java.

Set:
====
A set is a collection that cannot contain duplicate elements. How ever it makes no guarantees concerning the order of iteration.
Below are the classes Implement set interface
1. Hashset
2. LinkedHashSet
3. TreeSet

Implementation of Set interface:
============================
public class Hashset
{
 public static void main(String args[])
{
 HashSet<String> hs = new HashSet();
 hs.add(""Deepthi"");
 hs.add(""Keerthi"");
 hs.add(""Spoorthy"");
s.o.pn(hs);
}
}
Methods in iterator interface:
========================
hasNext()
next()
remove()

Iterator interface is used to traverse each element present in the set interface.

iterator<String> it = hs.iterator();
while(it.hasNext())
{
s.o.pln((it.next());   //Prints all elements present in the set
}

Map:
=====
A map is an object that maps keys to values. A map cannot contain duplicate keys.
Below are the main implementations of Map interfaces
1. HashMap
2. LinkedHashedMap
3. TreeMap

Implementation of Map Interface:
===============================
public class HashMap
{
 public static void main(String args[])
{
 HashMap<Integer, String> hm = new HashMap(Integer,String);
 hm.put(0, ""Deepthi"");
 hm.put(1, ""Keerthi"");
 hm.put(2,""Spoorthy"");
s.o.pn(hm);
Set s = hm.entryset();    // Storing in set interface
Iterator i = s.iterator();
while(i.next())
{
    Map.Entry m=(Map.Entry);
    i.next();
   s.o.pln(m.getKey());
   s.o.pln(m.getValue());
}
}


What is static and final in java?

static and final are the keywords used in java

The main difference between a static and final keyword is that static is keyword is used to define the class member that can be used independently of any object of that class. Final keyword is used to declare, a constant variable, a method which can not be overridden and a class that can not be inherited.

memory is allocated only once for static variable or method . It is a class variable or method.
final -> when applied to a variable or method or class, its value cannot be changed. It will be constant

How will you prevent the override method in java

In java, we know that child class can override the parent class methods. so in order to prevent the overriding in java , we need to make parent class methods as static or final. why because static and final methods can not be override.

Explain about collections.

it is a framework of classes and interfaces to make data manipulation easy


Post a Comment