Interview Questions and Answers - Order By Newest Q821. Write a Program to find age by birth date ? Core Java
Ans. LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);
Period p = Period.between(birthday, today); //Now access the values as below
System.out.println(period.getDays());
System.out.println(period.getMonths());
System.out.println(period.getYears()); Sample Code for LocalDate Sample Code for Period Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coding  code  date  LocalDate  Period Asked in 1 Companies Q822. Explain Inheritance and Polymorphism. Core Java
Ans. Getting parent class object properties into child class is called Inheritance.
The process of representing one form into multiple forms is called polymorphism. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inheritance  object oriented programming (oops)  oops concepts  polymorphism  object oriented programming (oops)  oops concepts Asked in 1 Companies Basic   frequent Q823. What will be the output of following code
class TestMain {
public static void main(String[] args) {
String s1 = "ravi";
String s2 = new String("ravi");
Integer i = 10;
Integer a1 = new Integer("10");
System.out.println(s1 == s1);
System.out.println(i == a1);
}
} 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  ==  coding  code Asked in 1 Companies Q824. Will the following code compile ?
final HashMap hm = new HashMap();
hm.put("Ravi", 221); Core Java
Ans. Yes, it will compile without error. final in this context means that the reference hm cannot be assigned to a new hash map but didn't restrict from changing the state of collection already held by hm. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  final  final variable Asked in 1 Companies Q825. Explain Java 8 concurrency mechanism. Core Java
This question was recently asked at 'IBM'.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  synchronization  concurrency  java 8  java8 Asked in 1 Companies Q826. What is the difference between being final and being immutable ? Are string final or immutable in Java ? Core Java
Ans. Being final in the sense of objects or object reference means that the reference cannot be reassigned to a different object whereas being immutable means that the object contents cannot be changed.
Strings in Java are immutable. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string  immutable  immutability  final vs immutable  immutability Q827. What does the following initialization mean ?
ArrayList<LinkedList> traversalPaths = new ArrayList<LinkedList>();
What could be the use of such a collection. Core Java
Ans. Initialize an ArrayList that will hold LinkedLists i.e every element of the arraylist will be a linked list.
Such collection could be used in algorithms that require first random access and then sequential traversal. For example - Storing traversal paths for a graph wherein we can start from any vertex. Implementing dictionary with each arraylist element holding staring with character and then linked list holding duplicate words. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  collections  linkedlist  arraylist Q828. What will be the output upon executing main method
public class Graph {
private static Multimap<Integer,Integer> adjacentDirectedNodesMap = ArrayListMultimap.create();
Graph(){
adjacentDirectedNodesMap.put(1, 2);
adjacentDirectedNodesMap.put(1, 3);
adjacentDirectedNodesMap.put(1, 4);
adjacentDirectedNodesMap.put(3, 5);
adjacentDirectedNodesMap.put(4, 5);
}
public static void main(String[] args){
ArrayList visited = new ArrayList();
Integer startNode = 1;
for(Integer adjacentNodes: adjacentDirectedNodesMap.get(startNode)){
System.out.println(adjacentNodes);
}
}
} Core Java
Ans. Nothing as we haven't yet created an instance and hence adjacentDirectedNodesMap won't be initialized with any value and hence is empty. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  constructor Q829. What is the difference between return and continue statement ? Core Java
Ans. return is used within a method to return control out of the method. It may be followed by a value which is returned from the method to calling method immediately preceding the point of call.
continue statement is used within a loop to start next iteration without executing the remaining statements in the loop. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  return statement  continue statement  return vs continue  for loop  control statements  loop statement Q830. What is the difference between return and break statement ? Core Java
Ans. return is used within a method to return control out of the method. It may be followed by a value which is returned from the method to calling method immediately preceding the point of call.
break statement is used within a loop to move control out of the loop. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  return statement  break statement  return vs break Q831. What is the difference between Java SE Map and Apache Commons MultiMap ? How can we implement functionality similar to multimap using Java SE map ? Core Java
Ans. We can have duplicate keys within MultiMap whereas we cannot have duplicate keys within Java Util Map.
We can have a Map with value as a "collection of values" instead of single value to have a similar function as Multimap. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Apache commons multimap  map vs multimap Q832. What is the difference between hasNextInt and nextInt method of scanner class ? Core Java
Ans. The hasNextInt() is used to check if there are any more elements left and the nextInt() is used to access that element. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  input output  Scanner Q833. Is it a bad practice to initialize object reference to Null ? Core Java
Ans. never initialise local to null,yes it is bad pratice Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  null  nullpointerexception Q834. What will be the output of following code and Why ?
int x = 5;
int y = 7;
float f = 5f;
float z = y / x * f;
System.out.println(z); Core Java
Ans. 5.0
operation between two ints generate int only. so 7/5 generates 1 and not 1.4. Multiplication between int and float generates float and hence
1 * 5.0 = 5.0 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arithmetic calculation  data types Q835. How do you know if a method is a constructor ? Core Java
Ans. Is the method name same as class Name - Yes
Does the method have any return type ( even void ) - No
It's a constructor Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  constructor Q836. Is a string an array of characters in Java ? Core Java
Ans. No, String is a class whose objects are called strings in Java. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  String Q837. Can we synchronize the run() method in Java? Core Java
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  synchronization  multithreading  run method Q838. Difference between if and switch ? Can we replace all switch statements with if statements ? Core Java
Ans. if statement is used to do 2 direction branching whereas switch is used to do multi direction branching in Java.
Yes we can replace all switch statements with nested if statements. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  switch  if vs switchAns. if statements nested within another if or else blocks are called nested if statements.
if(condition1) {
if(condition2){
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  if statement  control statements   nested if Q840. Write a Program to check if the entered number is a Duck Number ( Number having 0 in it ) ? Core Java
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  duck number Q841. What will the following code print ?
int x[] = new int[5];
x[0] = 1;
x[1] = 2;
for(int count=0;count<x.length;count++){
System.out.println(x[count]);
} Core Java
Ans. 1
2
0
0
0
As arrays are not dynamically expanded , we have declared the array for size 10. As we have only initialized only 2 values , it will print rest as their default values. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays Q842. What will the following code print
int x[] = new int[5];
x[0] = 1;
x[1] = 2;
for(int count=0;count<=x.length;count++){
System.out.println(x[count]);
} Core Java
Ans. 1
2
0
0
0
ArrayIndexOutOfBoundException: 5
As array index starts with 0 and ends with the index of (size - 1), the index 5 is inaccessible for the array and hence will throw the exception. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays Q843. what are different pointer operators used ? Core Java
This question was recently asked at 'Huawei 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   Asked in 1 Companies 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   Q845. Which java features makes java different from other languages ? Core Java
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   Q846. Why cant we start an identifier with a number in Java? Core Java
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   Q847. What is the default data type of Serialized methods?' Core Java
This question was recently asked at 'Intime Tec'.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   Asked in 1 Companies Q848. Do we have a method to reverse a string in String class ? Core Java
Ans. No but we have it in StringBuilder. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  String  StringBuilder  Reverse a string Q849. How can we convert char into a String ? Core Java
Ans. We can use StringBuilder. StringBuilder accepts char as the argument for it's constructor.
new StringBuilder('').toString(); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  convert char to String  char  String Q850. How can we check if a particular character is upper case in Java ? Core Java
Ans. 1. We can compare the ascii value of the character. Ascii for Capital case character is from 65 to 90.
2. We can compare to see if lowerCase of the character is not equal to character itself.
For example -
if character is 'a' the following code
new StringBuilder(char).toString().toLowerCase().equals(new StringBuilder(char)
will return true and hence char is lower case
if character is 'A' the following code
new StringBuilder(char).toString().toLowerCase().equals(new StringBuilder(char)
will return false and hence char is upper case Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  check upper case character  character