0% found this document useful (0 votes)
1 views16 pages

Core JAVA Question & Answer

This document provides a comprehensive list of core Java interview questions and answers, covering fundamental concepts such as Java's definition, uses, and key components like JDK, JRE, classes, objects, and variables. It also discusses advanced topics like constructors, inheritance, and OOP principles, along with Java conventions and data types. The content serves as a valuable resource for individuals preparing for Java-related interviews.

Uploaded by

MoOmin ManzOor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views16 pages

Core JAVA Question & Answer

This document provides a comprehensive list of core Java interview questions and answers, covering fundamental concepts such as Java's definition, uses, and key components like JDK, JRE, classes, objects, and variables. It also discusses advanced topics like constructors, inheritance, and OOP principles, along with Java conventions and data types. The content serves as a valuable resource for individuals preparing for Java-related interviews.

Uploaded by

MoOmin ManzOor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CORE JAVA INTERVIEW QUESTIONS & ANSWERS

.--. .-. . .--. .- .-. . -.. -… -.-- .- -- .- -.

1. What is “JAVA”?
➢ JAVA is a high-level, object-oriented, platform-independent, secure programming language.
• It was developed by James Gosling in June 1991.
• First name was OAE and rename it into JAVA on 1995.

2. What are the uses of JAVA?


➢ The uses of java are-
• Developing desktop application.
• To make Wave application.
• Mobile operating System.
• Embedded System.
• Robotics and Games.

3. What is the difference between “JDK” and “JRE”?


➢ JDK (JAVA Development Kit) gives us a compiler that translate ‘.java’ code to Byte code and
generates ‘.class’ file. And JRE (JAVA Runtime Environment) gives us Runtime Environment
which run ‘.class’ file and gives us output.

4. What is “Class” in Java?


➢ A Class in Java is a logical template to create object that share common properties and
methods.
• A Class consist of data members, methods, constructor, interface, nested class.

5. What is “Object” in Java?


➢ An Object is an instance of class that allows programmers to use variables and methods from
inside the class.

6. What is “new” keyword in JAVA?


➢ Its send request to the class to create an object.
• When object is created it get object address and store in a reference variable.

7. What is the deference between “stack” and “heap”?


➢ “Stack” maintains the program execution and “heap” create all the object inside RAM.

8. Where objects are created in JAVA?


➢ Inside heap memory.

9. What is “Garbage Collector”?


➢ Garbage Collector keep on removing unused object from the RAM. Which help us to avoid
over flow of memory and memory management became efficient.
10. What are the types of variable in JAVA?
➢ There are 4 types of variable in JAVA-
I. Static variable.
II. Non-static variable.
III. Local variable.
IV. Reference variable.

11. What is “static variable”?


➢ This type of variable created inside a class and outside the methods, with using “static”
keyword.
• It is belong to Class. Thats why we do not need to create object, we can access it
directly by tacking its Class name.
• It has global access. We can used it anywhere in the program.
• It is not mandatory to initialize it, depending on data type it stored the default
value.
• It’s stored in a dedicated space inside Stack memory called as “Meta Space”.

12. What is “non-static variable”?


➢ This type of variable created inside a class and outside the methods, without using “static”
keyword.
• It is belong to object. Thats why we need to create object to access this type of
variable.
• It is not mandatory to initialize it, depending on data type it stored the default
value.
• It’s stored in a dedicated space inside Stack memory called as “Meta Space”.

13. What is “Local Variable”?


➢ This type of variable created inside the method and should be use only within this method.
• Local variables we are can’t use without initialized it.

14. What is the “Reference Variable”?


➢ A reference variable can store either null value or an object’s address.
• Data type of a reference variable is as class name.
• Its mandatory to initialize.

15. What is “Local Reference variable”?


➢ If a reference variable is created inside a method, then is consider to a “Local reference
variable”. And it can’t be use outside that method.

16. What is “Static Reference variable”?


➢ If a reference variable is created outside method but inside class using static keyword this
type of reference variable known as “Static reference variable “. This variable has global
access & can be used anywhere in the program.
17. Can we change “static” variable’s Value??
➢ Yes, we can change its value by accessing the class name.

18. What is “instance variable“ in JAVA?


➢ All the non-static variables in JAVA also known as “instance variable”.

19. What is the difference between “static” and “non-static” variable?



static variable Non-static variable
1 It’s belong to class. 1 It belong to object.
we can access it by tacking when object is created then we can
2 2
direct class name. access it by tacking object address.
It has global access. We can use it Without object address we can’t
3 3
anywhere in the program. access it.
It can be accessed inside static It can’t be accessed inside a static
4 4
and non-static methods. method.

20. What is the difference between “static variable” and “local variable”?

static variable Local variable
It’s created inside class and
1 1 It’s created inside a method.
outside the methods.
It has global access and can be use
2 2 It is work only inside the method.
anywhere in to the program.
3 We can use it without initialize it 3 We can’t use without initialized it

21. What are the data types in java and how much their memory size?
➢ There are 8 types of data type-
Data Memory Default
S/L remarks
Type size value
1 byte 1 Byte 0 Max value= +127, Min Value= -128
2 short 2 Byte 0 -
Integer
3 int 4 Byte 0 - value
“long” value terminates with ‘l’ or
4 long 8 Byte 0 ‘L’. (ex- 1.02L) Primitive
“float” value terminates with ‘f’ or
5 float 4 Byte 0.0 ‘F’. (ex- 1.02f) Decimal data
value type
6 double 8 Byte 0.0 -
<empty
7 char 2 Byte One digit value (ex- ‘c’)
space>
Having no number and quotation (ex- true,
8 boolean N/A false false)
This are also data type but not a keyword
9 String - null This is not a keyword, it’s a class. (ex- “Mike”)
Not a keyword, it is a dynamic data type variable (available
10 var dynamic - after JDK version 10)

22. What is Primitive Data type in JAVA?


➢ Primitive data types are that reserved keyword that is defined by the system. In java 8
types of primitive data are present-
I. byte
II. short
III. int
IV. long
V. float
VI. double
VII. char
VIII. boolean

23. What is “var” in Java?


➢ A variable with the title “var” can store any kind of value in it. Its introduced in java after
JDK version 10.
• A variable with type var can’t be a methods argument.
• It can be only a local variable.
• Its memory size is dynamic. Its means depending on value it’s memory type will be
vary.
• Var not a keyword. Hence, we can use it as a variable also.
• It is created only inside a method.

24. Can we make a “var” type variable “static” or “non-static”?


➢ No, we can’t make a “var” type variable static or non-static. It can store only local variable.

25. What are the Java Conventions?


➢ The conventions of java are-
• All the keywords in java are in lowercase.
• Class name should start with Uppercase.
• If there two words in class name, then it should be in CamelCasing.
• Variable’s name should be in lowercase.
• If it has two words in a variable then then it should start 1st word with lowercase
and rest word will be CamelCasing.
• Variable’s name can’t be start with number in JAVA.
• Can’t create variable name with space.
• Only two special character are allowed in Java Under Score ‘_’ and Doller “$”.

26. What is “type casting” in Java?


➢ Converting particular data type into required data type is called as type casting.

27. How many types of “type casting” in Java?


➢ There are two types-
I. Auto Up Casting.
II. Explicit Down casting.
28. What is “Auto Up Casting”?
➢ Converting smaller data type into bigger data type without any loss of data is called as Auto
Up Casting.
• During auto up casting data loss should not happen.

29. What is “Explicit Down Casting”?


➢ Converting bigger data type into smaller data type is called as Explicit Down casting.
• There are chances of data loss might happen

30. What is “Class Up Casting”?


➢ Here we store child class object address into parent class reference variable.

