Core Java Real Time Interview Questions and Answers Top MNC Companies TCS, Wipro, Infosys, Tech Mahindra, Accenture, WellsFargo and Mindtree

By | February 14, 2017

Core Java Real Time Interview Questions and Answers Top MNC Companies TCS, Wipro, Infosys, Tech Mahindra, Accenture, WellsFargo and Mindtree

Core Java Real Time Interview Questions and Answers Top MNC Companies TCS, Wipro, Infosys, Tech Mahindra, Accenture, WellsFargo and Mindtree

Q) What is polymorphism ?

Ans) The ability to define a function in multiple forms is called Polymorphism. In java, c++ there are two types of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding).

Method overriding: happens when a child class implements the method with same signature as a method in a parent class. When you override methods, JVM determines the proper methods to call at the program’s runtime, not at the compile time.

Method overloading: happens when several methods have same names but different number or type of parameters. Overloading is determined at the compile time.

  • Overloading happens when:
  • Different method signature and different number or type of parameters.
  • Same method signature but different number of parameters.
  • Same method signature and same number of parameters but of different type

Example of Overloading

int add(int a,int b)
 float add(float a,int b)
 float add(int a ,float b)
 void add(float a)
 int add(int a)
 void add(int a) //error conflict with the  method int add(int a)
class BookDetails {
  String title;
  setBook(String title){}
class ScienceBook extends BookDetails {
  setBook(String title){} //overriding
  setBook(String title, String publisher,float price){} //overloading

Q) What is use of serialVersionUID?

Ans) The default Java serialization mechanism writes the metadata about the object, which includes the class name, field names and types, and superclass. This class definition is stored as a part of the serialized object. This stored metadata enables the deserialization process to reconstitute the objects and map the stream data into the class attributes with the appropriate type
Everytime an object is serialized the java serialization mechanism automatically computes a hash value. ObjectStreamClass’s computeSerialVersionUID() method passes the class name, sorted member names, modifiers, and interfaces to the secure hash algorithm (SHA), which returns a hash value.The serialVersionUID is also called suid.
So when the serilaize object is retrieved , the JVM first evaluates the suid of the serialized class and compares the suid value with the one of the object. If the suid values match then the object is said to be compatible with the class and hence it is de-serialized. If not InvalidClassException exception is thrown.

Changes to a serializable class can be compatible or incompatible. Following is the list of changes which are compatible:

  • Add fields
  • Change a field from static to non-static
  • Change a field from transient to non-transient
  • Add classes to the object tree

List of incompatible changes:

  • Delete fields
  • Change class hierarchy
  • Change non-static to static
  • Change non-transient to transient
  • Change type of a primitive field

So, if no suid is present, inspite of making compatible changes, jvm generates new suid thus resulting in an exception if prior release version object is used .

The only way to get rid of the exception is to recompile and deploy the application again.

If we explicitly mention the suid using the statement:

private final static long serialVersionUID = <integer value>

then if any of the metioned compatible changes are made the class need not to be recompiled. But for incompatible changes there is no other way than to compile again.

Core Java MNC Companies Interview

1) What is/are your favorite subject(s)? There may be many questions on the subject told.

2) Differences between C and C++.

3) What is include in a C program?

4) What is Dynamic Memory Allocation, example?

5) Differences between C/C++ and Java?

6) Simple programs like Bubble Sort, sum of a simple series, etc.

7) What is OOP?

8) What are encapsulation, Inheritance, polymorphism and abstraction?

9) Explain Runtime Polymorhism

10) What is BCNF?

11) What are inner and outer joins? Examples of both.

12) Questions on keys in DBMS like primary key, super key, difference between primary key and unique?

13) OSI Layers?

14) Why Accenture?

16) Are you ready to work in night shifts?

17) About final year project?

18) Your strengths and weaknesses?

19) Do you a know a language other than C, C++ and Java?

Q) What is the difference between final, finally and finalize() in Java?

Ans) final – A final variable acts as a constant, a final class is immutable and a final method cannot be override in a child class.

