Saturday, March 6, 2021

Multi-Threading

 Q1. What is Thread?

Ans:  Thread is a lightweight sub-process. it create by using extends Thread class and Implement Runnable interface.

Q. Why do we use Multithreading?

Ans: 

Q. What is Thread Lifecycle in Java?

Ans: Before moving on to creating a thread, we’ll first see the various phases through which the thread has to pass to complete its lifespan.

  • New
  • Runnable
  • Running
  • Non-Runnable (blocked)
  • Terminated



New- A thread is in New state when you create the instance of thread class, but the start() method is yet to get called.

Runnable- The thread is in runnable after executing the start() method. In this stage, it waits for the thread scheduler to run it.

Running- A thread picked by the thread scheduler for execution remains in running state.

Non-Runnable (blocked)- In this state, the thread remains alive, but it is not eligible to run. It may be due to some sleep operation, waiting for any File I/O to finish or in the locked state, etc.

Terminated- A thread is said to be in the terminated state when it’s Run() method exits.


wait() - Tells the current thread to release the lock and go to sleep until some other thread enters the same monitor and calls notify().

notify() - Wakes up the single thread that is waiting on this object's monitor.

notifyAll() - It wakes up all the threads that called wait() on the same object.

Synchronization:- 1. Synchronized is the keyword applicable for methods & blocks but not for classes and variables.

2. If a method or block declared as the Synchronized then at a time only one thread is allow to execute that method or block on the given Object.

3. The main advantages of synchronized keyword is we can resolve data inconsistency problems.

4. But the main disadvantage of synchronized keyword is it increases waiting time of the thread and effects performance of the system.

Friday, March 5, 2021

Basic Programs asked in Interview

 Q. Write a program to find Second largest number in the array?

Ans: Method:-

public static void secondLargestNumber(int arr){

int first = Integer.MIN_VALUE;

int second = Integer.MIN_VALUE;

for(int i=0;i<arr.length;i++){

if(arr[i] > first){

        second = first;

        first = arr[i];

      }else if(arr[i] > second){

        second = arr[i];

      }

}

System.out.println(" Second Largest Number = " + second);

}

Differences and sheet questions

 Q. What are the difference between @Controller and @RestController?

Ans: 1. The @Controller is a common annotation that is used to mark a class as Spring MVC Controller while @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody.

Q. What are the use of @PathVariable?

Ans: @PathVariable is used to bind method parameter to the value of URI template variable

ex: @RequestMapping(value="login/{id}",method=RequestMethod.post)

Q. What are difference between Abstract class and Interface in java?

Ans: 1. Abstract classes have partial implementation whereas Interface have only methods declaration/signature.

2. Abstract classes are used when parent classes need to provide some default behavior(method) and child classes need to provide some specialized behavior(method) whereas Interfaces are used in design phase when multiple behaviors from a subsystem are expected. OR I use interfaces when I see that any subsystem of my design will change frequently for example if my application need to support multiple databases then I will define interfaces and write implementation classes for each database.

Q. What are differences between String and StringBuffer classess?

Ans: 1. String is immutable whereas StringBuffer is mutable.

Q. What are differences between StringBuffer and StringBuilder classess?

Ans: 1. StringBuffer is synchronized whereas StringBuilder is not synchronized.

Q. Does Serializable interface have any method?

Ans: No, It is a marker interfaces.

Q. What is transient variable?

Ans: Attributes defines as transietn will not be serialized.

Q. What type of Variables are declared as transient?

Ans: Calculated filed, memory point and database pointer objects like ResultSet.

Q. Does cloneable interface have any method?

Ans: No, It is a marker interfaces.

Q. What are the difference between Overloading and Overriding?

And: Overloading vs Overriding in Java

-----------------------------------------------------------------------------------

OverloadingOverriding 
1. More than one method with same name, different prototype in name scope is called method overloading1. More than one method with same name, same prototype in different scope is called method overriding
2. In case of method overloading, parameter must be different.2. In case of method overriding, parameter must be same.
3. Method overloading is performed within class3. Method overriding occurs in two classes.
4. In case of method overloading, return type can be same or different.4. In case of method overriding, return type must be same.

Method Overloading:- If a class has multiple method having same name but different in parameter list, it is known as method overloading.

Method Overriding:- If child class have same method as declared in parent class, it is known as method overriding in java

Q. What is immutable object?

Ans: When object values can be initialized once and can never be changed, It is called immutable object.


Wednesday, March 3, 2021

Clone concept

 Q. What is object clone in java?

Ans: The object cloning is a way to create an exact copy of an object. The clone method of  an object class is used to clone an object. The clonneable interface must be implemented by a class whose object clone to create.

Monday, March 1, 2021

Collection Methods

 1. ArrayList: 

Add---

1. public boolean add(object)

2. public void add(index(int), element or collection)

3.public boolean addAll(Collection E)

4. public boolean addAll(index(int), collection E)

Remove---

1. public Element remove(int)

2. public boolean remove(object)

3. public boolean removeRange(int, int)

4. public boolean removeAll(collection)

5. public boolean removeIf(Predicate)

Replace----

1. public object set(index, object)-> it is like replace method and return old object

2. public replaceAll()


2. LinkedList:

Add----

1. public boolean add(object e)

2. public void add(index, Object element)

3. public boolean addAll(Collection c)

4. public boolean addAll(index, Collection c)

5. public void addFirst(e)

6. public void addLast(e)

Remove----

1. void remove()

2. remove()

3. remove()

4. removeAll()

5. removeFirst()

6. removeFirstOccurrence()

7. removeIf()

8. removeLast()

9. removeLastOccurrence()


Collections Programs

Q1. Write a program to traverse (or iterate) ArrayList?

Q2 Write a program to convert List to Array.

Q3 Write a program to traverse(or iterate) HashSet?  

Q4 Given an element write a program to check if element(value) exists in ArrayList?

Q5 Given an element write a program to check if element exists in HashSet?

Q6 Write a program to initialize a HashMap in java ?

Q7 Write a program to initialize an ArrayList in java?

Q8  Write a program to convert Array to List? 

Array can be converted to list using Arrays.asList() method.

Q9 Write a program to find the length of the ArrayList?

Q10 Write a program to add elements to the HashMap given the key and value data type is String?

Q11 Write a program to initialize a HashSet in java?

Q12 Write a program to add elements to ArrayList ?

Q13 Write a program to add elements to HashSet? 

Q14 Write a program to get size of HashMap?

You can use the size() method of HashMap class.

Q15  How to check if HashMap is empty?

You can check if HashMap is empty using isEmpty() method.

Q16 Write a program to iterate the HashMap ? (Solution)

There are many ways to iterate the HashMap.The best way to iterate the HashMap is using iterator.

Q17 Write a program to sort HashMap by keys ? (Solution)

You can sort HashMap by keys using TreeMap object.

Q18 Write a program to sort ArrayList using Comparable and Comparator? (Solution)

You can sort ArrayList of Objects using Comparable and Comparator.

Q19 Write a program to sort ArrayList in descending order? (Solution)

You can sort the ArrayList in descending order using Collections.reverseOrder() method.

Q20 Write a program to add element at particular index of ArrayList? (Solution)

You can add element at particular index of ArrayList using method add(int index, Object element).

Q21 Write a program to remove element from specified index of ArrayList? (Solution)


You can remove element at particular index of ArrayList using remove(int index) method.

Q22 Write a program to convert LinkedList to ArrayList? (Solution)

Q23 Write a program to convert HashSet to Array? (Solution)

Q24 Write a program to reverse ArrayList in java? (Solution)

Q25 Write a program to iterate TreeMap in java? (Solution)

Q26 Write a program to sort HashMap by value? (Solution)

Q27 How to serialize a HashMap in java? (Solution)

Q28 How to synchronize a HashMap in java? (Solution)

Q29 How to serialize an ArrayList in java? (Solution)

Q30 How to synchronize an ArrayList in java? (Solution)

--------------------

HashMap

1. Write a Java program to associate the specified value with the specified key in a HashMap. 

2. Write a Java program to count the number of key-value (size) mappings in a map. 

3. Write a Java program to copy all of the mappings from the specified map to another map. 

4. Write a Java program to remove all of the mappings from a map. 

5. Write a Java program to check whether a map contains key-value mappings (empty) or not. 

6. Write a Java program to get a shallow copy of a HashMap instance. 

7. Write a Java program to test if a map contains a mapping for the specified key. 

8. Write a Java program to test if a map contains a mapping for the specified value. 

9. Write a Java program to create a set view of the mappings contained in a map. 

10. Write a Java program to get the value of a specified key in a map. 

11. Write a Java program to get a set view of the keys contained in this map.

12. Write a Java program to get a collection view of the values contained in this map. 


String coding questioon

  How do you reverse a given string in place? ( solution ) How do you print duplicate characters from a string? ( solution ) How do you chec...