All Exams  >   Software Development  >   Basics of Java  >   All Questions

All questions of Methods in Java for Software Development Exam

1 Crore+ students have signed up on EduRev. Have you? Download the App

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;
   }
}
  • a)
    5
  • b)
    6
  • c)
    Compilation Error
  • d)
    Runtime Error
Correct answer is option 'B'. Can you explain this answer?

Explanation:
The given code defines a class named "MyClass" with a main method.
The main method initializes an integer variable "x" with the value 5.
Then, it calls the increment method and passes the value of "x" as an argument.
The returned value from the increment method is stored in the variable "y".
Finally, the value of "y" is printed.

Step-by-Step Execution:
1. The main method is called and the variable "x" is initialized with the value 5.
2. The increment method is called, and the value of "x" (i.e., 5) is passed as an argument.
3. In the increment method, the value parameter is received and stored in the variable "value".
4. The value of "value" is returned.
5. The returned value (i.e., 5) is stored in the variable "y".
6. The value of "y" (i.e., 5) is printed using the System.out.println statement.

Output:
The output of the code will be:
6

Explanation:
The output is 6 because the increment method does not actually increment the value passed to it. It simply returns the value as it is. So, when the value 5 is passed to the increment method, it returns 5, and that value is stored in the variable "y". Therefore, the output is 6.

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) {
      if (b == 0) {
         return 0;
      } else {
         return a + multiply(a, b - 1);
      }
   }
}
  • a)
    0
  • b)
    3
  • c)
    4
  • d)
    12
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The 'multiply' method is a recursive method that calculates the product of two numbers 'a' and 'b' using addition. If 'b' is 0, the method returns 0 (base case). Otherwise, it recursively calls itself with 'a' and 'b-1' as arguments, and the result is added to 'a'. The recursion continues until 'b' becomes 0.

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);
      }
   }
}
  • a)
    Hello
    Hi
  • b)
    Hello
    Hi Hi Hi
  • c)
    Compilation Error
  • d)
    Runtime Error
Correct answer is option 'B'. Can you explain this answer?

Explanation:

Method Overloading:
- The code demonstrates method overloading, where two methods with the same name but different parameters are defined.
- The first method printMessage(String message) prints the message once.
- The second method printMessage(String message, int count) prints the message multiple times based on the count provided.

Output:
- When the main method is executed, it first calls printMessage("Hello") which prints "Hello" once.
- Then, it calls printMessage("Hi", 3) which prints "Hi" three times.
- Hence, the output will be:

Hello
Hi
Hi
Hi

Which of the following is a valid method declaration in Java?
  • a)
    'void myMethod() {}'
  • b)
    'int myMethod() {}'
  • c)
    'myMethod() {}'
  • d)
    'myMethod(void) {}'
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
In Java, a method declaration consists of a return type (or 'void' if the method does not return a value), followed by the method name, parentheses, and optionally a parameter list.

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;
   }
}
  • a)
    10
  • b)
    20
  • c)
    Compilation Error
  • d)
    Runtime Error
Correct answer is option 'A'. Can you explain this answer?

Fawaz Al Ameri answered
Answer:

The output of the code will be 10.

Explanation:
When the code is executed, the following steps occur:

1. The variable x is declared and initialized with the value 10.
2. The method changeValue() is invoked and the value of x is passed as an argument.
3. In the changeValue() method, a new variable value is declared and initialized with the value of x, which is 10.
4. The value of value is then changed to 20.
5. However, this change does not affect the original value of x because Java passes arguments by value, which means that a copy of the variable's value is passed to the method. Therefore, changing the value of the parameter value does not modify the original variable x.
6. The method changeValue() completes execution and the control returns to the main() method.
7. The value of x is printed using the System.out.println() statement, which still retains its original value of 10.

Therefore, the output of the code is 10.

What is the output of the following code snippet?
public class Example {
    public static void main(String[] args) {
        String str = "Hello";
        updateString(str);
        System.out.println(str);
    }
    public static void updateString(String s) {
        s += " World";
    }
}
  • a)
    Hello
  • b)
    World
  • c)
    Hello World
  • d)
    Compilation error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The 'updateString' method in the code snippet takes a string parameter and concatenates " World" to it. However, the concatenation is performed on a copy of the reference to the original string. Hence, the modification is not reflected outside the method, and the output remains "Hello."

