1. Find out duplicate number between 1 to N numbers.
Description: |
You have got a range of numbers between 1 to N, where one of the number is repeated. You need to write a program to find out the duplicate number. |
Code: | ||
|
Output: |
Duplicate Number: 22 |
2. Find out middle index where sum of both ends are equal.
Description: |
You are given an array of numbers. Find out the array index or position where sum of numbers preceeding the index is equals to sum of numbers succeeding the index. |
Code: | ||
|
Output: |
Starting from index 0, adding numbers till index 2 and adding rest of the numbers can be equal |
3. Write a singleton class.
Description: |
Singleton class means you can create only one object for the given class. You can create a singleton class by making its constructor as private, so that you can restrict the creation of the object. Provide a static method to get instance of the object, wherein you can handle the object creation inside the class only. In this example we are creating object by using static block.
|
Code: |
package kundan; public class MySingleton { private static MySingleton myObj; static{ myObj = new MySingleton(); } private MySingleton(){ } public static MySingleton getInstance(){ return myObj; } public void testMe(){ System.out.println("Hey.... it is working!!!"); } public static void main(String a[]){ MySingleton ms = getInstance(); ms.testMe(); } } |
4. Write a program to create deadlock between two threads.
Description: |
Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.
Deadlocks can occur in Java when the synchronized keyword causes the executing thread to block while waiting
to get the lock, associated with the specified object. Since the thread might already hold locks associated with
other objects, two threads could each be waiting for the other to release a lock. In such case, they will end up waiting forever.
|
Code: | ||
|
5. Write a program to reverse a string using recursive algorithm.
Description: |
Write a program to reverse a string using recursive methods. You should not use any string reverse methods to do this. |
Code: | ||
|
Output: |
Result: nadnuk |
6. Write a program to reverse a number.
Description: |
Write a program to reverse a number using numeric operations. Below example shows how to reverse a number using numeric operations.
|
Code: | ||
|
Output: |
Result: 86871 |
7. Write a program to convert decimal number to binary format.
Description: |
Write a program to convert decimal number to binary format using numeric operations. Below example shows how to convert decimal number to binary format using numeric operations.
|
Code: | ||
|
Output: |
11001 |
8. Write a program to find perfect number or not.
Description: |
A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. Equivalently, a perfect number is a number that is half the sum of all of its positive divisors. The first perfect number is 6, because 1, 2 and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6. |
Code: | ||
|
Output: |
28 It is a perfect number Is perfect number: true |
No comments:
Post a Comment