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 Newest

   next 30
 Q31. What will be the output of following code ?

public static void main(String[] args){
String child = null;
File file = new File("/folder", child);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Core Java
Ans. NullPointerException at line:File file = new File("/folder", child);

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

   Like         Discuss         Correct / Improve     java   io   file   fileio   coding   code   file handling


 Q32. What will be the output of following code, assuming that currently we are in c:Project ?

public static void main(String[] args){
String child = null;
File file = new File("../file.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
try {
System.out.println(file.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Core Java
Ans. ..file.txt
C:WorkspaceProject..file.txt
C:Workspacefile.txt

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

   Like         Discuss         Correct / Improve     java   io   file   fileio   coding   code   file handling


 Q33. Which of the following code is correct ?

a.


FileWriter fileWriter = new FileWriter("../file.txt");
File file = new File(fileWriter );
BufferedWriter bufferedOutputWriter = new BufferedWriter(fileWriter);

b.

BufferedWriter bufferedOutputWriter = new BufferedWriter("../file.txt");
File file = new File(bufferedOutputWriter );
FileWriter fileWriter = new FileWriter(file);


c.

File file = new File("../file.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedOutputWriter = new BufferedWriter(fileWriter);

d.

File file = new File("../file.txt");
BufferedWriter bufferedOutputWriter = new BufferedWriter(file);
FileWriter fileWriter = new FileWriter(bufferedOutputWriter );
Core Java
Ans. c.

File file = new File("../file.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedOutputWriter = new BufferedWriter(fileWriter);

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

   Like         Discuss         Correct / Improve     java   io   file   fileio   coding   code   filewriter   bufferedwriter   scjp   ocjp  file handling


 Q34. Which exception should be handled in the following code ?

File file = new File("../file.txt");
FileWriter fileWriter = new FileWriter(file);
Core Java
Ans. IOException

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

   Like         Discuss         Correct / Improve     java   io   file   fileio   coding   code   exception   ioexception  file handling


 Q35. Which exceptions should be handled with the following code ?

FileOutputStream fileOutputStream = new FileOutputStream(new File("newFile.txt"));
Ans. FileNotFoundException

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

   Like         Discuss         Correct / Improve     java   io   file   fileio   coding   code   scjp   ocjp   filenotfoundexception   fileoutputstream  file handling


 Q36. What is the problem with this code ?

class BuggyBread1 {

private BuggyBread2 buggybread2;

public static void main(String[] args){
try {
BuggyBread1 buggybread1 = new BuggyBread1();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
objectOutputStream.writeObject(buggybread1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Core Java
Ans. Though we are trying to serialize BuggyBread1 object but we haven't declared the class to implement Serializable.

This will throw java.io.NotSerializableException upon execution.

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

   Like         Discuss         Correct / Improve     java   io   file   fileio   coding   code   serialization   notserializableexception   exception   file handling


 Q37. Will this code run fine if BuggyBread2 doesn't implement Serializable interface ?

class BuggyBread1 implements Serializable{
private BuggyBread2 buggybread2 = new BuggyBread2();

public static void main(String[] args){
try {
BuggyBread1 buggybread1 = new BuggyBread1();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
objectOutputStream.writeObject(buggybread1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Core Java
Ans. No, It will throw java.io.NotSerializableException.

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

   Like         Discuss         Correct / Improve     java   io   file   fileio   coding   code   serialization   notserializableexception   exception   file handling


 Q38. Will this code work fine if BuggyBread2 doesn't implement Serializable ?

class BuggyBread1 extends BuggyBread2 implements Serializable{
private int x = 5;

public static void main(String[] args){
try {
BuggyBread1 buggybread1 = new BuggyBread1();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
objectOutputStream.writeObject(buggybread1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Core Java
Ans. Yes.

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

   Like         Discuss         Correct / Improve     java   io   file   fileio   coding   code   serialization   yesno  file handling


 Q39. What will the following code print ?

java.util.Calendar c = java.util.Calendar.getInstance();
c.add(Calendar.MONTH, 5);
System.out.println(c.getTime());
Core Java
Ans. Date and Time after 5 months from now.

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

   Like         Discuss         Correct / Improve     java   date   calendar   scjp   ocjp   coding   code


 Q40. Write a program to see if the number is perfect number or not ?Core Java
Ans. http://www.c4learn.com/c-programs/program-to-check-whether-number-is.html

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

   Like         Discuss         Correct / Improve     java   at&t   coding


 Q41. What will be the output of this code ?

Set mySet = new TreeSet();
mySet.add("4567");
mySet.add("5678");
mySet.add("6789");
for(String s: mySet){
System.out.println(s);
}
Ans. 4567
5678
6789

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

   Like         Discuss         Correct / Improve     java   collections   set   treeset   coding   code


 Q42. public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

public class Test {
Set mySet = new HashSet();
mySet.add(Day.MONDAY);
mySet.add(Day.SUNDAY);
mySet.add(Day.SATURDAY);

for(Day d: mySet){
System.out.println(d);
}
}
Ans. SUNDAY
MONDAY
SATURDAY

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

   Like         Discuss         Correct / Improve     enum   set   collections   hashset   coding   code

Try 3 Question(s) Test


 Q43. What will be the output of the following code ?

enum Day {
   MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
}

public class BuggyBread1{
   public static void main (String args[]) {
      Set mySet = new HashSet();
      mySet.add(Day.SATURDAY);
      mySet.add(Day.WEDNESDAY);
      mySet.add(Day.FRIDAY);
      mySet.add(Day.WEDNESDAY);
      for(Day d: mySet){
         System.out.println(d);
      }
   }
}
Ans. FRIDAY , SATURDAY and WEDNESDAY will be printed but the order cannot be determined.

Only one FRIDAY will be printed as Set doesn't allow duplicates. Order cannot be determined as HashSet doesn't maintain elements in a particular order. 

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

   Like         Discuss         Correct / Improve     java   code   coding   enum   set   hashset

Try 3 Question(s) Test


 Q44. public class BuggyBread1{

public static void main (String args[]) {
Set<String> mySet = new TreeSet<String>();
mySet.add(""1"");
mySet.add(""2"");
mySet.add(""111"");
for(String d: mySet){
System.out.println(d);
}
}
}
Core Java
Ans. 1
111
2

TreeSet maintains the elements in the ascending order which is identified by the compareTo method. compareTo method in String has been defined such that it results in the natural alphabetic Order. Here the elements in the TreeSet are of String and not of Integer. In String Natural Order, 111 comes before 2 as ascii of 1st character first determines the order.

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

   Like         Discuss         Correct / Improve     java   code   coding   set   treeset   string   integer


 Q45. public class BuggyBread1{

public static void main (String args[]) {
Set<Integer> mySet = new TreeSet<Integer>();
mySet.add(1);
mySet.add(2);
mySet.add(111);
for(Integer d: mySet){
System.out.println(d);
}
}
}
Core Java
Ans. 1
2
111

TreeSet maintains the elements in the ascending order which is identified by the compareTo method. compareTo method in Integer has been defined such that it results in the natural numerical Order.

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

   Like         Discuss         Correct / Improve     java   code   coding   set   treeset


 Q46. Will this Code compile ?

abstract public class BuggyBread1{
abstract public void test(){};
}
Core Java
Ans. No. This will give a compilation error saying "Abstract methods do not specify a body".

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

   Like         Discuss         Correct / Improve     java   abstract classes   abstract methods   java compilation error   java coding   java code   coding   yes-no


 Q47. Write program for input: I am a boy, output: I1 am2 a1 boy3?
Ans. public static void main (String args[]) {
String inputStr = "I am a boy, output: I1 am2 a1 boy3";

String splitStr[] = inputStr.split(" ");

String outputStr = new String();

for(String str1: splitStr){
outputStr += str1 + str1.length() + " ";
}

System.out.println(outputStr);
}

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

   Like         Discuss         Correct / Improve     java   coding


 Q48. What is the difference between out.println(a+b); and out.println (a+" " +b); in Java? with a=2 and b=1Core Java
Ans. First will give the ouput as 3 and the second will give output as 2 1

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

   Like         Discuss         Correct / Improve     java   system.out.println   coding


 Q49. How many elements will be there in TreeSet after the last line and Why ?

TreeSet set = new TreeSet();
set .add(new String("abc"));
set .add(new String("abc"));
Core Java
Ans. One.

As we haven't specified the type of TreeSet, it being evaluated with the first element insertion. Once it's identified that it's of type String and as no comparator has been defined, the comparison is done using the String compareTo method. String compareTo method compares the elements by the content / value.

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

   Like         Discuss         Correct / Improve     java   set   treeset   string   compareto   coding   code


 Q50. What will the following code result ?

List> intList = new ArrayList>();

intList.add(Optional.empty());
intList.add(Optional.of(2));
intList.add(Optional.of(3));
intList.add(Optional.of(4));

System.out.println(intList.get(null));
Ans. Compile time error at last line as the get method expect argument of type native int.

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

   Like         Discuss         Correct / Improve     arraylist   list   coding   collections


 Q51. What will the following code result ? Will it compile ?

List> intList = new ArrayList>();

intList.add(Optional.empty());
intList.add(Optional.of(2));
intList.add(Optional.of(3));
intList.add(Optional.of(4));

System.out.println(intList.get((Integer)null));
Ans. Yes but the last line will throw NullPointerException upon execution.

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

   Like         Discuss         Correct / Improve     collections   arraylist   list   coding


 Q52. What will be the output ?Integer a = 10, b =10;Integer c = 10, d = 1000;System.out.println(a == b);System.out.println(c ==d);Core Java
Ans. truefalse

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

   Like         Discuss         Correct / Improve     java   coding   equality   object equality


 Q53. How many bits are used to represent Unicode ?

a. 8 bits
b. 16 bits
c. 24 bits
d. 32 bits
Ans. 16 bits

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

   Like         Discuss         Correct / Improve     java   character coding   character encoding


 Q54. Print natural numbers sequentially with two threads.Core Java
Ans. http://stackoverflow.com/questions/18799591/print-natural-sequence-with-help-of-2-threads1-is-printing-even-and-2nd-is-pri

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

   Like         Discuss         Correct / Improve     print natural numbers with two threads   multithreading   multi threading   threads   code   coding   makemytrip.com


 Q55. Find the repeating number using O(n) time and constant space.Algorithm
Ans. http://www.geeksforgeeks.org/find-duplicates-in-on-time-and-constant-extra-space/

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

   Like         Discuss         Correct / Improve     algorithm   program   code   coding  makemytrip.com


 Q56. Write a method to convert all elements of a set to lower case.Core Java
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?keyword=Method+to+convert+all+elements+of+a+collection&category=code

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

   Like         Discuss         Correct / Improve     code  coding  string  String.toLowerCase  set  collections


 Q57. Write code for singleton classDesign
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?keyword=singleton+class&category=code

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

   Like         Discuss         Correct / Improve     singleton  code  coding  design pattern     Asked in 1 Companies      Intermediate        frequent


 Q58. Write a program / method to input few numbers and then print the sum.
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?&category=code&searchOption&keyword=946

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

   Like         Discuss         Correct / Improve     code  coding  scanner      basic


Very frequently asked to fresh graduates. Frequently asked in NSEiT and Accenture India
 Q59. Write a program to Generate prime numbers till a given numberCore Java
Ans. public class Main {
public static void main(String args[]) {
int number = 2;
int count = 0;
long sum = 0;
int givenNumber = 1000;
while (count < givenNumber) {
if (isPrimeNumber(number)) {
sum = number;
count;
}
number;
}
System.out.println(sum);
}
private static boolean isPrimeNumber(int number) {
for (int i = 2; i <= number / 2; i) {
if (number % i == 0) {
return false;
}
}
return true;
}
}

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

   Like         Discuss         Correct / Improve     generate prime numbers  code  coding     Asked in 5 Companies      basic        frequent


 Q60. Write a program to calculate factorial of a number using recursionCore Java
Ans.

public class Factorial {
   public static void main(String[] args){
      int x = 5;
      
      System.out.println(calculateFactorial(x));
   
   }
   
   private static int calculateFactorial(int x){
      if(x==1){
         return 1;
      }
      return x * calculateFactorial(x-1);
   }
}

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

   Like         Discuss         Correct / Improve     factorial  calculate factorial  code  coding  recursion     Asked in 1 Companies      basic


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: