Saturday, December 21, 2013

java interview questions and answwers.

java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.


Java Interview Questons and  Answers

What is the output for the below code ?
1. public class A {
2. int add(int i, int j){
3. return i+j;
4. }
5.}
6.public class B extends A{
7. public static void main(String argv[]){
8. short s = 9;
9. System.out.println(add(s,6));
10. }
11.}
Options are
A.Compile fail due to error on line no 2
B.Compile fail due to error on line no 9
C.Compile fail due to error on line no 8
D.15
Answer :
B is the correct answer.
Cannot make a static reference to the non-static method add(int, int) from the type A. The
short s is autoboxed correctly, but the add() method cannot be invoked from a static
method because add() method is not static

What is the output for the below code ?
public class A {
int k;
boolean istrue;
static int p;
public void printValue() {
System.out.print(k);
System.out.print(istrue);
System.out.print(p);
}
}
public class Test{
public static void main(String argv[]){
A a = new A();
a.printValue();
}
}
Options are
A.0 false 0
B.0 true 0
C.0 0 0
D.Compile error - static variable must be initialized before use.


Answer :
A is the correct answer.
Global and static variable need not be initialized before use. Default value of global and
static int variable is zero. Default value of boolean variable is false. Remember local
variable must be initialized before u
  

What is the output for the below code ?
public class Test{
int _$;
int $7;
int do;
public static void main(String argv[]){
Test test = new Test();
test.$7=7;
test.do=9;
System.out.println(test.$7);
System.out.println(test.do);
System.out.println(test._$);
}
}
Options are
A.7 9 0
B.7 0 0
C.Compile error - $7 is not valid identifier.
D.Compile error - do is not valid identifier.
Answer :
D is the correct answer.
$7 is valid identifier. Identifiers must start with a letter, a currency character ($), or
underscore ( _ ). Identifiers cannot start with a number. You can't use a Java keyword as
an identifier. do is a Java keyword.
What is the output for the below code ?
package com;
class Animal {
public void printName(){
System.out.println("Animal");
}
}
package exam;
import com.Animal;
public class Cat extends Animal {
public void printName(){
System.out.println("Cat");
}
}
package exam;
import com.Animal;
public class Test {
public static void main(String[] args){
Animal a = new Cat();
a.printName();
}
}
Options are
A.Animal
B.Cat
C.Animal Cat
D.Compile Error
Answer :
D is the correct answer.
Cat class won't compile because its superclass, Animal, has default access and is in a
different package. Only public superclass can be accessible for different package.

What is the output for the below code ?
public class A {
int i = 10;
public void printValue() {
System.out.println("Value-A");
};
}
public class B extends A{
int i = 12;
public void printValue() {
System.out.print("Value-B");
}
}
public class Test{
public static void main(String argv[]){
A a = new B();
a.printValue();
System.out.println(a.i);
}
}
Options are
A.Value-B 11
B.Value-B 10
C.Value-A 10
D.Value-A 11
Answer :
B is the correct answer.
If you create object of subclass with reference of super class like ( A a = new B();) then
subclass method and super class variable will be executed.
What is the output for the below code ?
public enum Test {
BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);
private int hh;
private int mm;
Test(int hh, int mm) {
assert (hh >= 0 && hh <= 23) : "Illegal hour.";
assert (mm >= 0 && mm <= 59) : "Illegal mins.";
this.hh = hh;
this.mm = mm;
}
public int getHour() {
return hh;
}
public int getMins() {
return mm;
}
public static void main(String args[]){
Test t = new BREAKFAST;
System.out.println(t.getHour() +":"+t.getMins());
}
}
Options are
A.7:30
B.Compile Error - an enum cannot be instantiated using the new operator.
C.12:30
D.19:45
Answer :
B is the correct answer.
As an enum cannot be instantiated using the new operator, the constructors cannot be
called explicitly. You have to do like Test t = BREAKFAST;