finally – finally keyword is used with try-catch block for handling exception. The finally block is optional in try-catch block. The finally code block is always executed after try or catch block is completed. The general use case for finally block is to close the resources or clean up objects used in try block. For e.g. Closing a FileStream, I/O stream objects, Database connections, HTTP connections are generally closed in a finally block.

finalize() – This is the method of Object class.It is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.

Q4)What is difference between HashMap and HashTable?

Ans) Both collections implements Map. Both collections store value as key-value pairs. The key differences between the two are:

  1. Hashmap is not synchronized in nature but hashtable is(thread-safe). This means that in a multithreaded application, only one thread can gets access to a hashtable object and do an operation on it. Hashmap doesn’t gurantee such behavior and is not used in multithreaded environment.
  2. Hashmap is traveresed using an iterator, hashtable can be traversed by enumerator or iterator.
  3. Iterator in hashmap is fail-fast, enumerator in hashtable is not fail-fast
  4. HashMap permits null values and only one null key, while Hashtable doesn’t allow key or value as null.
  5. Since hashtable is synchornized, it is relatively slower in performance than hashmap

Q) What is difference between abstract class and interface ?

Ans) A class is called abstract when it is declared with keywordabstract. Abstract class may contain abstract method. It can also contain n numbers of concrete method. Interface can only contain abstract methods.

  • Interface can have only abstract methods. Abstract class can have concerete and abstract methods.
  • The abstract class can have public, private, protected or default variables and also constants. In interface the variable is by default public final. In nutshell the interface doesnt have any variables it only has constants.
  • A class can extend only one abstract class but a class can implement multiple interfaces. Abstract class doesn’t support multiple inheritance whereas abstract class does.
  • If an interface is implemented its mandatory to implement all of its methods but if an abstract class is extended its mandatory to implement all abstract methods.
  • The problem with an interface is, if you want to add a new feature (method) in its contract, then you MUST implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass.

Q) What is the difference between equals() and == ?

Ans) == operator is used to compare the references of the objects.
public bollean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. But since the method can be overriden like for String class. equals() method can be used to compare the values of two objects.

String str1 = "MyName"; 
String str2 = "MyName";
String str3 = new String(str2);

if (str1 == str2) {
  System.out.println("Objects are equal")
  System.out.println("Objects are not equal")
if(str1.equals(str2)) {
  System.out.println("Objects are equal")
} else {
  System.out.println("Objects are not equal")

Objects are not equal
Objects are equal
String str2 = "MyName";
String str3 = str2;
if (str2 == str3) {
System.out.println("Objects are equal")
System.out.println("Objects are not equal")
if (str3.equals(str2)) {
  System.out.println("Objects are equal")
} else {
  System.out.println("Objects are not equal")

Objects are equal
Objects are equal

Q) What is difference between a thread and a process?


  • Threads share the address space of the process that created it; process has it’s own address space.
  • Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
  • Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
  • Threads have almost no overhead; processes have considerable overhead.
  • New threads are easily created; new processes require duplication of the parent process.
  • Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
  • Changes to the main thread (cancellation, priority change etc.) may affect the behavior of the other threads of the process; changes to the parent process do not affect child processes.

Q) What is use of synchronized keyword?

Ans) synchronized keyword can be applied to static/non-static methods or a block of code. Only one thread at a time can access synchronized methods and if there are multiple threads trying to access the same method then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object and thus prevents race condition. E.g.

public void synchronized method(){} 
  public void synchronized staticmethod(){}
  public void myMethod(){
     synchronized (this){ 
        //synchronized keyword on block of code

Q)Is garbage collector a dameon thread?

Ans) Yes GC is a dameon thread. A dameon thread runs behind the application. It is started by JVM. The thread stops when all non-dameon threads stop.

Q)How is Garbage Collection managed?

Ans)The JVM controls the Garbage Collector; it decides when to run the Garbage Collector. JVM runs the Garbage Collector when it realizes that the memory is running low. The behavior of GC can be tuned by passing parameters to JVM. One can request the Garbage Collection to happen from within the java program but there is no guarantee that this request will be taken care of by jvm.

