Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8436341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:09:15+00:00 2026-06-10T07:09:15+00:00

Ths is a question from a past paper. I have been asked to create

  • 0

Ths is a question from a past paper. I have been asked to create a static method arrayMin to find the smallest value in the array arr.

I have to use a while loop and on each iteration, the variable min will return the smallest number from the first i elements.

Is there a way to do this without calling another method/for loop and strictly using the while loop, as the question is only worth 4%(including writing loop invariants and javadoc). Not sure if I am overcomplicating the problem.

public class Revision {

public static int arr[] = new int[] { 5, 8, 4, 3, 6, 2 };
public static int min = 1;

public static int arrayMin() {

    int i = 0;

    if (arr == null) {
        return 0;

    } else {
        while (i < arr.length) {
            // some function/method call to find smallest number of arr[i]
            i++;
            return min;
        }
    }
    return min;
}

public static void main(String[] args) {

    System.out.println(arrayMin());
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:09:17+00:00Added an answer on June 10, 2026 at 7:09 am

    A couple of things:

    1. The array shouldn’t be static, you should pass it as a parameter to the arrayMin method;
    2. min should be a local arrayMin variable, not static;
    3. min should be initialized to Integer.MAX_VALUE. If you initialize it with 1, and 2 happens to be the min value of the array, you’ll never return it;
    4. You can’t return multiple times from a method. As soon as you do return min, the method ends. There’s probably some confusion over the the variable min will return the smallest number from the first i elements phrase. It probably means that in each iteration, the variable min will have (not return) the smallest number from the first i elements.

    Here’s a refactor:

    public static int arrayMin(int[] arr) {
        int i = 0;
        int min = Integer.MAX_VALUE;
        if (arr == null) {
            return 0; // What if 0 is the minimum value? What do you want to do in this case?
        } else {
            while (i < arr.length) {
                if (arr[i] < min) {
                  min = arr[i];
                }
                i++;
            }
        }
        return min;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been asked before, but not with this exact issue. I have
suppose I have a model and a view ,ths view have two method:one is
I have been looking through ths hashlib documentation but haven't found anything talking about
I have ths function to query a set of records from the DB: public
I have this code: public static Account[] LoadXml(string fileName) { Account ths = new
I have been reading about ths approach and understand the theoretical part to a
I have ths line in my .vimrc: map gu :%s/\s\+$//<enter> :w<enter> to remove trailing
Hey there. been a busy debugger today. ill give ths short version. ive made
I have two TextViews that displays numbers from 1-6, I need to make a
Ive got ths really annoying issue I have grouped a set of data and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.