Interview Questions and Answers - Order By Newest Q1151. Write a Program for insertion sort. Core Java
This question was recently asked at 'Ola Cabs'.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  program  code  coding  insertion sort   sort Asked in 1 Companies Very frequently asked. Usually among very first few questions. Q1152. Define encapsulation in Java ? Core Java
Ans. Encapsulation is a feature of OOP's that binds the data and it's associated methods together as a single unit and facilitate protection and data hiding by providing minimal interface to outside. For example - member variables are declared private and are accessed through public methods. Moreover we have private methods that can only be used internally and hence providing minimal interface to outside class through use of public methods. Sample Code for encapsulation Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  encapsulation  object oriented programming (oops)  oops concepts  oops  oops concepts  oops features Asked in 4 Companies Basic   frequent Q1153. Can an inner class be subclass of it's parent class ? Core Java
Ans. Yes that can be done
public class OuterClass {
public class InnerClass extends OuterClass {
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inner classes  nested classes  sub class Basic Q1154. Is this code legal in java i.e nested class within interface implementing the parent interface ?
public interface MyInterface {
public class MyClass implements MyInterface {
}
} Core Java
Ans. Yes that's legal in java Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inner classes  nested classes  sub class Basic Q1155. What are the different types of loops in Java ? Core Java
Ans. for, while and do while Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  loops  different type of loops  for loop  control statements  loop statement basic Ans. Infinite loop is a programming condition wherein the control goes into an infinite loop because the loop termination condition can never be met. For example -
for(int x=1;x>0;x++){
}
in this loop, with each increment the condition x > 0 will remain true till infinity. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  loop  infinite loop  for loop  control statements  loop statement basic Q1157. Is String a Data type in Java ? Core Java
Ans. String is not a primitive type in java. Anything defined within double quotes like "abc" is an object of String class. Sample Code for String Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string  data type Basic Q1158. Are char,String and Char data types in java ? Core Java
Ans. char is a primitive data type. String is a class. Char is a wrapper class for primitive char. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  char  string  data type Basic Q1159. Can we call a constructor for an object twice ? 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  constructor Q1160. Can we compile and execute a Java class without main method ? Core Java
Ans. No without main method can not be executed it will throw an error illegal start of type. Main method is the entry point of application. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  jvm  compilation  main method Basic Q1161. Is JDK required on each machine where a Java application needs to be run ? Core Java
Ans. No, to run application, machine needs just a JRE (Java Runtime Environment) Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  jvm  jdk Basic Q1162. What is the purpose of re throwing the exception when we can provide the handling at the first catch itself ? Core Java
Ans. Sometimes the calls move across layers of classes and functions and hence each layer needs to perform some function like cleaning if something goes wrong deep inside. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  exceptions  exceptions handling  rethrowing exception Q1163. What is the problem we could face if we be very liberal with importing classes even if we are not using them ? For example
import java.util.*;
import java.io.*; Core Java
Ans. It makes application little heavy as we are importing classes that aren't required.
It creates class name conflicts as similar name classes might be available across different packages. In case of such conflicts, we will have to specify the package name with the class name at the time of it's usage. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  import statement   java import Q1164. Which java package is loaded by default and hence doesn't require an explicit import statement ? Core Java
Ans. java.lang Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java.lang  import statement basic Q1165. How do we define constant variables in Java ? Core Java
Ans. By using static and final modifiers. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  constant variables   static final basic   frequent Q1166. Can a source file contain more than one class declaration ? Core Java
Ans. Yes, but only one of them can be declared public. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  class declaration Q1167. Can an interface be defined inside a class just like we can have inner classes inside a class ? Core Java
Ans. Yes , we can have an interface that is part of a class. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inner classes   inner interface  nested classes Q1168. Can an interface implements another interface ? Core Java
Ans. No. No interface provides any implementation and hence the declaration isn't viable. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  interface basic Q1169. Why doesn't java support operator overloading ? 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  operator overloading Q1170. What is the use of upcasting in Java ? Core Java
Ans. Interfacing
Car extends Vehicle
Car car = new Car();
// now you need to call an API changeTires() which can change tires for all Vehicle Types and accepts argument of type Vehicle and hence you will have to upcast it
changeTires((Vehicle)car);
Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  upcasting  type casting Q1171. Why do we pass an array of strings to main method ? Core Java
Ans. Array of strings in the main method are the list of arguments or parameters which are sent to the application / program. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  main method   main method string array argument Q1172. In Java, what is the closest thing to pointers? Core Java
Ans. References.
Unlike pointers in C , Java references are strongly typed and we cannot do pointer arithmetics with them. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  pointers  pointers in java  references Asked in 1 Companies Q1173. In Java, would you like to see an explicit destructor? Core Java
Ans. Yes , a gc method to explicitly call garbage collection immediately on the object. It will provide better flexibility over application that require optimal usage of memory. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  destructor  garbage collection Asked in 1 Companies expert Q1174. How to calculate lcm of two numbers ? Core Java
This question was recently asked at 'HCL Tech'.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  coding  code Asked in 1 Companies Q1175. 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 Q1176. What is a session bean ? Java EE
This question was recently asked at 'BirlaSoft'.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  session bean   java bean Asked in 1 Companies Q1177. 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 Q1178. 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 Q1179. 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 Q1180. 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