EmSAT Achieve Exam  >  EmSAT Achieve Tests  >  Test: Methods in Java - 2 - EmSAT Achieve MCQ

Test: Methods in Java - 2 - EmSAT Achieve MCQ


Test Description

20 Questions MCQ Test - Test: Methods in Java - 2

Test: Methods in Java - 2 for EmSAT Achieve 2024 is part of EmSAT Achieve preparation. The Test: Methods in Java - 2 questions and answers have been prepared according to the EmSAT Achieve exam syllabus.The Test: Methods in Java - 2 MCQs are made for EmSAT Achieve 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Methods in Java - 2 below.
Solutions of Test: Methods in Java - 2 questions in English are available as part of our course for EmSAT Achieve & Test: Methods in Java - 2 solutions in Hindi for EmSAT Achieve course. Download more important topics, notes, lectures and mock test series for EmSAT Achieve Exam by signing up for free. Attempt Test: Methods in Java - 2 | 20 questions in 40 minutes | Mock test for EmSAT Achieve preparation | Free important questions MCQ to study for EmSAT Achieve Exam | Download free PDF with solutions
Test: Methods in Java - 2 - Question 1

What is the difference between call by value and call by reference in Java?

Detailed Solution for Test: Methods in Java - 2 - Question 1

In Java, when a method is called with call by value, a copy of the variable's value is passed to the method. Any modifications made to the parameter within the method do not affect the original variable. Call by reference, on the other hand, passes the actual variable itself, allowing modifications to affect the original variable.

Test: Methods in Java - 2 - Question 2

Can a method call itself directly or indirectly in Java?

Detailed Solution for Test: Methods in Java - 2 - Question 2

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.

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Methods in Java - 2 - Question 3

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;
   }
}

Detailed Solution for Test: Methods in Java - 2 - Question 3

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.

Test: Methods in Java - 2 - Question 4

What is method hiding in Java?

Detailed Solution for Test: Methods in Java - 2 - Question 4

Method hiding in Java occurs when a subclass defines a static method with the same signature as a static method in its superclass. The static method in the subclass "hides" the static method in the superclass.

Test: Methods in Java - 2 - Question 5

What is method overloading resolution in Java?

Detailed Solution for Test: Methods in Java - 2 - Question 5

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.

Test: Methods in Java - 2 - Question 6

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;
   }
}

Detailed Solution for Test: Methods in Java - 2 - Question 6

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.

Test: Methods in Java - 2 - Question 7

What is the difference between a method declaration and a method definition in Java?

Detailed Solution for Test: Methods in Java - 2 - Question 7

In Java, a method declaration specifies the method's return type, name, and parameter list, while a method definition includes the implementation of the method, including the method body.

Test: Methods in Java - 2 - Question 8

Can a method in Java have multiple return statements?

Detailed Solution for Test: Methods in Java - 2 - Question 8

In Java, a method can have multiple return statements. Multiple return statements allow flexibility in implementing conditional logic, where different values are returned based on different conditions.

Test: Methods in Java - 2 - Question 9

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);
      }
   }
}

Detailed Solution for Test: Methods in Java - 2 - Question 9

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.

Test: Methods in Java - 2 - Question 10

What is the difference between method overloading and method overriding in Java?

Detailed Solution for Test: Methods in Java - 2 - Question 10

Method overloading in Java occurs when a class has multiple methods with the same name but different parameter lists. Method overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass, using the same method name and parameter list.

Test: Methods in Java - 2 - Question 11

Which of the following statements about methods in Java is correct?

Detailed Solution for Test: Methods in Java - 2 - Question 11

A method in Java can have multiple return statements. The return statement is used to return a value from the method and terminate its execution.

Test: Methods in Java - 2 - Question 12

What is the purpose of the "static" keyword in Java method declarations?

Detailed Solution for Test: Methods in Java - 2 - Question 12

The "static" keyword in a Java method declaration allows the method to be called without creating an instance of the class. Static methods belong to the class itself rather than individual instances of the class.

Test: Methods in Java - 2 - Question 13

Which of the following is true about passing arguments by value in Java?

Detailed Solution for Test: Methods in Java - 2 - Question 13

In Java, objects are always passed by value. When an object is passed as an argument to a method, a copy of the reference to the object is passed by value. This means that changes made to the object within the method are reflected outside the method.

Test: Methods in Java - 2 - Question 14

Which of the following methods is not valid in Java?

Detailed Solution for Test: Methods in Java - 2 - Question 14

The missing access modifier in option d) makes the method invalid. In Java, methods should have an access modifier (e.g., public, private, protected) specified explicitly.

Test: Methods in Java - 2 - Question 15

What is method overloading in Java?

Detailed Solution for Test: Methods in Java - 2 - Question 15

Method overloading in Java involves defining multiple methods with the same name but different parameters. The parameters can differ in terms of number, types, or order.

Test: Methods in Java - 2 - Question 16

What is the output of the following code snippet?
public class Example {
    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;
    }
}

Detailed Solution for Test: Methods in Java - 2 - Question 16

The 'multiply' method in the code snippet takes two integer parameters and returns their product. In the 'main' 'method, 'multiply(3, 4)' is called, and the returned value, 12, is printed.

Test: Methods in Java - 2 - Question 17

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";
    }
}

Detailed Solution for Test: Methods in Java - 2 - Question 17

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."

Test: Methods in Java - 2 - Question 18

What is the output of the following code snippet?
public class Example {
    public static void main(String[] args) {
        int x = 5;
        updateValue(x);
        System.out.println(x);
    }

    public static void updateValue(int value) {
        value = value + 1;
    }
}

Detailed Solution for Test: Methods in Java - 2 - Question 18

The 'updateValue' method in the code snippet takes an integer parameter and increments it by 1. However, the parameter 'value' is a copy of the value of 'x' in the main method. Therefore, modifying 'value' does not affect the original variable 'x'.

Test: Methods in Java - 2 - Question 19

What is the output of the following code snippet?
public class Example {
    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;
    }

    public static double sum(double a, double b) {
        return a + b;
    }
}

Detailed Solution for Test: Methods in Java - 2 - Question 19

The 'sum' method in the code snippet is overloaded with two versions: one that takes two integers and another that takes two doubles. In the 'main' method, 'sum(3, 4)' is called, and since the arguments are integers, the first version of 'sum' is invoked, returning the sum of 3 and 4, which is 7.

Test: Methods in Java - 2 - Question 20

Which of the following methods can be used to calculate the factorial of a number in Java?

Detailed Solution for Test: Methods in Java - 2 - Question 20

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.

Information about Test: Methods in Java - 2 Page
In this test you can find the Exam questions for Test: Methods in Java - 2 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Methods in Java - 2, EduRev gives you an ample number of Online tests for practice

Top Courses for EmSAT Achieve

Download as PDF

Top Courses for EmSAT Achieve