May 28, 2020

Java interview questions amazon google microsoft


How to crack java interview ? 

  1. Abstraction in java

           Abstraction means we are exposing only essential parts to user, by hiding non -essential parts. For example car, rather than view individual components of car the user is viewing car as a car itself.
Abstraction is achieved in java through interface & abstract class



      2. What is system in System.out.println(); ?

         System is a class in java inside the package java.lang.package
         out is a static variable of type PrintStream
         println is a method to print message



3. What is system in JDK, JRE, JVM ?

4. Explain public static void main?

        To make java class as a main class
         Its is the entry point of java application
         public ( access specifier )  : method can be acess from anywhere from the application
        static ( keyword ) : static block / method/ keyword can be accessed before object creation
        static variable are class variables (  accessed ClassName.variableName)
        void ( empty) :  method doesnt return anything
        main : method name used to identfy the java as the entry method

5. Explain singleton?

 Only one instance of the class through out the application
 making constructor as private
public access for creation of object through a set method by checking the reference is null or not

6. Static in java?

 Static method : example connection class
Static class : Nested class ( grouping of similar class)
Static variable : can be shared among mutliple class ( example counter variable)
Static block : block of code

class Test {
    static int counter;
   
    // start of static block 
    static {
        counter = 10;
        System.out.println("static block called ");
    }
   
}









May 21, 2020

How and why we create an immutable class in java - complete tutorial


Immutable class : A class whoes object once created, its value should not be changed. Mostly used for cacheing purpose. Example Java wrapper class


 Java Source code

/**
 *
 */
/**
 *  Java Sample to create an immutable class : Hotel
 */
package com.cfed.pattern.flyweight;

/**
 * @author Konzernites
 *
 */
// The class must be created as a final class in order to restrict the creation of child class
public final class Hotel {


// Members in the class must be declared as final
private final String hotelName;
private final String hotelChain;
private final int hotelId;


// private final int hotelId =9; // Final method can be initialized only once.


// A parameterized constructor , to set values to the object
public Hotel(int hotelId, String hotelName, String hotelChain) {
this.hotelId = hotelId;
this.hotelChain = hotelChain;
this.hotelName = hotelName;
}

// variable can be accessed only through getters method
public String getHotelName() {
return hotelName;
}

// No setters are allowed, the changing of value is restricted
public String getHotelChain() {
return hotelChain;
}

public int getHotelId() {
return hotelId;
}

}


/**
 *
 */
package com.cfed.pattern.flyweight;

/**
 * @author Teena
 *
 */
public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Hotel hotel = new Hotel(1, "Ibis", "IB");
System.out.println(hotel.getHotelName());

}

}


How can we create an Immutable class?

1. Create final class.
2. Declare member variables as final.
3. No setter methods.
4. Parameterized constructor.
5. Only getter methods are allowed.


Explanation

Making a class final restrict creation of child class

Declaring variables as final restrict the value to be changed. final variables can be initialized only once.

No setter methods allowed to restrict the change in value.

Parameterized constructor to set the value once and only at the time constructing its object.

Getters methods to get the value of the object.




May 14, 2020

Sum up two Node to one in java


Our Idea is to sum up each elements in the given two nodes and create a new node.

/**
 * Sum up two Node to one in java
 */
package com.leetcode.algorithms;

/**
 * @author Cfed kozhikode
 *
 */
public class MainClass {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Solution s = new Solution();

System.out.println(989+989);

ListNode l1 = new ListNode(9);
l1.next = new ListNode(9);
l1.next.next = new ListNode(9);

ListNode l2 = new ListNode(1);
/* l2.next = new ListNode(8);
l2.next.next = new ListNode(9);*/

ListNode a = s.addTwoNumbers(l1, l2);

// System.out.println(a);

while(a!=null) {
System.out.print(a.val+" ");
a=a.next;
}

}

}


package com.leetcode.algorithms;
/**
 * 
 * @author Bithesh
 *
 */
class Solution {

private ListNode parentNode = null;

private int carryforward = 0;

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

int val1 = (null != l1) ? l1.val : 0;
int val2 = (null != l2) ? l2.val : 0;
int sum = val1 + val2 + carryforward;
carryforward = sum / 10;
int value = sum % 10;
parentNode = insertValue(parentNode, value);

ListNode n1 = (l1 == null || l1.next == null) ? null : l1.next;
ListNode n2 = (l2 == null || l2.next == null) ? null : l2.next;

if (n1 != null || n2 != null) {
addTwoNumbers(n1, n2);
}else if (carryforward > 0) {
parentNode = insertValue(parentNode, carryforward);
carryforward = 0;
}
return parentNode;
}

private ListNode insertValue(ListNode node, int data) {
if (node == null) {
return new ListNode(data);
} else {
node.next = insertValue(node.next, data);
}
return node;
}
}

/**
 * 
 */
package com.leetcode.algorithms;

/**
 * @author Bithesh
 *
 */
public class ListNode {
int val;
ListNode next;

ListNode(int x) {
val = x;
}
}

May 07, 2020

How to convert an array into a tree datastructure in java

We have an array of n numbers for example

arr = { 1, 2, 3, 4 };

Our idea is to convert the array to a Tree data structure.

Solution is given below


How to convert an array into a tree datastructure in java


/**
 * How to convert an array into a tree datastructure in java
 */
package com.leetcode.algorithms;

/**
 * @author Konzernites
 *
 */
public class InsertionList {

/**
* @param args
*/
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4 };
InsertionList in = new InsertionList();
ListNode node = null;
for (int i = 0; i <= arr.length; i++) {
node = in.insert(node ,arr[i]);
}
System.out.println(node);
}

private ListNode insert(ListNode node, int data) {
if(null == node) {
return new ListNode(data);
}else {
node.next = insert(node.next, data);
}
return node;
}}


How to print a Tree in Java ?

Consider ListNode a Tree, the java code for ListNode is below.

You need to span each node of the tree still element exist. so the best control structure to be used here is the while loop.


  ListNode tree;
while(tree!=null) {
System.out.print(tree.val+" ");
tree=tree.next;
}


ListNode Java Class

public class ListNode{
int data;
ListNode next;
ListNode(int data){
this.data = data;
next = null
}}

Thank you...

Facebook comments