Software Development Exam  >  Software Development Notes  >  Basics of Java  >  Assignment: Methods

Assignment: Methods | Basics of Java - Software Development PDF Download

Multiple Choice Questions (MCQs)

Q.1. Which of the following options is correct about methods in Java?
(a) A method is a sequence of statements that performs a specific task.
(b) A method is a data type in Java.
(c) A method is used to create objects in Java.
(d) A method is only used in advanced Java programming.

Ans. (a)

Q.2. What is the return type of a method that does not return any value?
(a) int
(b) void
(c) String
(d) boolean

Ans. (b)

Q.3. Which of the following is true about method overloading in Java?
(a) It allows a method to have multiple names.
(b) It allows a method to have multiple return types.
(c) It allows a method to have multiple parameters with the same name.
(d) It allows a method to perform different tasks with the same name but different parameters.

Ans. (d)

Q.4. Which keyword is used to invoke a method from another method within the same class?
(a) new
(b) this
(c) super
(d) invoke

Ans. (b)

Q.5. What is the term used to describe a method that is defined in a superclass and overridden in a subclass
(a) Overloaded method
(b) Static method
(c) Abstract method
(d) Instance method

Ans. (c)

Higher Order Thinking Skills (HOTS)

Q.1. Explain the difference between a method declaration and a method call in Java.

Method declaration is the process of defining a method in a class, including its name, return type, and parameters. It specifies the method's signature. On the other hand, a method call is the act of invoking or using a method within the code to execute its defined tasks.

Q.2. Write a Java method named "isEven" that takes an integer parameter and returns true if the number is even, and false otherwise. Provide the code for the method.

public boolean isEven(int number) {

    return number % 2 == 0;

}

Q.3. Write a Java method named "calculateAverage" that takes an array of integers as a parameter and returns the average value of the numbers in the array. Provide the code for the method.

public double calculateAverage(int[] numbers) {

    int sum = 0;

    for (int num : numbers) {

        sum += num;

    }

    return (double) sum / numbers.length;

}

Q.4. What is the purpose of the "static" keyword when defining a method in Java? Provide an example of a static method and explain its usage.

The "static" keyword is used to define a method as a class-level method. It means the method is associated with the class itself, rather than with instances of the class. A static method can be accessed and called directly using the class name, without creating an instance of the class. For example:
public static void printMessage() {

    System.out.println("Hello, world!");

}

Usage: ClassName.printMessage();

Q.5. Discuss the concept of method overloading in Java and provide an example to illustrate its use.

Method overloading is the concept of defining multiple methods with the same name but different parameters within a class. These methods have the same name, but they differ in the number, type, or order of parameters. Java determines which method to invoke based on the arguments provided during the method call.
Example:
public void displayInfo(String name) {

    System.out.println("Name: " + name);

}


public void displayInfo(String name, int age) {

    System.out.println("Name: " + name + ", Age: " + age);

}

Fill in the blanks

Q.1. A method that does not return any value has a return type of _______.

Ans. void

Q.2. The process of defining a method in a subclass with the same name as a method in its superclass is called _______.

Ans. method overriding

Q.3. The _______ keyword is used to indicate that a method can be accessed and called without creating an instance of the class.

Ans. static

Q.4. In Java, a method can have _______ parameters of the same name but with different types.

Ans. multiple

Q.5. The _______ keyword is used to invoke a method from the superclass within the subclass.

Ans. super

True/False

Q.1. A method can have multiple return statements.

Ans. True

Q.2. Method overloading allows a method to have multiple names.

Ans. False

Q.3. A method can be called without passing any arguments.

Ans. True

Q.4. The "final" keyword can be used to prevent a method from being overridden.

Ans. True

Q.5. A method defined in a subclass can access private members of its superclass.

Ans. False

Hands-On Questions

Q.1. Write a Java method named "printMessage" that takes a string parameter and prints the message to the console. Provide the code for the method.

public void printMessage(String message) {

    System.out.println(message);

}

Q.2. Write a Java method named "isPrime" that takes an integer parameter and returns true if the number is prime, and false otherwise. Provide the code for the method.

public boolean isPrime(int number) {

    if (number <= 1) {

        return false;

    }

    for (int i = 2; i <= Math.sqrt(number); i++) {

        if (number % i == 0) {

            return false;

        }

    }

    return true;

}

Q.3. Write a Java method named "calculateFactorial" that takes an integer parameter and returns the factorial of that number. Provide the code for the method.

public int calculateFactorial(int number) {

    if (number == 0) {

        return 1;

    }

    return number * calculateFactorial(number - 1);

}

Q.4. Create a class named "Rectangle" with instance variables "length" and "width." Write a method named "calculateArea" that calculates and returns the area of the rectangle. Provide the code for the class and method.

public class Rectangle {

    private int length;

    private int width;


    public void setLength(int length) {

        this.length = length;

    }


    public void setWidth(int width) {

        this.width = width;

    }


    public int calculateArea() {

        return length * width;

    }

}

Q.5. Create a class named "Circle" with an instance variable "radius." Write a method named "calculateCircumference" that calculates and returns the circumference of the circle. Provide the code for the class and method.

public class Circle {

    private double radius;


    public void setRadius(double radius) {

        this.radius = radius;

    }


    public double calculateCircumference() {

        return 2 * Math.PI * radius;

    }

}

The document Assignment: Methods | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Top Courses for Software Development

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

Assignment: Methods | Basics of Java - Software Development

,

Summary

,

study material

,

Sample Paper

,

Assignment: Methods | Basics of Java - Software Development

,

pdf

,

past year papers

,

Extra Questions

,

Exam

,

Assignment: Methods | Basics of Java - Software Development

,

Objective type Questions

,

Important questions

,

video lectures

,

Previous Year Questions with Solutions

,

Viva Questions

,

practice quizzes

,

MCQs

,

shortcuts and tricks

,

Semester Notes

,

Free

,

ppt

;