Core Java - Interview Questions and Answers for 'Coding' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers - Order By Rating

   next 30
 Q31. 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


 Q32. 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


 Q33. 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


 Q34. 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


 Q35. Write a Program to validate if a particular character occurs after another in a stringCore 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


 Q36. Write a Program to validate an email addressCore 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


 Q37. Write a Program to print pattern like following

*
**
***
****
*****
******
*******
********
*********
**********
Core Java
Ans. public class BuggyBread {
   public static void main(String args[]) {
      for(int x=1;x<=10;x++){
         for(int y=1;y<=x;y++){
            System.out.print("*");
         }
         
         System.out.println("");
      }   
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     coding  code


 Q38. Write a Program to print asterix like following using Recursion

**
***
****
*****
******
*******
********
*********
**********

Program should set the start and end index and then display this accordingly. For example the above pattern is for displayNumbersBetween(2,10) and following is for displayNumbersBetween(5,7)

*****
******
*******
Core Java
Ans. public class BuggyBread {
   public static void main(String args[]) {
       displayNumbersBetween(5,7);
   }
   
   private static void displayNumbersBetween(int start,int end){
      if(start > end){
         return ;
      } else {
         for(int x=1;x<=start;x++){
            System.out.print("*");
         }
         System.out.println("");
         displayNumbersBetween(start+1,end);
      }
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     recursion  code  coding


 Q39. Write a Program to display numbers between 2 numbers using recursionCore Java
Ans. public class BuggyBread {
   public static void main(String args[]) {
       displayNumbersBetween(2,10);
   }
   
   private static void displayNumbersBetween(int start,int end){
      if(start > end){
         return ;
      } else {
         System.out.println(start);
         displayNumbersBetween(start+1,end);
      }
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     recursion  code  coding


 Q40. Write a Program to display the words of a String in alpahbetical or Dictionary OrderCore Java
Ans. public class BuggyBread {
   public static void main(String args[]) {
       String str = "we are what we repeatedly do excellence then is not an act but a haBit";
       Set<String> wordSet = new TreeSet(); // Using Linked Hash Set as we would like to retrieve words in the insertion order
      
       for(String word: str.split(" ")){
          wordSet.add(word);
       }
      
       for(String word: wordSet){
          System.out.print(word);
          System.out.print(" ");
       }
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     String  coding  code


 Q41. Write a Program to remove duplicate words from the StringCore Java
Ans.
public class BuggyBread {
public static void main(String args[]) {
String str = "we are what we repeatedly Do excellence, then, is not an act but a haBit";
Set<String> wordSet = new LinkedHashSet(); // Using Linked Hash Set as we would like to retrieve words in the insertion order

for(String word: str.split(" ")){
wordSet.add(word);
}

for(String word: wordSet){
System.out.print(word);
System.out.print(" ");
}
}
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     String  coding  code     Asked in 1 Companies      basic


 Q42. Write a program to print count / no of common words in 2 separate stringsCore Java
Ans. public class BuggyBread {
   public static void main(String args[]) {
       String str1 = "we are what we repeatedly Do excellence, then, is not an act but a haBit";
       String str2 = "we are what we repeatedly Do is";
       String[] str1Words = str1.split(" ");
       String[] str2Words = str2.split(" ");
      
       Set str1WordsSet = new HashSet();
      
       for(String word:str1Words){
          str1WordsSet.add(word);
       }
      
       int commonWordsCount = 0;
      
       for(String word:str2Words){
          if(str1WordsSet.contains(word)){
             commonWordsCount++;
          }
       }
      
       System.out.println(commonWordsCount);
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     String  coding  code


 Q43. Write a Program to find common words in 2 separate stringsCore Java
Ans. public class BuggyBread {
   public static void main(String args[]) {
       String str1 = "we are what we repeatedly Do excellence, then, is not an act but a haBit";
       String str2 = "we are what we repeatedly Do is";
       String[] str1Words = str1.split(" ");
       String[] str2Words = str2.split(" ");
      
       Set str1WordsSet = new HashSet();
      
       for(String word:str1Words){
          str1WordsSet.add(word);
       }
      
       for(String word:str2Words){
          if(str1WordsSet.contains(word)){
             System.out.println(word);
          }
       }
      
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     String  coding  code


 Q44. Given two files with list of words, write a program to show the common words in both filesCore Java
Ans. import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.*;
public class Common {
public static void main(String ar[])throws Exception
   {
      File f=new File("a.txt");
File f1=new File("x.java");
      System.out.println(f.exists());
FileInputStream fin = new FileInputStream(f);
      FileInputStream fin1 = new FileInputStream(f1);

      byte b[]=new byte[10000];
byte b1[]=new byte[10000];
fin.read(b);
fin1.read(b1);
String s1 = new String(b);
String s2 =new String(b1);

String words1[] = s1.trim().split(" ");
String words2[] = s2.trim().split(" ");
Listlist1 = new ArrayList<>(Arrays.asList(words1));
Listlist2 = new ArrayList<>(Arrays.asList(words2));
list1.retainAll(list2);
System.out.println(list1);
}
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     file handling  file io  code  coding     Asked in 1 Companies


 Q45. Write a Program to only print characters that have occured more than once in a StringCore Java
Ans. public class BuggyBread{
public static void main (String args[]) {
Set<Character> set = new HashSet();
Set<Character> setWithDuplicateChar = new HashSet();
String str = "hello world";
for(char character: str.toCharArray()){
if(set.contains(character)){
setWithDuplicateChar.add(character);
} else {
set.add(character);
}
}

System.out.println(setWithDuplicateChar);
}
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     String  Code  Coding


 Q46. Write a Program to print count of each character in a String.Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
Map<Character, Integer> countMap = new HashMap();
String str = "hello world";
for (char character : str.toCharArray()) {
if (countMap.containsKey(character)) {
countMap.put(character, countMap.get(character) + 1);
} else {
countMap.put(character, 1);
}
}
System.out.println(countMap);
}
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     String  Code  Coding  Character     Asked in 1 Companies


 Q47. Write a program to print the index of the first non repeated character in a java stringCore Java
Ans. public class BuggyBread1{
public static void main (String args[]) {
   String str = "hheello world";
   char[] charArray = str.toCharArray();
   char selectedChar = 'a';
   for(char char1: charArray){
      if(!str.contains(Character.toString(char1).concat(Character.toString(char1)))){
         selectedChar = char1;
         break;
      }
   }
   System.out.println(str.indexOf(Character.toString(selectedChar)));
}
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  code  coding     Asked in 9 Companies


 Q48. Write a program to print all even numbers first and then all odd numbers till 100. For example the output should be
2
4
6
8
...... 100
and then
1
3
5
7
...... 99

using only 1 for loop ? Is it possible with just one loop ?
Core Java
Ans. Yes , It can be done using single for loop

public class BuggyBread{
public static void main (String args[]) {
   int x = 50;
   for(int i=1;i <= 100;i++){
      if(i<=50){
         System.out.println(i*2);
      } else {
         System.out.println(i-x);
         x = x - 1;
      }
   }
}
}

 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


 Q49. Write a program in Java that prints the numbers from 1 to 100. But for multiples of three print Fizz instead of the number and for the multiples of five print Buz. For numbers which are multiples of both three and five print FizzBuzzCore Java
Ans. public class Class{
public static void main(String[] args){
for(int i=1;i<= 100;i++){
if(i%3 == 0){
if(i%5 == 0){
System.out.println("FizzBuzz");
}
System.out.println("Fizz");
} else if(i%5 == 0){
System.out.println("Buzz");
} else {
System.out.println(i);
}

}
}
}

 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


 Q50. Write a Program to check if string is a Colidrome ?
(Colidrome is a word that has n alphabets followed by the reverse of the n alphabets, for ex - mallom)
Core Java
Ans.
public class Class {
   public static void main(String[] args) {
      String str = "mallam";
      String firstHalf = str.substring(0, str.length() / 2);
      String secondHalf = str.substring(str.length() / 2);

      if (firstHalf.equals(new StringBuilder(secondHalf).reverse().toString())) {
         System.out.println("It's a Colidrome");
      } else {
         System.out.println("It's not a Colidrome");
      }
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Colidrome  String  Code  Coding     Asked in 1 Companies


 Q51. Write a Program to check number of occurrences of one string within another ? Core Java
Ans. public class Class{
   public static void main(String[] args){
      String string1 = "Hello I am Jack. I live in United States. I live in california state.";

      String string2 = "I live in";

      int startIndex = 0;
      int endIndex = string1.length()-1;

      int countNoOfOccurences = 0;

      String remainingString = string1;

      while(startIndex < endIndex){
         if(remainingString.indexOf(string2) != -1){
            countNoOfOccurences++;
            startIndex = remainingString.indexOf(string2) + string2.length();
            remainingString = remainingString.substring(startIndex);
         } else {
            break;
         }
      }

      System.out.println(countNoOfOccurences);
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  code  coding     Asked in 2 Companies


 Q52. Write a program to check is a string is substring of another ?Core Java
Ans. public class Class{
   public static void main(String[] args){
      String string1 = "Hello I am Jack. I live in United States. I live in california state.";

      String string2 = "I live in";

      if(string1.indexOf(string2) >= -1){
         System.out.println("string2 is sub string of string1");
      } else {
         System.out.println("string2 is not sub string of string1");
      }
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  coding  code


 Q53. Write a class / program that takes few numbers from the input and then output the average ?Core Java
Ans. public class Class{
   public static void main(String[] args){
      List<Integer> collector = new ArrayList();

      Scanner scanner = new Scanner(System.in);
      int x = scanner.nextInt();
      while(x != 0){
         collector.add(x);
         x = scanner.nextInt();
      }
      System.out.println(collector.stream().collect(Collectors.averagingInt(p->((Integer)p))));
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     coding  code  collector  streams  Collectors.average     Asked in 1 Companies


 Q54. What will be the output of following code ?

Base Interface

public interface BaseInterface {
   int i = 4;
}

Derived Interfaces

public interface DerivedInterface1 extends BaseInterface{
   int i = 5;
}

public interface DerivedInterface2 extends BaseInterface{
   int i=6;
}

Implementing Class

public class Class implements DerivedInterface1,DerivedInterface2 {
int i=10;   

public static void main(String[] args){
      System.out.println(new Class().i);
   }
}

What will be the output upon executing main and Why ?
Core Java
Ans. 10

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     interfaces  coding  code      intermediate


 Q55. What will be the output of following code ?

Base Interface

public interface BaseInterface {
   int i = 4;
}

Derived Interfaces

public interface DerivedInterface1 extends BaseInterface{
   int i = 5;
}

public interface DerivedInterface2 extends BaseInterface{
   int i=6;
}

Implementing Class

public class Class implements DerivedInterface1,DerivedInterface2 {
   public static void main(String[] args){
      System.out.println(BaseInterface.i);
   }
}

What will be the output upon executing main and Why ?
Core Java
Ans. It will print 4 because member elements of an interface are implicitly static and hence the concept of overriding doesn't work.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     interfaces  coding  code  extending interfaces  diamond interfaces     Asked in 1 Companies      intermediate


Usually asked to entry level software developers.
  Q56. Write a program to swap two variables without using thirdCore Java
Ans. public static void main(String[] args) {
   int num1 = 1;   
   int num2 = 2;
   num1 = num1^num2;
   num2 = num1^num2;
   num1 = num1^num2;
   System.out.print("num1 = " + num1 +", num2 = "+num2);
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     code  coding     Asked in 37 Companies      basic        frequent


 Q57. Write an algorithm / Java Program to show duplicates in an array of n elements?Algorithm
Ans. int duplicateArray[] = { 1, 2, 2, 3, 4, 5, 6, 8, 9}
Set unique = new HashSet();
for (int i = 0; i < duplicateArray.length; i) {
if (unique.contains(duplicateArray[i])) {
System.out.println(duplicateArray[i]);
} else {
unique.add(duplicateArray[i]);
}
}

Complexity O(n) = nHashSet contains and add has O(n) = 1

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     coding  code     Asked in 2 Companies


 Q58. Trim a string without using String library methodCore Java
 This question was recently asked at 'Ariba'.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  trim a string     Asked in 1 Companies


Frequently asked in high end product companies.
 Q59. Write code for LRU CacheCore Java
Ans. https://www.geeksforgeeks.org/lru-cache-implementation/

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     cache  LRU cache  coding  code     Asked in 10 Companies      intermediate


 Q60. Provide an implementation for Producer / Consumer using threads.Core Java
Ans. https://www.tutorialspoint.com/javaexamples/thread_procon.htm

 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


previous 30   next 30

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: