Interview Questions and Answers - Order By Newest Q91. Write a Program to validate an email address Core Java
Ans. public class Class{
public static void main(String[] args){
String str = "xyz@123.com";
if(!str.contains("@") && !str.contains(".") && (str.indexOf('.') < str.indexOf('@'))){
System.out.println("Not a Valid Email Address");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  validate an email address  coding  code Asked in 3 Companies Q92. Write a Program to validate if a particular character occurs after another in a string Core Java
Ans. public class Class{
public static void main(String[] args){
String str = "xyz123.co@m";
if(str.indexOf('.') < str.indexOf('@')){
System.out.println("Not a Valid Email Address");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string  string.indexOf  coding  code Q93. How to find the median number in an array of integers ? Core Java
Ans.
Arrays.sort(numArray);
double median;
if (numArray.length % 2 == 0)
median = ((double)numArray[numArray.length/2] (double)numArray[numArray.length/2 - 1])/2;
else
median = (double) numArray[numArray.length-1/2]; Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coding Asked in 2 Companies Q94. Write a Java program to print 10 random numbers between 1 to 100? Core Java
This question was recently asked at 'karya technology'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coding  code Asked in 1 Companies Q95. Write a method / program that will determine if the parenthesis are balanced in a given string. Core Java
Ans. https://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/ Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string  code  coding Asked in 14 Companies Q96. Given array of integers, find first two numbers that adds up to 10. Core Java
This question was recently asked at 'Amazon'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Code  Coding Asked in 1 Companies Q97. Write a program to reverse words of a sentence. Core Java
This question was recently asked at 'Amazon'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  Coding Asked in 1 Companies This question was recently asked at 'Bind Software Innovations'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  oding  cod Asked in 1 Companies Q99. How can you extract integers from string values and add (sum it up) all the extracted integers? e.g "James34long4island322in3rdAvenue" ---> 34 4 322 3 = 363 Core Java
This question was recently asked at 'Horizon Solutions'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coding  code Asked in 1 Companies Q100. Write a Program to convert a binary to number ? Core Java
Ans. int convert(int binaryInt) {
int sumValue=0;
int multiple = 1;
while(binaryInt > 0) {
binaryDigit = binaryInt;
binaryInt = binaryInt /10;
sumValue = sumValue (binaryDigit * multiple);
multiple = multiple * 2;
}
return sumValue;
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  coding Asked in 1 Companies basic Q101. Given a string with multiple opening and closing brackets, determine if the string is valid or not ( to see if the string has all closing brackets for each opening bracket ) ? Core Java
This question was recently asked at 'Amazon'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  coding Asked in 1 Companies Q102. Write a method, that compare two numbers and return the bigger one ? Core Java
Ans. int compare(int a, int b) {
a>b? return a: return b;
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  program  coding Asked in 1 Companies basic Q103. Write a Program for insertion sort. Core Java
This question was recently asked at 'Ola Cabs'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  program  code  coding  insertion sort   sort Asked in 1 Companies Q104. How to calculate lcm of two numbers ? Core Java
This question was recently asked at 'HCL Tech'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coding  code Asked in 1 Companies Q105. Write a Program to find age by birth date ? Core Java
Ans. LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);
Period p = Period.between(birthday, today); //Now access the values as below
System.out.println(period.getDays());
System.out.println(period.getMonths());
System.out.println(period.getYears()); Sample Code for LocalDate Sample Code for Period Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coding  code  date  LocalDate  Period Asked in 1 Companies Q106. What will be the output of following code
class TestMain {
public static void main(String[] args) {
String s1 = "ravi";
String s2 = new String("ravi");
Integer i = 10;
Integer a1 = new Integer("10");
System.out.println(s1 == s1);
System.out.println(i == a1);
}
} Core Java
Ans. false
false Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  object equality  ==  coding  code Asked in 1 Companies Q107. Write a Program to check if the entered number is a Duck Number ( Number having 0 in it ) ? Core Java
This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  coding  duck number Q108. Suppose we have a string "Java is object oriented language" and we have to reverse every alternate word in string. How would we do that using Java program. Core Java
This question was recently asked at 'datalake solutions'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string manipulation  code  coding Asked in 1 Companies Q109. A Kid can go up 1, 2 or 3 stairs in one step. Write a method that takes number of steps in a stair will print all possible ways he can take.
For example , printAllCombinations(3) should print
111
12
21
3
Similarly , printAllCombinations(4) should print
1111
121
13
211
22
31
4 Core Java
Ans. public static String findStep(int n) {
if (n == 1 || n == 0)
return 1;
else if (n == 2)
return 2;
else
return findStep(n - 3) + findStep(n - 2) + findStep(n - 1);
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coding Asked in 1 Companies Q110. Write an efficient program for printing k largest elements in an array. Elements in array can be in any order. Data Structure
This question was recently asked at 'Amazon'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays  coding  code Asked in 1 Companies Q111. Create a custom hashmap using synchronization. Core Java
This question was recently asked at 'Deutsche Bank'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hashmap  coding Asked in 1 Companies advanced Q112. Write a Program to find number of lines , words and characters in a File. Core Java
Ans. import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileUtility {
public static void main(String[] args) {
System.out.println(printFile("/home/userme/notes"));
}
private static int printFile(String filePath) {
int wordCount = 0;
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line = null;
String[] wordsArray = null;
while ((line = br.readLine()) != null) {
wordsArray = line.split(" ");
wordCount = wordsArray.length;
}
} catch (IOException e) {
e.printStackTrace();
}
return wordCount;
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  coding Asked in 2 Companies Q113. Write code to get count of every 5 letter word in a string using lambda expression
Core Java
Ans. String str = "I had been saying that he had been there";
Map<String,Long> countWords = Arrays.asList(str.split(" ")).stream().filter(p->p.length() = 5).collect(Collectors.groupingBy(p->p,Collectors.counting()));
System.out.println(countWords); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Collectors  lambda  filter  coding  java 8 Q114. Find Intersection/union between two arrays. Core Java
This question was recently asked at 'Bristlecone,Amazon Lab126,Amazon,Microsoft,Facebook,NCR,Google'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays  code  coding Asked in 7 Companies   frequent Q115. Design a Program to clone an object and all it's children.
Design
This question was recently asked at 'Bloomberg'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  coding  cloning  design pattern   cloning design pattern Asked in 1 Companies Q116. Given a String with letters , numbers and special characters , extract Tokens from it. The rules for Token extraction are ac follows
1. Should for token of all characters till a number or special character is found
2. Should form token of all numbers till a character or special character is found
3. Special character in itself is a token
4. Ignore white spaces
For example - Bob Said "He is here"
should result in
Bob A
Said A
" S
He A
is A
here A
" S
where a is specifying alphabet and S as special character Design
This question was recently asked at 'Bloomberg'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  coding  string Asked in 1 Companies Q117. How many bytes a character takes with UTF-8 encoding ? Encoding
Ans. it takes 1 to 4 bytes Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  utf  unicode  utf-8 Asked in 1 Companies Q118. How to calculate string length without using inbuilt function Core Java
This question was recently asked at 'Softenger'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  coding  design  string length Asked in 1 Companies basic Q119. in the following class:
class A {
void methoda(Object o) {
Sysout("Object");
}
void methoda(String s) {
Sysout("String");
}
public static void main(String []args) {
A a = new A();
a.methoda(null);
}
}
what will be printed? Core Java
Ans. String Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Code  Coding  method overloading Asked in 1 Companies Q120. Can you share your experience with encoding and encryption ? Encoding
This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  encoding  encryption