Sie sind auf Seite 1von 21

www.einterviewquestions.

com
1. What is meant by pass by reference and pass by value in Java? Pass by reference means, passing the address itself rather than passing the value. Pass by value means passing a copy of the value. If youre overriding the method equals() of an object, which other method you might also consider? hashCode() 2. What is Byte Code? Or what gives java its write once and run anywhere nature? All Java programs are compiled into class files that contain bytecodes. These byte codes can be run in any platform and hence java is said to be platform independent. Expain the reason for each keyword of public static void main(String args[])? public- main(..) is the first method called by java environment when a program is executed so it has to accessible from java environment. Hence the access specifier has to be public. static: Java environment should be able to call this method without creating an instance of the class , so this method must be declared as static. void: main does not return anything so the return type must be void The argument String indicates the argument type which is given at the command line and arg is an array for string given during command line. 3. What are the differences between == and .equals() ? The == operator compares two objects to determine if they are the same object in memory i.e. present in the same memory location. It is possible for two String objects to have the same value, but located in different areas of memory. == compares references while .equals compares contents. The method public boolean equals(Object obj) is provided by the Object class and can be overridden. The default implementation returns true only if the object is compared with itself, which is equivalent to the equality operator == being used to compare aliases to the object. String, BitSet, Date, and File override the equals() method. For two String objects, value equality means that they contain the same character sequence. For the Wrapper classes, value equality means that the primitive values are equal. public class EqualsTest { public static void main(String[] args) { String s1 = "abc"; String s2 = s1; String s5 = "abc"; String s3 = new String("abc"); String s4 = new String("abc"); System.out.println("== comparison : " + System.out.println("== comparison : " + System.out.println("Using equals method System.out.println("== comparison : " +

(s1 == s5)); (s1 == s2)); : " + s1.equals(s2)); s3 == s4);

1|Page

www.einterviewquestions.com
System.out.println("Using equals method : " + s3.equals(s4)); } } Output == comparison : true == comparison : true Using equals method : true false Using equals method : true 4. How many methods in the Serializable interface? There is no method in the Serializable interface. The Serializable interface acts as a marker, telling the object serialization tools that your class is serializable. How many methods in the Externalizable interface? There are two methods in the Externalizable interface. You have to implement these two methods in order to make your class externalizable. These two methods are readExternal() and writeExternal(). 5. What is the difference between Serializalble and Externalizable interface? When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject() two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class's serialization process. 6. What is a transient variable in Java? A transient variable is a variable that may not be serialized. If you don't want some field to be serialized, you can mark that field transient or static. Which containers use a border layout as their default layout? The Window, Frame and Dialog classes use a border layout as their default layout. 7. How are Observer and Observable used? Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated, it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects. 8. What is NullPointerException and how to handle it? Security Tip Use Firefox instead of Internet Explorer and PREVENT Spyware ! Firefox is free and is considered the best free, safe web browser available today Browse the web faster. Get Firefox with Google Toolbar When an object is not initialized, the default value is null. When the following things happen, the NullPointerException is thrown: --Calling the instance method of a null object. --Accessing or modifying the field of a null object. --Taking the length of a null as if it were an array. --Accessing or modifying the slots of null as if it were an array. --Throwing null as if it were a Throwable value.

2|Page

www.einterviewquestions.com
The NullPointerException is a runtime exception. The best practice is to catch such exception even if it is not required by language design. An application needs to load a library before it starts to run, how to code? One option is to use a static block to load a library before anything is called. For example, class Test { static { System.loadLibrary("path-to-library-file"); } .... } When you call new Test(), the static block will be called first before any initialization happens. Note that the static block position may matter. 9. How could Java classes direct program messages to the system console, but error messages, say to a file? The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed: Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st); System.setOut(st); 10. What is the difference between final, finally and finalize? final - declare constant finally - handles exception finalize - helps in garbage collection

Variables defined in an interface are implicitly final. A final class can't be extended i.e., final class may not be subclassed. This is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant). finalize() method is used just before an object is destroyed and garbage collected. finally, a key word used in exception handling and will be executed whether or not an exception is thrown. For example, closing of open connections is done in the finally method. 11. What is the Java API? The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets. 12. What is the GregorianCalendar class? The GregorianCalendar provides support for traditional Western calendars. 13. What is the ResourceBundle class? The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to tailor the program's appearance to the particular locale in which it is being run. 14. Why there are no global variables in Java?

