Showing posts with label Java Interview Questions. Show all posts
Showing posts with label Java Interview Questions. Show all posts

Monday, October 31, 2016

25 Best Java programming interview questions and answers

java programming interview questions and answers pdf

Frequently Asked Java programming Interview questions and answers pdf for freshers and experienced


1. What do you know about Java?

Ans:
Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

2. What are the supported platforms by Java Programming Language?

Ans:
Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubuntu, CentOS, etc.

3. List any five features of Java?

Ans:
Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded

4. List two Java IDE’s?

Ans:
Netbeans, Eclipse, etc

5. What do you mean by Object?

Ans:
Object is a runtime entity and it’s state is stored in fields and behaviour is shown via methods. Methods operate on an object's internal state and serve as the primary mechanism for object-to object communication.




java-page3

16. Describe different states of a thread.

Ans:
A thread in Java can be in either of the following states:

Ready : When a thread is created, it’s in Ready state.
Running : A thread currently being executed is in running state.
Waiting : A thread waiting for another thread to free certain resources is in waiting state.
Dead : A thread which has gone dead after execution is in dead state.

17. Can variables be used in Java without initialization?

Ans:
In Java, if a variable is used in a code without prior initialization by a valid value, program doesn’t compile and gives an error as no default value is assigned to variables in Java.

18. Can a class in Java be inherited from more than one class?

Ans:
In Java, a class can be derived from only one class and not from multiple classes. Multiple inheritances is not supported by Java.

19. Is JDK required on each machine to run a Java program?

Ans:
JDK is development Kit of Java and is required for development only and to run a Java program on a machine, JDK isn’t required. Only JRE is required.

20. What is an Exception? Which are the two subclasses under Exception class?

Ans:
An exception is a problem that arises during the execution of a program. Exceptions are caught by handlers positioned along the thread's method invocation stack.
The Exception class has two main subclasses : IOException class and RuntimeException Class.



21. Define Packages in Java? Why Packages are used?

A Package can be defined as a grouping of related types classes, interfaces, enumerations and annotations providing access protection and name space management.
Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations, etc., easier.

22. What is an applet?

Ans:
An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.

23. What is the difference between an Applet and an Application?

Ans:
1) Applets can be embedded in HTML pages and downloaded over the Internet whereas Applications have no special support in HTML for embedding or downloading.
2) Applets can only be executed inside a java compatible container, such as a browser or appletviewer whereas Applications are executed at command line by java.exe or jview.exe.
3) Applets execute under strict security limitations that disallow certain operations(sandbox model security) whereas Applications have no inherent security restrictions.
4) Applets don't have the main() method as in applications. Instead they operate on an entirely different mechanism where they are initialized by init(), started by start(), stopped by stop() or destroyed by destroy().

24. What is Java Beans ?

Ans:
JavaBeans are classes that encapsulate many objects into a single object (the bean).
According to JavaSoft, "A Java Bean is a reusable software component that can be manipulated visually in a builder tool."


25. How are Java source code files named?

Ans:
A Java source code file takes the name of a public class or interface that is defined within the file. A source code file may contain at most one public class or interface. If a public class or interface is defined within a source code file, then the source code file must take the name of the public class or interface. If no public class or interface is defined within a source code file, then the file must take on a name that is different than its classes and interfaces. Source code files use the .java extension.





java-page2

6. Define class?

Ans:
A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behaviour of an object.

7. What is JVM?

Ans:
Java Virtual Machine (JVM) :
It is an interpreter for bytecode. JVM is a platform independent and converts Java byte code into machine language and executes it. Program calls compiler to translate the program code into executable code that computer can understand and execute. The executable code depends upon the computer operating system that we use to execute our program. It is machine dependent.

8. What is multithreading and what is the class in which these methods are defined?

Ans:
Multithreading is the mechanism which allows us to do many things simultaneously. A multithreaded enable us to executed two or more program at same time which are totally independent of each other. Each part of such a program is called a thread. Each thread defines a separate path of execution inside the program. To communicated between two threads we use methods like wait (), notify () and notifyAll() and these methods are in Object class.wait().

9. What is the difference between Process and Thread?

Ans:
The major difference between threads and processes is:

1) Threads share the address space of the process that created it whereas processes have their own address.
2) Threads can directly access to the data segment of its process whereas processes have their own copy of the data segment of the parent process.
3) Threads communicate directly with other threads of its process whereas processes use inter-process communication to communicate with child processes.
4) Threads have almost no overhead whereas processes have considerable overhead.
5) New threads are easily created whereas new processes require duplication of the parent process.
6) Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
7) Changes to the main thread affect the behavior of the other threads of the process whereas changes to the parent process do not affect child processes.

10. How do you declare a class as private?

Ans:
We can declare a private class as an inner class.

For example :

class MyPrivateClass
{
   private static class MyKey
   {
       String key = "91234";
   }
   public static void main(String[] args)
   {
       System.out.println(new MyKey().key);//prints 91234
   }
}




11. What do you mean by Constructor?

Ans:
Constructor gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.

12. 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.

13. 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.

14.  How garbage collection is done in Java?

Ans:
In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically. For automatic garbage collection java calls either System.gc() method or Runtime.gc() method.

15. What’s difference between Stack and Queue?

Ans:

Stack and Queue both are used as placeholder for a collection of data. The primary difference between a stack and a queue is that stack is based on Last in First out (LIFO) principle while a queue is based on FIFO (First In First Out) principle.



<Back   I   Next>