Pages

Subscribe Twitter Twitter

Monday, August 2, 2010

COREJAVA INTERVIEW QUESTIONS4


Core Java
Java Material Interview Reference

Q) OOPS concepts

Polymorphism
Ability to take more than one form, In java we achieve this using Method Overloading (compile time polymorphism), Method overriding (runtime polymorphism)

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

Encapsulation
Wrapping of data and function into a single unit called encapsulation. Ex:- all java programs.

Abstraction
Nothing but representing the essential futures without including background details.

Dynamicbinding
Code associated with a given procedural call is not known until the time of the call at runtime. Dynamic binding is nothing but late binding.

Q) class & object?
class

 class is a Template that describes the Kind of State(The Instance Variables) and Behavior (Methods)
 class is a blue print of an object component means u can use a piece of code like an
independent piece.like servlet,EJB...etc
Object  instance of class u can reuse it in any application

Q) System.out.println()
 println() is a methd of java.io.printWriter.
 “out” is an instance variable of java.lang.System class.

Q) Transient & volatile

Transient --> the object or variable will not persist.
Volatile --> value will be changed unexpectedly by the other part of the program.

Q) Access Specifiers & Access modifiers?
Access Specifiers  A.S gives access privileges to outside of application (or) others; they are Public, Protected, Private, Defaults
Access Modifiers  A.M which gives additional meaning to data, methods and classes, final cannot be modified at any point of time.

Private Public Protected No modifier
Same class No Yes Yes Yes
Same package Subclass No Yes Yes Yes
Same package non-subclass No Yes Yes Yes
Different package subclass No Yes Yes No
Different package non-subclass No Yes No NO

Q) Default Values
long  -2^63 to 2^63 –1  0L
Int  -2^31 to 2^31 –1  0
Short  -2^15 to 2^15 –1  0
Byte  -2^7 to 2^7 –1  0
char  0 to 2^7 –1  null character (or) ‘\u 0000’
double  0.0d
float  0.0f
Boolean  false
Character  ‘\u0000’

Q) Byte code & JIT compiler
Byte code is a highly optimized set of instructions. JVM is an interpreter for byte code. Translating a java program into byte code helps makes it much easier to run a program in a wide variety of environment.

JIT is a part of JVM, it compiles byte code into executable code in real time, will increase the performance of the interpretations.

Q) Diff Access Specifier & Access Modifiers

Access Specifier

Access Modifiers

Q) Wrapper classes

Primitive data types can be converted into objects by using wrapper classes. These are java.lang.package.

Q) Does Java pass method arguments by value or by reference?
A) Java passes all arguments by value, not by reference

Q) Arguments & Parameters
While defining method, variable passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.

Q) Public static void main (String [] args)

 What if the main method is declared as private?
The program compiles properly but at runtime it will give "Main method not public." Message

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

 We can write “static public void” instead of “public static void” but not “public void static”.

 If I do not provide the String array as the argument to the method?
Program compiles but throws a runtime error "NoSuchMethodError".

 If no arguments on the command line, String array of Main method will be empty of null?
It is empty. But not null.
 Variables can have the same name as a method or a class

Q) Can an application have multiple classes having main method?
A) Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.

Q) Can I have multiple main methods in the same class?
A) No the program fails to compile. The compiler says that the main method is already defined in the class.

Q) Constructor

The automatic initialization is performed through the constructor, constructor has same name has class name. Constructor has no return type not even void. We can pass the parameters to the constructor. this () is used to invoke a constructor of the same class. Super () is used to invoke a super class constructor. Constructor is called immediately after the object is created before the new operator completes.

 Constructor can use the access specifiers public, protected or private or have no access modifier (package access)
 Constructor can not use the modifiers abstract, static, final, native, synchronized or strictfp
 Constructor can be overloaded, we cannot override.
 You cannot use this() and Super() in the same constructor.


Class A(
A(){
System.out.println(“hello”);
}}

Class B extends A {
B(){
System.out.println(“friend”);
}}

Class print {
Public static void main (String args []){
B b = new B();
}

o/p:- Hello
friend

Q) Diff Constructor & Method

Constructor Method
Use to instance of a class Grouping java statement
No return type Void (or) valid return type
Same name as class name As a name except the class method name, begin with lower case.
“This” refer to another constructor in the same class Refers to instance of class
“Super” to invoke the super class constructor Execute an overridden method in the super class
“Inheritance” cannot be inherited Can be inherited
We can overload but we cannot overridden Can be inherited
Will automatically invoke when an object is created Method has called explicitly

Q) Garbage collection

G.C is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program cann't directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program. Every class inherits finalize () method from java.lang.Object, the finalize () method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use. In Java on calling System.gc () and Runtime.gc (), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.

Q) Final, Finally, Finalize

Final: - When we declare a sub class a final the compiler will give error as “cannot subclass final class” Final to prevent inheritance and method overriding. Once to declare a variable as final it cannot occupy memory per instance basis.

 Final class cannot have static methods
 Final class cannot have abstract methods
 Final class can have only a final method.

Finally: - Finally create a block of code that will be executed after try catch block has completed. Finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement match the exception. Any time a method is about to return to the caller from inside try/catch block, via an uncaught exception or an explicit return statement, the finally clause is also execute.

Using System.exit (); in try block will not allow finally code to execute

Finalize: - some times an object need to perform some actions when it is going to destroy, if an object holding some non-java resource such as file handle (or) window character font, these resources are freed before the object is going to destroy any cleanup processing before the object is garbage collected.

Q) Superclass & Subclass
A super class is a class that is inherited whereas subclass is a class that does the inheriting

Q) Diff forms of Polymorphism? Method overloading, Method overriding?
A) Method overloading, Method overriding through inheritance

Q) Method Overloading & Method Overriding?

Method Overloading (Compile time polymorphism)
Define two or more methods within the same class (or) subclass that share the same name and their parameter declarations are different then the methods are said to be overloaded. Overloaded methods must differ in number of parameters & return type.

• Overloaded methods are not required to have the same return type or the list of thrown exceptions.
• Overloading is particularly used while implementing several methods that implement similar behavior but for different data types.

Method Overriding (Runtime polymorphism)
When a method in a subclass has the same name, return type and parameters as the method in the super class then the method in the subclass is override the method in the super class.

0 comments:

Post a Comment