Java Interview Questions | 6 | -Interview of Geeks

Core java Interview Questions Interview Questions | 6

251-300 out of 300 Questions

There is the list of 300 core Java interview questions. If there is any core Java interview question that has been asked to you, kindly post it in the ask question section. We assure that you will get here the 90% frequently asked interview questions and answers.

The answers to the Core Java interview questions are short and to the point. The core Java interview questions are categorized in Basics of Java interview questions, OOPs interview questions, String Handling interview questions, Multithreading interview questions, collection interview questions, JDBC interview questions, etc.


  1. 251. How to perform Linear Search in Java?

    Consider the following program to perform Linear search in Java.

    import java.util.Scanner;

    public class Leniear_Search {
    public static void main(String[] args) {
    int[] arr = {10, 23, 15, 8, 4, 3, 25, 30, 34, 2, 19};
    int item,flag=0;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter Item ?");
    item = sc.nextInt();
    for(int i = 0; i <10; i++)
    {
    if(arr[i]==item)
    {
    flag = i+1;
    break;
    }
    else
    flag = 0;
    }
    if(flag != 0)
    {
    System.out.println("Item found at location" + flag);
    }
    else
    System.out.println("Item not found");

    }
    }

    Output

    Enter Item ?
    23
    Item found at location 2
    Enter Item ?
    22
    Item not found

  2. 252. How to perform merge sort in Java?

    Consider the following program to perform merge sort in Java.

    public class MyMergeSort
    {
    void merge(int arr[], int beg, int mid, int end)
    {

    int l = mid - beg + 1;
    int r = end - mid;

    intLeftArray[] = new int [l];
    intRightArray[] = new int [r];

    for (int i=0; i<l; ++i)
    LeftArray[i] = arr[beg + i];

    for (int j=0; j<r; ++j)
    RightArray[j] = arr[mid + 1+ j];


    int i = 0, j = 0;
    int k = beg;
    while (i<l&&j<r)
    {
    if (LeftArray[i] <= RightArray[j])
    {
    arr[k] = LeftArray[i];
    i++;
    }
    else
    {
    arr[k] = RightArray[j];
    j++;
    }
    k++;
    }
    while (i<l)
    {
    arr[k] = LeftArray[i];
    i++;
    k++;
    }

    while (j<r)
    {
    arr[k] = RightArray[j];
    j++;
    k++;
    }
    }

    void sort(int arr[], int beg, int end)
    {
    if (beg<end)
    {
    int mid = (beg+end)/2;
    sort(arr, beg, mid);
    sort(arr , mid+1, end);
    merge(arr, beg, mid, end);
    }
    }
    public static void main(String args[])
    {
    intarr[] = {90,23,101,45,65,23,67,89,34,23};
    MyMergeSort ob = new MyMergeSort();
    ob.sort(arr, 0, arr.length-1);

    System.out.println("\nSorted array");
    for(int i =0; i<arr.length;i++)
    {
    System.out.println(arr[i]+"");
    }
    }
    }

    Output

    Sorted array
    23
    23
    23
    34
    45
    65
    67
    89
    90
    101

  3. 253. How to perform quicksort in Java?

    Consider the following program to perform quicksort in Java.

    public class QuickSort {
    public static void main(String[] args) {
    int i;
    int[] arr={90,23,101,45,65,23,67,89,34,23};
    quickSort(arr, 0, 9);
    System.out.println("\n The sorted array is: \n");
    for(i=0;i<10;i++)
    System.out.println(arr[i]);
    }
    public static int partition(int a[], int beg, int end)
    {

    int left, right, temp, loc, flag;
    loc = left = beg;
    right = end;
    flag = 0;
    while(flag != 1)
    {
    while((a[loc] <= a[right]) && (loc!=right))
    right--;
    if(loc==right)
    flag =1;
    elseif(a[loc]>a[right])
    {
    temp = a[loc];
    a[loc] = a[right];
    a[right] = temp;
    loc = right;
    }
    if(flag!=1)
    {
    while((a[loc] >= a[left]) && (loc!=left))
    left++;
    if(loc==left)
    flag =1;
    elseif(a[loc] <a[left])
    {
    temp = a[loc];
    a[loc] = a[left];
    a[left] = temp;
    loc = left;
    }
    }
    } returnloc;
    }
    static void quickSort(int a[], int beg, int end)
    {

    int loc;
    if(beg<end)
    {
    loc = partition(a, beg, end);
    quickSort(a, beg, loc-1);
    quickSort(a, loc+1, end);
    }
    }
    }

    Output

    The sorted array is:
    23
    23
    23
    34
    45
    65
    67
    89
    90
    101

  4. 254. Write a program in Java to create a doubly linked list containing n nodes.

    Consider the following program to create a doubly linked list containing n nodes.

    public class CountList {

    //Represent a node of the doubly linked list

    class Node{
    int data;
    Node previous;
    Node next;

    public Node(int data) {
    this.data = data;
    }
    }

    //Represent the head and tail of the doubly linked list
    Node head, tail = null;

    //addNode() will add a node to the list
    public void addNode(int data) {
    //Create a new node
    Node newNode = new Node(data);

    //If list is empty
    if(head == null) {
    //Both head and tail will point to newNode
    head = tail = newNode;
    //head's previous will point to null
    head.previous = null;
    //tail's next will point to null, as it is the last node of the list
    tail.next = null;
    }
    else { //newNode will be added after tail such that tail's next will point to newNode
    tail.next = newNode;
    //newNode's previous will point to tail
    newNode.previous = tail;
    //newNode will become new tail
    tail = newNode;
    //As it is last node, tail's next will point to null
    tail.next = null;
    }
    }

    //countNodes() will count the nodes present in the list
    public int countNodes() {
    int counter = 0;
    //Node current will point to head
    Node current = head;

    while(current != null) {
    //Increment the counter by 1 for each node
    counter++;
    current = current.next;
    }
    return counter;
    }

    //display() will print out the elements of the list
    public void display() {
    //Node current will point to head
    Node current = head;
    if(head == null) {
    System.out.println("List is empty");
    return;
    }
    System.out.println("Nodes of doubly linked list: ");
    while(current != null) {
    //Prints each node by incrementing the pointer.

    System.out.print(current.data + " ");
    current = current.next;
    }
    }

    public static void main(String[] args) {

    CountList dList = new CountList();
    //Add nodes to the list
    dList.addNode(1);
    dList.addNode(2);
    dList.addNode(3);
    dList.addNode(4);
    dList.addNode(5);

    //Displays the nodes present in the list
    dList.display();

    //Counts the nodes present in the given list
    System.out.println("\nCount of nodes present in the list: " + dList.countNodes());
    }
    }

    Output

    Nodes of doubly linked list:
    1 2 3 4 5
    Count of nodes present in the list: 5

  5. 255. Write a program in Java to find the maximum and minimum value node from a circular linked list.

    Consider the following program.

    public class MinMax {
    //Represents the node of list.
    public class Node{
    int data;
    Node next;
    public Node(int data) {
    this.data = data;
    }
    }

    //Declaring head and tail pointer as null.
    public Node head = null;
    public Node tail = null;

    //This function will add the new node at the end of the list.
    public void add(int data){
    //Create new node
    Node newNode = new Node(data);
    //Checks if the list is empty.
    if(head == null) {
    //If list is empty, both head and tail would point to new node.
    head = newNode;
    tail = newNode;
    newNode.next = head;
    }
    else {
    //tail will point to new node.
    tail.next = newNode;
    //New node will become new tail.
    tail = newNode;
    //Since, it is circular linked list tail will points to head.
    tail.next = head;
    }
    }

    //Finds out the minimum value node in the list
    public void minNode() {
    Node current = head;
    //Initializing min to initial node data
    int min = head.data;
    if(head == null) {
    System.out.println("List is empty");
    }
    else {
    do{
    //If current node's data is smaller than min
    //Then replace value of min with current node's data
    if(min > current.data) {
    min = current.data;
    }
    current= current.next;
    }while(current != head);

    System.out.println("Minimum value node in the list: "+ min);
    }
    }

    //Finds out the maximum value node in the list
    public void maxNode() {
    Node current = head;
    //Initializing max to initial node data
    int max = head.data;
    if(head == null) {
    System.out.println("List is empty");
    }
    else {
    do{
    //If current node's data is greater than max
    //Then replace value of max with current node's data
    if(max < current.data) {
    max = current.data;
    }
    current= current.next;
    }while(current != head);

    System.out.println("Maximum value node in the list: "+ max);
    }
    }

    public static void main(String[] args) {
    MinMax cl = new MinMax();
    //Adds data to the list
    cl.add(5);
    cl.add(20);
    cl.add(10);
    cl.add(1);
    //Prints the minimum value node in the list
    cl.minNode();
    //Prints the maximum value node in the list
    cl.maxNode();
    }
    }

    Output

    Minimum value node in the list: 1
    Maximum value node in the list: 20

  6. 256. Write a program in Java to calculate the difference between the sum of the odd level and even level nodes of a Binary Tree.

    Consider the following program.

    import java.util.LinkedList;
    import java.util.Queue;

    public class DiffOddEven {

    //Represent a node of binary tree
    public static class Node{
    int data;
    Node left;
    Node right;

    public Node(int data){
    //Assign data to the new node, set left and right children to null
    this.data = data;
    this.left = null;
    this.right = null;
    }
    }

    //Represent the root of binary tree
    public Node root;

    public DiffOddEven(){
    root = null;
    }

    //difference() will calculate the difference between sum of odd and even levels of binary tree
    public int difference() {
    int oddLevel = 0, evenLevel = 0, diffOddEven = 0;

    //Variable nodesInLevel keep tracks of number of nodes in each level
    int nodesInLevel = 0;

    //Variable currentLevel keep track of level in binary tree
    int currentLevel = 0;

    //Queue will be used to keep track of nodes of tree level-wise
    Queue queue = new LinkedList();

    //Check if root is null
    if(root == null) {
    System.out.println("Tree is empty");
    return 0;
    }
    else {
    //Add root node to queue as it represents the first level
    queue.add(root);
    currentLevel++;

    while(queue.size() != 0) {

    //Variable nodesInLevel will hold the size of queue i.e. number of elements in queue
    nodesInLevel = queue.size();

    while(nodesInLevel > 0) {
    Node current = queue.remove();

    //Checks if currentLevel is even or not.
    if(currentLevel % 2 == 0)
    //If level is even, add nodes's to variable evenLevel
    evenLevel += current.data;
    else
    //If level is odd, add nodes's to variable oddLevel
    oddLevel += current.data;

    //Adds left child to queue
    if(current.left != null)
    queue.add(current.left);
    //Adds right child to queue
    if(current.right != null)
    queue.add(current.right);
    nodesInLevel--;
    }
    currentLevel++;
    }
    //Calculates difference between oddLevel and evenLevel
    diffOddEven = Math.abs(oddLevel - evenLevel);
    }
    return diffOddEven;
    }

    public static void main (String[] args) {

    DiffOddEven bt = new DiffOddEven();
    //Add nodes to the binary tree
    bt.root = new Node(1);
    bt.root.left = new Node(2);
    bt.root.right = new Node(3);
    bt.root.left.left = new Node(4);
    bt.root.right.left = new Node(5);
    bt.root.right.right = new Node(6);

    //Display the difference between sum of odd level and even level nodes
    System.out.println("Difference between sum of odd level and even level nodes: " + bt.difference());
    }
    }

    Output

    Difference between sum of odd level and even level nodes: 11

Last Updated:


Trending Technologies Interview Questions


@InterviewofGeeks, Some rights reserved.