Interview Questions and Answers for 'Java' | 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 Newest

   next 30
 Q941. What is memory leak ? How Java helps countering memory leaks compared to C++ ?Core Java
Ans. Memory Leak is a situation wherein we have a reserved memory location but the program has lost its reference and hence has no way to access it.

Java doesn't have concept of Pointers, Moreover Java has concept of Garbage collection that frees up space in heap that has lost its references.

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

   Like         Discuss         Correct / Improve     memory leak  garbage collection      intermediate        frequent


 Q942. What are the benefits of creating immutable objects ?Core Java
Ans. 1. Security and Safety - They can be shared across multiple threads as they are thread safe. Moreover, it protects then from bad state due to interception by the other code segment. One such problem due to mutability and access by alternate code segment could be the change of hash code and then the impact on its search with hash collections.

2. Reuse - In some cases they can be reused as only one copy would exist and hence it can be relied upon. For example - String Pool

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

   Like         Discuss         Correct / Improve     immutable  immutability objects     Asked in 1 Companies      Intermediate        frequent


 Q943. What is the difference between

File f = new File("homexyz.txt");

and

File f = File.createTempFile("xyz",".txt","home"); ?
Core Java
Ans. First will not create physical file in storage whereas the second will.

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

   Like         Discuss         Correct / Improve     file handling  File.createTempFile  file handling      intermediate        rare


 Q944. What are the different J2EE modules ?Java EE
Ans. 1. Application Client Module
2. Web Module
3. EJB
4. Resource Adapter

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

   Like         Discuss         Correct / Improve     j2ee  j2ee modules


 Q945. What does the Web module contains ?Java EE
Ans. JSP files, media files and deployment descriptor ( web.xml )

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

   Like         Discuss         Correct / Improve     j2ee  j2ee web module


 Q946. What is an applet ?Core Java
Ans. Applet is a J2EE component or a tiny application that gets executed in a widget engine ( like browser )

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

   Like         Discuss         Correct / Improve     applet


 Q947. What are callback methods ?Core Java
Ans. Component methods called by the container to notify the component of the events

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

   Like         Discuss         Correct / Improve          Asked in 1 Companies


 Q948. How and Why Strings are interned in Java ?Core Java
Ans. String class has a public method intern() that returns a canonical representation for the string object. String class privately maintains a pool of strings, where String literals are automatically interned.

these are automatically interned so as to have efficient String comparison using == operator instead of equals which is usually slower.

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

   Like         Discuss         Correct / Improve     string  string pool  string intern     Asked in 1 Companies


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


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


 Q951. What is the difference between compilation and decompilation ?Core Java
Ans. Compilation is the process of converting Java source files to class files which can be executed by the Java runtime environment whereas Decompilation is the process of converting class files back to the Java Source code.

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

   Like         Discuss         Correct / Improve     compilation vs decompilation  compiler vs decompiler


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


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


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


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


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


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


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


 Q959. Write a Program to print all numbers till 100 that are divisible by 5Core Java
Ans. public class BuggyBread{
public static void main (String args[]) {
   for(int counter=1;counter<=100;counter++){
      if(counter % 5 == 0){
         System.out.println(counter);
      }
   }
}
}

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

   Like         Discuss         Correct / Improve     


 Q960. Write a Program to print 1 (1 time) , 2 ( 2 times ) and so on till 100.Core Java
Ans. public class BuggyBread1{
public static void main (String args[]) {
   for(int x=1;x<=100;x++){
      for(int y=1;y <= x;y++){
         System.out.println(x);
      }
   }
}
}

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

   Like         Discuss         Correct / Improve     


 Q961. Write a Program to print series like

1
12
123
1234
12345

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

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

   Like         Discuss         Correct / Improve     


 Q962. Write a program for 0 and cross games? Core Java
 This question was recently asked at 'Compro Technologies'.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.0 and cross game     Asked in 1 Companies


 Q963. Given a sorted array, write a program to check if two elements sum up to third. Core Java
 This question was recently asked at 'Facebook'.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          Asked in 1 Companies


 Q964. Write a program to print two digit numbers that fulfil following criteria

Summing their digits and then multiplying with 3 should result in the number

For ex - 27 , (2+7) * 3 = 27


