Wednesday, December 23, 2015

Java Interview Questions for freshers

In this article we will learn some frequently asked basic java technical interview questions specially for freshers. Every java developer before attending technical interview must brush up these question and answers to clear interview successfully.

1) What is JVM? Why is java called as"Platform Independent Programming Language"?

Ans: JVM Stands for JAVA VIRTUAL MACHINE. It is a process of Virtual Machine that can execute java byte code on different Operating System irrespective of specific Operating System.That means java was designed to allow application programs that could be run on any platform,without having to be rewritten or recompiled by the java programmer for each separate platform.So JVM makes this possible.

2) Can you Override private or  Static method in Java?

Ans: A user can not override static methods in java, method overriding is based upon dynamic binding at runtime and static methods are statically binded  at compile time. A static method is not associated with any instance of a class so we can not override static method in java.

3) What are the Pass by reference and pass by value in java?

Ans: When an Object is passed by reference, this  means that the actual object is not passed,rather a reference of the  object is passed,thus any changes made by the external methods are also reflected in all places.

        When an object is passed by value, this means that a copy of the object is passed,thus even if changes are made to that object it does not effect it's original value.

4) What are differences between process and Threads?

Ans:  A process is self contained execution environment and it can be seen as program whereas Thread is a single task of execution within the process.Thread can be called lightweight process.we can call threads are lightweight process because thread requires less resources to create and exists in the process and thread share process resources.

5) How do you ensures that N threads can access N resources without deadlock?

Ans:  A simple way to avoid deadlock while using N threads is to impose an ordering on the locks and force each thread to fallow that ordering. Thus, if all threads lock and unlock the mutexes in the same order no deadlock can raise.

6) When will we use Arrays over ArrayList?

Ans: Arrays can contains Primitives and Objects, while an arraylist can contain only Objects.Arrays have fixed size whereas arraylist is dynamic.An arraylist provides more features than Arrays such as addAll, removeAll,iterator,etc..For list of Primitive data types,the collections use autoboxing to reduce the coding effort.

7) Why wait(), notify(),notifyall() methods have to be called from Synchronized method or block?

Ans:  when a Thread calls wait() method on any object it must have the monitor on the object that it will leave and goes in wait state until any other thread call notify() method on this object. similarly when a Thread calls notify() on any object it leaves the monitor on the object and other waiting threads can get the monitor on the object. so all these methods needs Thread to have Object monitor that can be achieved only by synchronization, this should by called from synchronized method or block.

8) How can we achieve Thread Safety in java?

Ans:  We can achieve Thread Safety in java by using several ways such as synchronization,using volatile keyword,using immutable classes and implementing concurrent Lock Interface.

9) What is Executors class in java?

Ans: Executors class provides utility methods for executor, ExecutorService,scheduledExecutorService,ThreadFactory and Callable classes.

Executor class is used to easily create ThreadPool in java and also this is the only class to supporting execution of Callable implementations.

10) How to create Demon Threads in java?

Ans:  This is very simple to create Daemon threads in java . 
Thread class setDaemon(true) can be used to create Daemon Threads in java.

Note:  we need to call this method before calling start() method otherwise it will throw IlleagalThreadStateException.




No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...