Java Programming Coding Interview Questions 1 | Code Factory
Donate : Link
WordPress Blog : Link
1. BigDecimal to work with currencies
- Why You Should Never Use Float and Double for Monetary Calculations
- Make cents with BigDecimal
- Choosing data type for monetary Calculation
package com.example.java;import java.math.BigDecimal;/**
* @author code.factory
*
*/
public class BigDecimalExample {
public static void main(String... strings) {
float f1 = 1.25f;
float f2 = 1.05f;
System.out.println("Float f1 - f2 : " + (f1 - f2));double d1 = 1.25;
double d2 = 1.05;
System.out.println("Double d1 - d2 : " + (d1 - d2));BigDecimal b1 = new BigDecimal("1.25");
BigDecimal b2 = new BigDecimal("1.05");
System.out.println("BigDecimal(\"\") b1 - b2 : " + (b1.subtract(b2)));BigDecimal b3 = new BigDecimal(1.25);
BigDecimal b4 = new BigDecimal(1.05);
System.out.println("BigDecimal() b3 - b4 : " + (b3.subtract(b4)));
}
}
Output :
Float f1 - f2 : 0.20000005
Double d1 - d2 : 0.19999999999999996
BigDecimal("") b1 - b2 : 0.20
BigDecimal() b3 - b4 : 0.1999999999999999555910790149937383830547332763671875
2. How to access protected variables in another class in Inheritance
package com.example.java.programming;/**
* @author code.factory
*
*/
public class Child extends Parent {
Child() {
System.out.println(protectedStr); // accessible
}
public static void main(String... args) {
System.out.println(protectedStr); // not accessible
System.out.println(new Child().protectedStr); // accessible
System.out.println(new Parent().protectedStr); // accessible
}
}class Parent {
protected String protectedStr;
}
Why does this work? -> Because you’ve inherited the class. That means you’ve got all of its methods and it’s variables. Now, because your method and variable is protected, it also means that it can be accessed from the subclass.
3. equals() and hashCode()
package com.example.java.programming;import java.util.HashSet;
import java.util.Set;public class EqualHashcode { public static void main(String... args) {
Employee e1 = new Employee(1, "parth");
Employee e2 = new Employee(2, "parth");
System.out.println(e1 == e2);
System.out.println(e1.equals(e2));
Set<Employee> set = new HashSet<Employee>();
set.add(e1);
set.add(e2);
System.out.println(set);
}}class Employee {
int id;
String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
} @Override
public boolean equals(Object arg0) {
Employee e = (Employee) arg0;
return this.name.equals(e.name);
} @Override
public int hashCode() {
return this.name.charAt(0);
} @Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}}
Output :
false
true
[Employee [id=1, name=parth]]
4. Wrapper class MIN-VALUE
package com.example.java.programming;/**
* @author code.factory
*
*/
public class WrapperMinValue {
public static void main(String... args) {
System.out.println(Math.min(Integer.MIN_VALUE, 0.0d));
System.out.println(Math.min(Float.MIN_VALUE, 0.0d));
System.out.println(Math.min(Double.MIN_VALUE, 0.0d));
System.out.println(Integer.MIN_VALUE);
System.out.println(Float.MIN_VALUE);
System.out.println(Double.MIN_VALUE);
}
}
Output :
-2.147483648E9
0.0
0.0
-2147483648
1.4E-45
4.9E-324
5. Daemon and User Thread
package com.example.java.programming;public class DaemonTest { public static void main(String[] args) {
new WorkerThread().start(); try {
Thread.sleep(7500);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("Main Thread ending");
}}class WorkerThread extends Thread { public WorkerThread() {
// When false, (i.e. when it's a user thread),
// the Worker thread continues to run.
// When true, (i.e. when it's a daemon thread),
// the Worker thread terminates when the main
// thread terminates.
setDaemon(true);
} public void run() {
int count = 0; while (true) {
System.out.println("Hello from Worker "+count++); try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Output :
Hello from Worker 0
Hello from Worker 1
Main Thread ending
6. instanceof example
package com.example.java.programming;import java.util.ArrayList;
import java.util.List;/**
* @author code.factory
*
*/
public class Test {
public static void main(String... strings) {
ArrayList<Integer> in = new ArrayList<Integer>();
System.out.println(in instanceof ArrayList); // true
System.out.println(in instanceof List); // true
}
}
7. Static Non Static Block
package com.example.java.programming;/**
* @author code.factory
*
*/
public class StaticNonStaticBlock { public static void main(String... args) {
new child1();
System.out.println();
new child1();
}
}class parent1 { static void beforeStaticBlock() {
System.out.println("beforeStaticBlock");
}
static {
beforeStaticBlock();
System.out.println("A");
}
{
System.out.println("B");
}
public parent1() {
System.out.println("C");
}
}class child1 extends parent1 { static {
System.out.println("D");
}
{
System.out.println("E");
}
public child1() {
System.out.println("F");
}
}
Output :
beforeStaticBlock
A
D
B
C
E
FB
C
E
F
8. String example
package com.example.java.programming;public class StringTest {
public static void main(String... args) {
String str1 = "code";
String str2 = str1;
System.out.println(str1 == str2); // true
str2 = "Code";
System.out.println(str1 == str2); // false
System.out.println(str1); // code
System.out.println(str2); // Code
String str3 = null;
String str4 = null;
System.out.println(str3 == str4); // true
}
}
9. Fibonacci series
package com.example.java.programming;/**
* @author code.factory
*
*/
public class Fibonacci {
public static void main(String... args) {
fibonacci(10);
}
public static void fibonacci(int no) {
int first = 0, second = 1, curr = 1;
System.out.print(curr + " ");
for(int i=0; i<10; i++) {
curr = first + second;
System.out.print(curr + " ");
first = second;
second = curr;
}
}
}
Output :
1 1 2 3 5 8 13 21 34 55 89
10. Reverse Number
package com.example.java.programming;/**
* @author code.factory
*
*/
public class ReverseNumber {
public static void main(String[] args) {
reverseNo(12345);
} public static void reverseNo(int i) {
int no = 0;
while(i > 0) {
no = (i % 10) + (no * 10);
i = i / 10;
}
System.out.println(no); // 54321
}
}
11. Armstrong Number
package com.example.java.programming;/**
* @author code.factory
*
*/
public class ArmstrongNumber {
public static void main(String... args) {
armstrongNo(153);
}
public static void armstrongNo(int i) {
int original = i;
int no = 0, temp;
while(i > 0) {
temp = i % 10;
no = no + (temp * temp * temp);
i = i / 10;
}
System.out.println(original == no); // true }
}
12. Leap Year
package com.example.java.programming;/**
* @author code.factory
*
*/
public class LeapYear {
public static void main(String... args) {
leapYear(1900);
leapYear(2000);
leapYear(2100);
leapYear(2200);
}
/**
* Leap Year is a year which is divisible by 4,
* but we skip those years which are divisible by 100 but not by 400
* means 1700, 1800, 1900 are divisile by 100 but not by 400 so these are Not a Leap Year
*/
public static void leapYear(int year) {
System.out.println((year % 4 == 0) && !((year % 400 != 0) && (year % 100 == 0)));
System.out.println((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0)));
if(year % 4 == 0) {
if(year % 100 == 0 && year % 400 != 0) {
System.out.println("no");
} else {
System.out.println("yes");
}
}
System.out.println();
}
}
Output :
false
false
notrue
true
yesfalse
false
nofalse
false
no