Skip to main content

Commonly asked programming question


Commonly asked program in the Interview

1)Write a program to sort a array?


  package sort;

import java.util.Scanner;
public class Sortarray 
{

 public static void main(String args[])

 {
  int n, temp;
  Scanner s = new Scanner(System.in);  
  System.out.println("Enter no of the elements:");
  n =s.nextInt();                                                      //getting input from the user 
  int a[] = new int[n];
  System.out.println("Enter all the elements:");
 
  for(int i=0; i<n;i++)
  {
   a[i]=s.nextInt();
  }
 
  for(int i=0;i<n;i++)
  {
  for(int j=i+1; j<n; j++)                             //passing and assigning the values
  {
   if(a[i]>a[j])
   {
    temp =a[i];
    a[i]=a[j];
    a[j]=temp;
   }
  }
 }
  System.out.println("Ascending order");
  for(int i=0; i<n-1; i++)
  {
   System.out.println(a[i]+",");
  }
  System.out.println(a[n-1]);
}
}


2) Program for Arraylist and Linkedlist and HashSet

          
import java.util.*;

public class Array{

   public static void main(String[] args) {
      //ArrayList 
      List a1 = new ArrayList();
      a1.add("Zara");
      a1.add("Mahnaz");
      a1.add("Ayan");
      System.out.println(" ArrayList Elements");
      System.out.print("\t" + a1);

      //LinkedList
      List l1 = new LinkedList();
      l1.add("Zara");
      l1.add("Mahnaz");
      l1.add("Ayan");
      System.out.println();
      System.out.println(" LinkedList Elements");
      System.out.print("\t" + l1);

      //HashSet
      Set s1 = new HashSet(); 
      s1.add("Zara");
      s1.add("Mahnaz");
      s1.add("Ayan");
      System.out.println();
      System.out.println(" Set Elements");
      System.out.print("\t" + s1);

      //HashMap
      Map m1 = new HashMap(); 
      m1.put("Zara", "8");
      m1.put("Mahnaz", "31");
      m1.put("Ayan", "12");
      m1.put("Daisy", "14");
      System.out.println();
      System.out.println(" Map Elements");
      System.out.print("\t" + m1);
   }
}
  

4) Program for the LinkedList ()


    import java.util.*; 
import java.util.Iterator;
public class LinkList
{  
 public static void main(String args[])
 {  
   
  LinkedList al=new LinkedList(); 
  LinkedList<String> a2=new LinkedList<String>();
  LinkedList<Character> a3=new LinkedList<Character>();
  
  
  al.add("Ravi");  
  al.add("Vijay");  
  al.add("Ravi");  
  al.add("Ajay"); 
  /*al.add(10);
  al.add(15);
  al.add(10);
  al.add(56);
  al.add("e");  */
  al.get(2);
  al.set(2, "hashed");
  System.out.println(al  );
  Iterator it = al.iterator();
 // it = al.iterator();
  while(it.hasNext()) 
  {
      Object element = it.next();
      System.out.print(element + " ");
  }
  //System.out.println(it.previous());
  
  System.out.println(" ");
 }
}
  

5)Program for to find a Largest  file in Java?

    import java.io.BufferedReader;
class Largestfile
{
 public static void main(String args[])
 {
  findMethod("My name is H GANESHKUMAR");
 }
 static public void findMethod(String s)
 {
  String str = s + " ";
  char ch = ' ';
  int len = str.length(), l = 0;
  int min = len, max = 0;
  String shortest_word = "", longest_word = "", word = "";
  for (int i = 0; i < len; i++)
  {
   ch = str.charAt(i);
   if (ch != ' ')
   {
    word += ch;
   }                                     //if ends
   else
   {
    l = word.length();
    if (l < min)
   
    {
     min = l;
     shortest_word = word;
    }                                     //if ends
    if (l > max)
    {
     max = l;
     longest_word = word;
    }
    word = "";
   }
  }
  System.out.println("Shortest word = " + shortest_word + " with length " + min);
  System.out.println("Longest word = " + longest_word + " with length " + max);
 }
}


Comments

Popular posts from this blog

Commonly asked Top 26 Java Interview Questions

1.what is a transient variable? A transient variable is a variable that may not be serialized. If you don't want to serialize any objects then we have to use Transient Keyword. 2. Why do threads block on I/O? Threads block on i/o (that enters the waiting state) so that other threads may execute while the i/o Operation is performed. So your blocked thread of execution blocks only that: the thread of execution. 3. How are Observer and Observable used? Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects(refer 1.4). 4. What is synchronization and why is it important?                      With respect to multithreading, synchronization is the capability to control the...

Top Most Important Multithreading interview question and answers

1) What is Thread in java? Threads consumes CPU in best possible manner, hence enables multi processing. Multi threading reduces idle time of CPU which improves performance of application. Thread are light weight process. It belongs to java.lang package. It can run mutiple threads simutaneously .                                                               2) How to implements threads in java?         By   implementing java.lang.Runnable interface or extending java.lang.Thread class and then extending run method.   Thread creation by implementing java.lang.Runnable interface.      And then create Thread object by calling constructor and passing reference of Runnable interface i.e. runnable object          Thread threa...

Program for a Non-Repeating Characters

1)Write a program for non repeating Characters?   class Char               {                    public static void  main(String arsg[])                     {                            public static char FirstNonRepatingChar(String word)                               {                                 Set<characters>nonrepeating = new HashSet<>();                                   List<characters>nonrepeating = new ArrayList<>();                 ...