Search Interview Questions | ![]() ![]() Click here and help us by providing the answer. ![]() Click Correct / Improve and please let us know. |
|
| ||||
Interview Questions and Answers - Order By Rating | ||||
![]() ![]() | ||||
| ||||
Ans. There can be two different elements with the same hashcode. When two elements have the same hashcode then Java uses the equals to further differentation. So there can be one or two objects depending on the content of the objects. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. They both represent the long primitive type. They are exactly the same. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. No | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. "this" keyword is a reference to the current object and can be used for following - 1. Passing itself to another method. 2. Referring to the instance variable when local variable has the same name. 3. Calling another constructor in constructor chaining. | ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
![]() | ||||
| ||||
Ans. 1. when an OS wants to start running program it creates new process means a process is a program that is currently executing and every process has at least one thread running within it. 2). A thread is a path of code execution in the program, which has its own local variables, program counter(pointer to current execution being executed) and lifetime. 3. When the JavaVirtual Machine (JavaVM, or just VM) is started by the operating system, a new process is created. Within that process, many threads can be created. 4. Consider an example : when you open Microsoft word in your OS and you check your task manger then you can see this running program as a process. now when you write something in opened word document, then it performs more than one work at same time like it checks for the correct spelling, it formats the word you enter , so within that process ( word) , due to different path execution(thread) all different works are done at same time. 5. Within a process , every thread has independent path of execution but there may be situation where two threads can interfere with each other then concurrency and deadlock come is picture. 6. like two process can communicate ( ex:u open an word document and file explorer and on word document you drag and drop another another file from file explorer), same way two threads can also communicate with each other and communication with two threads is relatively low. 7. Every thread in java is created and controlled by unique object of java.lang.Thread class. 8. prior to jdk 1.5, there were lack in support of asynchronous programming in java, so in that case it was considered that thread makes the runtime environment asynchronous and allow different task to perform concurrently. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. By putting code within static method. With Java 6 and earlier versions, even static block can be used. | ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
![]() | ||||
| ||||
Ans. 1. Multithreading provides better interaction with the user by distribution of task 2. Threads in Java appear to run concurrently, so it provides simulation for simultaneous activities.The processor runs each thread for a short time and switches among the threads to simulate sim-ultaneous execution (context-switching) and it make appears that each thread has its own processor.By using this feature, users can make it appear as if multiple tasks are occurring simultaneously when, in fact, each is running for only a brief time before the context is switched to the next thread. 3. We can do other things while waiting for slow I/O operations.In the java.iopackage, the class InputStreamhas a method, read(), that blocks until a byte is read from the stream or until an IOExceptionis thrown. The thread that executes this method cannot do anything elsewhile awaiting the arrival of another byte on the stream. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. No. Java doesn't allow multi thread access to object constructors so synchronization is not even needed. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Yes , for HashMap. HashMap implements Map interface. HashMap allows one null key and any number of null values. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. The value received from hashcode() is used as bucket number for storing elements. This bucket number is the address of the element inside the set/map. when you do contains() then it will take the hashcode of the element, then look for the bucket where hashcode points to and if more than 1 element is found in the same bucket (multiple objects can have the same hashcode) then it uses the equals() method to evaluate if object are equal, and then decide if contain() is true or false, or decide if element could be added in the set or not. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. class A { void test() { System.out.println("test() method"); } } class B { void test() { System.out.println("test() method"); } } Suppose if Java allows multiple inheritance like this, class C extends A, B { } A and B test() methods are inheriting to C class. So which test() method C class will take? As A & B class test() methods are different , So here we would Facing Ambiguity. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. Threads enters to waiting state or block on I/O because other threads can execute while the I/O operations are performed. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Transient variables are variable that cannot be serialized. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. When a object invokes yield() it returns to ready state. But when an object invokes sleep() method enters to not ready state. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. They are wrappers to primitive data types. They allow us to access primitives as objects. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. In preemptive scheduling, highest priority task continues execution till it enters a not running state or a higher priority task comes into existence. In time slicing, the task continues its execution for a predefined period of time and reenters the pool of ready tasks. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. Ready state. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializable Exception. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
![]() | ||||
| ||||
Ans. 1. String Pool - When a string is created and if it exists in the pool, the reference of the existing string will be returned instead of creating a new object. If string is not immutable, changing the string with one reference will lead to the wrong value for the other references. Example - String str1 = "String1"; String str2 = "String1"; // It doesn't create a new String and rather reuses the string literal from pool // Now both str1 and str2 pointing to same string object in pool, changing str1 will change it for str2 too 2. To Cache its Hashcode - If string is not immutable, One can change its hashcode and hence it's not fit to be cached. 3. Security - String is widely used as parameter for many java classes, e.g. network connection, opening files, etc. Making it mutable might possess threats due to interception by the other code segment. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. Cookie and Session are used to store the user information. Cookie stores user information on client side and Session does it on server side. Primarily, Cookies and Session are used for authentication, user preferences, and carrying information across multiple requests. Session is meant for the same purpose as the cookie does. Session does it on server side and Cookie does it on client side. One more thing that quite differentiates between Cookie and Session. Cookie is used only for storing the textual information. Session can be used to store both textual information and objects. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. 1. Stack Segment - Contains primitives, Class / Interface names and references. 2. Heap Segment - Contains all created objects in runtime, objects only plus their object attributes (instance variables), Static variables are also stored in heap. 3. Code Segment - The segment where the actual compiled Java bytecodes resides when loaded | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. Code segment. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. When a thread terminates its processing, it enters the dead state. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. An object's lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object's lock. All objects and classes have locks. A class's lock is acquired on the class's Class object. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides. The overriding method may not throw any exceptions that may not be thrown by the overridden method. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
![]() ![]() | ||||