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. The implicit return type of a constructor is

Answer: Option B

Explanation:

2. 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:

3. 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.

4. 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.

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

Answer: Option C

Explanation:

6. What is the output for the below code ? 1. public class A{ 2. int add(int i, int j){ 3. return i+j; 4. } 5. } 6. public class B extends A{ 7. public static void main(String argv[]){ 8. short s = 9; 9. System.out.println(add(s,6)); 10. } 11.}

Answer: Option B

Explanation:
Cannot make a static reference to the non-static method add(int, int) from the type A. The short s is autoboxed correctly, but the add() method cannot be invoked from a static method because add() method is not static.

7. class MyClass{ MyClass(){ System.out.print("one"); } public void myMethod(){ this(); System.out.print("two"); } } public class TestClass{ public static void main(String args[]){ MyClass obj = new MyClass(); obj.myMethod(); } }

Answer: Option D

Explanation:
A method can't have constructor call.

8. What is the return type of Constructors?

Answer: Option D

Explanation:
Constructors does not have any return type, not even void.

9. he variables declared in a class for the use of all methods of the class are called

Answer: Option

Explanation:

10. In which area of memory, the system stores parameters and local variables whenever a method is invoked?

Answer: Option

Explanation:

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

Answer: Option D

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

12. Which function is used to perform some action when the object is to be destroyed?

Answer: Option A

Explanation:

13. The following code contains one compilation error, find it? public class Test { Test() { } // line 1 static void Test() { this(); } // line 2 public static void main(String[] args) { // line 3 Test(); // line 4 } }

Answer: Option

Explanation:
a constructor call (super() or this() ) must be the first statement inside a constructor. No constructor calls are allowed inside a method. static void Tester() is a method not a constructor.

14. 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.

15. Determine Output: class A{ public static void method(int i){ System.out.print("Method 1"); } public static int method(String str){ System.out.print("Method 2"); return 0; } } public class Test{ public static void main(String args[]){ A.method(5); } }

Answer: Option A

Explanation:
Overloading depends only on the method or constructor name and its parameter list i.e. method(int i) and method(String str) So, overloading does not depend on access modifiers(public, private, protected), access specifiers(static, final) and return type.