


























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























































●● Why do we need to implement the Clonable interface? Answer: At runtime it would throw the CloneNotSupportedException if we don't implement the Cloneable interface. A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. ●● What is a marker interface? Answer: A Marker interface, has no method. Serializable, Clonable are marker interfaces. ●● How do you clone an object? Answer: MyClone a = (MyClone) c.clone()
Answer: Annotations can convey metadata about the class to its consumers without creating a separate type for it. Annotations let you pass information to classes that "consume" it. ●● What is Double-Checked Locking? Answer: To reduce the overhead of acquiring a lock by first testing the locking criterion without actually acquiring the lock. ●● How do you make an object serializable? Answer: The class must implement the java.io.Serializable interface ●● Java object serialization is performed using which classes? Answer: Java object serialization (writing) is done with the ObjectOutputStream and deserialization (reading) is done with the ObjectInputStream. ●● What is serialVersionUID? Answer: In addition to implementing the Serializable interface, a class intended for serialization should also contain a private static final long variable named serialVersionUID. The serialVersionUID variable is used by Java's object serialization API to determine if a deserialized object was serialized (written) with the same version of the class, as it is now attempting to deserialize it into. If you make changes to the class that affect serialization, you should also change its serialVersionUID value.
●● what is the size of double? Answer: 64 bits ●● What is the range of byte Answer: - 128 to 127 ●● What is the range of short? Answer: - 32,768 to 32, ●● What is the range of int? Answer: - 2,1 billion to 2,1billion ●● How do you check if a number is even? Answer: check if number AND 1 is 0 ●● How do you test if the n-th bit is set? Answer: Shift n times to the right and then AND ●● How can you set the n-th bit Answer: Shift n times to the left and OR
●● How can you unset the nth bit Answer: Shift n times to the left and invert, then AND ●● How can you toggle the nth Bit? Answer: Shift n times to the left and XOR ●● How can you turn off the rightmost 1Bit? Answer: AND with (number - 1) ●● How can you return only 1 or 0 for the rightmost 1Bit value? Answer: number & ( - number) ●● How can you also declare - x bitwise? Answer: ~x + 1 ●● What is ~0 bitwise? Answer: 0 ●● How can you right propagate the rightmost 1-bit? Answer: number OR (number - 1), does not work for 0 ●● What is - 1 in twos complement?
return 1; //if this > that return 0; //if this == that return - 1; //if this < that } } ●● How can you sort with different rules on the same array? Answer: use a comparator: ●● What can you use Arrays.parallelPrefix for? Answer: cumulate values in an array ●● How can you find a value fast in a sorted array? Answer: Arrays.binarysearch works on all primitives and on objects using a comparator ●● How can you compare arrays? Answer: Arrays.equals : two arrays are equal if they contain the same elements in the same order ●● How do you recognize overflow in two complement? Answer: Adding 2 positive results in negative and vice versa
●● What is the largest number binary representation in a two complement? Answer: ... ●● What is the smallest number binary representation in two complement? Answer: ... ●● How can you assign a specific value to each element of an array or subarray? Answer: public static void Arrays.fill ●● How can you create a duplicate of an array? Answer: Arrays.copyOf copies until a given length ●● How can you create a duplicate of an array range? Answer: Arrays.copyOfRange ●● How can you transform a List into an array? Answer: Collections.toArray ●● How can you transform an array into a List? Answer: Arrays.asList
Answer: method synchronization and block synchronization ●● How is the intrinsic lock used? Answer: Methods declared as synchronized and blocks that synchronize on the this reference both use the object as monitor (that is, its intrinsic lock) ●● What is the private lock object idiom? Answer: This idiom uses the intrinsic lock associated with the instance of a private final java.lang.Object declared within the class instead of the intrinsic lock of the object itself. This idiom requires the use of synchronized blocks within the class's methods rather than the use of synchronized methods. Lock contention between the class's methods is prevented. ●● Declaring a volatile Java variable means: Answer: 1) The value of this variable will never be cached thread- locally: all reads and writes will go straight to "main memory";
Answer: an because accessing a volatile variable never holds a lock, it is not suitable for cases where we want to read-update-write as an atomic operation (unless we're prepared to "miss an update"); ●● Attempting to synchronize on a null object will Answer: throw a NullPointerException. ●● May a volatile variable that is an object reference be null? Answer: Yes! (because you're effectively synchronizing on the reference, not the actual object). ●● May a primitive variable may be declared volatile or synchronized? Answer: Volatile, but not synchronized. ●● Are you holding a lock when you access a volatile variable? Answer: access to a volatile variable never has the potential to block: we're only ever doing a simple read or write, so unlike a synchronized block we will never hold on to any lock; ●● Are you holding a lock when you access a synchronized method?
Answer: If Thread A writes to a volatile variable and Thread B subsequently reads the same volatile variable, then all variables visible to Thread A before writing the volatile variable, will also be visible to Thread B after it has read the volatile variable. Instructions before and after can be reordered, but the volatile read or write cannot be mixed with these instructions. Whatever instructions follow a read or write of a volatile variable are guaranteed to happen after the read or write. ●● Give an example of a race condition when using a volatile variable. Answer: As soon as a thread needs to first read the value of a volatile variable, and based on that value generate a new value for the shared volatile variable, a volatile variable is no longer enough to guarantee correct visibility. The short time gap in between the reading of the volatile variable and the writing of its new value, creates an race condition where multiple threads might read the same value of the volatile variable, generate a new value for the variable, and when writing the value back to main memory - overwrite each other's values. ●● When is volatile Enough? Answer: n case only one thread reads and writes the value of a volatile variable and other threads only read the variable, then the reading threads are guaranteed to see the latest value written to the volatile variable. Without making the variable volatile, this would not be guaranteed. The volatile keyword is guaranteed to work on 32 bit and 64 variables.
●● What is Context Switching Overhead Answer: When a CPU switches from executing one thread to executing another, the CPU needs to save the local data, program pointer etc. of the current thread, and load the local data, program pointer etc. of the next thread to execute. This switch is called a "context switch". The CPU switches from executing in the context of one thread to executing in the context of another. Context switching isn't cheap. You don't want to switch between threads more than necessary. ●● Name some Concurrency Models Answer: Parallel Workers ●● What is the parallel worker concurrency model? Answer: A delegator distributes the incoming jobs to different workers. Each worker completes the full job. The workers work in parallel, running in different threads, and possibly on different CPUs. ●● Parallel Workers Advantages Answer: it is easy to understand. To increase the parallelization of the application you just add more workers.
Answer: ... ●● Describe the idea behind event driven concurrency! Answer: The system's workers react to events occurring in the system, either received from the outside world or emitted by other workers. ●● Explain the actor model! Answer: Each worker is called an actor. Actors can send messages directly to each other. Messages are sent and processed asynchronously. Actors can be used to implement one or more job processing assembly lines, as described earlier. ●● Explain the channel model! Answer: Workers do not communicate directly with each other. Instead they publish their messages (events) on different channels. Other workers can then listen for messages on these channels without the sender knowing who is listening. ●● Explain advantages of channel vs actor model! Answer: In the channel model, workers do not communicate directly with each other. Instead they publish their messages (events) on different channels. Other workers can then listen for messages on these channels without the sender knowing who is listening. ●● Name some advantages of event driven models!
Answer: No shared state between workers ●● Explain the consequences of workers NOT sharing state with each other! Answer: They can be implemented without having to think about all the concurrency problems that may arise from concurrent access to shared state. This makes it much easier to implement workers. You implement a worker as if it was the only thread performing that work - essentially a singlethreaded implementation. ●● Explain the consequences of workers having state. Answer: If workers can be stateful, they have to be sure there are no other threads modify their data. They can keep their data in memory, only writing changes back the eventual external systems. A stateful worker can therefore often be faster than a stateless worker. ●● What does Functional Parallelism mean? Answer: It is a third concurrency model. Functions can be seen as "agents" or "actors" that send messages to each other (call each other). ●● Explain how Functional Parallelism avoids race conditions? Answer: All parameters passed to the function are copied, so no entity outside the receiving function can manipulate the data. This copying is essential to avoiding race conditions on the shared data. This makes the function execution similar to an atomic operation. Each function call can be executed independently of any other function call.
Answer: An application is making progress on more than one task at the same time (concurrently). It does not completely finish one task before it begins the next. ●● What exactly is parallelism? Answer: Application splits its tasks up into smaller subtasks which can be processed in parallel, for instance on multiple CPUs at the exact same time. ●● What is the difference between static nested and inner classes? Answer: Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. ●● How do you create a Thread? Answer: 1) Extend Thread (java.lang.Thread)
Answer: For instance, if creating a subclass of Thread that can execute more than one Runnable. This is typically the case when implementing a thread pool. ●● What is the most common mistake when starting a thread? Answer: To call the run() method of the Thread instead of start(). in that case, the run() method is executed by the thread that created the thread. ●● What is a critical section? Answer: A section of code that is executed by multiple threads and where the sequence of execution for the threads makes a difference in the result ●● What is a race condition? Answer: When the result of multiple threads executing a critical section may differ depending on the sequence in which the threads execute. ●● How can you prevent race conditions? Answer: Make sure that the critical section is executed as an atomic instruction. So that when a thread is executing it, no other can execute it until the first thread has left the critical section. ●● How can you avoid race conditions? Answer: By proper thread synchronization in critical sections.