3|Page

www.einterviewquestions.com
Global variables are globally accessible. Java does not support globally accessible variables due to following reasons: * The global variables breaks the referential transparency * Global variables creates collisions in namespace. 15. How to convert String to Number in java program? The valueOf() function of Integer class is is used to convert string to Number. Here is the code example: String numString = "1000"; int id=Integer.valueOf(numString).intValue(); 16. What is the SimpleTimeZone class? The SimpleTimeZone class provides support for a Gregorian calendar. 17. What is the difference between a while statement and a do statement? A while statement (pre test) checks at the beginning of a loop to see whether the next loop iteration should occur. A do while statement (post test) checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the loop body at least once. 18. What is the Locale class? The Locale class is used to tailor a program output to the conventions of a particular geographic, political, or cultural region. 19. Describe the principles of OOPS. There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation. 20. Explain the Inheritance principle. Inheritance is the process by which one object acquires the properties of another object. Inheritance allows well-tested procedures to be reused and enables changes to make once and have effect in all relevant places 21. What is implicit casting? Implicit casting is the process of simply assigning one entity to another without any transformation guidance to the compiler. This type of casting is not permitted in all kinds of transformations and may not work for all scenarios. Example int i = 1000; long j = i; //Implicit casting Is sizeof a keyword in java? The sizeof operator is not a keyword. What is a native method?

4|Page

www.einterviewquestions.com
A native method is a method that is implemented in a language other than Java. 22. In System.out.println(), what is System, out and println? System is a predefined final class, out is a PrintStream object and println is a built-in overloaded method in the out object. 23. What are Encapsulation, Inheritance and Polymorphism? Polymorphism in simple terms means one name many forms. Polymorphism enables one entity to be used as a general category for different types of actions. The specific action is determined by the exact nature of the situation. Polymorphism exists in three distinct forms in Java: Method overloading Method overriding through inheritance Method overriding through the Java interface 24. What is explicit casting? Explicit casting in the process in which the complier are specifically informed to about transforming the object. Example long i = 700.20; int j = (int) i; //Explicit casting 25. What is the Java Virtual Machine (JVM)? The Java Virtual Machine is software that can be ported onto various hardwarebased platforms 26. What do you understand by downcasting? The process of Downcasting refers to the casting from a general to a more specific type, i.e. casting down the hierarchy 27. What are Java Access Specifiers? Access specifiers are keywords that determine the type of access to the member of a class. These keywords are for allowing privileges to parts of a program such as functions and variables. These are: Public : accessible to all classes Protected : accessible to the classes within the same package and any subclasses. Private : accessible only to the class to which they belong Default : accessible to the class to which they belong and to subclasses within the same package 28. Which class is the superclass of every class?

5|Page

www.einterviewquestions.com
Object. 29. What is the difference between the boolean & operator and the && operator? If an expression involving the boolean & operator is evaluated, both operands are evaluated, whereas the && operator is a short cut operator. When an expression involving the && operator is evaluated, the first operand is evaluated. If the first operand returns a value of true then the second operand is evaluated. If the first operand evaluates to false, the evaluation of the second operand is skipped. 30. How does Java handle integer overflows and underflows? It uses those low order bytes of the result that can fit into the size of the type allowed by the operation. 31. What if I write static public void instead of public static void? Program compiles and runs properly. 32. What is the difference between declaring a variable and defining a variable? In declaration we only mention the type of the variable and its name without initializing it. Defining means declaration + initialization. E.g. String s; is just a declaration while String s = new String ("bob"); Or String s = "bob"; are both definitions. 33. What type of parameter passing does Java support? In Java the arguments (primitives and objects) are always passed by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object. 34. Explain the Encapsulation principle? Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. Objects allow procedures to be encapsulated with their data to reduce potential interference. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper. 35. What do you understand by a variable? Variable is a named memory location that can be easily referred in the program. The variable is used to hold the data and it can be changed during the course of the execution of the program. 36. What do you understand by numeric promotion? The Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integral and floating-point operations may take place. In the numerical promotion process the byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required. 37. What do you understand by casting in java language? What are the types of casting? The process of converting one data type to another is called Casting. There are two types of casting in Java; these are implicit casting and explicit casting.

