All Exams  >   EmSAT Achieve  >   Java for EmSAT Achieve  >   MCQ Questions

Methods in Java MCQs for EmSAT Achieve Exam

It covers all Important Questions with answers on Methods in Java for the EmSAT Achieve exam. The questions are based on important topics. Details about the questions:
  • Topic: Methods in Java
  • Type of Questions: MCQs with solutions
  • Number of Questions: 40
  • You can attempt them on EduRev to score high in EmSAT Achieve exam.

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?

The given code is written in Java and it calculates the multiplication of two integers using a recursive function.

Let's break down the code and understand how it works:

1. The main method is the entry point of the code. It calls the multiply method with arguments 3 and 4 and stores the result in the variable 'result'.

2. The multiply method takes two integers, 'a' and 'b', as parameters. It is a recursive function that calculates the multiplication of 'a' and 'b' by reducing 'b' by 1 in each recursive call until 'b' becomes 0.

3. Inside the multiply method, it checks if 'b' is equal to 0. If it is, it means the multiplication is completed, so it returns 0 as the base case.

4. If 'b' is not equal to 0, it enters the else block. It recursively calls the multiply method with the same value of 'a' and 'b-1'. The return value of this recursive call is multiplied with 'a' and returned.

5. In each recursive call, 'b' is reduced by 1 until it becomes 0, at which point the base case is triggered, and the recursion stops.

6. Finally, the main method prints the value of 'result', which is the multiplication of 3 and 4.

Based on the above explanation, the output of the code will be 12.

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 true about passing arguments by value in Java?
  • a)
    The changes made to the passed argument within the method are reflected outside the method.
  • b)
    Only primitive data types can be passed by value in Java.
  • c)
    Objects are always passed by value in Java.
  • d)
    Passing arguments by value means passing the actual memory address of the variable.
Correct answer is option 'C'. Can you explain this answer?

Amna Al Jabri answered
Understanding Passing Arguments by Value in Java
In Java, all arguments passed to methods are handled by value. This concept can sometimes be misunderstood, especially in the context of objects. Here’s a detailed explanation of why option 'C' is correct.
What Does "Passed by Value" Mean?
- Definition: "Passed by value" refers to the fact that a copy of the variable’s value is made when passed to a method.
- Primitive Data Types: For primitive types (int, char, etc.), the actual value is copied.
- Objects: For objects, the reference (memory address) to the object is copied, not the object itself.
Why Option 'C' is Correct
- Objects Are Passed by Value: In Java, when an object is passed to a method, a copy of the reference to that object is made. Therefore, changes made to the object's attributes within the method will affect the original object because both the original and the copied reference point to the same object in memory.
- Example: If you modify an object’s field inside a method, the changes will persist outside the method. However, if you reassign the reference to point to a new object, this change will not affect the original reference.
Why Other Options Are Incorrect
- Option A: Changes made to the argument within the method are reflected only if you modify the object itself, not if you reassign the reference.
- Option B: Both primitive data types and object references are passed by value in Java.
- Option D: Passing by value does not mean passing the actual memory address; it means passing a copy of the reference.
Understanding these distinctions helps clarify how Java handles method arguments and object manipulation.

What is the difference between method overloading and method overriding in Java?
  • a)
    Method overloading occurs within the same class, while method overriding occurs between a superclass and its subclass.
  • b)
    Method overloading is done by redefining a method with the same name but different parameter lists, while method overriding is done by redefining a method in a subclass with the same signature as a method in its superclass.
  • c)
    Method overloading allows polymorphism, while method overriding does not.
  • d)
    Method overloading is used for static methods, while method overriding is used for instance methods.
Correct answer is option 'B'. Can you explain this answer?

Saeed Al Farsi answered
Method Overloading:
Method overloading is a feature in Java that allows a class to have multiple methods with the same name but different parameters. These methods can perform similar tasks but with different sets of inputs. The compiler decides which method to call based on the number and types of arguments passed to it.

Method Overriding:
Method overriding is a feature in Java that allows a subclass to provide a different implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass. The purpose of method overriding is to provide a specific implementation of the method in the subclass that is different from the one in the superclass.

Differences between Method Overloading and Method Overriding:
1. Scope:
- Method overloading occurs within the same class, where multiple methods with the same name but different parameters are defined.
- Method overriding occurs between a superclass and its subclass, where the subclass provides a different implementation of a method that is already defined in the superclass.

2. Definition:
- Method overloading is done by redefining a method with the same name but different parameter lists.
- Method overriding is done by redefining a method in a subclass with the same signature as a method in its superclass.

3. Polymorphism:
- Method overloading allows polymorphism, as the compiler determines which method to call based on the arguments passed.
- Method overriding does not allow polymorphism, as the method to be called is determined at runtime based on the actual object type.

4. Usage:
- Method overloading is used for both static and instance methods.
- Method overriding is only used for instance methods.

In conclusion, method overloading and method overriding are both important features in Java, but they have different purposes and usage scenarios. Method overloading is used to provide multiple methods with the same name but different parameters within the same class, while method overriding is used to redefine a method in a subclass with the same signature as a method in its superclass.

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?

Valid Method Declaration in Java

A method in Java is a block of code that performs a specific task. It is declared inside a class and can be called or invoked whenever required. A method can have a return type, parameters, and an optional access specifier.

Among the given options (a, b, c, and d), only option 'a' is a valid method declaration in Java. Let's understand why other options are not valid:

a) void myMethod() {}
- This is a valid method declaration in Java.
- The method name is "myMethod" and it has no parameters.
- The return type is "void", which means the method does not return any value.

b) int myMethod() {}
- This is not a valid method declaration.
- The return type is "int", which means the method is expected to return an integer value.
- However, the method body is empty, and there is no return statement. This results in a compilation error.

c) myMethod() {}
- This is not a valid method declaration.
- The return type is missing, which is a mandatory part of a method declaration.
- It should specify the type of value the method will return or use "void" if it does not return any value.
- Without a return type, the declaration is incomplete and will cause a compilation error.

d) myMethod(void) {}
- This is not a valid method declaration.
- The parameter declaration is incorrect.
- In Java, the parameter type should be specified before the parameter name.
- The correct syntax would be "myMethod(void parameterName)" or "myMethod(parameterType parameterName)".
- Additionally, using "void" as a parameter type is not allowed in Java.

Conclusion
In Java, a valid method declaration should include the return type (or "void" if no return value), the method name, and optionally the parameters. Among the given options, only option 'a' satisfies these requirements and is a valid method declaration.

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?

Sonal Yadav answered
The 'increment' method takes an integer as a parameter, increments it by 1, and returns the incremented value. In the 'main' method, the variable 'x' is assigned the value 5, and the 'increment' method is called with 'x' as an argument. The returned value (6) is then printed.

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.

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?

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.

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

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

Java for EmSAT Achieve

60 videos|37 docs|12 tests

Top Courses EmSAT Achieve