Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Explain some of the important methods on the File class


Download 1.52 Mb.
Pdf ko'rish
bet70/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   66   67   68   69   70   71   72   73   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Explain some of the important methods on the File class.
Answer:
A File class encapsulates a file or directory on the file system.
Some of the methods on the File class are as follows:
createNewFile(): Creates a new empty file
list(): Returns an array of the files/directories within the
current directory
delete: Deletes the file from the files system
mkdir: Creates a directory
getAbsolutePath: Returns the absolute path of the file



187.
188.
CHAPTER
18
Miscellaneous
Explain what happens when the following code snippet is
compiled.
public class Test {
public static void main(String argument[]) {
int iValue;
System.out.println(iValue);
}
}
Answer:
This code will cause a compilation error. This is because the
variable iValue is not initialized but used in the Sysout
statement. The error can be fixed by initializing the variable
iValue before it is used as follows:
int iValue = 0;
System.out.println(iValue);
Explain what happens when the following code is
compiled and executed.
class Calculate {
float fValue = 10.2f;
public static void main(String argument[]) {
System.out.println(“Float value is: “+fValue);


189.
190.
}
}
Answer:
The above code will cause a compilation error because the main
method tries to access the non–static instance variable fvalue.
Java does not allow accessing a static variable from a non–static
method.
Explain symmetric equals() contract.
Answer:
Suppose that there are two objects v1 and v2. If
v1.equals(v2) returns true then v2.equals(v1) must return
true. This equals() contract is called as symmetric contract.
Consider the following code:
Object v1; Object v2;
v1.equals(v2); //Line 1
v2.equals(v1); //Line 2
So, in case of a symmetric contract, Line 1 returns true if and
only if Line 2 returns true.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   66   67   68   69   70   71   72   73   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling