Methods in Java tutorial and Interview Questions and Answers
Table of Contents
Study and learn Interview MCQ Questions and Answers on Methods in Java . Attend job interviews easily with these Multiple Choice Questions
Methods in Java
Java Method Signature is nothing but a combination of Return-Type, Method’s Name and a Parameter list it accepts.
The method signature or syntax is as follows.
type METHODNAME(type param1, type param2..) { //statements //.. //return statement }
Let us know about parts of Java Method Signature.
- Return Type
- Method Name
- Parameter List
1. Return Type: A Java method may or may not return a value. If it returns nothing, we specify it explicitly mentioning a “void” before it. A method can return pri-mitive or Object type values.
2. Method Name: A Java Method Name can be a combination of Alphabets, Numbers and Special Symbols (Underscore and Dollar only).
Methods in Java Naming Rules:
- Method name can start with a Letter or Alphabet.
- Method name can start with either Underscore(_) or Dollar($) symbol.
- and Method name can not start with a number.
- Method name can contain numbers in-between or at the end.
- and Method name can not be a keyword.
3. Parameter List: Java methods can contain any number of parameters of any type. There can be zero parameters also. Variables which are part of a Parameter-list are also called Local Variables. Local variables are stored in Stack Memory. Also, the variables declared inside a method are also called Local Variables or Method Variables.
Naming Conventions or Rules for Local Variables:
- Name of a Local variable can start with an Alphabet lowercase or uppercase. Lowercase is preferred.
- Name of a Local variable can start with either an Underscore(_) or Dollar($) symbol.
- and Name of a Local variable can not start with a number but it can contain numbers in-between or at the end.
- Name can not be a keyword.
- No two Local variables should have the same name.
An example Java program below explains how to pass arguments to a Java method and return values or objects from a method.
class Table { int height; } public class JavaMethodExample { public static void main(String[] args) { JavaMethodExample je = new JavaMethodExample(); Table t1 = new Table(); t1.height = 10; je.show(t1); //Passing arguments to method int changedValue = je.increase(t1); System.out.println("New Height= " + changedValue); //System.out.println("New Height= " + t1.height); //This code also works } void show(Table tab) //Receiving parameters in method { System.out.println("Height= " + tab.height); } int increase(Table tab) //Returning a value { tab.height = tab.height + 20; return tab.height; } } //OUTPUT //Height= 10 //New Height= 30
Note
In the above example, the method “show” receives parameters. The method “increase” receives an object reference as a parameter and returns an integer value back to the calling method. Remember that “main” is also a method which is the starting point of program execution.
Share this Java Tutorial on Java Methods Signature Rules with your friends and colleagues to encourage authors
[WpProQuiz 158]
Methods in Java MCQ (Interview)
1) A Java method is comparable to a __ in c language.
A) structure
B) union
C) function
D) enum
Answer [=] C
2) All Java methods must have a return type. (TRUE / FALSE)
A) TRUE
B) FALSE
Answer [=] A
3) State TRUE or FALSE. A Java method can have the same name as the class name.
A) TRUE
B) FALSE
Answer [=] A
4) In Java, add a ___ to a constructor to convert it into a method.
A) if statement
B) static
C) return type
D) semicolon
Answer [=] C
5) Java method signature is a combination of ___.
A) Return type
B) Method name
C) Argument List
D) All the above
Answer [=] D
6) In Java, a method name can not start with a ___.
A) number
B) # (pound)
C) – (hyphen)
D) All the above
Answer [=] D
7) In Java, a method name can start with ___.
A) Alphabet
B) Underscore (_)
C) Dollar ($)
D) All the above
Answer [=] D
8) In Java, a method name can contain numbers from 2nd character onwards. (TRUE / FALSE).
A) TRUE
B) FALSE
Answer [=] A
9) Choose the correct identifier for a method name in Java.
A) 1show
B) $hide
C) *show$
D) 3_click
Answer [=] B
10) What is the output of the below Java program with an empty return statement?
public class TestingMethods2 { void show() { System.out.println("SHOW Method.."); return; } public static void main(String[] args) { TestingMethods2 t2 = new TestingMethods2(); t2.show(); } }
A) SHOW Method..
B) No output
C) Compiler error
D) None
Answer [=] A
Explanation:
Yes. A void method can use an empty return statement.
11) What is the output of the below Java program with a void method?
public class TestingMethods3 { void show2() { System.out.println("SHOW Method 2"); } public static void main(String[] args) { TestingMethods3 t3 = new TestingMethods3(); t3.show2(); } }
A) SHOW Method 2
B) No output
C) Compiler error
D) None
Answer [=] A
Explanation:
The empty return statement is not necessary for a void method.
12) A “this” operator used inside a Java method refers to ___ variable.
A) Global variable
B) Method local variable
C) Instance variable
D) None
Answer [=] C
13) What is the output of the below Java program with a “this” operator?
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); } }
A) CAKES=5
B) CAKES=0
C) CAKES=10
D) Compiler error
Answer [=] C
Explanation:
In the program, this.cakes refers to the instance variable cakes.
14) A local variable declared inside a method can not be used in expressions without initializing it first. (TRUE / FALSE).
A) TRUE
B) FALSE
Answer [=] A
15) What is the output of the below Java program?
public class TestingMethods5 { public static void main(String[] args) { int localVariable; System.out.println(localVariable); } }
A) 0
B) garbage value
C) NullPointerException
D) Compiler error
Answer [=] D
Explanation:
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.
16) In Java, local variables are stored in __ memory and instance variables are stored in ___ memory.
A) Stack, Stack
B) Heap, Heap
C) Stack, Heap
D) Heap, Stack
Answer [=] C
17) A static-method or a static-variable is shared among all instances of a class. (TRUE / FALSE)
A) TRUE
B) FALSE
Answer [=] A
Explanation:
Yes. a single copy of a static variable or method is common to all instance objects.
18) What is the output of the Java program with static variables?
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); } }
A)
t6 BIRDS before=25 t6 BIRDS after=25
B)
t6 BIRDS before=25 t6 BIRDS after=10
C)
t6 BIRDS before=25 t6 BIRDS after=0
D) None
Answer [=] B
Explanation:
The static variable “cats” is common to all objects. There is no separate copy like non-static variables.
19) What is the output of the below Java program with a final local variable?
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); } }
A) 30
B) 20
C) 10
D) Compiler error
Answer [=] D
Explanation:
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.
20) Java does not allow nesting of methods. (TRUE / FALSE)
A) TRUE
B) FALSE
Answer [=] A
21) What is the output of the below Java program?
class Road { static void show() { System.out.println("Inside static method."); } } public class TestingMethods10 { public static void main(String[] args) { Road.show(); } }
A) Inside static method.
B) empty message
C) Compiler error
D) Runtime error / exception
Answer [=] A
Explanation:
You can directly call static methods of a class with just a DOT operator and class-name.
22) What is the output of the below Java program?
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); } }
A)
Before: A After: B
B)
Before: A After: A
C)
Before: A After:
D)
Before: B After: B
Answer [=] B
Explanation:
Instance variable “batch” is separate for each instance. So the changes to one instance object does not affect another instance object.
For More Free online Quizzes of JAVA Programming Click here
For More Free online Quizzes of C Programming Click here