Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Explain the return keyword with a code sample


Download 1.52 Mb.
Pdf ko'rish
bet31/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   27   28   29   30   31   32   33   34   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Explain the return keyword with a code sample.
Answer:
The return keyword is used at the end of a method to return a
value which is the result of some computation. The value
returned must match the return type of the method. The following
code demonstrates this:
public int add(int num1, int num2){
int sum = num1+num2;
return sum;
}
This method has an int return type. It returns the sum of addition.
Even if a method does not return anything, the return keyword
may optionally be specified without any value. The following code
demonstrates this:
public void printSum(int a, int b) {
int sum = a+b;
System.out.println(sum);
return;
}


In this case, the method has a void return type which indicates
that the method does not return anything. However, the return
keyword is still specified.



080.
a.
b.
c.
d.
081.
CHAPTER
07
Inner Classes
What is an inner class? What are the different types of
inner classes?
Answer:
An inner class is any class defined inside the body of another
class. There are 4 types of inner classes as follows:
Nested class: Class is defined within the body of another
class. It has access to all the members of the outer class.
Static nested class: A nested class that is static is known as
a static nested class. It cannot access non–static members of
the outer class.
Method local inner class: A class that is defined inside a
method of another class is known as a method local inner
class. Such an inner class can only access final members of
the outer class.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   27   28   29   30   31   32   33   34   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling