Java unit 3

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/30

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

31 Terms

1
New cards

Why do we use methods in Java programming?

All of the above

2
New cards

What are parameters?

The formal names given to the data that gets passed into a method

3
New cards

What is a return value?

The value that a method outputs

4
New cards

What is a Javadoc

A comment that clearly states the functionality and usage of a class or a method

5
New cards

Does the following code snippet have a run time error, compile time error, both, or neither?

int x = 3
int y = 4;
int sum = x + y;

  1. Run time error

  2. Correct Answer

    Compile time error

  3. both 

  4. neither

Complete time error

6
New cards

Which exception will be thrown as a result of running this code snippet?

String hello = "hello";
char last = hello.charAt(10);

IndexOutOfBoundsException

7
New cards

What are parameters

The formal names given to the data that gets passed into a method

8
New cards

What is a method?

A procedure that is defined by the user

9
New cards

What is a Javadoc

A comment that clearly states the functionality and usage of a class or method

10
New cards

What is a return value?

The value that a method outputs

11
New cards

What does void mean?

Void means that a method returns no value

12
New cards

What does this method call output?

public double doubleOrNothing(double myDouble)
{
    return (double) (int) myDouble * 2;
}

doubleOrNothing(9.9);

18.0

13
New cards

What does this method call return?

public int doubleInt(int x)
{
    x * 2;
}

doubleInt(5);

The method is improperly written

14
New cards

What will the value of yourBankAccount be after the method call?

int yourBankAccount = 0;

public void depositMoney(int bankAccount, int deposit)
{
    bankAccount += deposit;
}

depositMoney(yourBankAccount, 1000000);

0

15
New cards

Write a method that will ask for user input until user inputs the String “no”. Allow the user to input any capitalization of the String “no” (“no”, “No”, “NO”, “nO”) Also, have the method return the number of loops.

Use readLine to get the user input.

public int loopTillNo()
{
    int count = 0;
    while(!readLine("Again?").equals("no"))
    {
        count++;
    }
    return count;
}
public int loopTillNo()
{
    int count = 0;
    while(readLine("Again?").toLowerCase().equals("no"))
    {
        count++;
    }
    return count;
}
public int loopTillNo()
{
    int count = 0;
    while(readLine("Again?").equals("no"))
    {
        count++;
    }
    return count;
}
public int loopTillNo()
{
    int count = 0;
    while(!readLine("Again?").toLowerCase().equals("no"))
    {
        count++;
    }
    return count;
}

public int loopTillNo()
{
    int count = 0;
    while(!readLine("Again?").toLowerCase().equals("no"))
    {
        count++;
    }
    return count;
}

16
New cards

What will this method call print?

public void patternGrid(int rows, int columns, char symbol)
{
    for(int m = 0; m < rows; m++)
    {
        for(int n = 0; n < columns; n++)
        {
            System.out.print(symbol);
        }
        System.out.println();
    }
}

####
####
####

17
New cards

What would this method call output?

public int myMethod(int num)
{
    while(num / 10 >= 10) 
    {
        num /= 10;
    }
    return num;
}

83

18
New cards

What is the return value of this method call?

public static String someWhereInTheMiddle(int number)
{
    String stringNum = "" + number;
    int middle = stringNum.length()/2;

    if(stringNum.length() % 2 == 1)
    {
        return stringNum.substring(middle,middle+1);
    } 
    else
    {
        return stringNum.substring(middle-1,middle+1);
    }
}

someWhereInTheMiddle(188603)

86

19
New cards

What is returned by this method call: translator("pokemon")?

public String translator(String word)
{
    return word.substring(1) + word.charAt(0) + "ay";
}

“okemonpay”

20
New cards

What will this method call output?

public int myMethod(boolean x, boolean y)
{
    if(!x)
    {
        if(y)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return -1;
    }
}

myMethod(false,false)

0

21
New cards

What could the method signature for a Karel command look like?

public void move() 
{
    //some code
}

22
New cards

What will this method call print to the screen?

public static void someMethod(int a, String b, int c)
{
    System.out.println(b + " " + a + ", " + c);
}

Mar 9, 1250

23
New cards

Write a method that loops forever until the user inputs the correct secret password or until the user fails to enter the correct password 10 times. The secret password the program should look for is the String "secret"

Use readLine(String prompt) for user input

public void secretPassword()
{
    int count = 1;
    while(true)
    {
        if(count == 10)
        {
            System.out.println("You are locked out!");
            return;
        }

        if(readLine("Password?").equals("secret"))
        {
            System.out.println("Welcome!");
            return;
        }

        count++;
    }
}
public void secretPassword()
{
    int count = 0;
    while(true)
    {
        if(count == 10)
        {
            System.out.println("You are locked out!");
        }

        if(readLine("Password?").equals("secret"))
        {
            System.out.println("Welcome!");
        }

        count++;
    }
}
  • public void secretPassword()
    {
        int count = 0;
        while(true)
        {
            if(count == 10)
            {
                System.out.println("You are locked out!");
                return;
            }
    
            if(readLine("Password?").equals("secret"))
            {
                System.out.println("Welcome!");
                return;
            }
    
            count++;
        }
    }
    
public void secretPassword()
{
    int count = 0;
    while(true)
    {
        if(count != 10)
        {
            System.out.println("You are locked out!");
            return;
        }

        if(!readLine("Password?").equals("secret"))
        {
            System.out.println("Welcome!");
            return;
        }

        count++;
    }
}

  • public void secretPassword()
    {
        int count = 0;
        while(true)
        {
            if(count == 10)
            {
                System.out.println("You are locked out!");
                return;
            }
    
            if(readLine("Password?").equals("secret"))
            {
                System.out.println("Welcome!");
                return;
            }
    
            count++;
        }
    }
    

24
New cards

What will this method call output?

public String yesOrNo(boolean myBoolean)
{
    if(myBoolean == true)
    {
        return "Yes";
    }
    else
    {
        return "No";
    }
}

“Yes”

25
New cards

What will this method call output?

public boolean someMethod(int number)
{
    if(number % 2 == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

true

26
New cards

What will this method call output?

public int mysteryMethod(String x, char y)
{
    int z = 1;
    for(int i = 0; i < x.length(); i++)
    {
        if(x.charAt(i) == y)
        {
            z++;
        }
    }
    return z;
}

3

27
New cards

What is wrong with this method definition?

public String outputString(String input)
{
    System.out.println(input);
}

Improper return value

28
New cards

What kind of error does this method cause?

public void buggyMethod(String x)
{
    for(int i = 0; i <= x.length(); i++)
    {
        System.out.println(x.substring(i, i+1));
    }
}

Runtime Error: String index out of range

29
New cards

What will this method call print to the screen?

public void numberMadness(double myDouble)
{
    int myInt = (int) myDouble;
    String myString = "";
    while(myInt != 0)
    {
        myString = myInt % 10 + myString;
        myInt /= 10;
    }
    System.out.println(myString);
}

12345

30
New cards

Why do we use methods in Java?

  1. To make code easier to understand

  2. To avoid repeated code

  3. To simplify code

All of the above

31
New cards

What does this method call output?

public int trickyMethod(int x, int y)
{
    int sum = 0;
    while (x <= 10)
    {
        sum += x % y;
        x++;
        y++;
    }
    return sum;
}

12