Saturday, June 8, 2013

15 Mandatory Core Java Interview Questions - Part 1

1. What is the Object class?

java.lang.object is the superclass of all Classes in java. All classes extend this implicitly, hence All Classes isA Object. They show Object behaviour.

In the sense that all methods in Object classes are available to all classes in Java.


Object of any class can be assigned to Object.



2. Is Java Truly and Object Oriented Language?

Yes, Java is an Object Oriented language but not a pure one. Question arises Why? It is because java supports primitive datatypes like int, float, double. We have wrapper classes Integer, Float, Double also, but for simplicity it also supports primitive classes.

3. Why is main() method static in Java?

Main() method is the entry point of any program. It is loaded before any Class Object is created. Since Static function can be accessed directly from Class level and don't need an object creation. In short, main() is called before Object creation, hence main() is static.

4. Is overloading main() supported?

Yes. main() function can be overloaded. Read here for more details.

5. Explain Method Overloading and Overriding.

When multiple methods in the same class have the same name, it is called method overloading. Function arguments list can differ.
When a subclass, redefine a method present in parent class, it is called Method Overriding. Function name, arguments and return type needs to be the same while Overriding.

6. Example of Multiple Inheritance in Java?

Multiple Inheritance is not allowed in Java. A class cannot extend multiple classes but can implement multiple interfaces. 

7. Difference between JDK, JRE, JVM.

Java Virtual Machine (JVM) is a layer on top of the real hardware. For those who interact with it, JVM acts as though it is the real boss (Machine). But in reality it’s just a Software Hoax on which program is executed. It makes Java platform independent.

Various implementations of JVM which are installed on various Platforms are called JRE (Java Runtime Environment). This is the software which runs and interact with the Real Hardware and provide a JVM. It also contains the libraries required to run applications.

JDK contains JRE, JVM and compiler (javac.exe) along with other things. With JDK one is able to compile (convert *.java to *.class bytecode) and run (java.exe) a program.

For more details read JDK-JRE-JVM.

8. Explain static member/method in Java?

static member/method in Java, can be accesses without instantiating a class and loading an Object of that class. (Class.method). An important quality of static method is that it cannot be overridden. Overriding is resolved runtime with class Objects. Since with static the concept is different, hence overriding is not possible.

9.  What are the primitive data types supported in java?

Below are the data types in Java. For more details read. primitive data types java.

Data Type
Size
Default Value
Min
Max
boolean
1 bit
false
false
true
char
2 byte
'\u0000'
'\u0000' (or 0)
'\uffff' (or 65,535)
byte
1 byte
0
-128 (-2^7)
127 (2^7 -1)
short
2 byte
0
-32,768 (-2^15)
32,767 (2^15 -1)
int
4 byte
0
- 2,147,483,648 (-2^31)
2,147,483,647 (2^31 -1)
long
8 byte
0L
-9,223,372,036,854,775,808
(-2^63)
9,223,372,036,854,775,807
(2^63 -1)
float
4 byte
0.0f


double
8 byte
0.0d



10. How can you define a class as static?

Class as such can't be define as static. All methods/members can be defined as static. We can call such a class as static.
However inner classes can be defined as static. 

11. What all datatypes can be used in switch/case?

A serious drawback of Switch-Case was it just worked with expressions that evaluated to int (int, short, byte, char, Enum). Not anymore. With Java 7 it can take Strings too. Isn’t is awesome?
String fruit = "apple";
switch (fruit) {
case "orange":

12. Give an example of Marker Interface.

Serilaizable/Cloneable are two famous examples of Marker Interface. They are empty interface with no methods inside, but are used to enabling some functionality in the implementing classes.



13. What is the difference between sleep/wait?

Difference in Sleep and Wait

14. Give a usage of instanceof Keyword.

instanceof keyword is used to check if a given Object belongs to a particular class or not. It becomes useful in case of inherited classes. All objects are instanceof Object class.



15. What is Thread in Java?

Thread relates to running of a program. A Thread is a single chain of execution within a program. click here for more details.


No comments:

Post a Comment