Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Tuesday, 24 June 2014

100 Core Java Interview Questions and Answers - Part 10

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.




91) What is synchronization in Java?

In java synchronization is the ability to control the access of multiple threads to shared resources. Synchronization ensures that only one thread can access a shared resource at a time.

92) What are wrapper classes in Java.

Java provides some equivalent classes to represent primitive types as object these classes are called wrapper classes. There are 8 wrapper classes in java ; Byte, Short, Integer, Long, Float, Double, Character and Boolean.

93) Can we have more than one class declaration in a single java file.

We can have only a single public class declaration per java file, whereas we can have multiple non-public classes in a single java file.

94) Can we declare main() method as private?

No, We cannot declare main() method as private. If we does so the program will compile fine but at runtime it will give “Main Method Not Public” message.

95) Explain System.out.println() briefly?

System is a predefined final class, out is a PrintStream object and println is a built-in overloaded method in the out object.











Thanks for reading !
Being Java Guys Team


100 Core Java Interview Questions and Answers - Part 9

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.




81) What if, we import a class or package more than once in our program?

We can import a class or an package as many times as we want to, neither the compiler or the JVM will complain about it. But internally the class or package will be loaded once no matters how many times we have written the import statement.

82) What is the difference between a checked or unchecked exception in Java?

The classes which extends Throwable class except RuntimeException and Error are known as checked exceptions. These exceptions are thrown at compile time. E.g. IOException, FileNotFoundException etc. The classes which extends RuntimeException class are called unchecked exceptions. These exceptions are thrown at runtime. E.g. ArithmeticException,NullPointerException etc.

83) What is the root class of all Exception and Errors in Java.

Throwable is the base class for all Exceptions and Errors in Java.

84) Can we have a try block without a catch block in Java?

Yes, we can have a try block without having a catch block but a try block must be followed by either a catch or a finaly block or with both.

85) What are nested classes in Java?

A class that is declared inside another class is called nested class, there are four types of a nested class in java : member inner class, local inner class, annonymous inner class and static nested class.

86) What is volatile keyword in Java?

Threads hold their own copy of variables in local memory, if a variable is marked as volatile it means that the variable will be stored in main memory, every time a volatile variable is read it will be read from the main memory similarly every time the volatile variable is written the changes will be written to the main memory only.

87) What is the difference between path and classpath in java?

In context of java path and classpath both are operating system level environment variables. Path says where can the system finds .exe files whereas classpath tells the location of .class files within a system.

88) What do you mean by downcasting in Java?

In java downcasting means casting a general type to a more specific type.











Thanks for reading !
Being Java Guys Team


100 Core Java Interview Questions and Answers - Part 8

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.




71) What are immutable objects in Java?

In java, immutable objects are the objects which can not be changed once created. Any change to the states of these object results in a new object. String objects are the example of immutable objects in java any change to the states of an String object results in a new object and the old object remains unchanged.

72) What is the difference between a StringBuffer and StringBuilder class in Java?

In java both StringBuffer and StringBuilder classes are immutable in nature, the only difference in between them is that StringBuffer class in thread-safe while StringBuilder is not thread safe in nature. StringBuilder class was introduced in Java5 to reduce the overhead of synchronization.

73) How many types of memory areas are allocated by JVM?

JVM allocated memory areas in many forms like:
1. Class(Method) Area
2. Heap
3. Stack
4. Program Counter Register
5. Native Method Stack

74) What if I changed the order of public static and void in ‘public static void main(String args[])’?

The program will compile fine and run properly, main() method implementation is regardless of the order of public, static and void keywords.

75) What is the default value for an instance variable in Java?

All instance variables are initialized to “null” by default.

76) What do you mean by composition in Java ?

Holding the reference of one class in another class in known as composition in Java.

77) Can be define an abstract method in an non abstract class ?

No, if a single method is declared as abstract in a class then the class must be declared as abstract itself.

78) Can we declare a class as abstract and final in Java?

No, we cannot declare an abstract method as final, abstract methods does not have body and they are meant to be overridden in another class, if we declare them final than its impossible to override them and hence no use of them.











Thanks for reading !
Being Java Guys Team


100 Core Java Interview Questions and Answers - Part 7

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.




61) What if we does not pass an String array of objects to main() method ?

Program will compile fine, but at runtime it will give an error “NoSuchMethodError”.

62) List all primitive types in Java ?

There are a total of 8 primitive types in java; byte, short, char, int, long, Boolean, float and double. In java String is an object and not a primitive type.

