Log In Register

Units


Constructors & Garbage Collection - Questions and Answers

 

Practice these Constructors & Garbage Collection under Java Programming walk-in interview Questions/ examination questions with best tricks and short cuts with solution. Student (candidate) who want to crack the walk in interview, competitive exams and want to find short cuts and tricks to solve questions on Constructors & Garbage Collection for following purpose.


– Anyone who is wishing to increase their perfect knowledge of Constructors & Garbage Collection of Java Programming.
– Anyone who is preparing for aptitude test and increase his in depth aptitude knowledge Java Programming specifically Constructors & Garbage Collection.
– Anyone who is preparing for interviews (competitive examinations, govt examinations and bank examinations, off-campus or on campus interviews, walk-in interview and company interviews) having questions on Constructors & Garbage Collection under Java Programming.

– Anyone who is preparing for aptitude test or competitive exam or entrance exam.
– Anyone who is preparing for entrance examinations and other competitive examinations.
– All Experienced, Freshers and Students.

Here is list of Questions and Answers covering all key area of  Constructors & Garbage Collection topic of Java Programming:

 

1. public class MyClass{ } For the above class(MyClass) what is the correct way of declaring constructor?

Answer: Option D

Explanation:
Choice A,C are the correct answers. The default access specifier for any constructor is same as the access specifier of class.so the options A and C are correct.

2. Which operator is used by Java run time implementations to free the memory of an object when it is no longer needed?

Answer: Option D

Explanation:
Java handles deallocation of memory automatically, we do not need to explicitly delete an element. Garbage collection only occurs during execution of the program. When no references to the object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed.

3. The main method should be static for the reason

Answer: Option C

Explanation:

4. What is the output for the below code? public class Test{ public static void printValue(int i, int j, int k){ System.out.println("int"); } public static void printValue(byte...b){ System.out.println("long"); } public static void main(String... args){ byte b = 9; printValue(b,b,b); } }

Answer: Option B

Explanation:
Primitive widening uses the smallest method argument possible. (For Example if you pass short value to a method but method with short argument is not available then compiler chooses method with int argument). But in this case compiler will prefer the older style before it chooses the newer style, to keep existing code more robust. var-args method is looser than wider.

5. What is the output of the above program ? class Num { Num(double x ){ System.out.println( x ) ; } } public class Test extends Num { public static void main(String[] args){ Num num = new Num( 2 ) ; } }

Answer: Option C

Explanation:

6. Which keyword is used by method to refer to the object that invoked it?

Answer: Option D

Explanation:
this keyword can be used inside any method to refer to the current object. this is always a reference to the object on which the method was invoked.

7. What will be the output? public class Test{ public static void main(String[] args){ String value = "abc"; changeValue(value); System.out.println(value); } public static void changeValue(String a){ a = "xyz"; } }

Answer: Option A

Explanation:
Java pass reference as value. passing the object reference, and not the actual object itself. Simply reassigning to the parameter used to pass the value into the method will do nothing, because the parameter is essentially a local variable.

8. Which of the modifier can't be used for constructors?

Answer: Option C

Explanation:

9. What is the output of the program? class Test{ public int display(int x, int y){ return ("The sum of x and y is " + x + y); } public static void main(String args[]){ Test test = new Test(); System.out.println(test.display(4,5)); } }

Answer: Option

Explanation:
Return type of method display is int and we are trying to return string.

10. Given the following piece of code: class Person{ public int number; } public class Test{ public void doIt(int i , Person p){ i = 5; p.number = 8; } public static void main(String args[]){ int x = 0; Person p = new Person(); new Test().doIt(x, p); System.out.println(x + " " + p.number); } } What is the result?

Answer: Option

Explanation:

11. The finalize() method is called just prior to

Answer: Option D

Explanation:

12. public class Test { } What is the prototype of the default constructor?

Answer: Option D

Explanation:
A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class). D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.

13. Determine output: public class Test{ public static void main(String args[]){ MyClass obj = new MyClass(); obj.val = 1; obj.call(obj); System.out.println(obj.val); } } class MyClass{ public int val; public void call(MyClass ref){ ref.val++; } }

Answer: Option B

Explanation:

14. What is the expected output? public class Profile { private Profile(int w) { // line 1 System.out.print(w); } public final Profile() { // line 5 System.out.print(10); } public static void main(String args[]) { Profile obj = new Profile(50); } }

Answer: Option B

Explanation:
Only public, protected, private and default(no modifier) are legal when declaring constructors.

15. Which of the following is a method having same name as that of its class?

Answer: Option D

Explanation:
A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.