Logical Interview Programs
Logical Interview Programs
class fibonacci
if (n <= 1)
return n;
int n = 9;
System.out.println(fib(n));
Output
34
Q.2.how to convert binary to decimal and decimal to binary conversion.
package com.java2novice.algos;
int decimal = 0;
int power = 0;
while(true){
if(binary == 0){
break;
} else {
binary = binary/10;
power++;
return decimal;
Output:
11 ===> 3
110 ===> 6
100110 ===> 38
*****DecimalToBinary*****
package com.java2novice.algos;
int index = 0;
binary[index++] = number%2;
number = number/2;
System.out.print(binary[i]);
dtb.printBinaryFormat(25);
Output:
11001
Q.3.write a program to swap two numbers without using third variable.
import java.util.*;
class Swap
/*Define variables*/
int x = sc.nextInt();
int y = sc.nextInt();
/*Swapping*/
x = x + y;
y = x - y;
x = x - y;
Output:
23 43
After swapping: 43 23
Q.4.write a program factorial number.
class Test
if (n == 0)
return 1;
return n*factorial(n-1);
// Driver method
int num = 5;
Output:
Factorial of 5 is 120
Q.5. write a program to check the given number is palindrome or not.
// is Palindrome or not.
class GFG
int rev_num = 0;
return rev_num;
if (rev_n == n)
return 1;
else
return 0;
}
int n = 4562;
n = 2002;
Output:
package com.java2novice.algos;
if(number % i == 0){
return false;
return true;
Output:
Description: Armstrong numbers are the sum of their own digits to the power of the number of digits. It
is also known as narcissistic numbers.
package com.java2novice.algos;
int sum = 0;
int div = 0;
while(tmp > 0)
int temp = 1;
for(int i=0;i<noOfDigits;i++){
temp *= div;
sum += temp;
tmp = tmp/10;
if(number == sum) {
return true;
} else {
return false;
Output:
package com.myjava.exceptions;
try{
MyOwnException.myTest(null);
} catch(MyAppException mae){
if(str == null){
}
class MyAppException extends Exception {
public MyAppException() {
super();
super(message);
this.message = message;
super(cause);
@Override
return message;
@Override
return message;
Example Output
Immutable class means that once an object is created, we cannot change its content. In Java, all the
wrapper classes (like String, Boolean, Byte, Short) and String class is immutable. We can create our own
immutable class as well.
• Class must be declared as final (So that child classes can’t be created)
• Data members in the class must be declared as final (So that we can’t change the value of it after
object creation)
• A parameterized constructor
• No setters(To not have option to change the value of the instance variable)
// An immutable class
this.name = name;
this.regNo = regNo;
return name;
return regNo;
}
}
// Driver class
class Test
System.out.println(s.getName());
System.out.println(s.getRegNo());
// s.regNo = 102;
Output :
ABC
101
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.
static{
private MySingleton(){
return myObj;
System.out.println("Hey.... it is working!!!");
MySingleton ms = getInstance();
ms.testMe();
}
11.>how 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.
package com.java2novice.algos;
while(true){
synchronized(str1){
synchronized(str2){
System.out.println(str1 + str2);
};
while(true){
synchronized(str2){
synchronized(str1){
System.out.println(str2 + str1);
}
}
};
mdl.trd1.start();
mdl.trd2.start();
Description: The hashcode of a Java Object is simply a number, it is 32-bit signed int, that allows an
object to be managed by a hash-based data structure. We know that hash code is an unique id number
allocated to an object by JVM. But actually speaking, Hash code is not an unique number for an object. If
two objects are equals then these two objects should return same hash code. So we have to implement
hashcode() method of a class in such way that if two objects are equals, ie compared by equal() method
of that class, then those two objects must return same hash code. If you are overriding hashCode you
need to override equals method also. The below example shows how to override equals and hashcode
methods. The class Price overrides equals and hashcode. If you notice the hashcode implementation, it
always generates unique hashcode for each object based on their state, ie if the object state is same,
then you will get same hashcode. A HashMap is used in the example to store Price objects as keys. It
shows though we generate different objects, but if state is same, still we can use this as key.
package com.java2novice.algos;
import java.util.HashMap;
class Price{
this.item = itm;
this.price = pr;
System.out.println("In hashcode");
int hashcode = 0;
hashcode = price*20;
hashcode += item.hashCode();
return hashcode;
System.out.println("In equals");
} else {
return false;
}
public String getItem() {
return item;
this.item = item;
return price;
this.price = price;
Output:
In hashcode
In hashcode
In hashcode
In hashcode
In hashcode
In equals
@Override
};
@Override
};
characters.add(homer);
characters.add(moe);
Collections.sort(characters);
System.out.println(characters);
}
As you can see, this code is complicated and includes a lot of repetition. We had to override the
compareTo() method twice for the same logic. If there were more elements we would have to replicate
the logic for each object.
Fortunately, we have the Comparator interface, which lets us detach the compareTo() logic from Java
classes. Consider the same example above rewritten using Comparator:
characters.add(homer);
characters.add(moe);
Collections.sort(characters, (Comparator.<SimpsonCharacter>
System.out.println(characters);
These examples demonstrate the main difference between Comparable and Comparator.Use
Comparable when there is a single, default comparison for your object. Use Comparatorwhen you need
to work around an existing compareTo(), or when you need to use specific logic in a more flexible way.
Comparator detaches the sorting logic from your object and contains the compareTo() logic within your
sort() method.