31. What is “Class Down Casting”?


➢ Here we store parent class object address into child class reference variable.

32. What is “unary operator”?


➢ It is an operator that can used only with an operand. It is use to represent the positive or
negative value increment or decrement by the value 1.
• It is four types-
I. Pre increment (++i).
II. Post increment (i++).
III. Pre decrement (--i).
IV. Post decrement (i--).

33. What is method in java?


➢ A method is a block of code, which runs only when it is called.
• It is used to perform certain actions.
• It helps us to reuse the code.
• It’s performs specific task then return to the calling statement.
• If method is void then it’s nothing return any value.
• They are also known as function.
• Methods can be static or non-static.
• For static method no need to create object for calling. We can call it directly with
class name.

34. Can we make multiple methods with same name in a same program?
➢ Yes, we can do it, by providing different type of arguments and different numbers of
argument.
• During calling statement we have to gives same type or same numbers of
arguments to call a particular one method.

35. Can we create more than one “main” methods in the same class?
➢ Yes, but providing different numbers of arguments and different types of arguments
36. What is the difference between “return” and “return value”?

return return value
We use return keyword only inside We can’t use return value statement
1 1
the void methods. inside a void method.
If a method is not a void, then
2 It’s optional to use inside method. 2
writing return value is mandatory.
return keyword will transfer control It will transfer control and value both
3 3
to method calling statement. to the method calling statement.

37. When we get “Unreachable Code Error”?


➢ If we are giving any statement after “return” then we get this type of error.

38. What is “void” in Java?


➢ The void keyword specifies that if a method is void then it should not return any value.

39. What is “Scanner” Class?


➢ Whenever a user wants to give input via keyboard, in Java there is inbuild class called as a
“Scanner” Class.

40. What is “Command Line Argument”?


➢ Are those arguments when we passed at the running time in a Java Program are known as
“Command Line Argument”.

41. What is “Constructor”?


➢ In JAVA a constructor is a block of codes which is automatically called when object of the
class is created.
• Constructors should have same name as the class name.
• Constructors are permanently void.
• A constructor having no return type.
• Every time when we create object constructor will called.
• A java constructor cannot be abstract, static or final.
• While creating a constructor if we use “void” keyword in constructor then it’s
treated as a method.

42. What is return type of a constructor?


➢ A constructor by default it is a void, So it’s doesn’t return any value.

43. What are the types of constructor?


➢ There are 3 types of constructor-
I. No-args constructor
II. Parameterized constructor
III. Default constructor

44. What is “Default Constructor”?


➢ Whenever we create object, it is mandatory to call a constructor. If there is no constructor
inside the class at that time java compiler provides a constructor by default which is known
as “Default Constructor”.
• Default Constructors are only for object without arguments.
▪ When an object with argument is created and an object without argument
is created in the same program then the concept of default constructor is
not applicable, hence we get an error.

45. What is “this” keyword?


➢ “this” is a special type reference variable that automatically gets created, to store current
object’s address.
• We can’t use “this” keyword inside a static method.
• Using “this” keyword we can access static members, but we should not do that
because this is the wrong way. During compilation “this” keyword automatically
replaced with class name that’s why we can access it.
• It is use to call a constructor, and this should happen from another constructor
only.
• When we call a constructor, “this” keyword can’t be a second statement, it always
will be 1st statement inside a constructor.

46. What is “Constructor Chaining”?


➢ When we call one constructor from another constructor in this concept known as
“Constructor Chaining”.

47. What is “Constructor Overloading”?


➢ When we create more than one constructor in a same class, by provides them different
number of arguments and different types of arguments, this concept known as
“Constructor Overloading”.

48. What are the deference between “Constructors” and “methods”?


➢ The deference is-
Constructor Method
1 Constructors are permanently void. 1 Methods can be void or can’t be void.
2 We can’t make a Constructor static 2 We can make a method static.
Constructor are called only when object Methods are called either with class
3 3
is created. name or after creating an object.
4 We can’t inherit a constructor. 4 We can inherit a method.

