Java Developer Interview Questions And Answers Pdf

1. Explain the features of interfaces in java?
Answer:
1) All the methods defined in interfaces are implicitly abstract even though abstract modifier is not
declared.
2) All the methods in an interface are public whether they are declared as public or not.
3) variables declared inside an interface are by default public, static and final.
4) Interfaces cannot be instantiated.
5) we cannot declare static methods inside the interface.
6) ‘ implements’ keyword is used to implement the interface.
7) Unlike class, an interface can extend any number of interfaces.
8) We can define a class inside interface and the class acts as the inner class to an interface.
9) An interface can extend a class and implement an interface
10) Multiple inheritances in java are achieved through interfaces.

2. Difference between object and reference?
Answer: Reference and object are both different. Objects are instances of a class that resides in heap memory.
Objects don’t have any name so to access objects we use references. There is no alternative way to
access objects except through references.
The object cannot be assigned to other object and object cannot be passed as an argument to a method.
Reference is a variable which is used to access contents of an object. A reference can be assigned to another reference, passed to a method.
different package nonsubclass.

3. Explain about anonymous inner classes in java?
Answer:
The inner class defined without any class name is called an anonymous inner class. An inner class is declared and instantiated using the new keyword. The main purpose of anonymous inner classes in java is to provide
interface implementation. We use anonymous classes when we need only one instance for a class. We can
use all members of the enclosing class and final local variables.
When we compile anonymous inner classes compiler creates two files

4. Explain restrictions for using anonymous inner classes?
Answer:
1) An anonymous inner class cannot have any constructor because there is no name for class.
2) An anonymous inner class cannot define static methods, fields or classes.
3) We cannot define an interface anonymously.
4) Anonymous inner class can be instantiated only once.

5. Explain about object-oriented programming and its features?
Answer: Java replaced traditional programming language developed in the 1970’s. In Object-oriented programming
everything is made up of the object. In this language, the bottom-up approach is followed. Each
object communicates with others as opposed to the traditional view.
Features:
1) In this bottom approach is followed. First concentrates on minute details like creating objects then
concentrates on implementation or solving the problem.
2) Concentrate more on data and give less importance for implementation.
3) Objects communicate with each other
The main advantage of object-oriented programming language works well for larger problems.

6. What is the difference between preemptive scheduling and time slicing?
Answer: Under preemptive scheduling, the highest priority task performs until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task performs for a predefined slice of time and then re-enters the pool of ready tasks.

7. Define an enumeration?
Answer: Usually, we called enumeration as an enum. An enumeration is an interface containing methods for accessing the original data structure from which the enumeration is obtained. It allows sequential access to all the elements stored in the collection.

8. What is the difference between a JDK and a JVM?
Answer: JDK – Java Development Kit (in short JDK) is Kit which provides the environment to develop and execute(run) the Java program. JDK is a kit(or package) which includes two things
Development Tools(to provide an environment to develop your java programs)
JRE (to execute your java program).
Note: JDK is only used by Java Developers.
JVM – Java Virtual machine(JVM) is a very important part of both JDK and JRE because it is contained or inbuilt in both. Whatever Java program you run using JRE or JDK goes into JVM and JVM is responsible for executing the java program line by line hence it is also known as an interpreter.

9. What is the preferred size of a component?
Answer: The minimum size of the component, enough to display its label is known as the preferred size of the component. For example, the button size should display its label with platform-specific decoration like dotted lines around the label, etc. The FlowLayout manager gives the preferred size to a component.

10. What is a pointer and does Java support pointers?
Answer: Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn’t support the usage of pointers.

11. What is synchronization and why is it important?
Answer: The term synchronization is the ability to control the access of multiple threads to shared resources. And it is important because, without it, it is not possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to major errors.

12. What are constants and how to create constants in java?
Answer: Constants are fixed values whose values cannot be changed during the execution of the program. We create
constants in java using the final keyword.
Ex : final int number =10;
final String str=”java-interview –questions”