What is the output for the below code ?
public class A {
static{System.out.println("static");}
{ System.out.println("block");}
public A(){
System.out.println("A");
}
public static void main(String[] args){
A a = new A();
}
Options are
A.A block static
B.static block A
C.static A
D.A
Answer :
B is the correct answer.
First execute static block, then statement block then constructor.

What is the output for the below code ?
1. public class Test {
2. public static void main(String[] args){
3. int i = 010;
4. int j = 07;
5. System.out.println(i);
6. System.out.println(j);
7. }
8. }
Options are
A.8 7
B.10 7
C.Compilation fails with an error at line 3
D.Compilation fails with an error at line 5
Answer :
A is the correct answer.
By placing a zero in front of the number is an integer in octal form. 010 is in octal form
so its value is 8.
What is the output for the below code ?
1. public class Test {
2. public static void main(String[] args){
3. byte b = 6;
4. b+=8;
5. System.out.println(b);
6. b = b+7;
7. System.out.println(b);
8. }
9. }
Options are
A.14 21
B.14 13
C.Compilation fails with an error at line 6
D.Compilation fails with an error at line 4
Answer :
C is the correct answer.
int or smaller expressions always resulting in an int. So compiler complain about Type
mismatch: cannot convert from int to byte for b = b+7; But b += 7; // No problem
because +=, -=, *=, and /= will all put in an implicit cast. b += 7 is same as b = (byte)b+7
so compiler not complain.
What is the output for the below code ?
public class Test {
public static void main(String[] args){
String value = "abc";
changeValue(value);
System.out.println(value);
}
public static void changeValue(String a){
a = "xyz";
}
}
Options are
A.abc
B.xyz
C.Compilation fails
D.Compilation clean but no output
Answer :
A is the correct answer.
Java pass reference as value. passing the object reference, and not the actual object itself.
Simply reassigning to the parameter used to pass the value into the method will do
nothing, because the parameter is essentially a local variable.

Monday, December 16, 2013

Spring Interview Questions and Answers


Spring Interview Questions and Answers.Spr ing Interview Questions and Answers Spring Interview Questions and Answers.Spring Interview Questions and Answers.Spring Interview Questions and Answers.Spring Interview Questions and Answers3.0.Spring Interview Questions and Answers.Spring Interview Questions and Answers.Spring Interview Questions and Answers.Spring Interview Questions and Answers.



1.  What is IOC (or Dependency Injection)?
The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up Applying IoC, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. That is, dependencies are injected into objects. So, IoC means an inversion of responsibility with regard to how an object obtains references to collaborating objects. 

2. What are the different types of IOC (dependency injection) ? 

There are three types of dependency injection:
  • Constructor Injection (e.g. Pico container, Spring etc): Dependencies are provided as constructor parameters.
  • Setter Injection (e.g. Spring): Dependencies are assigned through JavaBeans properties (ex: setter methods).
  • Interface Injection (e.g. Avalon): Injection is done through an interface.
Note: Spring supports only Constructor and Setter Injection

3. What are the benefits of IOC (Dependency Injection)?

Benefits of IOC (Dependency Injection) are as follows:
  • Minimizes the amount of code in your application. With IOC containers you do not care about how services are created and how you get references to the ones you need. You can also easily add additional services by adding a new constructor or a setter method with little or no extra configuration.
  • Make your application more testable by not requiring any singletons or JNDI lookup mechanisms in your unit test cases. IOC containers make unit testing and switching implementations very easy by manually allowing you to inject your own objects into the object under test.
  • Loose coupling is promoted with minimal effort and least intrusive mechanism. The factory design pattern is more intrusive because components or services need to be requested explicitly whereas in IOC the dependency is injected into requesting piece of code. Also some containers promote the design to interfaces not to implementations design concept by encouraging managed objects to implement a well-defined service interface of your own.
  • IOC containers support eager instantiation and lazy loading of services. Containers also provide support for instantiation of managed objects, cyclical dependencies, life cycles management, and dependency resolution between managed objects etc.

4.What is Spring ?
Spring is an open source framework created to address the complexity of enterprise application development. One of the chief advantages of the Spring framework is its layered architecture, which allows you to be selective about which of its components you use while also providing a cohesive framework for J2EE application development.



5. What are the advantages of Spring framework?
The advantages of Spring are as follows:
  • Spring has layered architecture. Use what you need and leave you don't need now.
  • Spring Enables POJO Programming. There is no behind the scene magic here. POJO programming enables continuous integration and testability.
  • Dependency Injection and Inversion of Control Simplifies JDBC
  • Open source and no vendor lock-in.
6. What are features of Spring ?
  • Lightweight:
spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 1MB. And the processing overhead is also very negligible.
  • Inversion of control (IOC):
Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.