Q. What is the difference between an Inner Class and a Sub-Class?

Ans: An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.

A sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class.

Q. What are the various access specifiers for Java classes?

Ans: In Java, access specifiers are the keywords used before a class name which defines the access scope. The types of access specifiers for classes are:

1. Public : Class,Method,Field is accessible from anywhere.

2. Protected:Method,Field can be accessed from the same class to which they belong or from the sub-classes,and from the class of same package,but not from outside.

3. Default: Method,Field,class can be accessed only from the same package and not from outside of it’s native package.

4. Private: Method,Field can be accessed from the same class to which they belong.

Q. What’s the purpose of Static methods and static variables?

Ans: When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects.

Q. What is data encapsulation and what’s its significance?

Ans: Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.

Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves data hiding purpose.

Q. What is a singleton class? Give a practical example of its usage.

A singleton class in java can have only one instance and hence all its methods and variables belong to just one instance. Singleton class concept is useful for the situations when there is a need to limit the number of objects for a class.

The best example of singleton usage scenario is when there is a limit of having only one connection to a database due to some driver limitations or because of any licensing issues.

Q. What are Loops in Java? What are three types of loops?

Ans: Looping is used in programming to execute a statement or a block of statement repeatedly. There are three types of loops in Java:

1) For Loops

For loops are used in java to execute statements repeatedly for a given number of times. For loops are used when number of times to execute the statements is known to programmer.

2) While Loops

While loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In while loops, condition is checked first before execution of statements.

3) Do While Loops

Do While Loop is same as While loop with only difference that condition is checked after execution of block of statements. Hence in case of do while loop, statements are executed at least once.

Q: What is an infinite Loop? How infinite loop is declared?

Ans: An infinite loop runs without any condition and runs infinitely. An infinite loop can be broken by defining any breaking logic in the body of the statement blocks.

Infinite loop is declared as follows:


Q.  What is the difference between continue and break statement?

Ans: break and continue are two important keywords used in Loops. When a break keyword is used in a loop, loop is broken instantly while when continue keyword is used, current iteration is broken and loop continues with next iteration.

In below example, Loop is broken when counter reaches 4.


In the below example when counter reaches 4, loop jumps tonext iteration and any statements after the continue keyword are skipped for current iteration.


Q. What is the difference between double and float variables in Java?

Ans: In java, float takes 4 bytes in memory while Double takes 8 bytes in memory. Float is single precision floating point decimal number while Double is double precision decimal number.

Q.  What is Final Keyword in Java? Give an example.

Ans: In java, a constant is declared using the keyword Final. Value can be assigned only once and after assignment, value of a constant can’t be changed.

In below example, a constant with the name const_val is declared and assigned avalue:

Private Final int const_val=100

When a method is declared as final,it can NOT  be overridden by the subclasses.This method are faster than any other method,because they are resolved at complied time.

When a class is declares as final,it cannot be subclassed. Example String,Integer and other wrapper classes.

Q. What is ternary operator? Give an example.

Ans: Ternary operator , also called conditional operator is used to decide which value to assign to a variable based on a Boolean value evaluation. It’s denoted as ?

In the below example, if rank is 1, status is assigned a value of “Done” else “Pending”.


Q: How can you generate random numbers in Java?


  • Using Math.random() you can generate random numbers in the range 0.1 to 1.0
  • Using Random class in package java.util

Q. What is default switch case? Give example.

Ans: In a switch statement, default case is executed when no other switch condition matches. Default case is an optional case .
It can be declared only once all other switch cases have been coded.

In the below example, when score is not 1 or 2, default case is used.

Core Java Real Time Interview Questions and Answers Top MNC Companies TCS, Wipro, Infosys, Tech Mahindra, Accenture, WellsFargo and Mindtree

Core Java Real Time Interview Questions and Answers Top MNC Companies TCS, Wipro, Infosys, Tech Mahindra, Accenture, WellsFargo and Mindtree

Leave a Reply

Your email address will not be published. Required fields are marked *