13. What are user-defined exceptions?
Answer: To create customized error messages we use user-defined exceptions. We can create user-defined
exceptions as checked or unchecked exceptions.
We can create user-defined exceptions that extend Exception class or subclasses of checked exceptions so
that user-defined exception becomes checked.
User-defined exceptions can extend RuntimeException to create user-defined unchecked exceptions.
Note: It is recommended to keep our customized exception class as unchecked,i.e we need to extend
Runtime Exception class but not the Exception class.

14. What is the catch or declare rule for method declarations?
Answer: If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause.

15. What is the difference between yielding and sleeping?
Answer: When a task invokes its yield() method, it returns to the ready state and when a task invokes its sleep() method, it returns to the waiting state.

16. What is an Iterator interface?
Answer: The Iterator interface is used to step through the elements of a Collection.

17. What are the wrapped, classes?
Answer: The Wrapped classes are those classes that allow primitive types to be accessed as objects.

18. Does garbage collection guarantee that a program will not run out of memory?
Answer: No, Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. Java training.

19. Explain Java Coding Standards for classes or Java coding conventions for classes?
Answer: Sun has created Java Coding standards or Java Coding Conventions. It is recommended highly to follow
java coding standards.
Class names should start with an uppercase letter. Class names should be nouns.

If Class name is of
multiple words then the first letter of inner word must be a capital letter.
Ex: Employee, EmployeeDetails, ArrayList, TreeSet, HashSet

20. Explain Java Coding standards for interfaces?
Answer:
1) The interface should start with uppercase letters
2) Interfaces names should be adjectives
Example: Runnable, Serializable, Marker, Cloneable

21. Difference between overriding and overloading in java?
Answer:
Overriding Overloading
In overriding method names must be same In overloading method names must be same
Argument List must be same Argument list must be different at least order of
arguments.
The return type can be the same or we can return covariant
type. From 1.5 covariant types are allowed
The return type can be different in overloading.
We cant increase the level of checked exceptions.
No restrictions for unchecked exceptions
In overloading different exceptions can be thrown.
A method can only be overridden in subclass A method can be overloaded in the same class or
subclass
Private, static and final variables cannot be
overridden.
Private, static and final variables can be
overloaded.
In overriding which method is called is decided at
runtime based on the type of object referenced at
run time
In overloading which method to call is decided at
compile-time based on the reference type.
Overriding is also known as Runtime polymorphism,
dynamic polymorphism or late binding
Overloading is also known as Compile time
polymorphism, static polymorphism or early
binding.

22. Which class is the superclass of all classes?
Answer:
java.lang.The object is the superclass class for all the java classes and we don’t need to extend it.

23. Can we overload run() method in java?
Answer: We can overload run method but Thread class start method will always cal run method with no
arguments. But the overloaded method will not be called by start method we have to explicitly call this
start() method.

24. What is a lock or purpose of locks in java?
Answer:
The lock also called monitor is used to prevent access to a shared resource by multiple threads.
A lock is associated with a shared resource. Whenever a thread wants to access a shared resource if must first
acquire a lock. If already a lock has been acquired by others it can’t access that shared resource. At this
moment the thread has to wait until another thread releases the lock on a shared resource. To lock an
object we use synchronization in java.
A lock protects a section of code allowing only one thread to execute at a time.

25. What are synchronized methods?
Answer: If we want a method of the object to be accessed by a single thread at a time we declare that method with
synchronized keyword.
Signature:
public synchronized void methodName(){}
To execute the synchronized method first lock has to be acquired on that object. Once the synchronized method
is called lock will be automatically acquired on that method when no other thread has a lock on that
method. once the lock has been acquired then synchronized method gets executed. Once synchronized
method execution completes automatically lock will be released. The prerequisite to executing a
synchronized method is to acquire a lock before method execution. If there is a lock already acquired by
any other thread it waits till the other thread completes. company

