Posts

Showing posts from March, 2019

[18 Feb 2020]Java Multithreading Concurrency

What is the difference between Process and Thread? What are the benefits of multi-threaded programming? What is difference between user Thread and daemon Thread? How can we create a Thread in Java? What are different states in lifecycle of Thread? Can we call run() method of a Thread class? How can we pause the execution of a Thread for specific time? What do you understand about Thread Priority? What is Thread Scheduler and Time Slicing? What is context-switching in multi-threading? How can we make sure main() is the last thread to finish in Java Program? How does thread communicate with each other? Why thread communication methods wait(), notify() and notifyAll() are in Object class? Why wait(), notify() and notifyAll() methods have to be called from synchronized method or block? Why Thread sleep() and yield() methods are static? How can we achieve thread safety in Java? What is volatile keyword in Java Which is more preferred – Synchronized method or Synchronized b...

[19 Feb 2020] Java Collection

[1] Vector vs ArrayList in Java ArrayList and Vectors both implement the List interface and both use (dynamically resizable) arrays for its internal data structure, much like using an ordinary array. Syntax:      ArrayList<T> al = new ArrayList<T>();    Vector<T> v = new Vector<T>(); Major Differences between ArrayList and Vector: Synchronization : Vector is synchronized, which means only one thread at a time can access the code, while arrayList is not synchronized, which means multiple threads can work on arrayList at the same time. For example, if one thread is performing an add operation, then there can be another thread performing a remove operation in a multithreading environment. If multiple threads access arrayList concurrently, then we must synchronize the block of the code which modifies the list structurally, or alternatively allow simple element modifications. Structural modification means addition or deletion of el...

Design a Logistics System

Design a Logistics System (Object Oriented Design). Tell about the different classes and their relationships with each-other. It is not a System Design question, so scope of this question is only to define different classes (with it’s attributes and methods) Solution: Let’s assume we want to design a Logistics System with following basic functionality: • The system can take an order to deliver it to a given destination. • The order will be a list of items and there is a cost of each order to process. • User has to register himself / herself to use this system. • User can track his / her order. • Orders will be shipped by bike or truck, but only a single order will be shipped by a single vehicle. These type of questions are asked in interviews to Judge the Object Oriented Design skill of a candidate. So, first of all we should think about the classes. The main classes will be: 1. User 2. Item 3. Vehicle 4. Location 5. PaymentDetails 6. Order 7. LogisticsSystem The ...

Very Basic Java Programs

[1] Count ways to reach a score using 1 and 2 with no consecutive 2s A cricket player has to score N runs, with condition he can take either 1 or 2 runs only and consecutive runs should not be 2. Find all the possible combination he can take. Examples: Input : N = 4 Output : 4 1+1+1+1, 1+2+1, 2+1+1, 1+1+2 Input : N = 5 Output : 6 This problem is a variation of count number of ways to reach given score in a game and can be solved in O(n) time and O(n) auxiliary space. Below is the recursive solution of above problem. // A simple recursive implementation for // counting ways to reach a score using 1 and 2 with // consecutive 2 allowed   import java.io.*;   class GFG {    static  int CountWays(int n, boolean flag) {     if (n == 0) // base case         return 1;       int sum = 0;       // 2 is not scored last time so we can score either 2 or 1     if (flag == fals...

Basic Java Knowledge

Core Java: Basics of Java Interview Questions 1) What is Java? Java is the high-level, object-oriented, robust, secure programming language, platform-independent, high performance, Multithreaded, and portable programming language. It was developed by James Gosling in June 1991. It can also be known as the platform as it provides its own JRE and API. 2) List the features of Java Programming language. There are the following features in Java Programming Language. Simple: Java is easy to learn. The syntax of Java is based on C++ which makes easier to write the program in it. Object-Oriented: Java follows the object-oriented paradigm which allows us to maintain our code as the combination of different type of objects that incorporates both data and behavior. Portable: Java supports read-once-write-anywhere approach. We can execute the Java program on every machine. Java program (.java) is converted to bytecode (.class) which can be easily run on every machine. Platform...