Wednesday, 16 March 2016

Tricky Java Interview Questions

Question 1:
Which of two below overridden method will get called when we call method(null)?
public class Demo1 {
public void method(Object obj){
System.out.println("Object");
}

public void method(String str){
System.out.println("String");
}

public static void main(String[] args) {
Demo1 demo1 = new Demo1();
demo1.method(null);
}
}

Answer 1 :  method(String str) will get called as the child class get preference in case of ambibuity.

Question 2:
Which of two below overridden method will get called when we call demo1.test(collection), demo1.test(list), demo1.test(arrayList)?
public class Demo1 {
public void test(Collection<String> c) {
System.out.println("Collections");
}

public void test(ArrayList<String> al) {
System.out.println("ArrayList");
}

public static void main(String[] args) {
Collection<String> collection = new ArrayList<String>();
List<String> list = new ArrayList<String>();
ArrayList<String> arrayList = new ArrayList<String>();
demo1.test(collection);
demo1.test(list);
demo1.test(arrayList);
}
}

Answer 2: test(Collection<String> c) will get calledin case of demo1.test(collection), and demo1.test(list) and test(ArrayList<String> al) will get called in case of demo1.test(arrayList). So the final output would be ...
Collections
Collections
ArrayList

Question 3 : What will be the result 
hashmap.put(new Employee(), "A")
hashmap.put(new Employee(), "B") 
hashmap.get(new Employee());

Answer 3 : It will return null;

Question 4: What is the core difference between Object Oriented and Object based languages
Answer 4: 
Object-based : Any language that offers the built-in ability to easily create and use objects. There is one major criterion: Encapsulation: 
As far as object-based-but-not-object-oriented languages go, a good example would be Visual Basic (not the .net stuff; i'm talking about VB6 and VBA). Classes exist, but can't inherit from each other.
Object-oriented languages are object-based languages that take things a step further. They have built-in support for the two other pillars of OOP:
Inheritance. 
Polymorphism.  
Question 5: What is Upcasting and downcasting
Answer 5: Upcasting and downcasting are NOT like casting primitives from one to other, and i believe that's what causes a lot of confusion, when programmer starts to learn casting objects

Object ---> Animal ---> Mammal ---> Dog
Object ---> Animal ---> Mammal ---> Cat
Upcasting <-------------------> Downcasting

When we cast Dog to Animal, Mammal or Object it is upcasting
When we cast Animal to Dog or Mammal it is downcasting

Casting does not actually change the object itself, but its just label it differently. 
For example, if you create a Cat and upcast it to Animal, then the object doesn't stop from being a Cat. It's still a Cat, but it's just treated as any other Animal and it's Cat properties are hidden until it's downcasted to a Cat again. 

Although there's no need to for programmer to upcast manually, it's allowed to do.Consider the following example:
Code: Mammal m = (Mammal)new Cat(); is equal to 
Code: Mammal m = new Cat(); 

But downcasting must always be done manually:
Code: Cat c1 = new Cat();  
  Animal a = c1; //automatic upcasting to Animal

  Cat c2 = (Cat) a;    //manual downcasting back to a Cat

Question 6: What is Just in Time JIT Compiler?
Answer:
In the Java programming language and environment, a just-in-time(JITcompiler is a program that turns Java bytecode (a program that contains instructions that must be interpreted) into instructions that can be sent directly to the processor. 
After you've written a Java program, the source language statements are compiled by the Java compiler into bytecode rather than into code that contains instructions that match a particular hardware platform's processor. The bytecode is platform-independent code that can be sent to any platform and run on that platform with the help of JIT.

Question 7: How many types of Memory area's are allocated by JVM?
Answer: 
The JVM performs four main tasks: 
· Loads code
· Verifies code
· Executes code
· Provides runtime environment

JVM provides definitions for the:
· Memory area
· Class file format
· Register set
· Garbage-collected heap
· Fatal error reporting etc.

Types of memory allocated by JVM:
· Class (Method) Area
· Heap
· Stack
· Program Counter Register
· Native Method Stack
1) Classloader:
Classloader is a subsystem of JVM that is used to load class files.
2) Class (Method) Area:
Class (Method) Area stores per-class structures such as the runtime constant pool, field and method data, the code for methods.
3) Heap:
It is the runtime data area in which objects are allocated.
4) Stack:
Java Stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return.
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
5) Program Counter Register:
PC (program counter) register. It contains the address of the Java virtual machine instruction currently being executed.
6) Native Method Stack:
It contains all the native methods used in the application.

Execution Engine:
It contains:
1) A virtual processor
2) Interpreter: Read bytecode stream then execute the instructions.

3) Just-In-Time(JIT) compiler: It is used to improve the performance.JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term compiler refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU

No comments: