Study and learn Interview MCQ Questions and Answers on Java Methods. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on Methods before reading these objective questions.
public class TestingMethods2 { void show() { System.out.println("SHOW Method.."); return; } public static void main(String[] args) { TestingMethods2 t2 = new TestingMethods2(); t2.show(); } }
Yes. A void method can use an empty return statement.
public class TestingMethods3 { void show2() { System.out.println("SHOW Method 2"); } public static void main(String[] args) { TestingMethods3 t3 = new TestingMethods3(); t3.show2(); } }
The empty return statement is not necessary for a void method.
public class TestingMethods4 { int cakes=5; void order(int cakes) { this.cakes = cakes; } public static void main(String[] args) { TestingMethods4 t4 = new TestingMethods4(); t4.order(10); System.out.println("CAKES=" + t4.cakes); } }
In the program, this.cakes refers to the instance variable cakes.
public class TestingMethods5 { public static void main(String[] args) { int localVariable; System.out.println(localVariable); } }
In the above program, the localVariable is a Local variable and it is not initialized. You can not use it in any expressions, not even printing.
Yes. a single copy of a static variable or method is common to all instance objects.
public class TestingMethods6 { static int cats=25; public static void main(String[] args) { TestingMethods6 t6 = new TestingMethods6(); System.out.println("t6 BIRDS before=" + t6.cats); TestingMethods6 t7 = new TestingMethods6(); t7.cats = 10; System.out.println("t6 BIRDS after=" + t6.cats); } }
t6 BIRDS before=25 t6 BIRDS after=25
t6 BIRDS before=25 t6 BIRDS after=10
t6 BIRDS before=25 t6 BIRDS after=0
The static variable "cats" is common to all objects. There is no separate copy like non-static variables.
public class TestingMethods8 { int cars = 20; void change(final int cars) { cars = 10; this.cars = cars; } public static void main(String[] args) { TestingMethods8 t8 = new TestingMethods8(); t8.change(30); System.out.println(t8.cars); } }
The argument that is marked final can not be reassigned or changed. So, the compiler error is produced. So, the statement cars=10; inside the change() method is wrong.
class Road { static void show() { System.out.println("Inside static method."); } } public class TestingMethods10 { public static void main(String[] args) { Road.show(); } }
You can directly call static methods of a class with just a DOT operator and class-name.
class SomeClass { char batch = 'A'; } public class TestingMethods11 { public static void main(String[] args) { SomeClass a1 = new SomeClass(); System.out.println("Before: " + a1.batch); SomeClass a2 = new SomeClass(); a2.batch = 'B'; System.out.println("After: " + a1.batch); } }
Before: A After: B
Before: A After: A
Before: A After:
Before: B After: B
Instance variable "batch" is separate for each instance. So the changes to one instance object does not affect another instance object.