Quiz
06-Objectives-Wise-Garbage-Collection
This quiz has a time limit, so it opens in the usual player.
Questions in this quiz (5)
1Consider the following code snippet:
public class Test{
void test( ) {
MyClass obj = new MyClass( ) ;
obj . name = " j ack" ;
// 1 insert code here
}
}/
/In MyClass. j ava
public class MyClass{
int value;
String name;
}Wha
t can be inserted at // 1, which will make the object referred to by obj eligible for
garbage collection?- Aobj . destroy( ) ;
- BRuntime. getRuntime( ) . gc( ) ;
- Cobj = null;
- Dobj . finalize( )
- Eobj . name = null; as well as obj = null;
2Which is the earliest line in the following code after which the object created on line
// 1 can be garbage collected, assuming no compiler optimizations are done?
public class NewClass{
private Obj ect o;
void doSomething( Obj ect s) { o = s; }
public static void main( String args[ ] ) {
Obj ect obj = new Obj ect( ) ; // 1
NewClass tc = new NewClass( ) ; //2
tc. doSomething( obj ) ; //3
obj = new Obj ect( ) ; //4
obj = null; //5
tc. doSomething( obj ) ; //6
}
}- ALine 1
- BLine 2
- CLine 3
- DLine 4
- ELine 5
- FLine 6
3Consider the following code:
class MyClass { }
public class TestClass{
MyClass getMyClassObj ect( ) {
MyClass mc = new MyClass( ) ; //1
return mc; //2
}p
ublic static void main( String[ ] args) {
TestClass tc = new TestClass( ) ; //3
MyClass x = tc. getMyClassObj ect( ) ; //4
System. out. println( " got myclass obj ect" ) ; //5
x = new MyClass( ) ; //6
System. out. println( " done" ) ; //7
}
}A
fter what line the MyClass object created at line 1 will be eligible for garbage
collection?- A2
- B5
- C6
- D7
- ENever till the program ends.
4In the following code, after which statement (earliest), the object originally held in s,
may be garbage collected ?
1. public class TestClass{
2. public static void main ( String args[ ] ) {
3. Student s = new Student( " Vaishali" , " 930012" ) ;
4. s. grade( ) ;
5. System. out. println( s. getName( ) ) ;
6. s = null;
7. s = new Student( " Vaishali" , " 930012" ) ;
8. s. grade( ) ;
9. System. out. println( s. getName( ) ) ;
10 s = null;
}
}
public class Student{
private String name, rollNumber;
public Student( String name, String rollNumber) {
this. name = name;
this. rollNumber = rollNumber;
}/
/valid setter and getter for name and rollNumber follow
public void grade( ) {
}
}- AIt will not be Garbage Collected till the end ofthe program.
- BLine 5
- CLine 6
- DLine 7
- ELine 10
5Given the following code:
class M { }
class N{
private M m = new M( ) ;
public void makeItNull( M pM) {
pM = null;
}p
ublic void makeThisNull( ) {
makeItNull( m) ;
}p
ublic static void main( String[ ] args) {
N n = new N( ) ;
n. makeThisNull( ) ;
}
}Whi
ch ofthe following statements are correct?- AThere are no instances ofM to be garbage collected till the program ends.
- BA call to n. makeThisNull( ) marks the private instance ofM for garbage
collection. - CSetting pM = null; in makeItNull( ) , marks the private instance ofM for garbage
collection. - Dprivate members ofa class become eligible for garbage collection only when the
instance ofthe class itselfbecomes eligible for garbage collection.
Information
- Description
- OCA-SE-7 Objective-Wise-Ethuware-OCA-SE-7 Quiz on 06-Objectives-Wise-Garbage-Collection, created by Wong Fei Hung on 01/08/2017.
- Author
- Wong Fei Hung
- Language
- Português
- Views
- 1
- Tags
- Rating
- No ratings yet Rate this quiz