What is method overloading resolution in Java?
  • a)
    It is the process of determining which method to call based on the method's return type.
  • b)
    It is the process of determining which method to call based on the method's access modifier.
  • c)
    It is the process of determining which method to call based on the method's parameter list.
  • d)
    It is the process of determining which method to call based on the method's name.
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
Method overloading resolution in Java is the process of determining which method to call when multiple methods with the same name but different parameter lists are available. The compiler matches the arguments provided during the method call with the method's parameter types to determine the most appropriate method to invoke.

What will be the output of the following code?
public class MyClass {
   public static void main(String[] args) {
      int x = 10;
      int y = modify(x);
      System.out.println(x + y);
   }
   public static int modify(int value) {
      value += 5;
      return value;
   }
}
  • a)
    10
  • b)
    15
  • c)
    20
  • d)
    Compilation Error
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The 'modify' method takes an integer as a parameter, adds 5 to it, and returns the modified value. In the 'main' method, the variable 'x' is assigned the value 10. When 'modify' is called with 'x' as an argument, 'x' itself is not modified, but the modified value (15) is returned and assigned to 'y'. The sum of 'x' (10) and 'y' (15) is 25, which is printed.

Which of the following is true about recursive methods in Java?
  • a)
    Recursive methods cannot call themselves.
  • b)
    Recursive methods must have a return type of 'void'.
  • c)
    Recursive methods must have at least one base case.
  • d)
    Recursive methods can only be used for sorting algorithms.
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
Recursive methods in Java must have at least one base case, which is a condition that terminates the recursion. Without a base case, the recursive method would continue calling itself indefinitely, leading to a stack overflow error.

Can a method call itself directly or indirectly in Java?
  • a)
    Yes, but it can lead to an infinite loop.
  • b)
    Yes, as long as the method has a return type of 'void'.
  • c)
    No, it is not allowed in Java.
  • d)
    Yes, but only if the method is defined as static.
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
Recursive methods in Java can call themselves directly or indirectly. However, without proper termination conditions (base cases), recursive calls can lead to an infinite loop and cause a stack overflow error.

Which of the following methods can be used to calculate the factorial of a number in Java?
  • a)
    public static int factorial(int n) { ... }
  • b)
    public static long factorial(long n) { ... }
  • c)
    public static int factorial(int n, int m) { ... }
  • d)
    public static int factorial(int[] arr) { ... }
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
Both option a) and b) are valid methods to calculate the factorial of a number. The return type of the factorial function can be either 'int' or 'long' depending on the range of values you want to support.
Note: The other options c) and d) are not suitable for calculating the factorial. Option c) includes an extra parameter m that is not required for factorial calculation, and option d) takes an array as a parameter, which is not necessary for the factorial computation.

What will happen if a method with a void return type is called and the result is not assigned to any variable?
  • a)
    Compilation Error
  • b)
    Runtime Error
  • c)
    The program will execute normally.
  • d)
    None of the above
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
When a method with a void return type is called, the method executes its statements but does not return a value. If the result is not assigned to any variable, it simply means that the result is not used, but the program will execute normally.

What will be the output of the following code?
public class MyClass {
   public static void main(String[] args) {
      int x = 5;
      int y = 10;
      swap(x, y);
      System.out.println(x + " " + y);
   }
   public static void swap(int a, int b) {
      int temp = a;
      a = b;
      b = temp;
   }
}
  • a)
    5 10
  • b)
    10 5
  • c)
    Compilation Error
  • d)
    Runtime Error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The 'swap' method takes two integers as parameters and swaps their values using a temporary variable. In the 'main' method, the variables 'x' and 'y' are passed to the 'swap' method. However, the method operates on copies of the variables, so the original values of 'x' and 'y' remain unchanged.

Chapter doubts & questions for Methods in Java - Basics of Java 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Methods in Java - Basics of Java in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Basics of Java

60 videos|37 docs|12 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev