Pages

April 06, 2016

#Java: Part 10-Core Java Interview Questions and Answers

> > Part 9-Core Java Interview Questions and Answers(Java Memory Model, Garbage Collectors in Java)

How  do you prevent SQL Injection in Java Code?
In SQL Injection attack, malicious user pass SQL meta-data combined with input which allowed them to execute sql query of there choice.

ALSO READ: JDBC Interview Questions And Answers

PreparedStatement can be used to avoid SQL injection in Java code. Use of the PreparedStatement for executing SQL queries not only provides better performance but also shield your Java and J2EE application from SQL Injection attack. All the parameters passed as part of place-holder will be escaped automatically by JDBC Driver.

What will happen if you call return statement or System.exit on try or catch block ? will finally block execute?
finally block will execute even if you put return statement in try block or catch block, but finally block won't run if you call System.exit from try or catch.

Can you access non static variable in static context?
No

What do the expression 1.0 / 0.0 will return? will it throw Exception? any compile time error?
It will not throw ArithmeticExcpetion and return Double.INFINITY.

What is the difference between StringBuffer and StringBuilder in Java?
StringBuilder in Java was introduced in JDK 1.5 and the only difference between both of them is that StringBuffer methods e.g. length(), capacity() or append() are synchronized while corresponding methods in StringBuilder are not synchronized. Thats why, concatenation of String using StringBuilder is faster than StringBuffer.

What is Re-factoring in Software?
- Re-factoring is the continuous process of changing a software system in such a way that it does not alter the external behavior of the code yet improves the internal structure of the code.
- It is advisory that developers should have habit to apply re-factoring as continuous process to improve the code structure.
- Re-factoring helps to keep the code clone and minimize the chances of introducing bugs.
- Re-factoring made the internal structure of the software, easier to understand and cheaper to enhance the functionality of the software.

If a class is declared without any access modifiers, where can the class be accessed?
  • A class declared without any access modifiers is said to have package access.
  • Such a class can only be accessed by other classes and interfaces defined within the same package.
What is the purpose of the Runtime class?
Runtime class is an example of Singleton  design pattern and is used to access the java runtime system. The runtime information – memory availability, invoking the garbage collector is possible by using Runtime class.
  • Runtime.freeMemory() - Returns the free memory of
  • Runtime.maxMemory() - Returns the memory that JVM can use at the maximum.
  • Runtime.gc() - Invoking garbage collector
Some other facilities that are provided by Runtime class are:

  • Reading data through key board
  • Using system properties and environment variables
  • Running non-java programs from within a java application.

Explain the difference between static and dynamic class loading.
The static class loading is done through the new operator. The retrieval of class definition and instantiation of the object is done at compile time.

Dynamic class loading is achieved through Run time type identification,also called as reflection. It is done when the name of the class is not known at compile time.

Dynamic class loading is done with the help of the following methods: getClass(); getName(); getDeclaredFields(). Instance can also be created using forName() method. It loads the class into the current class memory.

Difference between loadClass and Class.forName ?
loadClass only loads the class but doesn't initialize the object whereas Class.forName initialize the object after loading it.

What is J2EE(Java 2 Platform Enterprise Edition)?
J2EE is a platform-independent, Java-centric environment from Sun for developing, building and deploying Web-based enterprise applications online. The J2EE platform consists of a set of services, APIs, and protocols that provide the functionality for developing multitiered, Web-based applications.

A few key features and services of J2EE:
  • At the client tier, J2EE supports pure HTML, as well as Java applets or applications. It relies on Java Server Pages and servlet code to create HTML or other formatted data for the client.
  • Enterprise JavaBeans (EJBs) provide another layer where the platform's logic is stored. An EJB server provides functions such as threading, concurrency, security and memory management. These services are transparent to the author.
  • Java Database Connectivity (JDBC), which is the Java equivalent to ODBC, is the standard interface for Java databases.
  • The Java servlet API enhances consistency for developers without requiring a graphical user interface.
Name the eight primitive Java types.
The eight primitive types are byte, char, short, int, long, float, double, and boolean.

What is a String Pool ?
String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.

How many objects are created with this code ?
String city =new String("Munich");

Two objects will be created. One object creates memory in heap with new operator and second in stack constant pool with "Munich".

Why do member variables have default values whereas local variables don't have any default value?
A member variable are loaded into heap, so they are initialized with default values when an instance of a class is created. In case of local variables, they are stored in stack until they are being used.

Can I import same package/class twice? Will the JVM load the package twice at runtime?
One can import the same package or same class multiple times. Neither the compiler nor JVM complains will complain about it. And the JVM will internally load the class only once no matter how many times you import the same class.

ALSO CHECKCore Java Interview Questions And Answers

-K Himaanshu Shuklaa..

No comments:

Post a Comment