49. What are the four pillars of OOPs Concept?


➢ The four pillars are-
i. Inheritance.
ii. Polymorphism.
iii. Encapsulation.
iv. Abstraction.
50. What is “Inheritance”?
➢ Here we inherit the all members from parent class to child class with an intension of
reusing it.

51. What are the advantages of Inheritance?


➢ The advantages are-
I. The most important use of Inheritance in Java is code reusability.
II. The code that is present in the parent class can be direct use by the child class.
III. It makes the program flexible to change.
IV. With the help of inheritance, we can override the methods of the base class.

52. Does JAVA support Multiple Inheritance?


➢ In class level JAVA doesn’t support Multiple Inheritance, But in Interface level Java Support
Multiple Inheritance.

53. Can we inherit constructor?


➢ No, we can’t inherit a constructor by subclasses, but the constructor of the superclass can
be invoked from the subclass.

54. Does static variable get inherited?


➢ Static variables are not inherited.

55. Can we inherit static methods?


➢ No, we can’t Inherit static method in JAVA.

56. What is ‘final’ keyword in JAVA?


➢ If we make variable “final” then it’s value can’t be change.
• If we make a static/non-static variable “final” then initialization is mandatory.
• if we make a method “final” then overriding is not allowed.
• If we make a class “final” then inheritance is not allowed.

57. What is “Blank Field Error’’?


➢ When we make a static/non-static variable is “final”, If we not initialize it then we get an
error and this error known as “Blank Field Error”.

58. What is “Polymorphism” in JAVA?


➢ Here we can develop a feature in such a way that it can take more then one form that
depending upon the situation.
• Polymorphism is applicable only on methods and not on variables.
• There are two way to achieve polymorphism-
I. Override.
II. Overloading.

59. What is “override” concept?


➢ Here we inherit a method from parent class and then modify its logic in the child class by
once again create this method with the same name.
• With help of override, we can achieve modification of inherited method.

60. What is “@Override” annotation?


➢ During override we write “@Override” annotation in child class to instruct compiler to
check override is happening or not. If not happening then we get an error.
• It’s introduced in Java JDK version 1.5.

61. Can we override static methods?


➢ Static methods are never inherited that’s why we can’t override static methods.

62. What is “Overloading”?


➢ here we create more than one method in the same class with the same name provided they
have different numbers of arguments or different types of arguments.

63. What is “Run Time Polymorphism”?


➢ In run time polymorphism we perform overriding with class upcasting.

64. What is “Interface”?


➢ An interface in java is a blueprint of a class. It can consist of only incomplete methods or
abstract methods.

65. What is the relation between class and interface?


➢ A class extends another class, an interface extends another interface, but a class
implements an interface.

66. What is “Marker Interface”?


➢ All Empty Interfaces are called as “Marker Interface”.

67. Can we create object inside an Interface?


➢ No, we can’t create object inside an Interface.
• But reference variable can be created.

68. Can we develop a constructor inside an Interface?


➢ No, we can’t create its class object that why we can’t develop constructor inside an
Interface.

69. Can we create variable in an Interface?


➢ Yes, but all the variables by default are ‘final’ and ‘static’.

70. Does interface support multiple Interface?


➢ Yes, Interface support multiple Inheritance.

71. Can we develop static incomplete methods in Interface?


➢ No, because we can’t inherit static method in child class, for this Override is also not
possible. That why, we always create Static Complete method in an Interface.

72. Can we create ‘main’ method in an Interface?


➢ Yes, we can create ‘main’ method in an Interface.

73. What is “getters” and “setters”?


➢ These are use to protect our data when we creating classes. For each instance variable a
“getters” method return its value, while “setters” used to sets or update its value.

74. What is “Encapsulation”?


