Core Java Interview Questions

Core Java Interview Questions

#1 what is JVM/java byte codes?
Ans :- JVM stands for Java  virtual machine. It’s an virtual computer which run the compiled java program.  All java programs are compiled into bytecodes.  Java can only understand and execute java bytes codes
                Java compiler takes .java files and compiles it to bytecode file with .class file extension.
                                     
Source code (.java file) -> java compiler -> compiled java programs (.class file) -> JVM-> hardware platform and OS.

#2 What is just in time compilation?
Ans :- machine understand only binary language.  There two ways to generate the binary files.
#1 compiler - Compiler generated the binary files directly.
#2 interpreters –interpreter generates class file which is then run by VJM.it means binary files are generated run time.
 JVM does not compile full class fine in one shot.  Compilation is done on function basic/file basic. Advantage is that heavy parsing of original source code is avoided. Depending on need the compilation is done. This type of compilation is termed as JIT.

#3 what is OOP?
Ans :- problem solving technique to develop software system.

#4 what is class?
Ans :- blue print of object.

#5 what is object?
Ans:- objects has attributes, behavior and identity.

#5 what is relation between class and object?
Ans :- class is definition whereas object is instance of object.  Class is blue print whereas objects are actual object exist in real world.

#6  what are the different properties/features of OOS?
Ans :-       1. Abstraction
                2. Encapsulation
                3. Inheritance
                4. Polymorphism

#7 how do you implement inheritance in java?
Ans :- inheritance is implemented using EXTEND keyword.

#8 how can you implement polymorphism?
Ans:- two types of polymorphism:
                #1 method polymorphism through overloading 
                                Overloading – same method with different signature in same class.
                #2 object polymorphism through overriding
                                Overriding – same method with same signature in child class.

#9 what’s an interface and how will you go about implementing an interface?
Ans:- interface named collection of method definitions without implementation. Interface class not instantiate alone. It can be done though the class who implements that interface. Cannot instantiate the type interface. Class can implement many interfaces but can have only one supper class.


#10 what is abstract class?
Ans:- abstract can have some implementation. Cannot instantiate the type abstract class.

#11 what are abstract method?
Ans:- abstract class contains abstract methods and they do not have implementations. Abstract methods should be implemented in subclass which inherit them

#12 what is difference between static and non static variables of class?
Ans :- non static -> they are called as instance variables. Whenever object is created , same copy of instance variable is created.
Static -> only one copy of instance variable and will be shared among all object of class.

#13 what are inner class and what is the practical implementation ?
Ans:- inner class is nested in other class.  They have access  to outer class fields even though they are defined as private.
Practical use -  used as data structure .

#14 what are native methods?
Ans :-

 #15 depth of garbage collector?
Ans:- GC frees allocated memory. GC added over head to JVM. But GC has good algorithm which resolves that issues.

#16 how does GC determine that object has to be marked for deletion?
Ans : - object is garbage collected when the class is executed. Once the main method’s execution completed then objects gets release from GC.

#17 explain the finalize() method.
Ans:-  sometime object needs to do perform some actions before destroyed. JVM call that finalize() method before destroying that method.
Snippet :
protected void finalize() {
}

#18 how can you force GC to run?
Ans:- System.gc() or runtime.gc();

#19 what are class loaders?
Ans:- class loaders is the class responsible for finding and loading class at runtime. To load classes from across network or from other source like FTP,HTTP java provided class loaders.

#20. What is bootstrap, extension and system class loader?
Ans:- Bootstrap class loader -   bootstrap finds the necessary classes  fom jdk,jre,lib,
Responsible
#1) to load class which are essential for JVM to function properly,
#2) for loading all core java classes ( i.e. java.lang.*, java.io.* )

Extension class loader – it is child of bootstrap.

System class loader –  it is termed as application class loader. Responsible for loading code from path specified by classpath environment. Its also used to load an application’s entry point class that is the “static void main()” method in class.

#21 explain the flow of bootstrap, extension, system loader classes.
Ans:- take an example of below class.
Import com.server.test;
Public class MyClass {
                Public static void  main(String[] args) {
                                String str = “Hello”;
                                System.out.println(str);         
                }
}
                                                                                               
Above class uses the String class. JVM will request to system class loader to load sting class. System class loader will request to extension class and extension class will request to bootstrap class loader to load string class.
In the same fashion,  for import statements also, JVM follow same, but bootstrap class loader does not find the test class and the all child classes will return null, then system class loader uses the class path to load test class.

#22 static and dynamic class loading ?
Ans:- in static  class loading, new keyword needs to use. In dynamic class loading, reflection APIs needs to use. Reflection APIs are member  of lang class. Below is example of dynamic class loading
Public class DynamicClassLoading{
                Public static void main(String[] args) {
                                //Load Class
                                Class toRun = Class.forName(“com.server.test”);
               
                                //finds the main method
                                Method mainMethod;
                                Method[] methods = toRun.getMethods();
                                For(int  i=0; i<methods.length;i++) {
                                                If(“main”.equals(methods[i].getName())) {
                                                                mainMethod  = methods[i];
                                                                break;
                                                }
                                }

                                //invoke the method
                                mainMethod.invoke( null, new Object[] { toRun });                             
}
}


#23 Explain core collection interfaces.
Ans:-

collection <<interface>>                                                                         map<<interface>>
               

List  <<Interface>>                  set<<interface>>                                                    Sorted map <<interface>>    

                                                Sorted Set <<interface>>



#24String is immutable, explain the concept.
Ans:- immutable means , cant change the value of sting. For an example, consider below class.
Public class ClsString{
                Public static void main(String[] args) {
                                String str;
                                str = “Hi”;
                                Str=”Hello”;
                }
}

Hi and Hello both are created as object In memory. Ans str is not having an reference to Hello.





Comments

Popular posts from this blog

Custom DataType in Oracle Database.

NetBeans 6.1 :: Writing String Value to the method