Академический Документы
Профессиональный Документы
Культура Документы
Q6 : How does Garbage Collection prevent a Java application from going out of memory?
Q8 : What is the tradeoff between using an unordered array versus an ordered array?
Q23 : What is the difference between Serial and Throughput Garbage collector?
Why does String pose a threat to security when it comes to passwords? It feels inconvenient to use char[]?
Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how?
Will
Q32 : Given two double values d1, d2, what is the most reliable way to test their equality?
Q35 : Why ArrayList are preferable in many more use-cases than LinkedList?
Q37 : Provide some examples when a finally block won't be executed in Java?
Consider:
Topic: Java
Method overloading in Java occurs when two or more methods in the same class have the exact
same name, but different parameters.
class Dog{
public void bark(){
System.out.println("woof ");
}
//overloading method
public void bark(int num){
for(int i=0; i<num; i++)
System.out.println("woof ");
}
}
On the other hand, method overriding is defined as the case when a child class redefines the same
method as a parent class. 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.
class Dog{
public void bark(){
System.out.println("woof ");
}
}
class Hound extends Dog{
public void sniff(){
System.out.println("sniff ");
}
Topic: Java
Java provides and supports the creation both of abstract classes and interfaces. Both implementations share
some common characteristics, but they differ in the following features:
All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain
both abstract and non-abstract methods.
A class may implement a number of Interfaces, but can extend only one abstract class.
In order for a class to implement an interface, it must implement all its declared methods. However,
a class may not implement all declared methods of an abstract class. Though, in this case, the sub-
class must also be declared as abstract.
Abstract classes can implement interfaces without even providing the implementation of interface
methods.
Variables declared in a Java interface is by default final. An abstract class may contain non-final
variables.
Members of a Java interface are public by default. A member of an abstract class can either be
private, protected or public.
An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be
instantiated, but can be invoked if it contains a main method.
Topic: Java
1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded
applications, as unsynchronized Objects typically perform better than synchronized ones.
2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null
values.
3. One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable
iteration order (which is insertion order by default), you could easily swap out the HashMap for a
LinkedHashMap. This wouldn't be as easy if you were using Hashtable.
Topic: Java
An Error "indicates serious problems that a reasonable application should not try to catch."
An Exception "indicates conditions that a reasonable application might want to catch."
Topic: Java
The name reflection is used to describe code which is able to inspect other code in the same system (or
itself) and to make modifications at runtime.
For example, say you have an object of an unknown type in Java, and you would like to call a 'doSomething'
method on it if one exists. Java's static typing system isn't really designed to support this unless the object
conforms to a known interface, but using reflection, your code can look at the object and find out if it has a
method called 'doSomething' and then call it if you want to.
Method method = foo.getClass().getMethod("doSomething", null);
method.invoke(foo, null);
Q6: How does Garbage Collection prevent a Java application from going out of memory?
★★
Topic: Java
It doesn’t! Garbage Collection simply cleans up unused memory when an object goes out of scope and is no
longer needed. However an application could create a huge number of large objects that causes an
OutOfMemoryError.
Topic: Java
Q8: What is the tradeoff between using an unordered array versus an ordered array?
★★★
Topic: Java
The major advantage of an ordered array is that the search times have time complexity of O(log n),
compared to that of an unordered array, which is O (n). The disadvantage of an ordered array is that the
insertion operation has a time complexity of O(n), because the elements with higher values must be moved
to make room for the new element. Instead, the insertion operation for an unordered array takes constant
time of O(1).
Topic: Java
The JVM has a heap that is the runtime data area from which memory for all class instances and arrays is
allocated. It is created at the JVM start-up. Heap memory for objects is reclaimed by an automatic memory
management system which is known as a garbage collector. Heap memory consists of live and dead objects.
Live objects are accessible by the application and will not be a subject of garbage collection. Dead objects
are those which will never be accessible by the application, but have not been collected by the garbage
collector yet. Such objects occupy the heap memory space until they are eventually collected by the garbage
collector.
Topic: Java
The throw keyword is used to explicitly raise a exception within the program. On the contrary, the throws
clause is used to indicate those exceptions that are not handled by a method. Each method must explicitly
specify which exceptions does not handle, so the callers of that method can guard against possible
exceptions. Finally, multiple exceptions are separated by a comma.
Topic: Java
Java is always pass-by-value. Unfortunately, when we pass the value of an object, we are passing the
reference to it. There is no such thing as "pass-by-reference" in Java. This is confusing to beginners.
Dog myDog;
you're essentially passing the address of the created Dog object to the foo method.
Topic: Java
is a serializable object (that is, it implements java.io.Serializable, and does so correctly), that
has "properties" whose getters and setters are just methods with certain names (like, say, getFoo()
is the getter for the "Foo" property), and
has a public 0-arg constructor (so it can be created at will and configured by setting its properties).
There is no syntactic difference between a JavaBean and another class - a class is a JavaBean if it follows
the standards.
Topic: Java
Yes: enums have tight instance controls that allows you to use == to compare instances. Here's the
guarantee provided by the language specification.
Topic: Java
As a reminder, it needs to be said that generally, == is NOT a viable alternative to equals. When it is,
however (such as with enum), there are two important differences to consider:
Q15: What is the main difference between StringBuffer and StringBuilder? ★★★
Topic: Java
StringBuffer is synchronized, StringBuilder is not. When some thing is synchronized, then multiple
threads can access, and modify it with out any problem or side effect. StringBuffer is synchronized,
so you can use it with multiple threads with out any problem.
StringBuilder is faster than StringBuffer because it's not synchronized. Using synchronized methods
in a single thread is overkill.
Topic: Java
The transient keyword in Java is used to indicate that a field should not be part of the serialization.
By default, all of object's variables get converted into a persistent state. In some cases, you may want to
avoid persisting some variables because you don't have the need to persist those variables. So you can
declare those variables as transient. If the variable is declared as transient, then it will not be persisted.
Topic: Java
Details:
Answer:
The static initializer is a static {} block of code inside java class, and run only one time before the constructor
or main method is called. If you had to perform a complicated calculation to determine the value of x — or if
its value comes from a database — a static initializer could be very useful.
Consider:
class StaticInit {
public static int x;
static {
x = 32;
}
// other class members such as constructors and
// methods go here...
}
Topic: Java
Java has no way of making a top-level class static but you can simulate a static class like this:
Declare your class final - Prevents extension of the class since extending a static class makes no
sense
Make the constructor private - Prevents instantiation by client code as it makes no sense to
instantiate a static class
Make all the members and functions of the class static - Since the class cannot be instantiated no
instance methods can be called or instance fields accessed
Note that the compiler will not prevent you from declaring an instance (non-static) member. The
issue will only show up if you attempt to call the instance member
Q19: What do the ... dots in the method parameters mean? ★★★
Topic: Java
Details:
What do the 3 dots in the following method mean?
Answer:
That feature is called varargs, and it's a feature introduced in Java 5. It means that function can receive
multiple String arguments:
myMethod("foo", "bar");
myMethod("foo", "bar", "baz");
myMethod(new String[]{"foo", "var", "baz"}); // you can eve
Topic: Java
It is not possible to do something like you want in Java. Different Java applications will use different JVM's
fully separating themselves into different 'blackbox'es. However, you have 2 options:
Use sockets (or channels). Basically one application will open the listening socket and start waiting
until it receives some signal. The other application will connect there, and send signals when it had
completed something. I'd say this is a preferred way used in 99.9% of applications.
You can call winapi from Java (on windows).
Topic: Java
The JIT is the JVM’s mechanism by which it can optimize code at runtime.
JIT means Just In Time. It is a central feature of any JVM. Among other optimizations, it can perform code
inlining, lock coarsening or lock eliding, escape analysis etc.
The main benefit of the JIT is on the programmer’s side: code should be written so that it just works; if the
code can be optimized at runtime, more often than not, the JIT will find a way.
Q22: What is the difference between a synchronized method and a synchronized block?
★★★★
Topic: Java
Q23: What is the difference between Serial and Throughput Garbage collector? ★★★★
Topic: Java
Topic: Java
Topic: Java
Details:
Why does String pose a threat to security when it comes to passwords? It feels inconvenient to use char[]?
Answer:
Topic: Java
Topic: Java
Topic: Java
Details:
Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how?
Answer:
Topic: Java
Q30: Explain a use case for the Builder Design Pattern ★★★★
Topic: Java
Topic: Java
Details:
Will
Answer:
Q32: Given two double values d1, d2, what is the most reliable way to test their equality?
★★★★
Topic: Java
Topic: Java
Topic: Java
Q35: Why ArrayList are preferable in many more use-cases than LinkedList? ★★★★★
Topic: Java
Topic: Java
Q37: Provide some examples when a finally block won't be executed in Java? ★★★★★
Topic: Java
Topic: Java
Details:
Consider:
Answer:
Topic: Java
Topic: Java
Topic: Java
Topic: Java