  • Aspect oriented (AOP):
Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.
  • Container:
Spring contains and manages the life cycle and configuration of application objects.
  • MVC Framework:
Spring comes with MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But other frameworks can be easily used instead of Spring MVC Framework.
  • Transaction Management:
Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Spring's transaction support is not tied to J2EE environments and it can be also used in container less environments.




  • JDBC Exception Handling:
The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy. Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with Hibernate, JDO and iBATIS.




8. What are the types of Dependency Injection Spring supports?>
  • Setter Injection:
Setter-based DI is realized by calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.

  • Constructor Injection:
Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.


9. What is Bean Factory ?
A BeanFactory is like a factory class that contains a collection of beans. The BeanFactory holds Bean Definitions of multiple beans within itself and then instantiates the bean whenever asked for by clients.
  • BeanFactory is able to create associations between collaborating objects as they are instantiated. This removes the burden of configuration from bean itself and the beans client.
  • BeanFactory also takes part in the life cycle of a bean, making calls to custom initialization and destruction methods.






Thursday, December 12, 2013

Basic Interview questions on java


java basic interview questions and answers,java basic interview questions and answers,java basic interview questions and answers pdf,java basic interview questions and answers for freshers,java basic interview questions and answers for freshers pdf,java basic interview questions and answers for experienced,java technical interview questions and answers,java core interview questions and answers,java technical interview questions and answers pdf,java technical interview questions and answers for freshers,java technical interview questions and answers for experienced













JAVA INTERVIEW QUESTIONS AND ANSWERS




   1.The Java interpreter is used for the execution of the source code?.
                  a.True   b.False

Ans: TRUE
.
2) On successful compilation a file with the class extension is created.?

a) True
b) False

Ans:.True
3) The Java source code can be created in a Notepad editor?.
a) True
b) False

Ans: True.
4) The Java Program is enclosed in a class definition?.
a) True
b) False

Ans: True

5) What declarations are required for every Java application?

Ans: A class and the main( ) method declarations.

6) What are the two parts in executing a Java program and their purposes?

Ans: Two parts in executing a Java program are:

Java Compiler and Java Interpreter.

The Java Compiler is used for compilation and the Java Interpreter is used for execution of the application.

7) What are the three OOPs principles and define them?

Ans : Encapsulation, Inheritance and Polymorphism are the three OOPs
Principles.
Encapsulation:
Is the Mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.

Inheritance:
Is the process by which one object acquires the properties of another object.

Polymorphism:
Is a feature that allows one interface to be used for a general class of actions.



8) What is a compilation unit?

Ans : Java source code file.

9) What output is displayed as the result of executing the following statement?

System.out.println("// Looks like a comment.");

// Looks like a comment

The statement results in a compilation error
Looks like a comment
No output is displayed
Ans : Looks like a comment

10) In order for a source code file, containing the public class Test, to successfully compile, which of the following must be true?

a.It must have a package statement
b.It must be named Test.java
c.It must import java.lang
d.It must declare a public class named Test

Ans : It must be named Test.java

11) What are identifiers and what is naming convention?

Ans : Identifiers are used for class names, method names and variable names. An identifier may be any descriptive sequence of upper case & lower case letters,numbers or underscore or dollar sign and must not begin with numbers.

12) What is the return type of program’s main( ) method?

Ans : void

13) What is the argument type of program’s main( )
 method?

Ans : string array.

14) Which characters are as first characters of an identifier?

Ans : A – Z, a – z, _ ,$

15) What are different comments?
   
Ans::   Java contain three types of comments, namely


      1.SingleLine comments.

      2.Multiline comments.
     
      3.Javadocs






       1)// -- single line comment.
            

2) /* --
*/ multiple line comment .

3) /** --
*/ documentation.

16) What is the difference between constructor
method and method?


Ans : Constructor will be automatically invoked when an object is created.
Whereas method has to be call explicitly.

17) What is the use of bin and lib in JDK?

Ans : Bin contains all tools such as javac, applet viewer, awt tool etc.,

whereas Lib
contains all packages and variables

Wednesday, December 11, 2013

jdbc interview questions and answers

jdbc interview questions and answers,jdbc interview questions and answers,jdbc interview questions and answers for experienced,jdbc interview questions and answers pdf,jdbc interview questions and answers for freshers,jdbc interview questions and answers pdf download,jdbc interview questions and answers pdf free downloadjdbc interview questions and answers.jdbc interview questions and answers. jdbc interview questions and answers.jdbc interview questions and answers.jdbc interview questions and answers. jdbc interview questions and answers. jdbc interview questions and answers.jdbc interview questions and answers.jdbc interview questions and answers.jdbc interview questions and answers.jdbc interview questions and answers.

JDBC Interview Questions and Answers

 Is the JDBC-ODBC Bridge multi-threaded?
A. No. The JDBC-ODBC Bridge does not support multi threading. The JDBC-ODBC Bridge uses
synchronized methods to serialize all of the calls that it makes to ODBC. Multi-threaded Java programs may
use the Bridge, but they won't get the advantages of multi-threading

 What is the fastest type of JDBC driver?
A.JDBC Net pure Java Driver is the fastest JDBC driver. JDBC-ODBC Bridge Driver and Network
protocol Driver will be slower than Native API Partly Java Driver (the database calls are make at least three
translations versus two), and JDBC Net pure Java Driver, drivers are the fastest (only one translation)

What are the different JDB drivers available?

A.There are mainly four type of JDBC drivers available. They are:
Type 1 : JDBC-ODBC Bridge Driver - A JDBC-ODBC bridge provides JDBC API access via one or more ODBC
drivers. Note that some ODBC native code and in many cases native database client code must be loaded on
each client machine that uses this type of driver. Hence, this kind of driver is generally most appropriate when
automatic installation and downloading of a Java technology application is not important. For information on the
JDBC-ODBC bridge driver provided by Sun.

Type 2: Native API Partly Java Driver- A native-API partly Java technology-enabled driver converts JDBC calls
into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver,
this style of driver requires that some binary code be loaded on each client machine.

Type 3: Network protocol Driver- A net-protocol fully Java technology-enabled driver translates JDBC API calls
into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server. This net server
middleware is able to connect all of its Java technology-based clients to many different databases. The specific
protocol used depends on the vendor. In general, this is the most flexible JDBC API alternative. It is likely that
all vendors of this solution will provide products suitable for Intranet use. In order for these products to also
support Internet access they must handle the additional requirements for security, access through firewalls,
etc., that the Web imposes. Several vendors are adding JDBC technology-based drivers to their existing
database middleware products.

Type 4: JDBC Net pure Java Driver - A native-protocol fully Java technology-enabled driver converts JDBC
technology calls into the network protocol used by DBMSs directly. This allows a direct call from the client
machine to the DBMS server and is a practical solution for Intranet access. Since many of these protocols are
proprietary the database vendors themselves will be the primary source for this style of driver. Several
database vendors have these in progress.
 What is a ResultSet ?
A. A table of data representing a database result set, which is usually generated by executing a
statement that queries the database.
A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before
the first row. The next method moves the cursor to the next row, and because it returns false when there are no
more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.
 What are the steps required to execute a query in JDBC?
A.. First we need to create an instance of a JDBC driver or load JDBC drivers, then we need to register
this driver with DriverManager class. Then we can open a connection. By using this connection , we can create a statement object and this object will help us to execute the query.

 What is JDBC Driver ?
A. The JDBC Driver provides vendor-specific implementations of the abstract classes provided by the
JDBC API. This driver is used to connect to the database.

 What is JDBC?
A.JDBC technology is an API (included in both J2SE and J2EE releases) that provides cross-DBMS
connectivity to a wide range of SQL databases and access to other tabular data sources, such as spreadsheets
or flat files. With a JDBC technology-enabled driver, you can connect all corporate data even in a
heterogeneous environment .

 What are the steps in the JDBC connection?
A: While making a JDBC connection we go through the following steps :
Step 1 : Register the database driver by using :
Class.forName(\" driver classs for that specific database\" );

Step 2 : Now create a database connection using :
Connection con = DriverManager.getConnection(url,username,password);

Step 3: Now Create a query using :




Statement stmt = Connection.Statement("select * from TABLE NAME");
Step 4 : Exceute the query :
stmt.exceuteUpdate(); .





Saturday, December 7, 2013

Java Interview Questions and Answers


java interview questions and answer,java interview questions and answers,java interview questions and answers for experienced,java interview questions and answers pdf,java interview questions and answers for beginners,java interview questions and answers for experienced pdf,java interview questions and answers for qa,java interview questions and answers for 6 years experienced,java interview questions and answers for testers,java interview questions and answers pdf free download,java interview questions and answers book.




How to inject collections in spring IOC
Step1:we can take one pojoclass, then mentioned all values then provide setter and getter methods.
Step2::next pojoclass initialized in spring-config.xml.
Step3::finally take main class then get values.

Lets take small example::
package balu.javainterviewquestionsguru.blogspot.in;
import  java .util. *;
public class JavaCollections
{
 List addreslist;
 Set addresset;
 Map addresmap;
 Properties addprops;
//provide setter and getter methods
Public void  setAddreslist(addlist){
this.addlist=list;
}
Public list getAddreslist(){
System.out.println(“list Elements:”+ addlist’);

return addlist;
}
// a setter method to set setvalues and get values
Public void  setAddresSet(addset){
this.addSet=set;
}
Public Set getAddresSet(){
System.out.println(“set Elements:”+ addset’);

return addset;
}

/ a setter method to set mapvalues and get values
Public void  setAddresMap(addMap){
this.addMap=map;
}
Public Map getAddresMap(){
System.out.println(“map Elements:”+ addmap’);

return addset;
}


/ a setter method to set mapvalues and get values
Public void  setAddresMap(addMap){
this.addMap=map;
}
Public Map getAddresMap(){
System.out.println(“map Elements:”+ addmap’);

return addMap;
}


/ a setter method to set propertiesvalues and get values
Public void  setAddresproperties(addprops){
this.addprops=props;
}
Public Map getAddresproperties(){
System.out.println(“properties Elements:”+ addprops’);

return addprops;
}
}

main.java:::
package com.balu.javainterviewquestionsguru.blogspot.in;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class mainApp{
public static void main(String args[]){
ApplicationContext cxt= new ClassPathXmlApplicationContext(“Beans.xml”);
javaCollection jc =(javaCollcetion)cxt.getBean(“JavaCollection”);
jc.getAddList();
jc.getAddSet();
jc.getAddMap();
jc.getAddProps();
}
}

Beans.xml::
<? Xml  version=”1”>
<bean id =”javacollection”   class=”balu.javainterviewquestionsguru.blogspot.in”>
//results setAddres List
<property name=”addlist”>
<list><value>india</value><value>ap</value></list></property>
//results set Addres set
<property name=”addset”>
<set><value>usa</value><value>hyd</value></set></property>
// results set  Addres Map
<property name=”addmap”>
<map><entry key=”1”  value=”jermany”/><entry key=”2”  value=”goa”/></map></property>
//results set Addres props
<property name=”addprops”><props></props><prop key=”one”>balu</prop><prop key=”two”>pinky</prop></property>

o/p::
list values(duplicate values allowed)india,ap.
set values(duplicate not allowed)usa,hyd
map values(key,value anytype)1=jermany,2=goa.
props values(key,value both strings)one=balu,two=pinky