Interview Questions and Answers for 'Code' | 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
 Q91. In the following code , how many methods needs to be implemented in Class B ?

public interface A{
   public void method1();
   public void method2();
   public void method3();
}

abstract class B implements A{
}
Core Java
Ans. As Class B has been declared abstract , we can either implement any of these methods and just declare rest of them abstract.

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

   Like         Discuss         Correct / Improve     interfaces  abstract classes  code  coding

Try 2 Question(s) Test


 Q92. What will the following code print ?

Integer a = 100, b =100;
Integer c = 1000, d = 1000;
System.out.println(a == b);
System.out.println(c ==d);
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  code  coding        frequent


 Q93. How do you manage source code in repository if you have multiple releases lined up and different teams are working on it? Tools
Ans. We creates separate branches for each project if development work is going on parallel and they are to be released at different times. Once the first release is done, we merge the branch changes into trunk. If they all have to go at one time, we usually would merge everything in the trunk itself.

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

   Like         Discuss         Correct / Improve     scm  source code management  svn  version control  code repository  svn branch     Asked in 1 Companies


 Q94. Is it advisable to just hold checking in your changes to trunk if there is another release planned in between ?
Ans. If it's just smaller change, and single person is working, then this approach is fine. Otherwise there are risk on loosing it on your machine. Moreover , If there are multiple people working , it makes it hard to share code. It's better to create a separate branch and then merge it later to trunk.

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

   Like         Discuss         Correct / Improve     scm  source code management  svn  version control  code repository  svn trunk


 Q95. What are the steps to create a branch in SVN ?
Ans. If the branch is to be created from Trunk and we are using Eclipse.

Go to the Trunk Copy of the Project

Right Click the project and then Click Branch/Tag

In the Create Branch / Tag Dialog, Add the Destination Branch Url

Check whether we want to make copy from the Head Revision or some specific revision Number

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

   Like         Discuss         Correct / Improve     scm  source code management  svn  version control  code repository  svn branch


 Q96. What is the problem with following code ?

public static void main(String[] args) {
int x = 5;
return x*2;

int y = 10;
return y*2;

}
Ans. There are many problems with the code

1. The method returns void and hence we cannot return any integer value.

2. We cannot return more than one value from a method.

3. The code after 1st Return statement is unreachable.

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

   Like         Discuss         Correct / Improve     coding  code


 Q97. Is this code legal in Java ?

public class BuggyBread {
{
System.out.println("HelloWorld.");
}
}
Ans. Yes, It's an instance initialization block.

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

   Like         Discuss         Correct / Improve     code  instance initialization block

Try 1 Question(s) Test


 Q98. What is wrong with the following if statement ?

if(x==y && x.equals(y) {
}
Core Java
Ans. x==y means that both references have same type and are pointing to same memory location and hence would always mean that they have same value.

x.equals(y) is not required in this case.

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

   Like         Discuss         Correct / Improve     if statement  control statements  == and equals  ==  .equals   code optimization

Try 2 Question(s) Test


 Q99. What is wrong with the following if statement ?

if(x==y || x.equals(y) {
}
Core Java
Ans. if x==y turns out to be true x.equals(y) will be true too. If x.equals(y) could be true even if x==y is true or not.

So the only possible outcomes are

1 || 1 = 1
0 || 1 = 1
0 || 0 = 0

i.e the outcome of x.equals(y)

check for x==y is not required in this if statement.

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

   Like         Discuss         Correct / Improve     if statement  control statements  == and equals  ==  .equals   code optimization

Try 1 Question(s) Test


 Q100. What does the web status code 404,500 and 200 mean ?Java EE
Ans. 404 means the resource is not found on the server.The resource might not be deployed correctly.

500 means internal server error which means that resource was located but then it resulted in some exception or error.

200 means Ok.

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

   Like         Discuss         Correct / Improve     web status code     Asked in 1 Companies

Try 1 Question(s) Test


 Q101. Write a Java Program to check Armstrong Number ?Core Java
Ans. http://www.programmingsimplified.com/java/source-code/java-program-armstrong-number

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

   Like         Discuss         Correct / Improve     program  code  coding  armstrong number     Asked in 1 Companies


 Q102. How would you count the number of words in a string consisting of uneven number of spaces between words( not dictionary words)? With and without library functions.Core Java
Ans. package com.string;

import java.util.Scanner;

public class String13 {
public static void main(String[] args) {
System.out.println("Enter Sentence");
Scanner sc=new Scanner(System.in);
String sentence=sc.nextLine();
String[] words=sentence.split(" ");
int count=0;
for (String string : words) {
string.trim();
if(!string.equals("")){
count++;
System.out.println(string " " count);
}
}
}
}

 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


 Q103. Write a Program to print the positive and negative numbers separetlyCore Java
Ans. int[] arr = {1,-1,2,-3,3,-4,4,5,6,-5,-6,-7,-8,8,9,-9};
List positiveNumbers = new ArrayList<>();
List negativeNumbers = new ArrayList<>();
for(int i = 0; i < arr.length(); i ){
if(I < 0){
negativeNumbers.add(i);
} else {
positiveNumbers.add(i);
}
}

System.out.println("Positive Numbers:" + positiveNumbers);
System.out.println("Negative Numbers:" + negativeNumbers);

 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


 Q104. Explain some of the http status codes you know ?Java EE
Ans. 500 is Internal Server Error

404 is resource not found

400 is Bad Request

403 is Forbidden

401 is Unauthorized

200 is OK

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

   Like         Discuss         Correct / Improve     http  http status codes     Asked in 2 Companies        frequent


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


 Q106. How is hashcode calculated in java ?

or

What are the rules for hashcode calculation ?
Core Java
Ans. General contract of hashCode is:

1.Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer,

2.If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

3.It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.

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

   Like         Discuss         Correct / Improve     hashcode     Asked in 1 Companies      expert


Frequently asked in high end product companies.
 Q107. 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


 Q108. Do you feel that its useless to define hashCode method for a class ?Core Java
Ans. Yes its useless if we are not going to use its objects within Hash collection, For example - HashSet , HashMap. HashCode is used internally by these collections for Search.

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

   Like         Discuss         Correct / Improve     hashcode  hash collections  search


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


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


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


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


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


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


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


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


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


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


 Q119. What are the ways to Reuse Code in Java ?Core Java
Ans. 1. Object Level Reuse - private methods
2. Class Level Reuse - static methods
3. package level Reuse - default methods
4. Application Level Reuse - Classes
5. Multiple Applications Level Reuse - Libraries , Frameworks

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

   Like         Discuss         Correct / Improve     Code Reuse


 Q120. Write code to find second largest number in an array of integers.Core Java
Ans. int arr[]={1,3,5,6,4,8,9,2,10};
Arrays.sort();
System.out.println(arr[arr.length-1]);

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

   Like         Discuss         Correct / Improve     code  coding  find in array     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: