Friday, 22 March 2013

Core Java Interview Question - 2

Question: What are the kinds of variables in Java? What are their uses?
Answer: Java has three kinds of variables namely, the instance variable, the local variable and the class variable.

Local variables are used inside blocks as counters or in methods as temporary variables and are used to store information needed by a single method.

Instance variables are used to define attributes or the state of a particular object and are used to store information needed by multiple methods in the objects.

Class variables are global to a class and to all the instances of the class and are useful for communicating between different objects of all the same class or keeping track of global states.

Question: What is a literal? How many types of literals are there?
Answer: A literal represents a value of a certain type where the type describes how that value behaves.
There are different types of literals namely number literals, character literals, boolean literals, string literals,etc.

Question: What are operators and what are the various types of operators available in Java?
Answer: Operators are special symbols used in expressions.
The following are the types of operators:
1.     Arithmetic operators
2.     Assignment operators
3.     Increment & Decrement operators
4.     Logical operators
5.     Bitwise operators
6.     Comparison/Relational operators
7.     Conditional operators

Question: What is mean by garbage collection?
Answer: When an object is no longer referred to by any variable, Java automatically reclaims memory used by that object. This is known as garbage collection.

Question: What is immutable object? Can you write immutable object?
Answer: You need to make class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non-final but private and not modifying them except in constructor.

Question: Does all property of immutable object needs to be final?
Answer: Not necessary as stated above you can achieve same functionality by making member as non-final but private and not modifying them except in constructor.

Question: What is class loader?
Answer:  The class loader is a subsystem of JVM that is used to load classes and interfaces. There are many types of class loaders e.g. Bootstrap class loader, Extension class loader, System class loader, Plugin class loader etc.

Question: If you do not provide any arguments on the command line, then the String array of Main method will be empty or null?
Answer: It is empty. But not null.

Question: What if I write static public void instead of public static void?
Answer: Interchange the location of these words does not give any effect so Program compiles and runs successfully.

Question: What is the local variables and its default value?
Answer: Local variables are defined within the block or within the method. Local variables are not initialized to any default value, neither primitives nor object references.

Question: What will be the initial value of an object reference which is defined as an instance variable?
Answer: The object references are all initialized to null in Java.

Question: What is static variable?
Answer: Static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees, college name of students etc.
Static variable gets memory only once in class area at the time of class loading.

Question: What is static method?
Answer: A static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an instance of a class. Static method can access static data member and can change the value of it.

Question: Why main method is static?
Answer: because object is not required to call static method if It were non-static method, jvm create object first then call main() method that will lead to the problem of extra memory allocation.

Question: What is static block?
Answer: It is used to initialize the static data member. It is executed before the main method at the time of class loading.

Question: Can we execute a program without main() method?
Answer: Yes, one of the ways is, by static block.

Question: What if the static modifier is removed from the signature of the main method?
Answer: Program compiles. But at runtime throws an error "NoSuchMethodError".

Question: What is difference between static (class) method and instance method?
Answer: following are the differences:
1.     A method i.e. declared as static is known as static method. A method i.e. not declared as static is known as instance method.
2.     Object is not required to call static method. Object is required to call instance methods.
3.     Non-static (instance) members cannot be accessed in static context (static method, static block and static nested class directly. static and non-static variables both can be accessed in instance methods.
4.     For example: public static int cube (int n) {return n*n*n ;} For example: public void msg(){...}.

Question: What is object cloning?
Answer: The object cloning is used to create the exact copy of an object.

Question: Can you have virtual functions in Java?
Answer: Yes, all functions in Java are virtual by default.

Question: What is blank final variable?
Answer: A final variable, not initialized at the time of declaration, is known as blank final variable.

Question: Can we initialize blank final variable?
Answer: Yes, only in constructor if it is non-static. If it is static blank final variable, it can be initialized only in the static block.

Question: Can you declare the main method as final?
Answer: Yes, such as, public static final void main(String[] args){}.

Question: How does every Class extends Object?
Answer: Every Class extends Object class implicitly so that they provide basic features which according to Java recommendation every class should have. Such as clone(), equals(), hashCode(), toString(), etc.
By implicitly, it means that if you are not extending any class then only compiler will implicitly extends Object class.But if class already extends other class then compiler will not extend Object class. For eg. Class A{ }
Class B extends A{ }
Here compiler will implicitly add extends Object class in class A declaration.
Class A extends Object{ }
Class B extends A{ }
As class A extends Object class so it will provide basic functionality of Object class such as equals(), toString(),etc. And since Class B extends class A which implicitly extends Class Object, so class B also provides all those features.
Thus by following this approach every class objects(variables) complies to features which every Java Object should have, without going for Multiple Inheritance (a class extending more than one class) which Java doesn't allows. This approach follows Multi-Level Inheritance.
Question: Why does every object in Java implicitly extend java.lang.Object class?
Answer: I would say that the reason is to have a common API for all objects in java to supports basic functionality like
  • synchronization -> wait, notify, etc.
  • garbage collection callback -> finalize
  • collection support -> hashCode, equals
  • object cloning -> clone
And every object
  • has a class it belongs to -> getClass
  • can represent itself as a string, because we are humans and can read strings.

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.



Thanks for coming here. I hope this information helps you for preparing Java Interview.

Sachin Singh