6|Page

www.einterviewquestions.com
38. What is the first argument of the String array in main method? The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name. If we do not provide any arguments on the command line, then the String array of main method will be empty but not null. 39. How can one prove that the array is not null but empty? Print array.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print array.length. 40. Can an application have multiple classes having main method? Yes. 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. 41. When is static variable loaded? Is it at compile time or runtime? When exactly a static block is loaded in Java? Static variable are loaded when classloader brings the class to the JVM. It is not necessary that an object has to be created. Static variables will be allocated memory space when they have been loaded. The code in a static block is loaded/executed only once i.e. when the class is first initialized. A class can have any number of static blocks. Static block is not member of a class, they do not have a return statement and they cannot be called directly. Cannot contain this or super. They are primarily used to initialize static fields. 42. Can I have multiple main methods in the same class? We can have multiple overloaded main methods but there can be only one main method with the following signature : public static void main(String[] args) {} No the program fails to compile. The compiler says that the main method is already defined in the class. 43. Explain working of Java Virtual Machine (JVM)? JVM is an abstract computing machine like any other real computing machine which first converts .java file into .class file by using Compiler (.class is nothing but byte code file.) and Interpreter reads byte codes. 44. How can I swap two variables without using a third variable? Add two variables and assign the value into First variable. Subtract the Second value with the result Value. and assign to Second variable. Subtract the Result of First Variable With Result of Second Variable and Assign to First Variable. Example: int a=5,b=10;a=a+b; b=a-b; a=a-b; An other approach to the same question You use an XOR swap. for example: int a = 5; int b = 10;

7|Page

www.einterviewquestions.com
a = a ^ b; b = a ^ b; a = a ^ b;

45. What is data encapsulation? Encapsulation may be used by creating 'get' and 'set' methods in a class (JAVABEAN) which are used to access the fields of the object. Typically the fields are made private while the get and set methods are public. Encapsulation can be used to validate the data that is to be stored, to do calculations on data that is stored in a field or fields, or for use in introspection (often the case when using javabeans in Struts, for instance). Wrapping of data and function into a single unit is called as data encapsulation. Encapsulation is nothing but wrapping up the data and associated methods into a single unit in such a way that data can be accessed with the help of associated methods. Encapsulation provides data security. It is nothing but data hiding. 46. What is reflection API? How are they implemented? Reflection is the process of introspecting the features and state of a class at runtime and dynamically manipulate at run time. This is supported using Reflection API with built-in classes like Class, Method, Fields, Constructors etc. Example: Using Java Reflection API we can get the class name, by using the getName method. 47. Does JVM maintain a cache by itself? Does the JVM allocate objects in heap? Is this the OS heap or the heap maintained by the JVM? Why? Yes, the JVM maintains a cache by itself. It creates the Objects on the HEAP, but references to those objects are on the STACK. 48. What is phantom memory? Phantom memory is false memory. Memory that does not exist in reality. 49. Can a method be static and synchronized? A static method can be synchronized. If you do so, the JVM will obtain a lock on the java.lang. Class instance associated with the object. It is similar to saying: synchronized(XYZ.class) { } 50. What is difference between String and StringTokenizer? A StringTokenizer is utility class used to break up string. Example: StringTokenizer st = new StringTokenizer("Hello World"); while (st.hasMoreTokens()) {

8|Page

www.einterviewquestions.com
System.out.println(st.nextToken()); } Output: Hello World 51. What is the difference between procedural and object-oriented programs? a) In procedural program, programming logic follows certain procedures and the instructions are executed one after another. In OOP program, unit of program is object, which is nothing but combination of data and code. b) In procedural program, data is exposed to the whole program whereas in OOPs program, it is accessible with in the object and which in turn assures the security of the code. 52. What are Encapsulation, Inheritance and Polymorphism? Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse. Inheritance is the process by which one object acquires the properties of another object. Polymorphism is the feature that allows one interface to be used for general class actions. 53. What is the difference between Assignment and Initialization? Assignment can be done as many times as desired whereas initialization can be done only once. 54. What is OOPs? Object oriented programming organizes a program around its data, i. e. , objects and a set of well defined interfaces to that data. An objectoriented program can be characterized as data controlling access to code.

