Sunday, 26 February 2023
Core Java Questions & Answers | Set – 5
By : Gayatri Mishra
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/
Question - 1:
Syntax error in java comes under Exception or Error? What is the difference between Error and
Exception ?
Syntax error in java comes under Exception or Error?
*Syntax errors are recoverable .
*Syntax errors does not belongs to Error or Exception.
* Both Error , Exception are the sub classes of Throwable class in Java.
Difference between Error & Exception :
Error :
Error is irrecoverable or can not be recovered .
Can occur only during run time.
Example of Error :
OutOfMemoryError , IOError
Note : I have not come across any such Error
Exception:
Exceptions are recoverable using Exception handlers .
Exceptions can occur during compile time and run time.
Checked Exceptions are compile time exceptions , as these exceptions are checked the compiler
during compilation process.
Example of Exception :
NumberFormatException
NullPointerException
FileNotFoundException
ArrayIndexOutOfBoundsException
Question -2 :
What is the difference between final , finally and finalize ?
final :
final is a nonaccess modifier for class , attributes and methods to prevent inheritance, to create
constants and to prevent overriding.
finally :
finally is a block which which we can use with try and catch . finally block will get execute every time
whether there is an exception in code or there is no exception.
Sequence is :
try {
}
catch() {
}
finally {
finalize :
finalize is a method which is used to perform the cleanup processing just before the the object is
garbage collected.
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/
Question – 3:
What is the use of static in java?
*The main purpose of using static keyword in java program is to save memory by not creating
objects. In this way memory is getting saved.
* How the memory will be saved ?
Static members are considered as class members. We can access them without creating object.
* Static can be used for variable , method and blocks .
* Static variable will automatically get initialized to 0 , can be accessed without any object. Static
variable can be accessed by using class name.
Example of static variable :
static int empid;
static double sal ;
* Static block will execute before the main method . It is used to initialize the static data members.
Example of static block :
static {
// statement 1;
// statement 2;
* Static method can be called without any object , by using respective class name. Static method
may or may not return value.
Example :
static void displayEmpDetails( ) {
// statement 1;
// statement 2;
}
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/
Please find the below sample code with output:
O/p :
Good morning Connections !
45000.50
49001.00
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/
Question -4:
What is Object class in Java ?How to view only object class methods ?
# Object class is the parent class for all the classes in java.
# Object class is useful if you want to use any object whose type is unknown .
# Object class provides multiple methods .
# Few methods are listed below.
equals( )
toString( )
clone( )
getClass( )
hashCode( )
# equals ( ) , toString( ) are frequently used methods from object class # As these are object class
methods, we can use these methods for string , array and other collection classes to compare and to
convert to string .
# How to view the object class methods in Eclipse or any other IDE :
Class Test {
public static void main(String args[]) {
// create an object
Test obj = new Test( );
// To view object class method just use obj. , automatically all the methods you can see.
obj.
}
}
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/