Core Java
Ans. public class BuggyBread {
   public static void main(String args[]) {
      for(int x=10;x<100;x++){
         int tensDigit = x/10;
         int unitDigit = x%10;
         
         if((tensDigit+unitDigit)*3 == x){
            System.out.println(x);
         }
      }
   }
}

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

   Like         Discuss         Correct / Improve          Asked in 1 Companies


 Q965. Write Code showing InheritanceCore Java
Ans. class A {
public void test() {
System.out.println("Class A Test");
}
}

class B extends A {
public void test() {
System.out.println("Class B Test");
}
}

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

   Like         Discuss         Correct / Improve          Asked in 1 Companies


 Q966. Write a Program / Code showing Inheritance and overriddingCore Java
Ans. class A {
public void test() {
System.out.println("Class A Test");
}
}

class B extends A {
public void test() {
System.out.println("Class B Test");
}

public static void main(String[] args){
A a = new B();
a.test(); // will print Class B Test
}
}

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

   Like         Discuss         Correct / Improve          Asked in 1 Companies


 Q967. Write a Program to find the longest word in a 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.";
      String longestWord = "";
      
      str = str.replaceAll(",","");
      str = str.replaceAll(";","");
      str = str.replaceAll("/.","");
      
      for(String word: str.split(" ")){
         if(word.length() > longestWord.length()){
            longestWord = word;
         }
      }
      
      System.out.println(longestWord);
   }
}

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

   Like         Discuss         Correct / Improve     


 Q968. Write a Program that prints the repeated words in a String

For example -

We are what we repeatedly do; excellence, then, is not an act but a habit.

should print we, should count for case insensitive words too
Core Java
Ans. public class BuggyBread {
   public static void main(String args[]) {
      Map<String,Integer> wordLength = new HashMap();
      
      String str = "We are what we repeatedly do; excellence, then, is not an act but a habit.";
      String longestWord = "";
      
      str = str.replaceAll(",","");
      str = str.replaceAll(";","");
      str = str.replaceAll("/.","");
      str = str.toLowerCase();
      
      for(String word: str.split(" ")){
         if(wordLength.containsKey(word)){
            wordLength.put(word, wordLength.get(word).intValue() + 1);
         } else {
            wordLength.put(word, 1);
         }
      }
      
      for(Map.Entry<String, Integer> entry:wordLength.entrySet()){
         if(entry.getValue().intValue() > 1){
            System.out.println(entry.getKey());
         }
      }
   }
}

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

   Like         Discuss         Correct / Improve     


 Q969. Write a Program that prints words longer than 5 characters in a String

For example

We are what we repeatedly do; excellence, then, is not an act but a habit

should print

repeatedly
excellence
Core Java
Ans. public class BuggyBread1 {
   public static void main(String args[]) {
      Map<String,Integer> wordLength = new HashMap();
      
      String str = "We are what we repeatedly do; excellence, then, is not an act but a habit";
      String longestWord = "";
      
      str = str.replaceAll(",","");
      str = str.replaceAll(";","");
      str = str.toLowerCase();
      
      for(String word: str.split(" ")){
            wordLength.put(word, word.length());
      }
      
      for(Map.Entry<String, Integer> entry:wordLength.entrySet()){
         if(entry.getValue().intValue() > 5){
            System.out.println(entry.getKey());
         }
      }
   }
}

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

   Like         Discuss         Correct / Improve     


 Q970. Write a Program to print 3 smallest words in a StringCore Java
Ans. public class BuggyBread {
   public static void main(String args[]) {
      Map<String,Integer> wordLength = new HashMap();
      
      String str = "We are what we repeatedly do; excellence, then, is not an act but a habit";
      
      str = str.replaceAll(",","");
      str = str.replaceAll(";","");
      str = str.toLowerCase();
      
      for(String word: str.split(" ")){
            wordLength.put(word, word.length());
      }
      
      List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>( wordLength.entrySet() );
      
      Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o1.getValue()).compareTo( o2.getValue() );
}
} );
      
      int countWords = 1;
      
      for(Map.Entry<String, Integer> entry:list){
         if(countWords <= 3){
            System.out.println(entry.getKey());
         }
         
         countWords++;
         
      }
   }
}

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

   Like         Discuss         Correct / Improve     


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: