Saturday, April 27, 2019

what will be the output of the following java code?

Problem statement:
What will be the output of the following java code?
  1. public class ConstructorCall {
  2.     int num = 200;
  3.     public void run(int num) {
  4.         this.num = num;
  5.     }
  6.     public void printE() {
  7.         System.out.println(2 * num);
  8.     }
  9.     public static void main(String[] args) {
  10.         ConstructorCall obj = new ConstructorCall();
  11.         obj.run(10);
  12.         obj.printE();
  13.     }
  14. }
Output: 20

what will be the output of following java code?

Problem statement:
what will be the output of the following java code?
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.HashMap;

  4. public class Solutions{
  5.     public static void main(String[] args) {
  6.         HashMap<Integer, String> m = new HashMap<>();
  7.         m.put(1001,"A");
  8.         m.put(1002,"B");
  9.         Collections.unmodifiableMap(m);
  10.         m.put(1001,"C");
  11.         System.out.println(m);
  12.     }
  13. }
Output: {1001=C, 1002=B}

what will be the output of the following java code?

Problem statement:
what will be the output of the following code?

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
  1. public class Solutions {
  2.     public static void main(String[] args) {
  3.         HashMap<Integer, String> m = new HashMap<>();
  4.         m.put(1001,"A");
  5.         m.put(1002,"B");
  6.         //Collections.unmodifiableMap(m);
  7.         m.put(1001,"C");
  8.         System.out.println(m);
  9.     }
  10. }
Output: {1001=C, 1002=B}

what will be the output of the following code?

Problem statement:
What will be the output of the following code?
  1. import java.util.ArrayList;
  2. public class ArrayListExample {
  3.     public static void main(String[] args) {
  4.         ArrayList list = new ArrayList();
  5.         list.add("a");
  6.         list.add("b");
  7.         list.add(1,"c");
  8.         list.add("d");
  9.         list.add(2,"e");
  10.         System.out.println(list);
  11.     }
  12. }
Output: [a, c, e, b, d]

Which of the following is not the java 8 features?

Problem statement:
which of the following is not java 8 features?
  1. Stream API
  2. Lambda expression
  3. Serialization
  4. Spliterator
  5. Optional class
  6. Functional interfaces
Output: Serialization, it is not a feature of java 8

what is the output of the code?

Problem statement:
Given the code for constructor overloading. What will be the output?
  1. public class ConstructorCall {
  2.     ConstructorCall() {
  3.         System.out.println("default constructor");
  4.     }
  5.     ConstructorCall(int a){
  6.         this();
  7.         System.out.println("parameterized constructor");
  8.         System.out.println(10);
  9.     }
  10.     public static void main(String[] args) {
  11.         ConstructorCall st = new ConstructorCall(3);
  12.     }
  13. }
Output:
default constructor
parameterized constructor
10

Can we have final constructor in java class?

Problem statement:
Can we have final constructor in java class?

No ! we cannot have final constructor in java class.
  1. public class NoFinalConstructor {
  2.     public final NoFinalConstructor() {
  3.         System.out.println("default");
  4.     }
  5.     public static void main(String[] args) {
  6.         NoFinalConstructor st = new NoFinalConstructor();
  7.     }
  8. }
Output: compile time error - modifier 'final' is not allowed.

Can we have private constructor in java?

Problem statement:
Can we have private constructor in java class?

Yes ! we can have private constructor in java class.
  1. public class YesPrivateConstructor {
  2.     private YesPrivateConstructor() {
  3.         System.out.println("default");
  4.     }
  5.     public static void main(String[] args) {
  6.         YesPrivateConstructor st = new YesPrivateConstructor();
  7.     }
  8. }
Output: default

Can we have static constructor in java?

Problem statement:
Can we have static constructor in java?

No ! we cannot have static constructor in java. The purpose of constructor is to initialise the object of the class. So there is no meaning to make constructor as static and thus compiler gives an error at compile time saying modifier 'static' not allowed.
  1. public class NoStaticConstructor {
  2.     public static NoStaticConstructor() {
  3.         System.out.println("default");
  4.     }

  5.     public static void main(String[] args) {
  6.         NoStaticConstructor st = new NoStaticConstructor();
  7.     }
  8. }
Output: compile time error - modifier 'static' is not allowed

Tuesday, April 23, 2019

