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

   



Core Java - Interview Questions and Answers for 'Integer' - 6 question(s) found - Order By Newest

 Q1. What will the following code print ?

public static void main(String[] args){
Integer i1 = new Integer("1");
Integer i2 = new Integer("2");
Integer i3 = Integer.valueOf("3");
int i4 = i1 + i2 + i3;
System.out.println(i4);
}
Core Java
Ans. 6

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

   Like         Discuss         Correct / Improve     java   int   integer   scjp   ocjp


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


 Q3. Name few Integer related classes and interfaces ?
Ans. http://www.buggybread.com/2015/01/java-data-types-integer-classes-and.html

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

   Like         Discuss         Correct / Improve     java   data types   integer


 Q4. How does Java handle integer overflows and underflows?Core Java
Ans. It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.

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

   Like         Discuss         Correct / Improve     integer  data types     Asked in 1 Companies        rare


 Q5. What will be result of following code and why

Integer int1 = 1;
Integer int2 = 1;
String str1 = new String("str");
String str2 = new String("str");
String str3 = "str";
String str4 = "str";

      
System.out.println(int1 == int2);
System.out.println(str1 == str2);
System.out.println(str3 == str4);
Core Java
Ans. true
false
true

Just like Strings, java maintains an integer constant pool too. So 1 will be maintained in integer constant pool and hence reference int2 will point to same integer in pool.

String pool is only for string literals ( defined by "" ) and not for newly created objects and hence str1 == str2 will return false as they are separate objects in memory.

String pool is used for storing string literals so that they can be reused and str3 and str4 will point to same string literal in string pool.


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

   Like         Discuss         Correct / Improve     string pool  integer constant pool  string comparison  integer comparison


 Q6. Do we have Integer constant pool of primitive int or Integer objects ? Core Java
Ans. It's of Integer objects and not primitive int.

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

   Like         Discuss         Correct / Improve     integer constant pool



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: