All Exams Test series for 1 year @ ₹349 only
Question

Which of the following statement(s) is/are correct regarding java programming?

I. Place a semicolon immediately after a method header

II. Define a method outside the braces of a class definition

The correct answer is

Neither I nor II

Analyzing Java Programming Statements

Let's carefully examine each statement provided regarding Java programming practices.

Statement I: Place a semicolon immediately after a method header

In Java, a method header typically looks like this:

public static void main(String[] args)

This header is followed by the method body enclosed in curly braces {}. A semicolon is not placed immediately after the method header in a standard method definition with a body.

For example, the following is incorrect syntax in Java:

public void myMethod() ; { // Method body }

A semicolon is only used after a method declaration that does not have a body, such as in interfaces or abstract classes:

public interface MyInterface { public void myMethod(); // Semicolon used here }

However, the statement refers to a general "method header", and in the context of defining concrete methods within a class, placing a semicolon immediately after the header is incorrect. Therefore, statement I is incorrect.

Statement II: Define a method outside the braces of a class definition

In Java, methods are fundamental members of a class. They define the behavior of objects created from that class. All methods must be defined inside the curly braces {} that enclose the class definition.

Here is the basic structure:

public class MyClass {
    // Class members (fields, methods, etc.) go here

    public void myMethod() {
        // Method body
    }

    // Other members...
}

You cannot define a method as a standalone block of code outside of any class definition's braces in standard Java programming. Doing so would result in a compilation error.

Therefore, statement II is incorrect.

Conclusion on Statements I and II

Based on the analysis:

  • Statement I is incorrect because a semicolon is not placed after a method header in a standard Java method definition.
  • Statement II is incorrect because methods must be defined inside the braces of a class definition.

Since both statements are incorrect, the correct option is "Neither I nor II".

Revision Table: Java Method Syntax

Feature Standard Method Definition Method Declaration (Interface/Abstract)
Placement Inside class braces {} Inside interface/abstract class braces {}
Header modifiers returnType methodName(parameters) modifiers returnType methodName(parameters)
Termination Followed by method body {} Followed by a semicolon ;

Additional Information: Java Class Structure

A Java program is built around classes and objects. A class serves as a blueprint for creating objects. A typical class definition includes:

  • Class Declaration: Specifies access modifiers (like public), the keyword class, and the class name.
  • Fields (Instance Variables): Data members that define the state of an object.
  • Constructors: Special methods used to initialize objects of the class.
  • Methods: Define the behavior that objects of the class can perform. Methods are blocks of code that run when they are called.

Understanding the correct structure and syntax for defining classes and methods is fundamental to Java programming.

Was this answer helpful?

Important Questions from Java

  1. Which of the following statement(s) is/are TRUE regarding Java Servelets?

    (a) A Java Servlet is a server-side component that runs on the webserver and extends the capabilities of a server.

    (b) A Servelet can use the user interface classes like AWT or Swing.

  2. What is the output of the following JAVA program?

    class Test

    {

    public static void main (String [ ] args)

    {

    Test obj = new Test ();

    obj.start ();

    }

    void start ()

    {

    String stra="do”;

    String strb = method (stra);

    System.out.print(“:” + stra + strb);

    }

    String method (String stra)

    {

    stra=stra+”good”;

    System.out.print (stra);

    return “good”;

    }

    }

  3. A java program that is embedded in HTML document and runs in the context of java capable browser is known as ______.

  4. JAVA compiler translates source code into _______.

  5. Arrange the following applet life cycles methods in sequence 

    A. start() 

    B. paint() 

    C. init() 

    D. stop() 

    Choose the correct answer from the options given below:

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App