26. Explain about narrowing conversion in java?
Answer:
When destination type is smaller than source type we use narrowing conversion mechanism in java.
Narrowing conversion has to be done manually if destination type is smaller than source type. To do
narrowing conversion we use a cast. The cast is nothing but explicit type conversion.
Example: long a;
byte b;
b=(byte)a;
Note: casting to be done only on valid types otherwise class cast exception will be thrown.

27. Explain the importance of import keyword in java?
Answer:
Import keyword is used to import a single class or package into our source file. import statement is
declared after package decalaration. We use the wild character (*) to import package.
Note: After compilation, the compiled code does not contain import statement it will be replaced with fully
qualified class names

28. Explain try and catch keywords in java?
Answer:
In the try block, we define all exception causing code. In java try and catch forms a unit. A catch block catches
the exception thrown by preceding try block. Catch block cannot catch an exception thrown by another try
block. If there is no exception causing code in our program or exception is not raised in our code JVM
ignores the try-catch block.
Syntax:
try
Catch(Exception e)

29. Can we have a try block without catch block?
Answer: Each try block requires at least one catch block or finally block. A try block without a catch or finally will
result in a compiler error. We can skip either of catch or finally block but not both.

30. Can we have multiple catch block for a try block?
Answer: In some cases, our code may throw more than one exception. In such a case, we can specify two or more
catch clauses, each catch handling different type of exception. When an exception is thrown JVM checks
each catch statement in order and the first one which matches the type of exception is execution and
remaining catch blocks are skipped.
Try with multiple catch blocks is highly recommended in java.
If try with multiple catch blocks are present the order of catch blocks is very important and the order
should be from child to parent.

31. What are thread groups?
Answer: Thread Groups are a group of threads and other thread groups. It is a way of grouping threads so that
actions can be performed on a set of threads for easy maintenance and security purposes.
For example, we can start and stop all thread groups. We rarely use thread group class. (SVR Technologies) By default, all the
threads that are created belong to default thread group of the main thread. Every thread belongs to a
thread group. Threads that belong to a particular thread group cannot modify threads belonging to
another thread group.

32. Difference between ‘IS-A’ and ‘HAS-A’ relationship in java?
Answer: IS-A relationship HAS- A RELATIONSHIP
Is a relationship also known as inheritance Has a relationship also known as composition or
aggregation.
For IS-A relationship we use extends keyword For Has a relationship we use the new keyword
Ex: Car is a vehicle. Ex: Car has an engine. We cannot say Car is an
engine
The main advantage of inheritance is reusability of
code
The main advantage of has a relationship is
reusability of code.

33. Explain about instanceof operator in java?
Answer: Instanceof operator is used to testing the object is of which type.
Syntax: instanceof
Instanceof returns true if reference expression is a subtype of the destination type.
Instanceof returns false if reference expression is null.
Example: public classInstanceOfExample {public static voidmain(String[] args) {Integer a =
newInteger(5);if (a instanceof java.lang.Integer) {
System.out.println(true);
else {
System.out.println(false);
Since a is integer object it returns true.
There will be a compile-time check whether reference expression is a subtype of the destination type. If it is not
a subtype then a compile-time error will be shown as Incompatible types.

34. Can we have multiple classes in a single file?
Answer: Yes we can have multiple classes in single file but it people rarely do that and not recommended. We can
have multiple classes in File but only one class can be made public. If we try to make two classes in File
public we get following compilation error.
“The public type must be defined in its own file”.

35. What all access modifiers are allowed for top class?
Answer: For top-level class only two access modifiers are allowed. public and default. If a class is declared as
public it is visible everywhere.
If a class is declared default it is visible only in the same package.
If we try to give private and protected as access modifier to class we get the below compilation error.
Illegal Modifier for the class only public, abstract and final are permitted.

36. What are packages in java?
Answer: Package is a mechanism to group related classes , interfaces and enums into a single module.
The package can be declared using the following statement :
Syntax: package Coding Convention: package name should be declared in small letters.
package statement defines the namespace.
The main use of the package is
1) To resolve naming conflicts
2) For visibility control: We can define classes and interfaces that are not accessible outside the class.

37. Can we have more than one package statement in the source file?
Answer: We can’t have more than one package statement in the source file. In any java program, there can be almost
only 1 package statement. We will get a compilation error if we have more than one package statement in
the source file.

38. Explain about Varargs in java?
Answer: Beginning with Java 5 has a new feature Varargs which allows methods to have a variable number of
arguments. It simplifies the creation of methods when there is a number of arguments. Earlier to java 5
Varargs are handled by creating method with an array of arguments.
Ex: public static void main(String[] args)
A variable-length argument is specified using ellipsis with type in signature. main method with var args
is written as follows:
public static void main(String … args)
If no arguments are passes we get an array with size 0. There is no need for null check if no arguments are
passed.

39. Explain the life cycle of thread?
Answer:
A thread can be in any of the five states :
1) New: When the instance of the thread is created it will be in New state.
Ex: Thread t= new Thread();
In the above example, t is in the new state. The thread is created but not inactive state to make it active we
need to call start() method on it.
2) Runnable state: A thread can be in the runnable state in either of the following two ways:
a) When the start method is invoked or
b) A thread can also be in the runnable state after coming back from blocked or sleeping or waiting for the state.
3) Running state: If thread scheduler allocates CPU time, then the thread will be in running state. (company)