55. What are Class, Constructor and Primitive data types? Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that determines how an object is initialized when created. Primitive data types are 8 types and they are: byte, short, int, long, float, double, boolean, char. 56. What is an Object and how do you allocate memory to it? Object is an instance of a class and it is a software unit that combines a structured set of data with a set of operations for inspecting and manipulating that data. When an object is created using new operator, memory is allocated to it. 57. What is the difference between constructor and method? Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.

9|Page

www.einterviewquestions.com
58. What are methods and how are they defined? Methods are functions that operate on instances of classes in which they are defined. Objects can communicate with each other using methods and can call methods in other classes. Method definition has four parts. They are name of the method, type of object or primitive type the method returns, a list of parameters and the body of the method. A methods signature is a combination of the first three parts mentioned above. 59. What is the use of bin and lib in JDK? Bin contains all tools such as javac, appletviewer, awt tool, etc., whereas lib contains API and all packages. 60. How many ways can an argument be passed to a subroutine and explain them? An argument can be passed in two ways. They are passing by value and passing by reference. Passing by value: This method copies the value of an argument into the formal parameter of the subroutine. Passing by reference: In this method, a reference to an argument (not the value of the argument) is passed to the parameter. 61. What is the difference between an argument and a parameter? While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments. 62. What is final, finalize() and finally? final : final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods. A final method cant be overridden. A final variable cant change f rom its initialized value. finalize() : finalize() method is used just before an object is destroyed and can be called just prior to garbage collection. finally : finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. For example, if a method opens a file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. This finally keyword is designed to address this contingency. 63. What is UNICODE? Unicode is used for internal representation of characters and strings and it uses 16 bits to represent each other. 64. What is Garbage Collection and how to call it explicitly? 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. System. gc() method may be used to call it explicitly. 65. What are Transient and Volatile Modifiers? Transient: The transient modifier applies to variables only and it is not stored as part of its objects Persistent state. Transient variables are not serialized. Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.

10 | P a g e

www.einterviewquestions.com
66. What is method overloading and method overriding? Method overloading: When a method in a class having the same method name with different arguments is said to be method overloading. Method overriding: When a method in a class having the same method name with same arguments is said to be method overriding. 67. What is difference between overloading and overriding? a) In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method. b) Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass. c) In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass. d) Overloading must have different method signatures whereas overriding must have same signature. 67. What is meant by Inheritance and what are its advantages? Inheritance is the process of inheriting all the features from a class. The advantages of inheritance are reusability of code and accessibility of variables and methods of the super class by subclasses. 68. What is the difference between this() and super()? this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor. 69. What is the difference between superclass and subclass? A super class is a class that is inherited whereas sub class is a class that does the inheriting. 70. What modifiers may be used with top-level class? public, abstract and final can be used for top-level class. 71. What are inner class and anonymous class? Inner class: classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private. Anonymous class: Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors. 72. What is a package? A package is a collection of classes and interfaces that provides a high-level layer of access protection and name space management. 73. What is a reflection package? java. lang. reflect package has the ability to analyse itself in runtime. 74. What is interface and its use?

11 | P a g e

