Java Programming Coding Interview Questions 1 | Code Factory

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)));
}
}
Float f1 - f2 : 0.20000005
Double d1 - d2 : 0.19999999999999996
BigDecimal("") b1 - b2 : 0.20
BigDecimal() b3 - b4 : 0.1999999999999999555910790149937383830547332763671875
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;
}
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 + "]";
}
}
false
true
[Employee [id=1, name=parth]]
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);
}
}
-2.147483648E9
0.0
0.0
-2147483648
1.4E-45
4.9E-324
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();
}
}
}
}
Hello from Worker 0
Hello from Worker 1
Main Thread ending
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
}
}
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");
}
}
beforeStaticBlock
A
D
B
C
E
F
B
C
E
F
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
}
}
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;
}
}
}
1 1 2 3 5 8 13 21 34 55 89
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
}
}
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
}
}
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();
}
}
false
false
no
true
true
yes
false
false
no
false
false
no

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

RESTful API Designing Guidelines

Becoming a Product Manager (and Reader)

Send push message to Hangouts Chat with Apps Script

Setting up a ESXI server on Bare-metal

Is String Objects in Java are Immutable?

Java-Lombok: Do we need getters and setters?

Using Go Interfaces for Testable Code

Allowing User on CentOS to Run Docker Command without sudo

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

12. Integer to Roman | LeetCode | Medium | Java

Print a sequence using 3 threads | Java

Important Topics in Core Java For Interview

How to read and write to file as a string in java using five simple ways