Saturday, April 27, 2019

Java Program Coding Interview Questions

Q- How to sort array using single loop in java?

Q- How to count duplicate elements in array in java? or How to find duplicate values in array in java?
package com.array.example;
import java.util.Arrays;

public class ArrayElementCount {

 public static void main(String[] args) {

  int b[] = { 1, 1, 1, 2, 2, 3, 4, 3, 5, 4, 5, 5, 5 };
  Arrays.sort(b);
  int count = 1;
  int temp = 0;
  for (int j = 0; j < b.length; j++) {
   if (j + 1 < b.length) {
    temp = b[j + 1];
   }
   if (b[j] == temp) {
    count++;
   } else {
    System.out.println("Element :" + b[j] + " Count:" + count);
    count = 1;
   }
   if (j == b.length - 1) {
    System.out.println("Element :" + b[j] + " Count:" + count);
   }
  }
 }
}
Output:
Element :1 Count:3
Element :2 Count:2
Element :3 Count:2
Element :4 Count:2
Element :5 Count:5

Q- Write a program for conversion of a decimal number to binary number.

package com.shubh.example;

public class DecimalToBinaryExample {
 public static void main(String[] args) {
  int number = 25;
  DecimalToBinaryExample.decimalToBinary(number);
 }

 private static void decimalToBinary(int n) {
  int a;
  System.out.println("Decimal number: " + n);
  String x = "";
  while (n > 0) {
   a = n % 2;
   x = x + "" + a;
   n = n / 2;
  }
  System.out.println("Binary number:" + x);
 }
}
Output:

Decimal number: 25
Binary number:10011
Q- What is the output of below program?

package com.shubh.example;

public class PrintPerson {

 public static void main(String[] args) {
  PrintPerson instance = new PrintPerson();
  Person person = instance.new Person();
  person.setName("In Main Block");
  System.out.println(person.getName());
  instance.pouplatePerson(person);
 }

 public void pouplatePerson(Person person) {
  person = new Person();
  person.setName("In pouplatePerson");
  System.out.println(person.getName());
 }

 class Person {
  String name;

  public String getName() {
   return name;
  }

  public void setName(String name) {
   this.name = name;
  }
 }
}
Output:

In Main Block
In pouplatePerson

Q- How to sort array in java without using sort method? or How to sort array in java without using collections?

Q- How to eliminate duplicate values from array in java without using collections? or How to remove duplicates from array in Java without using collections?

Q- How to Shuffle an Array or a List?
 Shuffle an Array or List with the Collections.
We can use Collections.shuffle(list) to shuffle a list. And Collections.shuffle(Arrays.asList(a))  to shuffle an array.

Q- How to shuffle an Array or ArrayList without collections?
Ansewr :- Shuffle Array or ArrayList Without Using Collections Example





LinkedList : A singly linked list is unidirectional linked list, it can traversed  only one direction from head to the tail (last node). Each element in a linked list is called a node. A single node has data and pointer(next), pointer contains the address of next node. for linear linked list, last node pointer has NULL value. 
Stack
Queue
BlockingQueue
Multithreading
Spring & Spring Boot
Array & ArrayList

No comments:

Post a Comment