package com.demo; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public class PlaindromTest { public static void main(String[] args) { String str = "my as a aaaaaa mom madam"; biggestPlainDrom(str); } private static void biggestPlainDrom(String str) { StringBuilder builder = new StringBuilder(); int size = 0; Listlist = new CopyOnWriteArrayList (); String[] strArr = str.split(" "); for (String st : strArr) { for (int i = st.length() - 1; i >= 0; i--) { builder.append(st.charAt(i)); } if (builder.toString().equals(st)) { if (size < st.length()) { if (list.size() > 0) { list.remove(list.size()-1); } size = st.length(); list.add(st); } } builder = new StringBuilder(); } System.out.println(list); } }
Java program to find the biggest palindrome from the given string.
Java program to print the output like- input will be "a4k3b2" and output should be after 'a' 4th character, after 'k' 3rd character and the final output : "aeknbd"
public class StringTest { public static void main(String[] args) { String str = "a4k3b2";// output aeknbd System.out.println(replaceChar(str));; } private static StringBuilder replaceChar(String str) { StringBuilder stB = new StringBuilder(); char[] c = str.toCharArray(); for (int i = 0; i < c.length; i++) { char cha = c[i]; if(Character.isAlphabetic(c[i])) { stB.append(cha); }else{ int v = c[i-1]; char value = c[i]; char digitChar = (char) (v + (value-48)); stB.append(digitChar); } } return stB; } }
Sample Questions: Java Test
Please read all the questions and select the correct option in the form below.
1. Given the code fragment: publicclass App { void calcBill() { // Line n1 new Invoice().print(); } } Which code fragment can be inserted at Line n1 to enable the class compile? A) privateclass Invoice { void print() {System.out.println("Invoice Printed");} } B) publicclass Invoice { void print() {System.out.println("Invoice Printed");} } C)class Invoice { void print() {System.out.println("Invoice Printed");} } D) protectedclass Invoice { void print() {System.out.println("Invoice Printed");} }2. Given: publicinterface MyInt { public void method1() { System.out.println("method1"); } public default void method2() { System.out.println("method2"); } public static void method3() { System.out.println("method3"); } public abstract void method4(); } Which statement is true? A) Only method4() compiles B) Only method2() and method4() compiles. C) Only method2(), method3(), and method4() compiles. D) MyInt.java compiles.3. Given the code fragment: public static void main(String[] args) { Listcourses = Arrays.asList("Java", "Oracle", "JSF", "EJB"); // Line n1 System.out.println(count); } Which code fragment can be inserted at Line n1 to enable the code to print 2? A) int count = courses.stream().filter(s -> s.startsWith("J")).count(); B) long count = courses.stream().filter(s -> s.startsWith("J")).count(); C) int count = courses.filter(s -> s.startsWith("J")).stream().count(); D) long count = courses.filter(s -> s.startsWith("J")).stream().count(); 3. Given the code fragment: public static void main(String[] args) { Listcourses = Arrays.asList("Java", "Oracle", "JSF", "EJB"); // Line n1 System.out.println(count); } Which code fragment can be inserted at Line n1 to enable the code to print 2? A) int count = courses.stream().filter(s -> s.startsWith("J")).count(); B) long count = courses.stream().filter(s -> s.startsWith("J")).count(); C) int count = courses.filter(s -> s.startsWith("J")).stream().count(); D) long count = courses.filter(s -> s.startsWith("J")).stream().count(); 4. Given the code fragment: publicclass App{ public static void main(String[] args) { String[] fruits = {"banana", "apple", "pears", "grapes"}; Arrays.sort(fruits, (a, b) -> a.compareTo(b)); for (String s : fruits) { System.out.print(" "+s); } } } What is the result? A) apple banana grapes pears B) pears grapes banana apple C) banana apple pears grapes D) Compilation fails.5. Given the code fragment: LocalDate date1 = LocalDate.of(2016, Month.JANUARY, 1); LocalDateTime date2 = LocalDateTime.of(2017, Month.JUNE, 1, 1, 1); Period p = Period.between(date1, date2); System.out.print(p.getYears() + ":" + p.getMonths() + ":" + p.getDays()); What is the result? A) 1:5:0 B) 1:6:0 C) 0:0:0 D) Compilation fails.6. Given that /report/jun.txt and report/data/jundata.txt files are accessible and given the code fragment: public static void main(String[] args) { try (Streamst1 = Files.find(Paths.get("/report"), 2, (p, a) -> p.toString().endsWith("txt")); Stream st2 = Files.walk(Paths.get("/report"), 2);) { st1.forEach(s -> System.out.println("Found: " + s)); st2.filter(s -> s.toString() .endsWith("txt")) .forEach(s -> System.out.println("Walked: " + s)); } catch (IOException ioe) { System.out.println("Exception"); } } What is the result? A) Found: \report\data\jundata.txt Found: \report\jun.txt Walked: \report\data\jundata.txt Walked: \report\jun.txt B) Found: \report\data\jundata.txt Found: \report\jun.txt Walked: \report\data\jundata.txt Walked: \report Walked: \report\jun.txt Walked: \report\data\ C) Found: \report\jun.txt Walked: \report\data\jundata.txt Walked: \report\jun.txt D) Found: \report\jun.txt Walked: \report Walked: \report\jun.txt Walked: \report\data\ Walked: \report\data\jundata.txt 7. Given the code fragment: public static void main(String[] args) { Streamnums = Stream.of(1, 2, 3, 4, 5); nums.filter(n -> n % 2 == 1); nums.forEach(p -> System.out.print(p)); } What is the result? A) 135 B) 12345 C) Compilation fails. D) An exception is thrown at runtime. 8. Given the code fragment:class MyResource1implements Closeable { public void close() { System.out.print("r1 "); } }class MyResource2implements AutoCloseable { public void close() throws IOException { System.out.print("r2 "); throw new IOException(); } } publicclass App2 { public static void main(String[] args) { try (MyResource1 r1 = new MyResource1(); MyResource2 r2 = new MyResource2();) { System.out.print("try "); } catch (Exception e) { System.out.print("catch "); for (Throwable t : e.getSuppressed()) { System.out.println(t.getClass().getName()); } } } } What is the result? A) try r2 r1 catch java.io.IOException B) try r2 r1 catch C) try r1 r2 catch D) Compilation fails.
Find the number which occurs odd number of time. All numbers occur even number of times except one.
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
//integer array declared with values
int array[] = { 20, 40, 50, 40, 50, 20, 30, 30, 50, 20, 40, 40, 20};
//declared map
Map<Integer, Integer> map = new HashMap<>();
//iterating int array to get the frequency of numbers
for (int i = 0; i < array.length; i++) {
if (map.containsKey(array[i])) {
Integer value = (Integer) map.get(array[i]);
map.put(array[i], ++value);
} else {
int key = array[i];
map.put(key, 1);
}
}
//iterating map to get the number which is present odd times
map.forEach((key, value) -> {
if(value % 2 != 0) {
System.out.print("Number which occurs odd number of times is :
"+key);
}
});
}
}
How to parse data from String in java.
package com.problem;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
public class Solution {
public static void main(String[] args) {
String str = "234$$##Shivaji$$##22.00";
String patternStr = "[^A-Za-z0-9.]";
Pattern pattern = Pattern.compile(patternStr);
String[] s = pattern.split(str);
String name = null;
Double doub = null;
Integer in = null;
for (String ss : s) {
if (StringUtils.isAlpha(ss)) {
name = ss;
} else if (StringUtils.isBlank(ss)) {
} else if (StringUtils.isAlphanumeric(ss)) {
in = Integer.valueOf(ss);
} else {
doub = Double.valueOf(ss);
}
}
System.out.println(name);
System.out.println(doub);
System.out.println(in);
}
}
Java program to find the occurence of integer in a given integer array.
package com.javatechtube;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class IntegerOccurence {
public static void main(String[] args) {
int[] arr = {1,2,3,2,4,5};
List<Integer> list = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();
for(int i=0; i<arr.length;i++)
{
if(map.containsKey(arr[i]))
{
Integer value = (Integer)map.get(arr[i]);
map.put(arr[i],++value);
}
else
{
int key = arr[i];
map.put(key, 1);
list.add(arr[i]);
}
}
System.out.println(map);
}
}
Java program to add all numbers present in AlphaNumeric String
First Approach
Second Approachpackage com.nt.logical; import java.util.Scanner; public class AddNoFromString { public static void main(String[] args) { //getting the dynamic values from keyboard Scanner sc=new Scanner(System.in); System.out.println("Enter the String with number: "); String str=sc.nextLine(); //logic for removing the alpha characters String[] s=str.split("[A-Za-z]"); String ss=""; //logic to remove white spaces for(int i=0;i<s.length;i++) { ss=ss+String.valueOf(s[i]); } //getting number from ss obj int num=Integer.valueOf(ss); int sum=0; //logic to add the numbers while(num>0) { sum=sum+num%10; num=num/10; } //printing the value after adding System.out.println("Value :"+sum); } }
package com.nt.logical; import java.util.Scanner; public class AddNoFromString { public static void main(String[] args) { //getting the dynamic values from keyboard Scanner sc=new Scanner(System.in); System.out.println("Enter the String with number: "); String str=sc.nextLine(); //logic for removing the characters String[] s=str.split("[A-Za-z]"); int sum=0;
//logic to add the numbers for(int i=0;i<s.length;i++) { if(!(sArr[i].equals("")||(sArr[i].isBlank()))) { sum=sum+Integer.parseInt(s[i]); } } //printing the value after adding System.out.println("Value :"+sum); } }