➢ Wrapping of data with the methods and operate this data only through that methods, to
avoid direct accessing on that variables, this concept known as “Encapsulation”.
• Here we make all variables private, so that we avoided direct access of that
variables.
• We generally create publicly define getter and setter methods to operate on that
variable.

75. What is “Abstraction”?


➢ Hiding of implementation details is called as abstraction.
• The way we achieve this in Java by using Interface and Abstract class.

76. What is “abstract” keyword?


➢ It helps us to define that, the method and class is Incomplete.
• However, it is optional to use “abstract” keyword while developing incomplete
methods in Interface.

77. What is “Abstract Class”?


➢ When we make an incomplete method in a class using “abstract” keyword this type of class
known as “Abstract Class”.
• An abstract class can consist of both complete and incomplete method.
• To developed an incomplete method in an Abstract Class it is mandatory to use
“abstract” keyword.
• In an Abstract Class we can create ‘main’ method.
• We can’t create object of an Abstract Class, hence we can’t create constructor here.
• In Abstract Class doesn’t support multiple Inheritance.
• In an Abstract Class We can make static, non-static & final variables.
• We can inherit an Abstract Class by using ‘extends’ keyword.

78. Can we create Constructor inside an Abstract Class?


➢ No, we can’t create object of an Abstract Class, hence we can’t create constructor here.

79. What are the differences between “Interface” and “Abstract Class”?
➢ The Differences are-
Interface Abstract Class
1 There are 100% incomplete. 1 There 0 to 100% incomplete.
Interface can contain only incomplete Abstract Class can Consists of complete and
2 2
methods incomplete both methods.
3 Support multiple Inheritance. 3 Doesn’t support multiple Inheritance.
We create here only static and final variables,
Here we can create static, non-static & final
4 because all variables here by default static 4
variables.
and final.

80. What is “Packages” in JAVA?


➢ Packages are nothing but folder created in java where programs can be store in an organized
manner.
• Its help us to resolve naming convention problem.
• A package name can’t be a keyword.
• Don’t give package name in uppercase.

81. What is “Non-Sub-Class”?


➢ When we create a class’s object in another class then it’s called as “Non-Sub-Class”.

82. What is “Sub-class”?


➢ A class that is derived from another class then, it’s called as “Sub-Class”.

83. What are the Access specifiers in Java?


➢ There are 4 types of Access Specifier in Java-
i. private
▪ We can access only inside a class
ii. Default
▪ We can access only within the same Package.
iii. Protected
▪ We Can access within the same Package and Different Packages only throw
Inheritance.
iv. Public
▪ We can access anywhere from inside Package or outside package.

Access specifier-> private default protected public
Same class + same package + Same class + Same
Same class +
Access Area-> Same class different packages (only throw package
Same package
Inheritance) + Different packages
Variables Yes Yes Yes Yes
Methods Yes Yes Yes Yes
Class No Yes No Yes
constructor Yes Yes Same class + Same package Yes

84. What is “Data Hiding”?


➢ When we make a variable ‘private’, so that we can’t access that variable from outside the
class. This concept known as “Data Hiding”.
• Data Hiding is applicable only on variables, not on methods.
85. What are the new Fetchers of Java Version 8 (or JAVA JDK Version 1.8)?
➢ The new fetchers are-
i. “default” keyword
ii. Functional Interface
iii. Lambdas Expression
iv. Stream API (Collection in Java)
v. Optional Class (Exception in Java)

86. What is “default” keyword?


➢ In Java version 8 introduced “default” keyword, using this keyword we can create complete
method in an interface.

87. What is “Functional Interface”?


➢ A Functional Interface introduce in Java version 8, it consist of exactly one incomplete
method in it. Which can’t be zero or more than one.
• In Functional Interface can have any numbers of default method (complete method) but
incomplete method should be one.

88. Can we do multiple inheritance on Functional Interface?


➢ No, we can’t do that.

89. What is “Lambdas Expression”?


