Java for Testers – Interview Questions and Answers Part-13
1) Java Program: Change a string such that first character is upper case, second is lower case and so on?
public static String changeToUpperCase(String str) { StringBuffer s = new StringBuffer(); char ch = ' '; for (int i = 0; i < str.length(); i++) { if (ch == ' ' && str.charAt(i) != ' ') s.append(Character.toUpperCase(str.charAt(i))); else s.append(str.charAt(i)); ch = str.charAt(i); } return s.toString().trim(); }
2) Java Program: To find duplicates in a list?
public static Set<String> findDuplicates(List<String> listDuplicates) { final Set<String> set1 = new HashSet<String>(); final Set<String> set2 = new HashSet<String>(); for (String str : listDuplicates) { if (!set1.add(str)) { set2.add(str); } } return set2; }
3) Write a program to compare two Hashmap for equality?
public static Boolean compareMaps(){ Map<String, String> country1 = new HashMap<String, String>(); country1.put("Japan", "Tokyo"); country1.put("China", "Beijing"); Map<String, String> country2 = new HashMap<String, String>(); country2.put("Japan", "Tokyo"); country2.put("China", "Beijing"); if(country1==country2){ return true; } else return false; }
4) Write a Java program to demonstrate creating an Array?
public static void main (String[] args) { int[] arr; arr = new int[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; for (int i = 0; i < arr.length; i++) System.out.println("Element at index " + i + " : "+ arr[i]); }
5) Java Program: String s = “sub53od73th”; Eliminate the numbers alone. Print the Alphabets.
public static String removeNumFromString(String str) { if (str == null) { return null; } char[] ch = str.toCharArray(); int length = ch.length; StringBuilder sb = new StringBuilder(); int i = 0; while (i < length) { if (Character.isDigit(ch[i])) { i++; } else { sb.append(ch[i]); i++; } } return sb.toString(); }
6) Java program: Reverse the words in the sentence?
public static String reverseTheSentence(String inputString) { String[] words = inputString.split("\s"); String outputString = ""; for (int i = words.length-1; i >= 0; i--) { outputString = outputString + words[i] + " "; } return outputString; }
7) Java program: Find the count of each element in a two dimensional matrix?
static void printCount(int a[][], int n, int m, int z[], int l) { for (int i = 0; i < n; i++) { Map<Integer,Integer> mp = new HashMap<>(); for (int j = 0; j < m; j++) mp.put(a[i][j], 1); int count = 0; for (int j = 0; j < l; j++) { if (mp.containsKey(z[j])) count += 1; } System.out.println("row" +(i + 1) + " = " + count); }
8) Java Program: Find duplicate elements in an array of numbers?
public static void main(String[] args) { int [] arr = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3}; System.out.println("Duplicate elements in given array: "); for(int i = 0; i < arr.length; i++) { for(int j = i + 1; j < arr.length; j++) { if(arr[i] == arr[j]) System.out.println(arr[j]); } } }
9) Write a program to read/write data from a Property file?
public static void main(String[] args){ Properties prop = new Properties(); prop.setProperty("db.url", "localhost"); prop.setProperty("db.user", "mkyong"); prop.setProperty("db.password", "password"); prop.store(outputStream, ""); prop.load(inputStream) prop.getProperty("db.url"); prop.getProperty("db.user"); prop.getProperty("db.password"); prop.keySet(); prop.forEach((k, v) -> System.out.println("Key : " + k + ", Value : " + v)); }
10) Write a Java program to demonstrate creating factorial of a number using recursion?
static int factorial(int n){ if (n == 0) return 1; else return(n * factorial(n-1)); } public static void main(String args[]){ int i,fact=1; int number=4; fact = factorial(number); System.out.println("Factorial of "+number+" is: "+fact); } }
11) Java Program: To print the frequency of words in a paragraph?
public static void frequencyWords(String strInput){ Map<String, Integer> occurrences = new HashMap<String, Integer>(); String[] words = strInput.split("\\s+"); for(String word: words){ if(occurrences.containsKey(word)){ int count = occurrences.get(word); map.put(word,count+1); } else { map.put(word,1); } }for(Map.Entry<String,Integer> entry: map.entrySet()){ System.out.println(entry.getKey() + ":" + entry.getValue()); } }
12) Write a Java program to see the output as 0,1,1,2,3?
public static void main(String args[]) { int n1=0,n2=1,n3,count=5; System.out.print(n1+" "+n2); for(int i=2;i<count;i++) { n3=n1+n2; System.out.print(" "+n3); n1=n2; n2=n3; } }
13) Write a java program to find the factorial of a given number ?
public static void findFactorial(int number){ int i,fact=1; for(i=1;i<=number;i++){ fact=fact*i; } System.out.println("Factorial of " + number + "is: " + fact); }
14) Write a Java program to demonstrate ArrayList?
class ArrayListExample { public static void main(String[] args) { int n = 5; ArrayList<Integer> arrList = new ArrayList<Integer>(n); for (int i = 1; i <= n; i++) arrList.add(i); System.out.println(arrList); arrList.remove(3); System.out.println(arrList); for (int i = 0; i < arrList.size(); i++) System.out.print(arrList.get(i) + " "); } }
15) Write a Java program to demonstrate LinkedList?
public class LinkedListExample { public static void main(String args[]) { LinkedList<String> list = new LinkedList<String>(); list.add("A"); list.add("B"); list.addLast("C"); list.addFirst("D"); list.add(2, "E"); System.out.println(list); list.remove("B"); list.remove(3); list.removeFirst(); list.removeLast(); System.out.println(list); } }
16) Write a Java program to demonstrate ArrayList using List interface?
class ArrayListWithListInterface { public static void main(String[] args) { List<String> arrayList = new ArrayList<String>(); arrayList.add("Java"); arrayList.add("C#"); arrayList.add("Python"); System.out.println(arrayList); } }
17) Write a Java program to demonstrate Hashset?
class HashSetExample{ public static void main(String[] args){ Set<String> hs = new HashSet<String>(); hs.add("India"); hs.add("Australia"); hs.add("South Africa"); hs.add("India"); System.out.println(hs); } }
18) Write a Java program to demonstrate LinkedHashSet?
public class LinkedHashSetExample { public static void main(String[] args) { LinkedHashSet<String> linkedset = new LinkedHashSet<String>(); linkedset.add("A"); linkedset.add("B"); linkedset.add("C"); linkedset.add("D"); System.out.println(linkedset); } }
19) Write a Java program to demonstrate TreeSet?
class TreeSetExample { public static void main(String[] args) { TreeSet<String> ts1 = new TreeSet<String>(); ts1.add("A"); ts1.add("B"); ts1.add("C"); ts1.add("C"); System.out.println(ts1); } }
20) Write a Java program to demonstrate PriorityQueue?
class PriorityQueueExample { public static void main(String args[]) { PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>(); pQueue.add(10); pQueue.add(20); pQueue.add(15); System.out.println(pQueue.peek()); System.out.println(pQueue.poll()); } }
21) Write a Java program to demonstrate creating HashMap using Map interface?
class HashMapExample{ public static void main(String[] args){ Map<Integer,String> h1 = new HashMap<>(); h1.put(1,"Java"); h1.put(2,"C#"); h1.put(3,"Python"); System.out.println(h1); } }
22) Write a Java program to demonstrate LinkedHashMap ?
public class LinkedHashMapExample { public static void main(String a[]) { LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); map.put("one", "www.gmail.com"); map.put("two", "www.ymail.com"); map.put("four", "www.outlook.com"); System.out.println(lhm); } }
23) Write a Java program to demonstrate TreeMap?
class TreeMapExample{ public static void main(String args[]){ TreeMap<Integer,String> map=new TreeMap<Integer,String>(); map.put(100,"Amit"); map.put(102,"Ravi"); map.put(101,"Vijay"); map.put(103,"Rahul"); for(Map.Entry m:map.entrySet()){ System.out.println(m.getKey()+" "+m.getValue()); } } }
24) Write a Java program to demonstrate Hashtable?
class HashtableExample{ public static void main(String args[]){ Hashtable<Integer,String> hm=new Hashtable<Integer,String>(); hm.put(100,"Jeorge"); hm.put(102,"Smith"); hm.put(101,"Sarah"); hm.put(103,"John"); for(Map.Entry m:hm.entrySet()){ System.out.println(m.getKey()+" "+m.getValue()); } } }
25) Write a Java program to demonstrate creating Multidimensional array?
class MultiExample { public static void main(String[] args) { int[][] arr = { { 1, 2 }, { 3, 4 } }; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) System.out.println("arr[" + i + "][" + j + "] = " + arr[i][j]); } }
Next Steps:
> More interview questions and answers on Java, continue to the next post (Click on Next Post link below)
> Check complete Java interview questions and answers here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning 🙂
Connect to me on Linked In (Click here)
Visit me on my site (Click here)
On a mission to contribute to the Software Testing Community in all possible ways.
Very helpful java questions.Thanks a lot.
Welcome Kpg 🙂