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


Which of the following are invalid statements and why?


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

Which of the following are invalid statements and why?
System.out.println(2+2); //line 1
int i= 2+’2’; //line 2
String s= “one”+’two’; //line 3
byte b=256; //line 4
Answer:
Line 1 and line 2 are valid statements.


191.
192.
Line 3 is invalid. The value two is not valid since single quotes
cannot be used for a String, they can only be used for a character
constant. Strings need to be enclosed in double quotes.
Line 4 is invalid. The byte variable b cannot be assigned the
value 256 since it is out of the range of a byte.
What is the use of transient keyword?
Answer:
The transient keyword can be specified on an instance variable.
Specifying transient for an instance variable indicates that its
state should not be saved when the object is serialized. Consider
the following code snippet:
class Electronics implements Serializable {
transient private int price; //Line 1
private int quantity; //Line 2
}
When an instance of the Electronics class is serialized, the
price field will not be serialized, only the quantity field will be
serialized.
What is Garbage Collection in Java?
Answer:
Garbage collection is the process of freeing memory allocated to
objects that are no longer used. During runtime, when an object
is created, the JVM allocates some memory to hold the object.
The JVM periodically checks for objects that are in use and de–
allocates the memory for those objects which are not in use
anymore. Garbage collection is this process of removing objects


193.
194.
195.
that are no longer in use. So, the programmer does not have to
bother about manual de–allocation of memory.

Download 1.52 Mb.

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




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