www.einterviewquestions.com
Interface is similar to a class which may contain methods signature only but not bodies and it is a formal set of method and constant declarations that must be defined by the class that implements it. Interfaces are useful for: a)Declaring methods that one or more classes are expected to implement b)Capturing similarities between unrelated classes without forcing a class relationship. c)Determining an objects programming interface without revealing the actual body of the class. 75. What is an abstract class? An abstract class is a class designed with implementation gaps for subclasses to fill in and is deliberately incomplete. 76. What is the difference between Integer and int? a) Integer is a class defined in the java. lang package, whereas int is a primitive data type defined in the Java language itself. Java does not automatically convert from one to the other. b) Integer can be used as an argument for a method that requires an object, whereas int can be used for calculations. 77. What is a cloneable interface and how many methods does it contain? It is not having any method because it is a TAGGED or MARKER interface. 78. What is the difference between abstract class and interface? a) All the methods declared inside an interface are abstract whereas abstract class must have at least one abstract method and others may be concrete or abstract. b) In abstract class, key word abstract must be used for the methods whereas interface we need not use that keyword for the methods. c) Abstract class must have subclasses whereas interface cant have subclasses. 79. Can you have an inner class inside a method and what variables can you access? Yes, we can have an inner class inside a method and final variables can be accessed. 80. What is the difference between String and String Buffer? a) String objects are constants and immutable whereas StringBuffer objects are not. b) String class supports constant strings whereas StringBuffer class supports growable and modifiable strings. 81. What is the difference between Array and vector? Array is a set of related data type and static whereas vector is a growable array of objects and dynamic. 82. What is the difference between exception and error?

12 | P a g e

www.einterviewquestions.com
The exception class defines mild error conditions that your program encounters. Exceptions can occur when trying to open the file, which does not exist, the network connection is disrupted, operands being manipulated are out of prescribed ranges, the class file you are interested in loading is missing. The error class defines serious error conditions that you should not attempt to recover from. In most cases it is advisable to let the program terminate when such an error is encountered. 83. What is the difference between process and thread? Process is a program in execution whereas thread is a separate path of execution in a program. 84. What is multithreading and what are the methods for inter-thread communication and what is the class in which these methods are defined? Multithreading is the mechanism in which more than one thread run independent of each other within the process. wait (), notify () and notifyAll() methods can be used for interthread communication and these methods are in Object class. wait() : When a thread executes a call to wait() method, it surrenders the object lock and enters into a waiting state. notify() or notifyAll() : To remove a thread from the waiting state, some other thread must make a call to notify() or notifyAll() method on the same object. 85. What is the class and interface in java to create thread and which is the most advantageous method? Thread class and Runnable interface can be used to create threads and using Runnable interface is the most advantageous method to create threads because we need not extend thread class here. 86. What are the states associated in the thread? Thread contains ready, running, waiting and dead states. 87. What is synchronization? Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time. 88. When you will synchronize a piece of your code? When you expect your code will be accessed by different threads and these threads may change a particular data causing data corruption. 89. What is deadlock? When two threads are waiting each other and cant precede the program is said to be deadlock. 90. What is daemon thread and which method is used to create the daemon thread? Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread. 91. Are there any global variables in Java, which can be accessed by other part of your program? No, it is not the main method in which you define variables. Global variables is not possible because concept of encapsulation is eliminated here.

13 | P a g e

www.einterviewquestions.com
92. How is an interface? An interface is a collection of method declarations and constants. In java interfaces are used to achieve multiple inheritance. It sets a behavioral protocol to all implementing classes. 93. What is an abstract class? An abstract class is an incomplete class. An abstract class is defined with the keyword abstract. We cannot create an object of the abstract class because it is not complete. It sets a behavioural protocol for all its child classes. 94. How will you define an interface? An interface is defined with the keyword interface. Eg: public interface MyInterface { } 95. How will you define an abstract class? An abstract class is defined with the keyword abstract Eg: public abstract class MyClass {} 96. What is any an anonymous class? An anonymous class is a local class with no name. 97. What is a JVM heap? The heap is the runtime data area from which memory for all class instances and arrays is allocated. The heap may be of a fixed size or may be expanded. The heap is created on virtual machine start-up. 98. What is difference between string and StringTokenizer? StringTokenizer as its name suggests tokenizes a String supplied to it as an argument to its constructor and the character based on which tokens of that string are to be made. The default tokenizing character is space " ". 99. What is the difference between array and ArrayList? Array is collection of same data type. Array size is fixed, It cannot be expanded. But ArrayList is a growable collection of objects. ArrayList is a part of Collections Framework and can work with only objects. 100. What is difference between java.lang .Class and java.lang.ClassLoader? What is the hierarchy of ClassLoader \? Class 'java.lang.Class'represent classes and interfaces in a running Java application. JVM construct 'Class' object when class in loaded. Where as a ClassLoader is also a class which loads the class files into memory in order for the Java programs to execute properly. The hierarchy of ClassLoaders is: Bootstrap ClassLoaders Extensive ClassLoaders System Classpath ClassLoaders Application ClassLoaders

101. What is daemon thread?

14 | P a g e

www.einterviewquestions.com
Theards which are running on the background are called deamon threads. daemon thread is a thread which doesn't give any chance to run other threads once it enters into the run state it doesn't give any chance to run other threads. Normally it will run forever, but when all other non-daemon threads are dead, daemon thread will be killed by JVM. 102. What is a green thread? Native threads can switch between threads pre-emptively. Green threads switch only when control is explicitly given up by a thread ( Thread.yield(), Object.wait(), etc.) or a thread performs a blocking operation (read(), etc.). On multi-CPU machines, native threads can run more than one thread simultaneously by assigning different threads to different CPUs. Green threads run on only one CPU. Native threads create the appearance that many Java processes are running: each thread takes up its own entry in the process table. One clue that these are all threads of the same process is that the memory size is identical for all the threads - they are all using the same memory. The process table is not infinitely large, and processes can only create a limited number of threads before running out of system resources or hitting configured limits. 103. What is volatile variable? A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in "main" memory. Volatile modifier requests the JVM to always access the shared copy of the variable so the its most current value is always read. 104. Why java does not support multiple inheritance? Because the multiple inheritance causes the redundancy. Also we cannot solve diamond problem. 105. What is diamond problem? The diamond problem is an ambiguity that can occur when a class multiply inherits from two classes that both descend from a common super class 106. How many JVM's we can run in a system? Any number of JVMs can run in a system. Whenever we issue the command 'java' a new JVM will start. 107. Why Java is not 100% pure object oriented language? Because java uses primitives. 108. Why ArrayList is faster than Vector? Because Vector is synchronized. Synchronization reduces the performance. 109. What is the security mechanism used in java? A Java uses sand box security model. 110. What is sandbox? A sandbox is a security mechanism for safely running programs. The sandbox typically provides a tightly-controlled set of resources for guest programs to run in, such as scratch space on disk and memory.

15 | P a g e

www.einterviewquestions.com
111. What is phantom memory? Phantom memory is the memory that does not exist in reality. 112. What is reflection? Reflection is the process of finding out the different features of a class dynamically. 113. What are the differences between JIT and HotSpot? The Hotspot VM is a collection of techniques, the most important of which is called adaptive optimization. The original JVMs interpreted byte codes one at a time. Secondgeneration JVMs added a JIT compiler, which compiles each method to native code upon first execution, then executes the native code. Thereafter, whenever the method is called, the native code is executed. The adaptive optimization technique used by Hotspot is a hybrid approach, one that combines byte code interpretation and run-time compilation to native code. Hotspot, unlike a regular JIT compiling VM, doesn't do "premature optimization" 114. What are the advantages and disadvantages of reference counting in garbage collection? An advantage of this scheme is that it can run in small chunks of time closely linked with the execution of the program. These characteristic makes it particularly suitable for realtime environments where the program can't be interrupted for very long time. A disadvantage of reference counting is that it does not detect cycles. A cycle is two or more objects that refer to one another. Another disadvantage is the overhead of incrementing and decrementing the reference count each time. Because of these disadvantages, reference counting currently is out of favour. 115. How would you implement a thread pool? The ThreadPool class is a generic implementation of a thread pool, which takes the following input Size of the pool to be constructed and name of the class which implements Runnable (which has a visible default constructor) and constructs a thread pool with active threads that are waiting for activation. Once the threads have finished processing they come back and wait once again in the pool. 116. What is the difference between throw and throws clause? throw is used to throw an exception manually, where as throws is used in the case of checked exceptions, to tell the compiler that we haven't handled the exception, so that the exception will be handled by the calling function. 117. What is JAR file? A JAR file (short for Java Archive) is a ZIP file used to distribute a set of Java classes. It is used to store compiled Java classes and associated metadata that can constitute a program. 118. What is a classloader? A class loader is an object that is responsible for loading classes. 119. What is the difference between Comparable and Comparator? The Comparable is for natural ordering and Comparator is for custom ordering. But we can override the compareTo method of comparable interface to give a custom ordering.

16 | P a g e

www.einterviewquestions.com
120. What is the difference between List, Set and Map? Set is a collection that has no duplicate elements. A List is a collection that has an order associated with its elements. A map is a way of storing key/value pairs. The way of storing a Map is similar to two-column table. 121. What is the difference between Exception and Error? Error is unrecoverable. 122. What is meant by Open Source? In general, open source refers to any program whose source code is made available for use or modification as users or other developers see fit. Open source software is usually developed as a public collaboration and made freely available. 123. How do you send data from an applet to Servlet ? What are the steps involved in it? You can use the java.net.URLConnection and java.net.URL classes to open a standard HTTP connection to the web server. The server then passes this information to the servlet in the normal way. Basically, the applet pretends to be a web browser, and the servlet doesn't know the difference. As far as the servlet is concerned, the applet is just another HTTP client. 124. What is polymorphism? It is the ability of an object to behave differently on different situations for the same message. 125. What is a class, member and local variable? Variables declared within a method are local variables. Variables declared within the class are member variables. Variables declared within the class with static modifier are class variables. 126. How do I convert a numeric IP address like 66.29.36.130 into a hostname like www.javacertificate.net? A String hostname = InetAddress.getByName("66.29.36.130").getHostName(); 127. What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. We cannot invoke a constructor directly. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 128. What are the different inner classes types? There are mainly four types available. They are Member classes, Nested top-level classes, Local classes, and Anonymous classes 129. What is a Nested top-level class? A class declared within a class with static modifier is called nested top level class. Any class outside the declaring class can access the nested top level class with the declaring class dot nested top level class. Top-level inner classes have access to static variables only.

17 | P a g e

www.einterviewquestions.com
130. What are Member classes? A class declared inside a class without static modifier is called member class. Member classes are just like any other member methods or member variables. 131. What is Local inner classes? Local inner classes are class declared inside a block of code. They are visible only within the block of their declaration. 132. Can a top level class be private or protected? No. A top level class cannot be private or protected. It can have either "public" or no modifier. 133. How will you invoke any external process in Java? By using Runtime.getRuntime().exec(.) 134. What is a package? To group set of classes into a single unit is known as packaging. Packages provides wide namespace visibility. 135. What is the use of assert keyword? Assert keyword validates certain expressions. It replaces the if block effectively and throws an AssertionError on failure. The assert keyword should be used only for critical arguments (means without that the method does nothing). 136. What is composition? Holding the reference of the other class within some other class is known as composition. 137. What is aggregation? It is a special type of composition. If you expose all the methods of a composite class and route the method call to the composite method through its reference, then it is called aggregation 138. What are the methods in Object? A clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll, toString 139. What is the relationship between synchronized and volatile keyword? The JVM is guaranteed to treat reads and writes of data of 32 bits or less as atomic. For long or double variable, programmers should take care in multithreading environment. Either put these variables in a synchronized method or block, or declare them volatile. 140. What factors are used to decide using synchronized or volatile? You can make a variable synchronized or volatile under the following cases: 1) If you are not updating many variables often in a multithread environment, consider using volatile. 2) If you are updating many variables, consider using synchronized, because using volatile might be slower.

18 | P a g e

www.einterviewquestions.com
141. What are the drawbacks of inheritance? Since inheritance inherits everything from the super class and interface, it may make the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation. In addition, the inheritance may make peers hardly understand your code if they don't know how your super-class acts. 142. What is the difference between static synchronized and synchronized methods? Both are synchronized methods. One is instance method, the other is class method. Method with static modifier is a class method. That means the method belongs to class itself and can be accessed directly with class name and is also called Singleton design. The method without static modifier is an instance method. That means the instance method belongs to its object. Every instance of the class gets its own copy of its instance method. 143. What is the purpose of the Runtime class? The purpose of the Runtime class is to provide access to the Java runtime system. 144. What is the purpose of the System class? The purpose of the System class is to provide access to system resources. 145. Does the code in finally block get executed if there is an exception and a return statement in a catch block? If an exception occurs and there is a return statement in catch block, the finally block is still executed. The finally block will not be executed when the System.exit(1) statement is executed earlier or the system shut down earlier or the memory is used up earlier before the thread goes to finally block. 146. Considering notepad/IE or any other thing as process, What will happen if you start notepad or IE 3 times? Where 3 processes are started or 3 threads are started? 3 processes will start. 147. What are the restrictions placed on the values of each case of a switch statement? At compile time, each case values of switch statement must evaluate to a an int value. 148. If aaaa is an array then why aaaa.length why not aaaa.length()? Because length is a property not a method. 149. What is dynamic typing? Dynamic typing means type errors are detected at run time by checking the actual data types of the values against the required data types 150. What is static typing? Static typing means type errors are detected at compile time by checking the inferred data type is a subtype of the required type 151. What is HashMap and Map? Map is Interface and HashMap is class that implements that.

19 | P a g e

www.einterviewquestions.com
152. What is an Object and how do you allocate memory to it? Object is an instance of a class and it is a software unit that combines a structured set of data with a set of operations for inspecting and manipulating that data. When an object is created using new operator, memory is allocated to it. 153. What is UNICODE? Unicode is used for internal representation of characters and strings and it uses 16 bits to represent each other. 154. What is adapter class? An adapter class provides a default implementation of all methods in an event listener interface. Adapter classes are useful when you want to process only some of the events that are handled by a particular event listener interface. You can define a new class by extending one of the adapter classes and implementing only those events relevant to us. 155. What is a stream and what are the types of Streams and classes of the Streams? A stream is a flow of data from one direction to another. . There are two types of Streams Byte Streams: Provide a convenient means for handling input and output of bytes. And Character Streams: Provide a convenient means for handling input & output of characters. 156. What is the difference between TCP/IP and UDP? TCP/IP is a two-way communication between the client and the server and it is a reliable and there is a confirmation regarding reaching the message to the destination. UDP is a one-way communication only between the client and the server and it is not a reliable and there is no confirmation regarding reaching the message to the destination. 157. What is Inter-Thread communication? Exchange of information between two threads. 158. What is a policy? It's an abstract class for representing the system security policy for a Java application environment (specifying which permissions are available for code from various sources). Java security properties file resides in <JAVAHOME>/ lib/security/java.security directory. 159. What is a thread group? A thread group is a data structure that controls the state of collection of thread as a whole managed by the particular runtime environment. 160. Why is UTFDataFormatException thrown by DataOutputStream.writeUTF() when serializing a String? DataOutputStream.writeUTF() does not support writing out strings larger than 64K. The first two bytes of a UTF string in the stream are the length of the string. If a java.lang.String is larger than 64K, it needs to be stored in the stream by an alternative method rather than depending on the default method of storing a String in the stream, writeUTF. 161. Why is OutOfMemoryError thrown after writing a large number of objects into an ObjectOutputStream?

20 | P a g e

www.einterviewquestions.com
The ObjectOutputStream maintains a table mapping objects written into the stream to a handle. The first time an object is written to a stream, its contents are written into the stream; subsequent writes of the object result in a handle to the object being written into the stream. This table maintains references to objects that might otherwise be unreachable by an application, thus, resulting in an unexpected situation of running out of memory. A call to the ObjectOutputStream.reset() method resets the object/handle table to its initial state, allowing all previously written objects to be eligible for garbage collection. 162. How can I get the serialVersionUID of a class? By running the serialver tool with the name of the class as the command line argumet, as shown in the example that follows:serialver java.lang.String 163. What is serialVersionUID? The serialVersionUID is a universal version identifier for a Serializable class. Deserialization uses this identifier number to ensure that a loaded class corresponds to a serialized object. 164. What is abstraction? An abstraction is an idea, concept, or word which defines the phenomena which make up the concrete events or things which the abstraction refers to, the referents. 165. What is encapsulation? Encapsulation describes the ability of an object to hide its data and methods from the rest of the world 166. What is inheritance? Inheritance is the ability to create new classes based on existing classes. It is useful to reuse existing code.

21 | P a g e

Das könnte Ihnen auch gefallen