Skip to main content

Posts

Java interview questions

 I have created a github page with Java solved questions. The page also has Java topic-wise quiz. The design is still under construction but. :) Feel free to visit my java page and comment.
Recent posts

Java a program to convert text file to HTML

Write a program to convert  a text file into html file with questions and collapsible answers. I have a text file which has questions and answers. The file was extracted as a csv file from an sqlite database with ^^^ as a row separator and | as  a column separator. I read the file using nio  Files. readAllBytes() method.     Path filepath = Paths.get(filename); try { byte[] byteArray = Files.readAllBytes(filepath); String str = new String(byteArray); }catch(IOException e){ System.out.println(e.getMessage()); } As the method may throw IOException, I have wrapped it in try block.  The method reads the entire file and stores it in a bytearray which is conv   erted to string. Next to split the content into individual questions, I use string.split(regex) method.  This method splits the string into an array, breaking the string at the occurrence of regex. Unfor...

Prime Factors of a given number

Write a program to find all prime factors of a given number.  The program should print the factors of a number which are prime.  One way of writing this is  If the number is divisible by 2, repeatedly divide the number by 2 until it is no longer divisible by 2. Print 2 as a factor if number is divisible.  In a loop for values of i from 3 to n in steps of 2, find if a number is divisible by i. If yes repeatedly divide by i as long as number is divisible.    void printFactors(int num){           if(num%2==0){             System.out.println("2 ");             while (num%2==0)              num = num/2;           }              ...

Program to find LCM

Write a program to find LCM of two numbers. LCM - is the least common multiple. If the numbers are 20 and 18, then LCM of these numbers is 90. The easiest and fastest way of finding LCM is finding GCD(greatest common divisor or highest common factor - the largest number which divides two numbers) and then dividing GCD by product of numbers. LCM * GCD = n1 * n2 Now to find GCD we can use the following algorithm. If num1 is divisible by num2 return num2 If not, return gcd of num2 and num2%num1  set num2 = num1%num2 set num1 = num2 find gcd of num1 and num2 and return it.      We will write gcd as a recursive function. A recursive function is a function which calls itself. Similar to our factorial program.      int gcd(int n1,int n2){          if(n1%n2==0)             return n2;          return gcd(n2,n1%n2);...

Program on method overloading

Write a program with overloaded methods to print values. Method overloading is the process where you write multiple methods in a class with same name but different number/type or parameters. When such an overloaded method is called, then the compiler selects the appropriate method depending on the number and type of arguments. e.g. class Number{       void printSquare(int n){          .....       }       void printSquare(double n){          ......      } }   The class above has two method with the same name printSquare(). Now if the we call Number obj = new Number(); obj.printSquare(10); we are calling the first method with integer parameter. But if we call obj.printSquare(1.56) we are calling second method with double parameter. Please remember that the overloading can not be done based only on return type of methods. Here is our requir...

Compare two Hashsets

Write a program to compare two hashsets in Java. HashSet is one of a data structures in the Java Collections API . It stores unique elements and permits nulls.It's backed by a HashMap. It doesn't maintain insertion order. And it's  not thread-safe. Let us write a function to create a HashSet with n elements and return it. HashSet<string> createSet(int n){ System.out.println("Enter the set elements"); HashSet<string>set = new HashSet<string>(); for(int i=0;i<n;i++){ Scanner scanner = new Scanner(System.in); System.out.println("Enter a string"); String str = scanner.nextLine(); if(set.add(str)==false){ System.out.println("Not able to add the string to set"); } } return set; } We create a Hashset using its constructor - in this example we have a set of Strings - hence new HashSet...

TreeSet Program Java

Write a Java program to find the numbers less than n in a tree set.   Java TreeSet class implements the Set interface that uses a tree for storage. It inherits AbstractSet class and implements the NavigableSet interface. The objects of the TreeSet class are stored in ascending order. And as it is a set, the elements in a treeset are unique - no duplicates. The difference between a hashset and treeset is that, in hashset the elements are not in any order but in a treeset they are in descending order.  Once we create a TreeSet, you can add elements to it using add () method.  To iterate over the elements of a TreeSet, you can use an iterator interface or you can use a for each loop. But if you just want to print the elements, you can give the treeset object as a parameter to println() method. Numbers less than n :  To obtain elements which are less than a given key, you can use the method headSet (). This method returns a SortedSet w...