Monday, March 12, 2018

Java interview question for experienced at GS Lab.

Q1) How multi threading happens in JAVA.

Q2) How to avoid in imutable class not to create a new instance if mutable object setter is called within?
Ans) Hint - Use clone of object within the constructor

Q3) How can we avoid performance hit if used synchronize in Singleton class.
Ans ) Hint - use Enum instead of singleton instance creation.

Please provide you answers in comment secssion.

Tuesday, February 27, 2018

Java Interview questions by DataMatic

Java Interview questions by DataMatic  -

1) Big file (.CSV) file of 3 GB. How will you process it and put in DB?
Ans) Use of MappedByteBuffer 

2) What is Callable interface in JAVA (Threads)?

3) Working of CuncerentHasMap?

Your comments are welcomed in comments secession.


Monday, February 5, 2018

Java harbinger company Pune interview questions on architecture role

Q1. Describe IOC and DI aligned with SOLID design principle.

Q2. Can we implement Polymorphism in RDBMS.

Q3. What is ORM.

Answers are welcomed in comment secession.

Thursday, June 26, 2014

What is MyBatis?




MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results.
web.xml




sql-map-config.xml

For Spring Mybatis, there are 3 ways to retrieve the sessions
A. extend SqlSessionDaoSupport
Step 1. Create SqlSessionFactory bean in config file
Step 2. Extend your dao class with SqlSessionDAOSupport and use getSQLSession() which will return SqlSessionTemplate
B. Use SqlSessionTemplate
Step 1. Create template bean



Step 2. user getSqlSession() in dao
C. Using MapperFactoryBean
Step 1. Create DAO interfaces (CustomerMapperInterface.java)
Step 2. Create base mapper object (BaseMapperInterface is a marker interface.)




Step 3. Define object specific mapper



Step 4. Define CustomerMapper.xml

Your queries

Step 5. Use mapper in service implementation class
@Service
public class CustomerService implements CustomerBaseService{
@Autowired
CustomerMapperInterface custMapper;
@Override
public Customer getCustomerById(long empId){
//retrieve from database
Customer cust = custMapper.getCustomerWithId(custId);
return cust;
}
}
ResultMap
Describes how to load your objects from the database result sets.



typeHandler="com.aci.data.dao.mybatis.util.StringToBooleanTypeHandler" />




javaType="com.aci.model.Address">





javaType="com.aci.model.Address" resultMap="AddressSQL.addressMap" />
javaType="java.util.List" ofType="com.aci.data.Accounts" select="selectAccounts" />







Select/ Insert/ Update/ Delete statements syntax
Select Statement

Insert Statements
„h If your database supports auto-generated key fields (e.g. MySQL and SQL Server), then you can simply set useGeneratedKeys="true" and set the keyProperty to the target property

insert into Author (username,password,email,bio,isActive) values
(#{username},#{password},#{email},#{bio}, #{isActive, jdbcType=VARCHAR, typeHandler=com.wm.ping.data.dao.mybatis.util.StringToBooleanTypeHandler})

„h MyBatis has another way to deal with key generation for databases that don't support auto-generated column types,
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
keyProperty=""
keyColumn=""
useGeneratedKeys="false"
timeout="20000">

SELECT Author_SEQ.NEXTVAL FROM DUAL

your query

Update Statements
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20000"> your query

Delete Statements
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20000"> your query

Reusable SQL fragment
id,username,password

select from some_table where id = #{id}

Type Aliases



TypeHandler
public class StringToBooleanTypeHandler implements TypeHandler {
override getResult for ResultSet
override getResult for Callable statements
override setParameter for prepared statements
}
ResultHandlers
Allows you to handle each row as per your requirement.
Extend your class with ResultHandler and override method handleResult(ResultContext rc)
Cache
By default no caching except local session caching.
To enable 2nd level cache add following :

(Here I have added default values for properties, you can change it)
You can also implement custom cache by implementing Cache Interface given by mybatis
Dynamic SQL
¡E if statement

Add field / add where clause etc

¡E choose (when, otherwise)
For multiple if else conditions
¡E trim (where, set)
You can use with to dynamically add conditions

¡K

Also can use with to dynamically include columns to update statement


SELECT id, title, author FROM article WHERE id_category IN

#{category}

Thursday, January 16, 2014

How to sort collection on multiple attributes



 


Q) How to sort collection on multiple attributes?
A) For classes that don’t implement
comparable interface, or when one needs even more control over ordering based on multiple attributes, a
Comparator interface should be used.


Example:

public class graduationCeremony {
    String campus;
    String faculty;
    String building;
}

public class GraduationCeremonyComparator implements Comparator<graduationCeremony> {
    public int compare(graduationCeremony o1, graduationCeremony o2) {
        int value1 = o1.campus.compareTo(o2.campus);
        if (value1 == 0) {
            int value2 = o1.faculty.compareTo(o2.faculty);
            if (value2 == 0) {
                return o1.building.compareTo(o2.building);
            } else {
                return value2;
        }
        return value1;
    }
}


Wednesday, January 15, 2014

Tell me about Java 5 changes

Q) Tell me about Java 5 Var args changes.


A)

In C we can call printf() method with multiple arguments.
printf("%s", 50);
printf("%d %s %s", 250, "Hello", "World");
Varargs was added in Java 5 and the syntax includes three dots  (also called ellipses). Following is the syntax of vararg method.
public void testVar(int count, String... vargs) { }
Notice the dots … in above code. That mark the last argument of the method as variable argument. Also the vararg must be the last argument in the method.
Now let us check simple hello world varargs code.
public class HelloWorldVarargs {
 
    public static void main(String args[]) {
        test(215, "India", "Delhi");
        test(147, "United States", "New York", "California");
    }
 
    public static void test(int some, String... args) {
        System.out.print("\n" + some);
        for(String arg: args) {
            System.out.print(", " + arg);
        }
    }
}


Q) Enhanched for loop?
A) The usual way to step through all the elements of an array in order is with a "standard" for loop, for example,
for (int i = 0; i < myArray.length; i++) {
    System.out.println(myArray[i]);
}
The so-called enhanced for loop is a simpler way to do this same thing. (The colon in the syntax can be read as "in.")
for (int myValue : myArray) {
    System.out.println(myValue);
}
The enhanced for loop was introduced in Java 5 as a simpler way to iterate through all the elements of a Collection (Collections are not covered in these pages). It can also be used for arrays, as in the above example, but this is not the original purpose.
Enhanced for loops are simple but inflexible. They can be used when you wish to step through the elements of the array in first-to-last order, and you do not need to know the index of the current element. In all other cases, the "standard" for loop should be preferred.
Two additional statement types, break and continue, can also control the behavior of enhanced for loops.

Q) Static import?
A) Static imports concurrency utilities in package java.util.concurrent.