Top 43 Java Developer Interview Questions And Answers Pdf

1. Explain features of interfaces in java ? (Java Developer Interview Questions)

Answer:

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

Java Developer Interview Questions

2. Difference between object and reference ?

Answer:

Reference and object are both different. Objects are instances of class that resides in heap memory.
Objects does’nt have any name so to access objects we use references. There is no alternative way to
access objects except through references.

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 other
reference ,passed to a method.
fferent package non subclass.

3. Explain about anonymous inner classes in java ?

Answer:

Inner class defined without any class name is called anonymous inner class. Inner class is declared and
instantiated using new keyword.The main purpose of anonymous inner classes in java are to provide
interface implementation. We use anonymous classes when we need only one instance for a class. We can
use all members of 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 1970’s. In Object oriented programming
everything is made up of object. In this language bottom up approach is followed. Each
object communicates with other as opposed to 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 is 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 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 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.

Best 48 SAP Simple Finance Interview Questions Pdf

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 program. We create
constants in java using 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 userdefined 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 userdefined exception becomes checked.
Userdefined exceptions can extend RuntimeException to create userdefined 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 Excpetion 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.

Java Developer Interview Questions

17. What are 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 traninig

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.
Classnames should start with uppercase letter. Classnames names should be nouns. If Class name is of
multiple words then the first letter of inner word must be capital letter.
Ex : Employee, EmployeeDetails, ArrayList, TreeSet, HashSet

20. Explain Java Coding standards for interfaces ?

Answer:

1) 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 atleast order of
arguments.
Return type can be same or we can return covariant
type. From 1.5 covariant types are allowed
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 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 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.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:

Lock also called monitor is used to prevent access to a shared resource by multiple threads.
A lock is associated to 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 other it can’t access that shared resource. At this
moment the thread has to wait until another thread releases the lock on shared resource. To lock an
object we use synchronization in java.
A lock protects section of code allowing only one thread to execute at at a time.

Java Developer Interview Questions

25. What are synchronized methods ?

Answer:

If we want a method of object to be accessed by single thread at a time we declare that method with
synchronized keyword.
Signature :
public synchronized void methodName(){}
To execute synchronized method first lock has to be acquired on that object. Once synchronized method
is called lock will be automatically acquired on that method when no other thread has lock on that
method. once lock has been acquired then synchronized method gets executed. Once synchronized
method execution completes automatically lock will be released. The prerequisite to execute a
synchronized method is to acquire 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 cast. 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 classcastexception will be thrown.

27. Explain the importance of import keyword in java ?

Answer:

Import keyword is used to import single class or package in to our source file.import statement is
declared after package decalaration. We use 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 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 try block without catch block ?

Answer:

Each try block requires atleast one catch block or finally block. A try block without catch or finally will
result in 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 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 group of threads and other thread groups. It is a way of grouping threads so that
actions can be performed on 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. 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 uses extends keyword For Has a relationship we use 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.

Java Developer Interview Questions

33. Explain about instanceof operator in java ?

Answer:

Instanceof operator is used to test the object is of which type.
Syntax : instanceof
Instanceof returns true if reference expression is subtype of 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 subtype of destination type. If it is not
a subtype then compile time error will be shown as Incompatible types.

34. Can we have multiple classes in 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 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 in to a single module.
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 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 source file ?

Answer:

We can’t have more than one package statement in source file. In any java program there can be atmost
only 1 package statement. We will get compilation error if we have more than one package statement in
source file.

Java Developer Interview Questions

38. Explain about Varargs in java ?

Answer:

Beginning with Java 5 has a new feature Varargs which allows methods to have variable number of
arguments. It simplifies creation of methods when there are more number of arguments. Earlier to java 5
Varargs are handled by creating method with array of arguments.
Ex : public static void main(String[] args)
A variable length argument is specified using ellispses 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 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 thread is created it will be in New state.
Ex : Thread t= new Thread();
In the above example t is in new state. The thread is created but not in active 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 runnable state after coming back from blocked or sleeping or waiting state.
3) Running state : If thread scheduler allocates cpu time, then the thread will be in running state.
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 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 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 class name, method name or variable name.
Rules for defining identifiers in java:
1) Identifiers must start with letter,Underscore or dollar($) sign.
2) Identifiers can’t start with numbers .
3) There is no limit on number of characters in identifier but not recommended to have more than 15
characters
4) Java identifiers are case sensitive.
5) First letter can be alphabet ,or underscore and dollar sign. From second letter we can have numbers
.
6) We should’nt 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 a default
access.

42. What is 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 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 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 what to do when some abnormal situation arises in 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 program normally. This can be done with help of Exception handling.

Check Demo Sesstion