Java Loop


  1. import jaa.util.Scanner;

  2. public class LoopAlgorithm {
  3.     public static void main(String[] args) {
  4.         Scanner in = new Scanner(System.in);
  5.         int t = in.nextInt();
  6.         for (int i = 0; i < t; i++) {
  7.             int a = in.nextInt();
  8.             int b = in.nextInt();
  9.             int n = in.nextInt();
  10.             int result = 0;
  11.             for (int j = 0; j < n; j++) {
  12.                 if (j == 0) {
  13.                     result = (result +a + (int) (Math.pow(2, j) * b));
  14.                 } else {
  15.                     result = (int) Math.pow(2, j) * b;
  16.                     System.out.println(result+" ");
  17.                 }
  18.             }
  19.             System.out.print("");
  20.         }
  21.         in.close();
  22.     }
  23. }








How do you print table using loop?

Problem Statement: 
Given an integer, N, print its first 10 multiples. Each multiple N x i (where 1<=i<=10) should be printed on a new line in the form: N x i = result.

Input Format
A single integer, N

Constraints
2<=N<=20

Output Format
Print 10 lines of output; each line i (where 1<=i<=10) contains the result of N x i in the form: 
N x i = result.

Sample Input
2

Sample Output
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

  1. public class PrintTable {
  2.     public static void main(String[] args) {
  3.         int N = 2;
  4.         for (int i = 1; i <= 10; i++) {
  5.             System.out.println(N + " x " + i + " = " + N * i);
  6.         }
  7.     }
  8. }

How do you convert int to string using java?

Problem statement:
You are given an integer n, you have to convert it into a string.
If your code successfully converts into a string s the code will print "Good job". Otherwise it will print "Wrong answer".
n can range between 100 to 100 inclusive.
Sample Input 0
100
Sample Output 0
Good job

public class ConvertIntToString {
    public static void main(String[] args) {
        int n = 100;
        //String s = String.valueOf(n);    // method - 1
        //String s = "" + n;                    // method - 2
        String s = Integer.toString(n);     // method - 3
        if (n == Integer.parseInt(s)) {
            System.out.println("Good Job");
        } else {
            System.out.println("Wrong Answer");
        }
    }
}
Output:
Good Job

Saturday, April 13, 2019

Spiral Order Traversal of a Tree [Binary Tree Zigzag Level Order Traversal]

Problem statement:
Write a function to print spiral order traversal of a tree. 
For example.

Input - 
10
J
H
I
A
C
D
F
E
B
G
where 10 is the number of elements in the input array.

Output -
A
BC
FED
GHIJ

Generate Parentheses

Problem statement:
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

Wednesday, April 3, 2019

How do you add two numbers represented by linked lists ??

Problem statement:
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.


Example:


Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

class Node {
    int data;
    Node next;

    Node(int data) {

        this.data = data;
    }

    Node() {

    }

    @Override

    public String toString() {
        return "Node{" +
                "data=" + data +
                ", next=" + next +
                '}';
    }
}

public class AddTwoNumbers {

    static Node head = null;

    public static void main(String[] args) {

        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);

        n1.next = n2;

        n2.next = n3;

        Node n4 = new Node(1);

        Node n5 = new Node(2);
        Node n6 = new Node(3);

        n4.next = n5;

        n5.next = n6;

        Node result = calculate(n1, n4);

        System.out.println(result);
    }

    static Node calculate(Node a, Node b) {

        //        //FIXME: Write your logic
        //  1, 2, 3
        //  1, 2, 3
        int carry = 0;

        while (a != null && b != null) {

            addLast(a.data + b.data);

            a = a.next;

            b = b.next;
        }
        return head;
    }

    public static void addLast(int data) {


        if (head == null) {

            head = new Node(data);
            return;
        }
        Node last = head;
        while (last.next != null) {
            last = last.next;
        }
        last.next = new Node(data);
    }
}

Output:
Node{data=2, next=Node{data=4, next=Node{data=6, next=null}}}

Two Sum [find a pair of index or two sum of index in a list]

Problem statement:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

import java.util.HashMap;
public class Solutions {
    public static void main(String args[]) {
        int nums[] = {2, 7, 11, 15};
        int target = 9;
        int idx[] = twoSum(nums, target);
        for (int index : idx) {
            System.out.print(index + " ");
        }
    }

    public static int[] twoSum(int nums[], int target) {
        HashMap m = new HashMap();
        int arr[] = new int[2];

        for (int i = 0; i < nums.length; i++) {
            Integer val = (Integer) m.get(target - nums[i]);
            if (val == null) {
                m.put(nums[i], i);
            } else {
                arr[0] = val;
                arr[1] = i;
            }
        }
        return arr;
    }
}
Output:    [0,1]
Expected: [0,1]

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...