1) Write a program
to reverse a String
String str="navya";
int strlen = str.length();
int j=0;
Char[] chararray = new Char[strlen];
for(int i = (strlen-1) ; i >0; i--){
chararray[j] =
str.charAt(i);
j=j+1;
if(j == strlen-1)
{
chararray[strlen-1]
= str.charAt(0);
}
System.out.print(chararray);
2)What is a Transaction in Java?
Transaction
is a way to make sure that data is consistent in database. Suppose you
dont want to show the month to date unless year to date gets updated,
otherwise yeartodate will point to a old value which leads to data
inconsistencies.Transaction makes sure that either both the statements
gets executed or none of the statement gets executed.
Transaction is used to group together the statements.
3)How to disable the Auto Commit?
Connection con = DriverManager.getConnection("jdbc.oracle:thin:@localhost :1521:orcl","username","password");
con.setAutoCommit(false);
once
the autocommit is disabled, till you call the commit, no statement is
going to get updated in the database.All the statements are executed as a
single transaction ,once the commit is called.
It
is advisable to use setautocommit(false) in case of transactions
mode.This way you avoid holding the lock for Database till the multiple
statements get executed.
4)Explain the real world scenario where transactions are useful?
Transactions are useful when multiple users are trying to update the database.
For
E.g in a ticket reservation system, one counter is trying to update the
(available ticket - no of ticket sold), while the other counter is
trying to update may see the older value of available ticket and it also
tries to update(available ticket - no of ticket sold), this leads to
data inconsistency.
For e.g in a price tool, when multiple users are trying to update the new price for an item,or a new discount for an item.
This
kind of situations can be avoided by using the transactions, which
provides some level of protections against the data conflicts.
5)What is a lock on the Database row?
To
avoid conflicts in the database during a transaction, Database uses
locks mechanism to prevent other users to access the data which is
accessed by the transaction.Once the lock is set, it remains till the
transaction is committed or rolled back.In auto commit mode, locks are
held for each statement.
6)What is Transaction isolation level?
How the locks are set into the database is determined by transaction isolation level.
usually you do not need to do anything about your Transaction Isolation Level. DBMS has default transaction Isolation Level set. you can make use of default Transaction Isolation Level,
There are 5 Transaction Isolation Levels : These values are in CONNECTION Interface
TRANSACTION_NONE >> Transactions not supported
TRANSACTION_READ_UNCOMMITTED >>dirty phantom and non repeatable reads are allowed.
TRANSACTION_READ_COMMITTED >> dirty reads are prevented.
TRANSACTION_REPEATABLE_READ >>dirty and non-repeatable reads are prevented.
TRANSACTION_SERIALIZABLE>>dirty , phantom, non-repeatable reads are prevented.
JDBC provides method to get the transaction isolation level of the DBMS.using connection.getTransactionIsolation()
7)What are dirty phantom and non-repeatable read?
Dirty Read :
value can be accessed before it is committed and once it is changed by the other user.
Non-Repeatable Read :
A Non-repeatable read occurs when transaction A retrieves a row, and transaction B subsequently updates the new Row, then if the transaction A later retrieves the same row again, it sees a different data.
Phantom Read:
A phantom read occurs when transaction A retrieves a set of rows satisfying a given condition, transaction B subsequently inserts or updates a row such that the row now meets the condition in transaction A, and transaction A later repeats the conditional retrieval. Transaction A now sees an additional row. This row is referred to as a phantom.
8) What are the differences between ArrayList and Linked List?
Array-List is backed by an array and Linked List is backed by a Doubly Linked List .
index based search is possible using an ArrayList whereas you need to traverse through the Linked list to search for a particular element.Array list is best used on set() and get() whereas worst on Add() remove() elements , whereas Linked List is () remove() and worst on get() set(). Linked List implements List and Queue whereas Array List implements only List, so Linkedlist has more methods like pool(),peek() etc. Its good practice to create an arrayist with a higher initial capacity to avoid the re-size cost.
9)Explain singleton and factory pattern.
There are 5 Transaction Isolation Levels : These values are in CONNECTION Interface
TRANSACTION_NONE >> Transactions not supported
TRANSACTION_READ_UNCOMMITTED >>dirty phantom and non repeatable reads are allowed.
TRANSACTION_READ_COMMITTED >> dirty reads are prevented.
TRANSACTION_REPEATABLE_READ >>dirty and non-repeatable reads are prevented.
TRANSACTION_SERIALIZABLE>>dirty , phantom, non-repeatable reads are prevented.
JDBC provides method to get the transaction isolation level of the DBMS.using connection.getTransactionIsolation()
7)What are dirty phantom and non-repeatable read?
Dirty Read :
value can be accessed before it is committed and once it is changed by the other user.
Non-Repeatable Read :
A Non-repeatable read occurs when transaction A retrieves a row, and transaction B subsequently updates the new Row, then if the transaction A later retrieves the same row again, it sees a different data.
Phantom Read:
A phantom read occurs when transaction A retrieves a set of rows satisfying a given condition, transaction B subsequently inserts or updates a row such that the row now meets the condition in transaction A, and transaction A later repeats the conditional retrieval. Transaction A now sees an additional row. This row is referred to as a phantom.
8) What are the differences between ArrayList and Linked List?
Array-List is backed by an array and Linked List is backed by a Doubly Linked List .
index based search is possible using an ArrayList whereas you need to traverse through the Linked list to search for a particular element.Array list is best used on set() and get() whereas worst on Add() remove() elements , whereas Linked List is () remove() and worst on get() set(). Linked List implements List and Queue whereas Array List implements only List, so Linkedlist has more methods like pool(),peek() etc. Its good practice to create an arrayist with a higher initial capacity to avoid the re-size cost.
9)Explain singleton and factory pattern.
Singleton pattern can be achieved in various ways in
java.
·
Eager initialization
·
Lazy initialization
·
Double Checked Locking
Eager
initialization:- the instance is created much before it is required i.e.
instance is created as static variable when the class is loaded all the static
variables are created. So new instance also created. Please ensure that you are
using volatile keyword on your instance variable , otherwise you enter into out
of order write error.(This means, JVM has just allocated the memory for your
instance, but not executed the constructor yet, so it may return null pointer
exception or crash application with out of write error)
public class Singleton
{
private
static
volatile
Singleton
instance = new Singleton();
//
private constructor
private
Singleton()
{
}
public
static
Singleton
getInstance() {
return
instance;
}
}
Lazy
Initialization: So to solve this problem, just declare the instance, and if
the instance is null then only create a new instance. On the first call it
checks if the instance is null, if the instance is null, then a new instance is
created. if it is not null, the same instance is returned.
public
final class Singleton
{
private static volatile Singleton
instance = null;
// private constructor
private Singleton()
{
}
public
static Singleton
getInstance() {
if (instance
== null){
synchronized (Singleton.class) {
instance = new Singleton();
}
}
return instance;
}
}
but there is a problem with
this method, two threads T1 and T2 comes to the block (instance== null) and
both can create the instance of the Singleton class, so u need to have double
checked locking.
public final class Singleton
{
private static volatile Singleton
instance = null;
// private
constructor
private Singleton()
{
}
public static Singleton
getInstance() {
if
(instance == null){
synchronized (Singleton.class)
{
// Double check
if(instance == null){
instance = new Singleton();
}
}
}
return
instance;
}
}
This is the Correct
implementation of Double checked locking.
Factory pattern: We usually use "new" to create an instance in java. but that is old fashioned now, especially when you have lot of initializations to do or if you want to change the method of creating an object in various places of application, you need to go to different places of application to make the code changes.In Such scenarios we can use factory pattern.Factory Pattern provides loose Coupling between the classes which is important principal while designing the architecture.
E.g Spring Dependency Injection.
10) What is the javascript library you have used?
To use a JavaScript framework library in your web pages, just include the library in a <script> tag:
Including jQuery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
11)How would you programatically get unique list of primarykeys, (E.g you have a list of primarykeys with duplicates in it from a webservice) and save the unique records to database?
Use HashSet
12)What is agile methodology?
Factory pattern: We usually use "new" to create an instance in java. but that is old fashioned now, especially when you have lot of initializations to do or if you want to change the method of creating an object in various places of application, you need to go to different places of application to make the code changes.In Such scenarios we can use factory pattern.Factory Pattern provides loose Coupling between the classes which is important principal while designing the architecture.
E.g Spring Dependency Injection.
10) What is the javascript library you have used?
To use a JavaScript framework library in your web pages, just include the library in a <script> tag:
Including jQuery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
11)How would you programatically get unique list of primarykeys, (E.g you have a list of primarykeys with duplicates in it from a webservice) and save the unique records to database?
Use HashSet
12)What is agile methodology?
- Rapid delivery of the software.
- Welcome changing requirements even in late development
- Working software is delivered in weeks
- Daily cooperation between business and developers
- face to face conversations
- technical excellence and good design
- regular adoption to changing circumstances.
No comments:
Post a Comment