➢ Lambdas Expression Introduce in Java Version 8. Its help us to reduce the numbers of line in
Java Code.
• It can only be applied on “Functional Interface”.
• For using this we don’t need to create an object, implement or overriding, does it all
automatically.
• We can access non-static member of the class using lambdas Expression.

90. What is “IIB”?


➢ IIB (Instance Initialization Block) is a block where initialize all the instance variable in one
place and that gives us better readability of the code.
• IIBs are execute when objects are created.
• Numbers of times we create an object, same times IIB will be called.
• We can initialize both static and non-static variable inside IIB.
• We can add more than one IIB in a Java Program.
• Always IIB will call first then Constructor.
• If we create an object inside IIB, we will not get any error, but then the program will halt
abruptly (Suddenly).

91. What is “SIB”?


➢ SIB (Static Initialization Block) is a block which created inside the class with “static” keyword
and mainly use to initialize all the static variable in one place.
• SIB run before ‘main’ method and its does not required any invoking statement.
• Its run only one time.
• We can’t initialize non-static variables inside SIB.
• We can create object inside SIB.
• We can add more than one SIB in a Java Program.

92. What are the differences between “SIB” and “IIB”?


➢ The differences are-
SIB IIB
1 It’s run before ‘main’ method. 1 It’s run after creating object.
2 2 Numbers of times we create an object,
It’s run only one time.
same times IIB will be run.
3 It’s mainly used to initialize static 3 It’s mainly used to initialize non-static
variable. variable.
4 We can’t initialize non-static variable 4 We can initialize static and non-static
inside IIB. variable both inside IIB.

93. What is “super” keyword?


➢ During Inheritance we use “super” keyword to access the members of parent class.
• We can access static and non-static members both using this keyword.
• “super” keyword can’t be use inside a static method.
• We can use “super” keyword when inheritance is happening, otherwise we can’t use
“super” keyword.
• Using “super” keyword we can called a constructor of a parent class but then we should
use “super” keyword in child class’s constructor and it should be the first statement.
▪ If we don’t keep “super” keyword inside child class’s constructor, then
compiler will automatically place the “super” keyword and it automatically
called no-args constructor from parent class.
▪ If we don’t create no-args constructor in child class then, compiler will
automatically place no-args constructor with “super” keyword.
▪ “Super” keyword automatically kept in every child class constructor (with or
without arguments constructor)
▪ For calling a constructor having arguments we have to write explicitly
“super” keyword by passing same matching of arguments.
▪ We can’t use “this” keyword and “super” keyword in a same constructor to
call another constructor, because both of them must be a first statement
inside a constructor.
▪ If a child class constructor consists of “this” keyword then in that constructor
“super” keyword will not be place automatically.

94. What is “Exception” in JAVA?


➢ Whenever a bad user input is given, it will halt the program execution abruptly (suddenly).
This is called “Exception”.
95. What is “try-catch” block? (or, How to Handle an Exception in Java?)
➢ To handle Exception in Java we used “try-catch” block. If an Exception Occurs in “try” block,
then “try” block automatically creates an Exception object and gives that object’s address to
the “catch” block. “catch” block will now suppress the Exception and the further code from
there will continue to run.

96. What are the types of Exception in Java?


➢ There are two types of exception-
I. Run time Exception or Unchecked Exception.
II. Compile time Exception or Checked Exception.

97. What is “Run time Exception”? (or, What is “Unchecked Exception”?)


➢ If the Exception occurs when we run ‘.class’ file, then this type of Exception known as “Run
time Exception” or “Unchecked Exception”.
• Example-
I. ArithmeticException. (Arithmetic Exception)
II. NumberFormatException. (Number Format Exception)
III. NullPointerException. (Null Pointer Exception)
IV. ArrayIndexOutOfBoundException. (Array Index Out Of Bound Exception)
V. StringIndexOutOfBoundException. (String Index Out Of Bound Exception)
VI. ClassCastException. (Class Cast Exception)

98. What is “Compile time Exception”? (or, What is “Checked Exception”?)


➢ If the Exception occurs when we converting ‘.java’ file to ‘.class’ file then it’s called “Compile
time Exception” or “Checked Exception”.
• Example-
I. FileNotFoundException. (File Not Found Exception)
II. IOException. (I/O Exception)
III. SQLException. (SQL Exception)
IV. ClassNotFoundException. (Class Not Found Exception)
V. CloneNotSupportedException. (Clone Not Supported Exception)

99. What are differences between “Compile time” and “Run time” Exception?
➢ The differences are-
Compile time Exception Run time Exception
It found at during Program Execution
1 It found during compile time 1
time.
2 It’s detected by the compiler 2 It’s not detectable by the compiler.
It’s doesn’t detectable that why we
For this case the compiler prevents the
3 3 can’t prevent the code from the
code from the execution
execution
The example of this type of error is:
The example of this type of error is:
syntax and semicolon error. Such as
4 4 division bye zero, determining the
missing semicolon at the end of the
square root of a negative number.
statement.
100. What is “ArithmeticException”?
➢ This class handle Arithmetic Exception like dividing a number by zero.

101. What is “NumberFormatException”?


➢ When an invalid string to number conversion is done, then we get
“NumberFormatException”.

102. What is “NullPointerException”?


➢ When we access a non-static member with a ‘null’ reference variable. Then we get
“NullPointerException”.

103. What is the difference between using “Exception” class and “NumberFormatException”
class in catch block?
➢ When we use “NumberFormatException” class in catch block, then it can handle only
Number Format Exception, but if we use “Exception” class in catch block, then it can be
handling all the Exceptions occur in try block.

104. What is “finally” block?


➢ “finally” block is an extension of try-catch block. Regardless of whether Exception happen or
not “finally” block will be 100% run.

105. Give a practical example where “finally” keyword is used?


➢ When we write JDBC code, at that time closing of database connection we can do that in
“finally” block.

106. What is “System.exit(0)”?


➢ After writing “System.exit(0)” statement we can forcefully stop any kind of block in Java.
• (Note: Instead of zero (‘0’) we can use here any integer value)

107. How to forcefully stop to execute “finally” block?


➢ By Writing ‘System.exit(0)’ statement we can forcefully stop to execute “finally” block.

108. Can we make only “try-finally” block?


➢ Yes, but if any Exception occurs in “try” block at that time this Exception will not be handle.
If Exception will not handle then only finally block run and then program will be stop.

109. Can we create Multiple “catch” block in a Java Program?


➢ Yes, but we should start with Child class Exception, and then at last parent class (Exception
e) Exception will be there.
• During Exception occurring if there are no matching child class Exception found then
Parent class Exception handle that Exception.

110. What are the differences between “final”, “finally” and “finalize”?
➢ The differences are-
Compared by “final” “finally” “finalize”
Basic 1 It’s a keyword in Java. 1 It’s a block in Java. 1 It’s a method in Java.
It’s always associated
It’s applicable for class, It’s applicable to any
Applicable 2 2 with “try-catch” 2
methods and variables. object.
block.
1> If a variable “final” then
it value can’t be
“finalize” method
change. A “finally” block clean
performs to clean up
2> If a method is “final” up the resources
Working 3 3 3 activities related to
then it can’t be override which use in try
the object before its
by the child class. block,
destruction.
3> If a class is “final” then
it can’t be inherited.
“finally” block “finalize()” method
“final” method is executes just after execute just before
Execution 4 4 4
executed when is call. the execution of try- the destruction of
catch block the object.

111. W
112. W
113. W
114. W
115. W
116. W
117. W
118. W
119. W
120. W
121. W
122. W
123. W
124. W
125. W
126. W
127. W
128. W
129. W
130. W
131. w

You might also like