Interview Questions and Answers - Order By Rating Q121. How and Why Strings are interned in Java ? Core Java
Ans. String class has a public method intern() that returns a canonical representation for the string object. String class privately maintains a pool of strings, where String literals are automatically interned.
these are automatically interned so as to have efficient String comparison using == operator instead of equals which is usually slower. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string  string pool  string intern Asked in 1 Companies Q122. What is the difference between authentication and authorization ? Authentication
Ans. Authentication is the process of verifying the identity and credentials of the user to authenticate him into the system.
whereas
Authorization is the process by which access to a segment , method or resource is determined.
Authorization is usually a step next to authentication. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  authentication  authorization  authentication vs authorization Asked in 9 Companies basic   frequent Usually asked to entry level software developers. Q123. Write a program to swap two variables without using third Core Java
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 Q124. Who invokes a thread's run method ? Core Java
Ans. After a thread is started using a call to start method, JVM invokes the thread’s run method when the thread is initially executed. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  threads  thread run method Asked in 1 Companies Ans. No exit is a method to exit execution of a program. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  exit  keywords  exit a program Asked in 1 Companies Ans. No, delete is not a keyword in Java. Destruction of objects is taken care by Java Garbage Collection mechanism. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  delete  keywords Asked in 1 Companies Q127. 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 Q128. What are the different ways to handle an exception ? Core Java
Ans. 1. Wrap the desired code in try block followed by a catch / finally to catch the exception
2. Declare the throws clause with exceptions that the code is expected to throw as an indication to the calling method to take care of it. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  exception handling Asked in 4 Companies Try 1 Question(s) Test Q129. Is there a way to know size of an object in Java ? Core Java
Ans. No, Java doesn't have a sizeOf operator. In C / C++ , its required to determine how much memory allocation is required which is not the case with Java. Java handles memory allocation and deallocation intrinsically. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  sizeOf  size of object  sizeOf operator Asked in 1 Companies   rare Q130. Can we declare static variables as transient ? Core Java
Ans. It's weird that compiler doesn't complain if we declare transient with static variable because it makes no sense. At least a warning message saying "transient is useless in this situation" would have helped with code cleaning.
Static variables are never serialized and transient is an indication that the specified variable shouldn't be serialized so its kind of double enforcement not to serialize.
It could be that as it makes no different to the variable behavior and hence using both keywords with a variable are permitted. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  static  transient  serialization Asked in 1 Companies expert   rare Try 1 Question(s) Test Q131. Which elements of a class are ignored during serialization ? Core Java
Ans. 1. Objects are serialized and not classes and hence Static variables are ignored.
2. Transient is an explicit declaration to ignore the variable during serialization and hence transient instance variables are ignored too.
3. Base class instance variables if the base class hasn't been declared serializable. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  serialization Asked in 1 Companies intermediate   frequent Try 1 Question(s) Test Q132. What is the default value of a declared object reference ? Core Java
Ans. Null Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  object reference  null Asked in 1 Companies basic Q133. Is it a good practice to use Asterick(*) in import statement to load multiple packages with single import statement ? Core Java
Ans. No, we should be lean in loading only packages which are required. Loading packages which are not being used result in memory overheads. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  import  import packages Asked in 1 Companies Q134. Does importing a package imports all its sub packages too ? Core Java
Ans. No, but we can use wild card (*) to do so
For example -
import java.util.*
will import all packages with name starting with java.util Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  package   import packages Asked in 1 Companies basic Q135. Can we have multiple main methods in a single class ? Core Java
Ans. We can overload the main method by specifying different argument types. For example -
2 main methods with different arguments is perfectly legal
public static void main();
public static void main(String[] args);
The following are not legal as compiler will complain of duplicate methods
public static void main(String[] args);
public static void main(String[] args);
Even The following are not legal as we cannot overload on return types
public static String main(String[] args);
public static void main(String[] args); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  main method Asked in 2 Companies Q136. Can an application have multiple main methods within different classes ? If yes, How will the app decide which one to be executed ? Core Java
Ans. Yes we can have a main method with string[] argument in every class of an application. When we execute an app we specify the starting point i.e the class that will get the control first and hence main method of that class gets executed. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  main method Asked in 3 Companies Q137. Which environment variables do we need to set in order to run Java programs? Core Java
Ans. PATH, CLASSPATH and JAVA_HOME Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  environment variables  path  classpath  java_home Asked in 3 Companies basic   frequent Q138. Scale yourself on Java on the scale of 1 to 10 General
Ans. [Open Ended Answer]
The objective of the question is to check how efficient one is in the language so that appropriate level questions are asked. Don't tell too high number if you are being interviewed for a junior or mid level position as the interviewer may throw advance level questions. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 17 Companies Very frequently asked. Usually asked in this format or as difference with interfaces / concrete classes. Ans. Abstract class is the class that is not supposed to be instantiated. The purpose of the class to only have extension to the derived class. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  abstract class Asked in 6 Companies basic   frequent Frequently asked in Cognizant ( Based on 2 feedback ) Q140. What are Inner , Outer , Left and Right Joins in SQL ? Database
Ans. Inner join is the intersection of two tables on the condition defined by the where clause i.e will get records from both tables matched by a column.
Outer join is the union of two tables i.e will get all records from both tables and will put null in the columns where related records are not present.
Left Outer join is the left union of two tables i.e all records from the table on the left and values from the right table for related records else null for the columns from right table.
Right Outer join is the right union of two tables i.e all records from the table on the right and values from the left table for related records else null for the columns from left table. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  sql  inner join  outer join  right join  left join Asked in 24 Companies basic   frequent Q141. What is an exception and exception handling in Java ? Core Java
Ans. An Exception in java is the occurrence during computation that is anomalous and is not expected.
Exception handling is the mechanism which is used to handle such situations.
Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  exception handling Asked in 18 Companies basic   frequent Q142. What is database Normalization ? Database
Ans. https://en.wikipedia.org/wiki/Database_normalization Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  normalization  database Asked in 35 Companies intermediate   frequent Q143. Why java doesn't support multiple Inheritance ?
or
Explain Java Diamond Problem. Core Java
Ans. class A {
void test() {
System.out.println("test() method");
}
}
class B {
void test() {
System.out.println("test() method");
}
}
Suppose if Java allows multiple inheritance like this,
class C extends A, B {
}
A and B test() methods are inheriting to C class.
So which test() method C class will take? As A & B class test() methods are different , So here we would Facing Ambiguity. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  multiple inheritance  object oriented programming (oops)  oops concepts  diamond problem Asked in 20 Companies basic   frequent Q144. Which class is the root class of all Exceptions in Java ?
Ans. Throwable Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies basic   frequent Q145. Does java supports operator overloading ? Core Java
Ans. No Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  operator overloading Asked in 1 Companies basic Q146. What is Maven ? Maven
Ans. Maven is a build automation tool used primarily for Java projects. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  maven  build Asked in 6 Companies basic   frequent Q147. Write a Program to print the positive and negative numbers separetly Core Java
Ans. int[] arr = {1,-1,2,-3,3,-4,4,5,6,-5,-6,-7,-8,8,9,-9};
List positiveNumbers = new ArrayList<>();
List negativeNumbers = new ArrayList<>();
for(int i = 0; i < arr.length(); i ){
if(I < 0){
negativeNumbers.add(i);
} else {
positiveNumbers.add(i);
}
}
System.out.println("Positive Numbers:" + positiveNumbers);
System.out.println("Negative Numbers:" + negativeNumbers); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  program  coding Asked in 1 Companies Ans. Collections in java is a framework of classes that provides an abstracted data structure to store and manipulate the group of objects. Each class is unique in the way it stores , search , sort and modify the elements. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  collections  collection classes Asked in 1 Companies Basic   frequent Q149. What are fields that gets displayed when you execute "Top" command and briefly explain each field ? Unix
Ans. PID - process identification number is an identification number that is automatically assigned to each process when it is created
USER - User Name
PR - PR is the process actual priority
NI is the nice value, which is a user-space concept.
VIRT -Virtual Image (kb). The total amount of virtual memory used by the task.
RES - Resident size (kb). The non-swapped physical memory a task has used.
SHR - Shared Mem size (kb). The amount of shared memory used by a task.
S - Process Status. The status of the task which can be one of:
D = uninterruptible sleep
R = running
S = sleeping
T = traced or stopped
Z = zombie
%CPU - % CPU usage
%MEM - % MEM Usage
TIME - Total CPU time the task has used since it started.
COMMAND - Command which was used to execute the process Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  top command  unix command Asked in 1 Companies Q150. Write a unix command to find top 10 files by size ? Unix
Ans. du -a /var | sort -n -r | head -n 10 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  unix command Asked in 1 Companies