Which of the following statements about methods in Java is correct?
What is the output of the following code?
public class MyClass {
public static void main(String[] args) {
int result = sum(3, 4);
System.out.println(result);
}
public static int sum(int a, int b) {
return a + b;
}
}
1 Crore+ students have signed up on EduRev. Have you? Download the App |
What will happen if a method with a void return type is called and the result is not assigned to any variable?
What is the purpose of the "return" keyword in a method?
What will be the output of the following code?
public class MyClass {
public static void main(String[] args) {
int result = multiply(3, 4);
System.out.println(result);
}
public static int multiply(int a, int b) {
return a * b;
}
}
Which of the following is a valid method declaration in Java?
What will be the output of the following code?
public class MyClass {
public static void main(String[] args) {
printMessage("Hello");
printMessage("Hi", 3);
}
public static void printMessage(String message) {
System.out.println(message);
}
public static void printMessage(String message, int count) {
for (int i = 0; i < count; i++) {
System.out.println(message);
}
}
}
What is the difference between instance methods and static methods in Java?
What is the return type of a method that does not return any value?
What will be the output of the following code?
public class MyClass {
public static void main(String[] args) {
int x = 5;
int y = increment(x);
System.out.println(y);
}
public static int increment(int value) {
return ++value;
}
}
What is the scope of a local variable declared inside a method?
Which of the following is true about method parameters in Java?
What will be the output of the following code?
public class MyClass {
public static void main(String[] args) {
int result = calculate(5, 3);
System.out.println(result);
}
public static int calculate(int a, int b) {
int result = a - b;
return result;
}
}
Which of the following is true about method signatures in Java?
What will be the output of the following code?
public class MyClass {
public static void main(String[] args) {
int x = 10;
changeValue(x);
System.out.println(x);
}
public static void changeValue(int value) {
value = 20;
}
}
Which of the following is true about recursive methods in Java?
What will be the output of the following code?
public class MyClass {
public static void main(String[] args) {
printNumber(5);
}
public static void printNumber(int num) {
if (num <= 0) {
return;
}
System.out.println(num);
printNumber(num - 1);
}
}
60 videos|37 docs|12 tests
|
60 videos|37 docs|12 tests
|