Java Interview Questions And Answers

2018 Latest 60 Java Interview Questions And Answers Pdf

1. What is the right data type to represent a price in Java ? (Java Interview Questions And Answers)

Answer:
BigDecimal if memory is not a concern and Performance is not critical, otherwise double with predefined precision.

Java Interview Questions And Answers

2. What is class in Java ?

Answer:
Java encapsulates the codes in various classes which define new data types. These new data types are used to create objects.

3. What is a JVM ?

Answer:
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.

4. Explain method overloading ?

Answer:
When a Java program contains more than one methods with the same name but different properties, then it is called method overloading.

5. Does Java support multiple inheritance ?

Answer:
Java doesn’t support multiple inheritance.

java online training

6. What restrictions are placed on the location of a package statement within a source code file ?

Answer:
A package statement must appear as the first line in a source code file (eliminating blank lines and comments).

Read Out: Common Interview Mistakes

7. What is a transient variable ?

Answer:
A transient variable is a variable that may not be serialized.

8. Is null a keyword ?

Answer:
No, the null is not a keyword.

Java Interview Questions And Answers

9. What’s new with the stop(), suspend() and resume() methods in JDK 1.2 ?

Answer:
These methods have been deprecated in JDK 1.2.

10. What method is used to specify a container’s layout ?

Answer:
The setLayout() method is used to specify a container’s layout.

Best 48 SAP Simple Finance Interview Questions Pdf

11. What is the immediate superclass of the Applet class ?

Answer:
The Panel class is the immediate superclass of the Applet class.

12. Can we rethrow the same exception from catch handler ?

Answer:
Yes we can rethrow the same exception from our catch handler. If we want to rethrow checked exception
from a catch block we need to declare that exception.

13. what value is a variable of the String type automatically initialized ?

Answer:
The default value of an String type is null.

14. When a thread blocks on I/O, what state does it enter ?

Answer:
When it blocks on I/O, A thread enters the waiting state.

15. Which containers use a Flow Layout as their default layout ?

Answer:
The Panel and Applet classes use the Flow Layout as their default layout.

java training online

16. What modifiers may be used with an inner class that is a member of an outer class ?

Answer:
A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.

Java Interview Questions And Answers

17. Which java.util classes and interfaces support event handling ?

Answer:
The Event Object class and the Event Listener interface support event processing.

18. What is the Vector class ?

Answer:
The term Vector class provides the ability to implement a growable array of objects.

19. What is the difference between the >> and >>> operators ?

Answer:
The >> operator carries the sign bit when shifting right while the >>> zero-fills bits that have been shifted out.

20. What is a native method ?

Answer:
A native method is a method that is applied in a language other than Java.

21. What value does read Line() return when it has reached the end of a file ?

Answer:
The readLine() method returns null, when it has reached the end of a file.

22. What is clipping ?

Answer:

Clipping is the process of confining paint operations to a limited area or shape.

23.Can a for statement loop indefinitely ?

Answer:
Yes, a for statement can loop indefinitely. For example, consider the following:for(;;) .

Java Interview Questions And Answers

24. Explain Java Coding standards for Methods ?

Answer:
1) Method names should start with small letters.
2) Method names are usually verbs
3) If method contains multiple words, every inner word should start with uppercase letter.
Ex : toString()
4) Method name must be combination of verb and noun
Ex : getCarName(),getCarNumber()

25. Explain Java Coding Standards for Constants ?

Answer:
Constants in java are created using static and final keywords.
1) Constants contains only uppercase letters.
2) If constant name is combination of two words it should be separated by underscore.

3) Constant names are usually nouns.
Ex:MAX_VALUE, MIN_VALUE, MAX_PRIORITY, MIN_PRIORITY

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

27. Explain Java Coding Standards for variables ?

Answer:
1) Variable names should start with small letters.
2) Variable names should be nouns
3) Short meaningful names are recommended.
4) If there are multiple words every innerword should start with Uppecase character.
Ex : string,value,empName,empSalary

28. Name three Component subclasses that support painting ?

Answer:
The Canvas, Frame, Panel, and Applet classes support painting.

29. What is the difference between JDK and JVM ?

Answer:
we’re presenting the difference between JDK and JVM in tabular format, take a look…

JDK JVM
Full Form: Java Development Kit Full Form: Java Virtual Machine
For Development Purpose

To execute the java programs
It provides all the tools, executables and binaries required to compile, debug and execute a Java Program The execution part is handled by JVM to provide machine independence