63) Can we have multiple main() methods in the same class?

No, We can have multiple overloaded methods in a same class with name main(), but there must be a single method with signature ‘public static void main(String args[])’. Unless the compiler says main method already defined.

64) What is difference between “String” and “StringBuffer” class in Java?

In java, “String” is an immutable class whereas “StringBuffer” is not immutable in nature. This means that once a String object is created we cannot change its state. Every time an operation is performed on a String object, it results the creation of a new Object and the old object remains unchanged. Whereas in ‘StringBuffer’ because of being a non-immutable class, the changes are reflected to the same object on which the operation is performed.

65) What is transient variable in Java?

If we declare a variable as transient, it prevents that variable from being serialized. If a class is serialized to a stream and one tries to deserialize it again than the variable having transient with it return a null value.

66) Are “true” ,“false” and “null” keywords in Java?

No, Neither of the three true, false or null is a keyword in java.

67) Can we declare an abstract class as final.

No, we cannot declare an abstract class as final because abstract classes are meant to be extended by some other class to make it meaningful. If we declare a class final it cannot be extended by some other class and hence an abstract class cannot be declared as final.

68) What is the default value of a boolean type in Java?

In java a Boolean type variable is initialized to “false” by default.











Thanks for reading !
Being Java Guys Team


100 Core Java Interview Questions and Answers - Part 6

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.




51) What is a List interface in Java? What are its implementation classes?

List interface is a part of Java Collections API, List interface extends Collection interface provides support for ordered collections of Objects. There are three well known implementation classes for List interface i.e. ArrayList, Vector and LinkedList , all these classes provides an ordered collection of objects and support duplicate values.

52) What is a Set interface in Java? What are the implementation classes ?

Like List interface Set is another interface which is a part of Java Collections API and extends Collection interface, it does not permit duplicate values in it. The well known implementation classes for a Set interface are HashSet and TreeSet.

53) What is a Map interface in Java ? What are the implementation classes ?

Like List and Set interfaces, Map is also a part of Java Collections API but it does not extends Collection interface. Map provides a key value pair of Objects and some implementation like HashMap permits null whereas some implementation like HashTable does not allows null. HashMap, TreeMap and HashTable are three well known implementation of a Map interface.

54) What is the advantage of an ArrayList over Arrays in Java ?

ArrayList are resizable arrays, it can grow dynamically. ArrayList provides better implementation of searching, iterating, deleting or inserting an element than arrays.

55) What are well known implementations of a List interface in Java?

List interface have three well known implementations, described below:
1) ArrayList: ArrayList is an growable array with fast iteration and fast random access. It is an ordered by index collection and is not sorted in nature. It can be considered over linkedlist when we need more iteration and random access and less insertion or deletion.
2) Vector: Vector are almost as same as the ArrayList except that, methods of an Vector are synchronized and hence safe to use in a thread environment. One should use arraylist over vector if there is no need of thread safety.
3) LinkedList: Linked list is another implementation of List interface in java, elements in a linked list are ordered by index and are double-linked to each other. Because of this it provides adding or removing elements from both sides and hence it may iterate slow than ArrayList but insertion and deletion is much more fast that ArrayList.

56) What are the well known implementations of Set interface in Java?

There are three well known implementation of a Set interface as described below:
1) HashSet: A HashSet is an unsorted and unordered set, it uses the hash code of the objects being inserted, so the performance all depends on how well the hash code implementation is. This ensures uniqueness of elements and does not allow duplicates.
2) TreeSet: TreeSet is a sorted Set which ensures elements will be in ascending order naturally, it uses a red-black tree structure to store elements. Optionally we can construct a TreeSet with descending ordering of elements.
3) LinkedHashSet: LinkedHashSet is an ordered version of HashSet, when we uses HashSet we cannot predict the order of iteration, while in LinkedHashSet the order of elements remains unchanged.

57) What are the well know implementations of a Mao interface in Java?

There are three well known implementation of a Map interface as described below:
1) HashMap: HashMap provides an unsorted and unordered collection to store key value pairs. It cares about uniqueness of keys, HashMap allows one null key and multiple null values in it.
2) HashTable: HashTable is a synchronized version of HashMap, all the methods in a HashTable are thread safe. HashTable does not allows null keys or null values.
3) TreeMap: Like TreeSet, TreeMap is an ordered Map, which persists elements in ascending order.

58) What do you mean by pass by reference and pass by value in context of Java?

In context of a Java program, pass by value means passing a copy of the value, while pass by reference means passing address of the value rather than the value itself.











