Core Java - Interview Questions and Answers

61. What is the difference between Assignment and Initialization?

Ans: Assignment can be done as many times as desired whereas initialization can be done only once.

62. What are Class, Constructor and Primitive data types?

Ans: Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform.

Constructor is a special kind of method that determines how an object is initialized when created.

Primitive data types are 8 types and they are:
byte, short, int, long
float, double
boolean
char

63. What is an Object and how do you allocate memory to it?

Ans: Object is an instance of a class and it is a software unit that combines a structured set of data with a set of operations for inspecting and manipulating that data. When an object is created using new operator, memory is allocated to it.

64. What is the difference between constructor and method?

Ans: Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.

65. What are methods and how are they defined?

What are methods and how are they defined?
Ans: Methods are functions that operate on instances of classes in which they are defined. Objects can communicate with each other using methods and can call methods in other classes.

Method definition has four parts. They are name of the method, type of object or primitive type the method returns, a list of parameters and the body of the method. A method's signature is a combination of the first three parts mentioned above.

66. What is the use of bin and lib in JDK?

Ans: Bin contains all tools such as javac, appletviewer, awt tool, etc., whereas lib contains API and all packages.

67. What is Garbage Collection and how to call it explicitly?

Ans: When an object is no longer referred to by any variable, java automatically reclaims memory used by that
object. This is known as garbage collection.

System.gc() method may be used to call it explicitly.

68. What are Transient and Volatile Modifiers?

Transient: The transient modifier applies to variables only and it is not stored as part of its object's
Persistent state. Transient variables are not serialized.
Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by
volatile can be changed unexpectedly by other parts of the program.

69. What is method overloading and method overriding?

Method overloading: When a method in a class having the same method name with different arguments is said to be method overloading.
Method overriding : When a method in a class having the same method name with same arguments is said to be method overriding.

70. What is difference between overloading and overriding?

a) In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method.
b) Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass.
c) In overloading, separate methods share the same name whereas in overriding,subclass method replaces the superclass.
d) Overloading must have different method signatures whereas overriding must have same signature.


Post a Comment

Mới hơn Cũ hơn