Q1. What are the Wrapper classes available for primitive types ?
Ans. boolean - java.lang.Boolean
byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void
Q2. Will this code give error if i try to add two heterogeneous elements in the arraylist. ? and Why ?
List list1 = new ArrayList<>();
list1.add(5);
list1.add("5");
Ans. If we don't declare the list to be of specific type, it treats it as list of objects.
int 1 is auto boxed to Integer and "1" is String and hence both are objects.
Q3. How to find whether a given integer is odd or even without use of modules operator in java?
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 ");
}
}
}
Q4. Is it legal to initialize List like this ?
LinkedList<Integer> l=new LinkedList<int>();
Ans. No, Generic parameters cannot be primitives.
Q5. 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);
}
Ans. 6
Q6. Which of the following syntax are correct ?
a. LinkedList<Integer> l=new LinkedList<int>();
b. List<Integer> l=new LinkedList<int>();
c. LinkedList<Integer> l=new LinkedList<Integer>();
d. List<Integer> l = new LinkedList<Integer>();
Ans. c and d are correct.
Q7. Can we compare Integers by using equals() in Java ?
Ans. Yes for the Wrapper class Integer but not for the primitive int.
Q8. Name few Integer related classes and interfaces ?
Ans. http://www.buggybread.com/2015/01/java-data-types-integer-classes-and.html
Q9. Which is the Parent Class of Integer class?
Ans.Number
Q10. Which interfaces are implemented by Integer?
Ans.[Comparable]
Q11. What is the package name for Integer class?
Ans.java.lang
Q12. If you have access to a function that returns a random integer from one to five, write another function which returns a random integer from one to seven.
Ans. We can do that by pulling binary representation using 3 bits ( random(2) ).
getRandom7() {
String binaryStr = String.valuesOf(random(2))+String.valuesOf(random(2))+String.valuesOf(random(2));
binaryInt = Integer.valueOf(binaryStr);
int sumValue=0;
int multiple = 1;
while(binaryInt > 0){
binaryDigit = binaryInt%10;
binaryInt = binaryInt /10;
sumValue = sumValue + (binaryDigit * multiple);
multiple = multiple * 2;
}
}
Q13. Write a method to convert binary to a number ?
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;
}
Q14. How to find whether a given integer is odd or even without use of modules operator in java?
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 ");
}
}
}
Q15. What will be the output of following code ?
public static void main(String[] args)
{
int x = 10;
int y;
if (x < 100) y = x / 0;
if (x >= 100) y = x * 0;
System.out.println("The value of y is: " + y);
}
Ans. The code will not compile raising an error that the local variable y might not have been initialized. Unlike member variables, local variables are not automatically initialized to the default values for their declared type.
Q16. 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);
}
Ans. 6
Ans. boolean - java.lang.Boolean
byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void
Q2. Will this code give error if i try to add two heterogeneous elements in the arraylist. ? and Why ?
List list1 = new ArrayList<>();
list1.add(5);
list1.add("5");
Ans. If we don't declare the list to be of specific type, it treats it as list of objects.
int 1 is auto boxed to Integer and "1" is String and hence both are objects.
Q3. How to find whether a given integer is odd or even without use of modules operator in java?
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 ");
}
}
}
Q4. Is it legal to initialize List like this ?
LinkedList<Integer> l=new LinkedList<int>();
Ans. No, Generic parameters cannot be primitives.
Q5. 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);
}
Ans. 6
Q6. Which of the following syntax are correct ?
a. LinkedList<Integer> l=new LinkedList<int>();
b. List<Integer> l=new LinkedList<int>();
c. LinkedList<Integer> l=new LinkedList<Integer>();
d. List<Integer> l = new LinkedList<Integer>();
Ans. c and d are correct.
Q7. Can we compare Integers by using equals() in Java ?
Ans. Yes for the Wrapper class Integer but not for the primitive int.
Q8. Name few Integer related classes and interfaces ?
Ans. http://www.buggybread.com/2015/01/java-data-types-integer-classes-and.html
Q9. Which is the Parent Class of Integer class?
Ans.Number
Q10. Which interfaces are implemented by Integer?
Ans.[Comparable]
Q11. What is the package name for Integer class?
Ans.java.lang
Q12. If you have access to a function that returns a random integer from one to five, write another function which returns a random integer from one to seven.
Ans. We can do that by pulling binary representation using 3 bits ( random(2) ).
getRandom7() {
String binaryStr = String.valuesOf(random(2))+String.valuesOf(random(2))+String.valuesOf(random(2));
binaryInt = Integer.valueOf(binaryStr);
int sumValue=0;
int multiple = 1;
while(binaryInt > 0){
binaryDigit = binaryInt%10;
binaryInt = binaryInt /10;
sumValue = sumValue + (binaryDigit * multiple);
multiple = multiple * 2;
}
}
Q13. Write a method to convert binary to a number ?
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;
}
Q14. How to find whether a given integer is odd or even without use of modules operator in java?
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 ");
}
}
}
Q15. What will be the output of following code ?
public static void main(String[] args)
{
int x = 10;
int y;
if (x < 100) y = x / 0;
if (x >= 100) y = x * 0;
System.out.println("The value of y is: " + y);
}
Ans. The code will not compile raising an error that the local variable y might not have been initialized. Unlike member variables, local variables are not automatically initialized to the default values for their declared type.
Q16. 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);
}
Ans. 6