Thanks for reading !
Being Java Guys Team


100 Core Java Interview Questions and Answers - Part 5

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.




41) Can a class extend more than one class in Java?

No, java does not support multiple inheritance. In java a class cannot extends more than one class but it can implement as many interfaces as it wants.

42) Can we declare a class inside an interface and vice verse?

Yes, we can declare an interface inside a class and a class can be declared inside an interface, both statements are true.

43) What do you mean by a marker interface In Java?

An marker interface is an interface which does not contain any variable or method in it, these interfaces are implemented by some classes to provide special instructions to JVM. The example is serializable interface this interface does not contain anything in it, it tell the JVM that the class which implements this interface needs to be serialized.

44) What modifiers are allowed inside an interface in Java?

In java, interfaces can have public and abstract types of variables and methods in them. All the variables and methods are public abstract by default in an interface.

45) What is a constructor in java? Can constructors be overridden?

Constructors are special type of methods that have the same name as the class itself and do not have a return type not even void. Constructors are used to create an object of a class, if we do not declare any constructor in our class than JVM provides a non argument constructor by default. Constructors cannot be overridden, though a subclass can call the constructor of its super class.

46) What access specifies are available in Java?

In java there are four access specifies listed below their scope and usability.
1) public : public classes, methods and variables can be accessed from anywhere in the application, No restriction at all.
2) private: private variables and methods can be accessed in the same class only in which they are declared and not outside that, private variables and methods can not be accessed even in the subclasses.
3) protected: protected variables and methods can be accessed from anywhere in the same package and in the subclasses of same class outside the package.
4) default(no specifies): If a variable or method does not have any specifier than these can be accessed from anywhere within the same package and not outside the package.

47) What are static blocks in Java?

Static blocks are executed exactly once even before the main method is executed, these blocks are executed by jvm at the time of classes loaded in memory.

48) What is an Iterator in Java?

The Iterator is an interface which is used to step through a collection in java, Iterator has its own implementation for difrenet type of collections. Iterator has three methods next(), hashNext() and remove().











Thanks for reading !
Being Java Guys Team


100 Core Java Interview Questions and Answers - Part 4

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.




31) How to prevent a method from being overridden in Java?

To prevent a method from being overridden in java we have declare the method as final, if a method is declared as final that a subclass cannot override it.

32) What if a class is declared as final in Java?

If we declare a class as final in java, than it cannot be extended by other class. A abstract class cannot be declared as final.

33) What is the difference between final, finally and finalize in Java ?

‘final’ is a modifier which can be applied to a class, variable or a method. A final class can not be subclassed , a final method can not be overridden and a final variable can not be changed once created. finally is a exception handling block which is executed regardless the exception is being raised or not in try catch blocks. finalize() is a method from Object class which is being called by the garbage collector to give a final chance to unused objects to release their activity before removed from the memory.

34) What if a method is declared as static in Java?

We declare a method as static in java, when we need to access that object even before the creation of the object of that class, static methods can be called with the name of class only.

35) What if a variable is declared as static in Java?

If a variable is declared as static, than it is assumed as a class level variable. All the instances of that class will share a common copy of thet variable that means of one instance changed the value of a static variable than the change is being reflected to all other instances of the class.

36) Can we declare a local variable as static in Java?

No, static variables are class level variables we cannot declare a local variable as static. If we do so, it will give a compile time error.

37) What is an Abstract class in Java? And why do we need it?

An abstract class is a declared with abstract modifier, we cannot instantiate an abstract class, so abstract class must be extended by some other class to make sense. Abstract class can have both abstract and concrete methods in it. Abstract class is useful in obtaining abstraction in own program.

38) Can an interface implement another interface in Java ?

No, not possible. In java one interface extends another interface and not implement that.











Thanks for reading !
Being Java Guys Team


100 Core Java Interview Questions and Answers - Part 3

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.




21) What are instance variable in Java?

In java, variables which are declared at class level outside methods body are called instance variables. Instance variables need not to initialize before using them, instance variables are automatically initialized to their default value. Each instance of a class have their own copy of instance variables.

22) What is return type of main method in Java?

In java, main method does not return anything hence return type of main method is ‘void’.

23) Why do we declare main method as static in Java?

In Java, main method is called by JVM itself end even before the instantiation of the class, hence main method in declared as static.

24) What arguments do a main method accepts in Java?

In Java, main() method accepts an array of String objects as parameter.

25) Which package is imported by default in Java ?

java.lang packages is imported by default in all java programs.

26) What if a variable is declared as final in Java?

If a primitive type variable is declared as final than its value cannot be changed once initialized. If a referenced type variable is declared as final than it can not refer to any other object.

27) What if a method is declared as protected in Java ?

If a method is declared as protected, than it can be accessed in the classes of same package only or in the subclasses of the class in other packages.

28) Can we declare a class as protected in Java?

No, we cannot declare a class or interface as protected. However we can declare methods or variables or a class as protected, methods or variables inside an interface cannot be declared as protected.











Thanks for reading !
Being Java Guys Team


100 Core Java Interview Questions and Answers - Part 2

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.




11) What is Polymorphism in Java ?

Polymorphism means many forms, in java it is possible to have many forms of a single method on the basis of difference in between their parameters. In java polymorphism can be seen in the form of Overloading or Overriding.

12) What is Inheritance in Java ?

When an object acquires the properties(variables) or behavior(methods) of another object, it is called inheritance. In java inheritance is obtained using extending a class or implementing an interface, it helps in reusing the code and eliminate core delicacy.

13) What is encapsulation in Java?

Encapsulation means to provide a protective wrapper over code called class, so that the code outside the wrapper cannot access the internal code directly. In java encapsulation is obtained using access specifies like private, protected etc.

14) What is method overloading in java?

In java, method overloading refers to having two or more methods with same name and in difference of either of the following. 1) Either the number of arguments are different of two or more methods of same name. 2) Or data types of the arguments are different of two or more methods of same name. 3) Or both (1) and (2). Two or more methods are not overloaded if those are different in only their return types or placement of their parameters.

15) What is method overriding in Java?

When a subclass have a method with same name and same arguments as in it’s superclass, this is called method overriding. The benefits of overriding is to have subclass specific behavior of a method defined in their superclass.

16) How to prevent a method from being overridden In Java?

In java a method can be prevented from being overridden in subclass by using final keyword, it means that this method declaration is final now and can not be overridden in any of the subclass.

17) What is multiple inheritance? Does java support multiple inheritance?

When a class or object extends more than one class or object, this is called multiple inheritance. Java does not support multiple inheritance.

18) What are local variables in Java ?

In java all variables which are declared inside a block or a method are called local variables. Local variables must be initialized before using them.











Thanks for reading !
Being Java Guys Team


100 Core Java Interview Questions and Answers - Part 1

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.




1) What do you mean by platform independency. Is java a platform independent language ?

In general platform independency means, we can write and compile the source code on one machine having (windows, linux ..) and compiled code can be run on any other machine having same or different platform (windows, linux etc). In java source code is being compiled into byte code first, this bytecode can run on any other machine or platform having JVM(java virtual machine installed). So Java is considered to be a platform independent language.

2) What is JVM. Is JVM platform independent ?

Java or Java Virtual Machine is a Java runtime environment that makes the java compiled code run on a machine. JVM is not platform independent, different platform vendors provides platform specific JVM i.e. windows and linux have their own copy of JVM.

3) What is the difference between JVM and JDK ?

JDK is java development kit, it consists of Java Compiler, Java Libraries and JVM as well in it. JDK helps in development for Java programs, compile them and run them because of having JVM inside. JVM is purely Java virtual machine which is platform dependent runtime environment that makes java compile code run, we cannot compile java code using JVM.

4) What is the root class for all classes in Java ?

‘Object ’ class which resides in java.lang package is the base class for all classes in java, it means every class written in java extends Object class by default.

5) Is java a purely object oriented language, why ?

Java is not considered as a purely object oriented language because of having primitive datatypes like (int, char, boolean etc ). Although java contains equivalent wrapper classes(Integer, Char, Boolean etc) for those primitive types yet can not be considered a purely object oriented language.

6) Are Arrays primitive data types in java ?

No, In java everything is denoted by a class or Object, except those primitive data types. In java arrays are Objects and not primitive types.

7) What is OOPS ? Is java a object oriented language ?

An programming language is considered to be OOPS(Object oriented programming System) oriented. If it contains following four properties: 1) Abstraction 2) Polymorphism 3) Inheritance 4) Encapsulation Java contains all 4 mentioned attributes in it, hence Java is a object oriented language.

8) What is Abstraction in Java ?

Abstraction means to hide internal details and show only the essential details of an implementation. In java abstraction is gained using Abstract Classes and Interfaces.











Thanks for reading !
Being Java Guys Team


Search Tutorials
Like Us on Facebook

Like Us On Google+