30. Why Java doesn’t support multiple inheritance ?

Answer:
Because of “Diamond Problem”, the Java doesn’t support multiple inheritance in classes.

31. Why Java is not pure Object Oriented language ?

Answer:
Java support primitive types such as int, byte, short, long etc that why it is not said to be pure object oriented language. Java traning

Java Interview Questions And Answers

32. What are access modifiers ?

Answer:
Java provides three access controlssuch as public, private and protected access modifier. When none of these are used, it’s called default access modifier.

33. Can we overload main method ?

Answer:
Yes, we can overload the main methodwith syntax as public static void main(String args[]).

34. What is method in java ?

Answer:
It contains the executable body that can be applied to the specific object of the class.
Method includes method name, parameters or arguments and return type and a body of executable code.
Syntax : type methodName(Argument List){

ex : public float add(int a, int b, int c)
methods can have multiple arguments. Separate with commas when we have multiple arguments.
thrown in the method are instances of their subclass.

35. Can we use catch statement for checked exceptions ?

Answer:
If there is no chance of raising an exception in our code then we can’t declare catch block for handling
checked exceptions .This raises compile time error if we try to handle checked exceptions when there is
no possibility of causing exception. (Best 47 Java Programming Interview Questions And Answers)

36. Explain a situation where finally block will not be executed ?

Answer:
Finally block will not be executed whenever jvm shutdowns. If we use system.exit(0) in try statement
finally block if present will not be executed.

37. Explain about main() method in java ?

Answer:
Main() method is starting point of execution for all java applications.
public static void main(String[] args) {}
String args[] are array of string objects we need to pass from command line arguments.
Every Java application must have atleast one main method. company

38. What is constructor in java ?

Answer:
A constructor is a special method used to initialize objects in java.
we use constructors to initialize all variables in the class when an object is created. As and when an object
is created it is initialized automatically with the help of constructor in java.
We have two types of constructors
Default Constructor
Parameterized Constructor

39. How can we find the actual size of an object on the heap ?

Answer:
In Java, there is no way to find out the actual size of an object on the heap.

Java Interview Questions And Answers

40. In how many ways we can do synchronization in java ?

Answer:
There are two ways to do synchronization in java:
1) Synchronized methods
2) Synchronized blocks
To do synchronization we use synchronize keyword.

41. Explain about Automatic type conversion in java ?

Answer:
Java automatic type conversion is done if the following conditions are met :
1) When two types are compatible
Ex : int, float
int can be assigned directly to float variable.
2) Destination type is larger than source type.
Ex : int, long

Int can be assigned directly to long .Automatic type conversion takes place if int is assigned to long
because long is larger datatype than int.
Widening Conversion comes under Automatic type conversion.

42. In how many ways we can do exception handling in java ?

Answer:
We can handle exceptions in either of the two ways :
1) By specifying try catch block where we can catch the exception.
2) Declaring a method with throws clause .

43. What does null mean in java ?

Answer:
When a reference variable doesn’t point to any value it is assigned null.
Example : Employee employee;
In the above example employee object is not instantiate so it is pointed no where.

44. Can we define package statement after import statement in java ?

Answer:
We can’t define package statement after import statement in java. package statement must be the first
statement in source file. We can have comments before the package statement.

Check More Tutorial Videos

 

java course content

45. Explain where variables are created in memory ?

Answer:
When we declare variables variables are created in stack. So when the variable is out of scope those
variables get garbage collected.

46. When do we use synchronized blocks and advantages of using synchronized blocks ?

Answer:
If very few lines of code requires synchronization then it is recommended to use synchronized blocks. The
main advantage of synchronized blocks over synchronized methods is it reduces the waiting time of
threads and improves performance of the system.

47. What is the difference between access specifiers and access modifiers in java ?

Answer:
In C++ we have access specifiers as public,private,protected and default and access modifiers as static,
final. But there is no such divison of access specifiers and access modifiers in java. In Java we have
access modifiers and non access modifiers.
Access Modifiers : public, private, protected, default
Non Access Modifiers : abstract, final, stricfp.

Java Interview Questions And Answers

48. What access modifiers can be used for class ?

Answer:
We can use only two access modifiers for class public and default.
public: A class with public modifier can be visible
1) In the same class
2) In the same package subclass
3) In the same package nonsubclass
4) In the different package subclass
5) In the different package non subclass.
default : A class with default modifier can be accesed
1) In the same class
2) In the same package subclass
3) In the same package nonsubclass
4) In the different package subclass
5) In the different package non subclass.

49. Explain about abstract classes in java ?

Answer:
Sometimes we may come across a situation where we cannot provide implementation to all the methods
in a class. We want to leave the implementation to a class that extends it. In such case we declare a class
as abstract.To make a class abstract we use key word abstract. Any class that contains one or more
abstract methods is declared as abstract. If we don’t declare class as abstract which contains abstract
methods we get compile time error. We get the following error.
“The type must be an abstract class to define abstract methods.”
Signature ; abstract class

For example if we take a vehicle class we cannot provide implementation to it because there may be two
wheelers , four wheelers etc. At that moment we make vehicle class abstract. All the common features of
vehicles are declared as abstract methods in vehicle class. Any class which extends vehicle will provide
its method implementation. It’s the responsibility of subclass to provide implementation.
The important features of abstract classes are :
1) Abstract classes cannot be instantiated.
2) An abstract classes contains abstract methods, concrete methods or both.
3) Any class which extends abstract class must override all methods of abstract class.
4) An abstract class can contain either 0 or more abstract methods.

Though we cannot instantiate abstract classes we can create object references . Through superclass
references we can point to subclass.

50. Can we create constructor in abstract class ?

Answer:
We can create constructor in abstract class , it does’nt give any compilation error. But when we cannot
instantiate class there is no use in creating a constructor for abstract class.

51. What are abstract methods in java ?

Answer:
An abstract method is the method which does’nt have any body. Abstract method is declared with
keyword abstract and semicolon in place of method body.
Signature : public abstract void ();
Ex : public abstract void getDetails();
It is the responsibility of subclass to provide implementation to abstract method defined in abstract class.
Java Exception Handling Interview questions.

52. State some situations where exceptions may arise in java ?

Answer:
1) Accesing an element that does not exist in array.
2) Invalid conversion of number to string and string to number.
(NumberFormatException)
3) Invalid casting of class
(Class cast Exception)
4) Trying to create object for interface or abstract class
(Instantiation Exception)

53. What is an exception in java ?

Answer:
In java exception is an object. Exceptions are created when an abnormal situations are arised in our
program. Exceptions can be created by JVM or by our application code. All Exception classes are defined
in java.lang. In otherwords we can say Exception as run time error.

54. What is an eror in Java ?

Answer:
Error is the subclass of Throwable class in java. When errors are caused by our program we call that as
Exception, but some times exceptions are caused due to some environment issues such as running out of
memory. In such cases we can’t handle the exceptions. Exceptions which cannot be recovered are called
as errors in java.
Ex : Out of memory issues.

55. What are advantages of Exception handling in java ?

Answer:
1) Separating normal code from exception handling code to avoid abnormal termination of program.
2) Categorizing in to different types of Exceptions so that rather than handling all exceptions with
Exception root class we can handle with specific exceptions. It is recommended to handle exceptions with
specific Exception instead of handling with Exception root class.
3) Call stack mechanism : If a method throws an exception and it is not handled immediately, then that
exception is propagated or thrown to the caller of that method. This propogation continues till it finds an
appropriate exception handler ,if it finds handler it would be handled otherwise program terminates
Abruptly.

56. In how many ways we can create threads in java ?

Answer:
We can create threads in java by any of the two ways :
1) By extending Thread class
2) By Implementing Runnable interface.

57. Explain creating threads by implementing Runnable class ?

Answer:
This is first and foremost way to create threads . By implementing runnable interface and implementing
run() method we can create new thread.
Method signature : public void run()
Run is the starting point for execution for another thread within our program.
Example :
public class MyClass implements Runnable {
@Override
public void run()

58. When do we use synchronized methods in java ?

Answer:
If multiple threads tries to access a method where method can manipulate the state of object , in such
scenario we can declare a method as synchronized.

59. Explain importance of finally block in java ?

Answer:
Finally block is used for cleaning up of resources such as closing connections, sockets etc. if try block
executes with no exceptions then finally is called after try block without executing catch block. If there is
exception thrown in try block finally block executes immediately after catch block.
If an exception is thrown,finally block will be executed even if the no catch block handles the exception.

60. Can we catch more than one exception in single catch block ?

Answer:
From Java 7, we can catch more than one exception with single catch block. This type of handling reduces
the code duplication.
Note : When we catch more than one exception in single catch block , catch parameter is implicity final.
We cannot assign any value to catch parameter.
Ex : catch(ArrayIndexOutOfBoundsException || ArithmeticException e)

In the above example e is final we cannot assign any value or modify e in catch statement.