4) Waited /Blocking/Sleeping state:
In this state, the thread can be made temporarily inactive for a short period of time. A thread can be in
the above state in any of the following ways:
1) The thread waits to acquire a lock of an object.
2) The thread waits for another thread to complete.
3) The thread waits for notification of other thread.
5) Dead State: A thread is in the dead state when thread’s run method execution is complete. It dies
automatically when thread’s run method execution is completed and the thread object will be garbage
Collected.

40. What are identifiers in java?
Answer: Identifiers are names in java program. Identifiers can be a class name, method name or variable name.
Rules for defining identifiers in java:
1) Identifiers must start with a letter, Underscore or dollar($) sign.
2) Identifiers can’t start with numbers.
3) There is no limit on the number of characters in identifier but not recommended to have more than 15
characters
4) Java identifiers are case sensitive.
5) The first letter can be the alphabet or underscore and dollar sign. From the second letter, we can have numbers
6) We couldn’t use reserve words for identifiers in java.

41. What are access modifiers in java?
Answer: The important feature of encapsulation is access control. By preventing access control we can misuse of
class, methods, and members.
A class, method or variable can be accessed is determined by the access modifier. There are three types
of access modifiers in java. public,private,protected. If no access modifier is specified then it has default
access.

42. What is the final access modifier in java?
Answer: final access modifier can be used for class, method, and variables. The main advantage of final access
modifier is security no one can modify our classes, variables, and methods. The main disadvantage of final
access modifier is we cannot implement oops concepts in java.

Ex: Inheritance, polymorphism.
final class: A final class cannot be extended or subclassed. We ar e preventing inheritance by marking a
class as final. But we can still access the methods of this class by composition. Ex: String class
final methods: Method overriding is one of the important features in java. But there are situations where
we may not want to use this feature. Then we declared the method as final which will print overriding. To
allow a method from being overridden we use final access modifier for methods.
final variables: If a variable is declared as final, it behaves like a constant. We cannot modify the value
of the final variable. Any attempt to modify the final variable results in compilation error. The error is as
follows
“final variable cannot be assigned.”

43. What is Exception handling in java?
Answer: Exception handling is a mechanism for what to do when some abnormal situation arises in the program. When an
exception is raised in program it leads to termination of program when it is not handled properly. The significance of exception handling comes here in order not to terminate a program abruptly and to
continue with the rest of the program normally. This can be done with the help of Exception handling.

Note: Browse latest Java Interview Questions and JAVA Tutorial Videos. Here you can check Java training details and JAVA Training Videos for self learning. Contact +91 988 502 2027 for more information.

Leave a Comment

FLAT 30% OFF

Coupon Code - GET30
SHOP NOW
* Terms & Conditions Apply
close-link