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. |
|
| ||||
Core Java - Interview Questions and Answers for 'Coding' - 122 question(s) found - Order By Newest | ||||
| ||||
Ans. public static void main(String ar[]) { int n=5; if((n/2)*2==n) { System.out.println("Even Number "); } else { System.out.println("Odd Number "); } } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   tricky questions   interesting questions   modulus operator Asked in 4 Companies   frequent | ||||
| ||||
Ans. FALSE. == operator compares object references, a and b are references to two different objects, hence the FALSE. .equals method is used to compare string object content. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  string   string class   java   ==   object references   coding Asked in 2 Companies basic   frequent | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. NullPointerException at line: "File file = new File("/folder", name);" | ||||
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 | ||||
| ||||
Ans. Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  character coding   ascii   unicode   utf | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. It will create the file myfile.txt in the current directory. | ||||
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 | ||||
| ||||
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 | ||||
Usually asked to entry level software developers. | ||||
| ||||
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 | ||||
| ||||
Ans. true, due to String Pool, both will point to a same String object. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   tricky questions   interesting questions   string   string pool   .equal   == intermediate   frequent | ||||
| ||||
Ans. Compile time error as it won't find the constructor matching BuggyBread2(). Compiler won't provide default no argument constructor as programmer has already defined one constructor. Compiler will treat user defined BuggyBread2() as a method, as return type ( void ) has been specified for that. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   tricky questions   interesting questions   default constructor   constructor | ||||
Try 3 Question(s) Test | ||||
| ||||
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   objectoutputstream   fileoutputstream   yesno  file handling | ||||
| ||||
Ans. matcher.find() should have been used instead of matcher.next() within while. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   regex   pattern.matcher   java.util   coding   code | ||||
| ||||
Ans. It will print 4567,5678 and 6789 but Order cannot be predicted. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   collections   set   hashset   coding   code | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. This will give compile time error as we cannot retrieve the element from a specified index using Set. Set doesn't maintain elements in any order. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   collections   set   hashset   coding   code | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. WEDNESDAY FRIDAY SATURDAY Only one FRIDAY will be printed as Set doesn't allow duplicates.Elements will be printed in the order in which constants are declared in the Enum. TreeSet maintains the elements in the ascending order which is identified by the defined compareTo method. compareTo method in Enum has been defined such that the constant declared later are greater than the constants declared prior. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   enum   set   treeset   advanced | ||||
Try 3 Question(s) Test | ||||
| ||||
Ans. int findMax(int[] items){ int maxNumber = 0; for(int x:items){ if(x > maxNumber){ maxNumber = x; } } return maxNumber; } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  coding  code  find max number in an array Asked in 1 Companies | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
Ans. private void permutation(String prefix, String sufix) { int ln = sufix.length(); if(ln == 0) { System.out.println(prefix); } else { IntStream.range(0, ln).forEach(i->permutation(prefix sufix.charAt(i), sufix.substring(0,i) sufix.substring(i 1, ln))); } } call:permutation("", "abcdef"); | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  String  code  coding Asked in 1 Companies | ||||
| ||||
Ans. File folder = new File(path); if(!folder.exists()){ try { folder.mkdir(); } catch (Exception e) {} } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  code  coding  file handling basic | ||||
| ||||
Ans. A way of encoding characters as binary numbers. The Unicode character set includes characters used in many languages, not just English. Unicode is the character set that is used internally by Java. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   character encoding   unicode   architecture | ||||
| ||||
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 | ||||
| ||||
Ans. public class TotalUsingThreads extends Thread{ private static List<TotalUsingThreads> collector = new ArrayList<TotalUsingThreads>(); private int startFrom = 0; private Integer threadTotal = null; Test(int startFrom){ this.startFrom = startFrom; } public static void main(String[] args) throws InterruptedException{ int totalSum = 0; // Create all the threads and set the count starting point for all of them for(int count=0;count<10;count++){ TotalUsingThreads newThread = new TotalUsingThreads(count*100); newThread.start(); collector.add(newThread); } boolean allCollected = false; // Make sure that all thread totals are collected and all threads have completed their tasks while(!allCollected){ for(int count=0;count<10;count++){ if(collector.get(count).threadTotal == null){ Thread.sleep(100); break; } } allCollected = true; } // sum totals of all threads for(int count=0;count<10;count++){ totalSum += collector.get(count).threadTotal; } System.out.println(totalSum); } public void run(){ threadTotal = 0; for(int count=startFrom;count<startFrom+100;count++){ threadTotal += count; } } } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  coding  code  threads  multithreading Asked in 1 Companies | ||||
| ||||
Ans. convert(int binaryInt) { int sumValue=0; int multiple = 1; while(binaryInt > 0){ binaryDigit = binaryInt%10; 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  java   binary   interesting question   coding | ||||
| ||||
Ans. equal 1 | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string   string pool   code   coding basic   frequent | ||||
| ||||
Ans. BuggyBread | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference | ||||
| ||||
Ans. Buggy | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference | ||||
| ||||
Ans. Buggy | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference | ||||
| ||||
Ans. Compile Time Error: Unhandled exception type Exception | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   overridding   late binding   exception handling   abstract class   abstract methods | ||||
| ||||
Ans. file.txtC:file.txtC:file.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 | ||||