Codehs Unit 2: Using objects

studied byStudied by 10 people
0.0(0)
Get a hint
Hint

What are parameters?

  1. The value that a method returns.

  2. The formal values that a method prints to the screen.

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

  4. The type that is given to a variable.

1 / 24

25 Terms

1

What are parameters?

  1. The value that a method returns.

  2. The formal values that a method prints to the screen.

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

  4. The type that is given to a variable.

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

New cards
2

A procedure that is defined by the user is called a

  1. method.

  2. keyword.

  3. variable.

  4. variable type.

  1. method.

New cards
3

All non-void methods must have which of the following?

  1. a print statement.

  2. an argument.

  3. a parameter.

  4. a return value.

  1. a return value.

New cards
4

What is true of a void method?

  1. It returns any value.

  2. It takes no parameters.

  3. It returns no value.

  4. It can take any parameter.

  1. It returns no value.

New cards
5

What does this method call doubleNumber(7.8); return, given the following method?

public double doubleNumber(double myNumber)
{
return (double) (int) myNumber * 2;
}
  1. 16

  2. 15.6

  3. 14.0

  4. This method is improperly written.

  5. 15.0

  1. 14.0

New cards
6

What does the method call mystery("APCSA"); return for the following method?

public String mystery(String x)
{
return x * 3;
}

  1. APCSAAPCSAAPCSA

  2. APCSA APCSA APCSA

  3. APCSA3

  4. This method is improperly written.

  5. APCSA 3

  1. This method is improperly written.

New cards
7

What will the value of myBankAccount be after the method call depositMoney(myBankAccount, 572);?

int myBankAccount = 122;

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

  1. 694

  2. 122

  3. 572

  4. The code will error.

  1. 122

New cards
8

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

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

  1. “pokemonpay”

  2. “okemonpay”

  3. “okemonay”

  4. This method call will error.

  1. “okemonpay”

New cards
9

Question: 9

What will this method call print to the screen?

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

formatText(2018, 17, "Dec")

  1. Dec 17, 2018

  2. Dec 17 2018

  3. 17 Dec 2018

  4. 17 Dec, 2018

  1. 17 Dec, 2018

New cards
10

What is wrong with this method definition?

public int printPayAmount(int amount)
{
System.out.println(amount);
}

  1. This method should be private

  2. Nothing is returned from this method

  3. This is a String type method

  4. The method is never called

  1. Nothing is returned from this method

New cards
11

Why do we use methods in Java?

I. To make code easier to understand
II. To define global variables
III. To avoid repeated code
IV. To simplify code
V. To avoid needing to define them as public or private

  1. I - V all

  2. III, IV, and V

  3. I, II, III, and IV

  4. I, III, and IV

  1. I, III, and IV

New cards
12

A coffee shop has created a DrinkOrder class. The class contains variables to represent the following:

  • A String variable called name to represent the name of the drink.

  • An int variable called ounces to indicate how many ounces the drink should be.

  • A boolean variable called isIced to indicate whether the drink is iced.

An object latte has been declared as type DrinkOrder after someone has ordered a Latte.

Which of the following descriptions is accurate?

  1. An instance of latte is DrinkOrder.

  2. An attribute of latte is DrinkOrder.

  3. An attribute of name is latte.

  4. An instance of the DrinkOrder class is latte.

  5. An attribute of DrinkOrder is latte.

  1. An instance of the DrinkOrder class is latte.

New cards
13

A coffee shop has created a DrinkOrder class. The class contains variables to represent the following:

  • A String variable called name to represent the name of the drink.

  • An int variable called ounces to indicate how many ounces the drink should be.

  • A boolean variable called isIced to indicate whether the drink is iced.

An object Latte has been declared as type DrinkOrder after someone has ordered a Latte.

Based on the information provided, which would be the most accurate class declaration for the DrinkOrder class?

  1. public class DrinkOrder { private String name; private int ounces; private boolean isIced; public DrinkOrder(String theName, int theOunces, boolean hasIce) { name = theName; ounces = theOunces; isIced = hasIce; } }

  2. public class DrinkOrder { private String name; private int ounces; private boolean isIced; public DrinkOrder(String name, int ounces, boolean isIced) { name = name; ounces = ounces; isIced = isIced; } }

  3. public class DrinkOrder { public DrinkOrder(String theName, int theOunces, boolean hasIce) { name = theName; ounces = theOunces; isIced = hasIce; } }

  4. DrinkOrder { private String name; private int ounces; private boolean isIced; public DrinkOrder(String theName, int theOunces, boolean hasIce) { name = theName; ounces = theOunces; isIced = hasIce; } }

  1. public class DrinkOrder { private String name; private int ounces; private boolean isIced; public DrinkOrder(String theName, int theOunces, boolean hasIce) { name = theName; ounces = theOunces; isIced = hasIce; } }

New cards
14

After the execution of the following lines of code, what will be output onto the console?

String letters = "ABCde";
String name = "Karel the Dog";
String letter = "D";

System.out.println(name.indexOf(letter));
System.out.println(letters.indexOf(name.substring(3,4)));
System.out.println(letters.indexOf(letter));

  1. 10

    4

    -1

  2. -1

    7

    10

  3. 0

    4

    -1

  4. 10

    7

    3

  5. 10

    3

    -1

  1. 10

    4

    -1

New cards
15

Consider the following class:

public class Coin
{
private String name;
private double value;

public Coin(String theName, double theValue)
{
name = theName;
value = theValue;
}

public double getValue()
{
return value;
}
}

Assume that a Coin object quarter has been properly declared and initialized. Which of the following code snippets will successfully assign the value of quarter to a new variable coinWorth?

  1. quarter.getValue();

  2. double coinWorth = quarter.getValue();

  3. int coinWorth = quarter.getValue();

  4. double coinWorth = quarter;

  1. double coinWorth = quarter.getValue();

New cards
16

Consider the following class:

public class Insect
{
private String name;
private int numLegs;
private boolean hasWings;
private boolean hasExoskeleton;

public Insect(String theName, int legNumber, boolean isWinged, boolean isExoskeleton)
{
name = theName;
numLegs = legNumber;
hasWings = isWinged;
hasExoskeleton = isExoskeleton;
}

public Insect(String theName, int legNumber, boolean isWinged)
{
name = theName;
numLegs = legNumber;
hasWings = isWinged;
hasExoskeleton = true;
}

}

Which of the following is NOT a possible header for a new constructor for the Insect class?

  1. Insect(String theName, int legNumber)

  2. Insect(String theName, int legNumber, boolean isExoskeleton)

  3. Insect(String theName)

  4. Insect()

  1. Insect(String theName, int legNumber, boolean isExoskeleton)

New cards
17

Which of the following code segments would successfully square the minimum value between two int variables x, y?

  1. Math.pow(Math.min(x, y), 2);

  2. Math.min(Math.pow(x, y));

  3. Math.pow(Math.min(x, y));

  4. Math.pow(2, Math.min(x, y));

  1. Math.pow(Math.min(x, y), 2);

New cards
18

Consider the following class:

public class Dog
{
private String name;
private String breed;

public String getName()
{
return name;
}
}

An object Karel is created using the Dog class. What would the result of the command System.out.println(Karel.getName()); be?

  1. This code would result in an error, as name has yet to be initialized.

  2. This code would result in an error, as there is no constructor present in the Dog class.

  3. " "

  4. null

  5. false

4.null

New cards
19

Consider the following method:

public double doubleVal(double x)
{
return x *2;
}

The following code segment calls the method doubleVal:

Double val = 6.5;
System.out.println(doubleVal(val));

What is printed when the code segment is executed?

  1. 13

  2. 13.0

  3. 12.0

  4. 12.5

  5. Double values cannot be used in double methods before being converted.

  1. 13.0

New cards
20

Consider the following class:

public class RaffleTicket
{
private String ownerName;
private int ticketNum;

public RaffleTicket(String name)
{
ownerName = name;
ticketNum = getTicketNum();
}

private int getTicketNum()
{
/* Code to be implemented */
}

Which of the following can be used to replace /*Code to be implemented */ so that the code segment produces a random number between 1-100?

  1. return Math.random() * 100;

  2. return Math.random() * 100 + 1;

  3. return (int) (Math.random() * 100);

  4. return (int) (Math.random() * 100 +1);

  5. return (int) (Math.random() * 101);

  1. return (int) (Math.random() * 100 +1);

New cards
21

Consider the following class:

public class RandomCalculator
{
private int x;
private int y;

public RandomCalculator(int one, int two)
{
x = one;
y = two;
}

public int add(int num)
{
return x + y + num;
}

public int add(double num)
{
return (int)(num) + x / y;
}

public int add(double num, int num2)
{
return (int)(num2 + num + x - y);
}

public double add(int num, double num2)
{
return num + num2 - x + y;
}

What would the output be of the following code segment:

RandomCalculator calc = new RandomCalculator(4, 5);
System.out.println(calc.add(5.0));
System.out.println(calc.add(5.0, 5));
System.out.println(calc.add(5, 5.0));

  1. 14

    9

    11.0

  2. 5

    9.0

    11

  3. 5

    9

    11.0

  4. 14

    11.0

    9

  1. 5

    9

    11.0

New cards
22

Consider the following code segment:

String str = "I am";
str += 10 + 3;
String age = "years old";
System.out.println(str + age);

What is printed as a result of executing the code segment?

  1. I am103years old

  2. I am 13 years old

  3. I am13years old

  4. I am 103 years old

  5. years oldI am 13

3. I am13years old

New cards
23

Which of the following code segments would correctly assign word from the String sentence = "Grab the last word" to the variable lastWord?

I. String lastWord = sentence.substring(sentence.length()-4, sentence.length());
II. String lastWord = sentence.substring(sentence.indexOf("w"), sentence.indexOf("d"));
III. String lastWord = sentence.substring(14, 16) + sentence.substring(1, 2) + sentence.substring(sentence.length()-1, sentence.length());

  1. I only

  2. III only

  3. I and II only

  4. I and III only

  5. I, II, & III

4.I and III only

New cards
24

Consider the following class:

public class Greetings
{
private String greetings;

public Greetings(String greeting)
{
greetings = greeting;
System.out.println(greetings);
}

public void hello()
{
System.out.println("Hello");
}

public void translate()
{
greetings = "Hola";
}

public void changeGreeting(String greeting)
{
greetings = greeting;
}

public void greeting()
{
System.out.println(greetings);
}
}

Which of the following code segments will produce this output in the console:

"Hello"
"Hola"

I.

Greetings hi = new Greetings("Hello");
hi.translate();
hi.greeting();

II.

Greetings.hello();
Greetings.translate();
Greetings.greeting();

III.

Greetings hello = new Greetings();
hello.hello();
hello.translate();
hello.greeting();

IV

Greetings hola = new Greetings("Hello");
hola.changeGreeting("Hola");
hola.greeting();

  1. II only

  2. I, III and IV

  3. IV only

  4. I and IV

  5. I and III

4.I and IV

New cards
25

A science teacher wants to create an Elements class for the Periodic Table. They want to include several attributes to the Elements class: the atomic weight, the element name, and the atomic number for the element.

For example, if you wanted to create an entry for Oxygen you would use the following data:
Name: Oxygen
Atomic Weight: 15.999
Atomic Number: 8

Which of the following instance variables makes the most sense based on these attributes?

  1. private int atomicWeight; private String name; private int atomicNum;

  2. private int atomicWeight; private String name; private double atomicNum;

  3. private double atomicWeight; private String name; private int atomicNum;

  4. private double atomicWeight; private String name; private double atomicNum;

  5. int atomicWeight; String name; int atomicNum;

3.private double atomicWeight; private String name; private int atomicNum;

New cards

Explore top notes

note Note
studied byStudied by 352 people
... ago
5.0(1)
note Note
studied byStudied by 44 people
... ago
4.0(1)
note Note
studied byStudied by 34 people
... ago
4.5(2)
note Note
studied byStudied by 8 people
... ago
5.0(1)
note Note
studied byStudied by 141 people
... ago
4.3(7)
note Note
studied byStudied by 18 people
... ago
5.0(1)
note Note
studied byStudied by 24 people
... ago
5.0(1)
note Note
studied byStudied by 9 people
... ago
5.0(3)

Explore top flashcards

flashcards Flashcard (102)
studied byStudied by 23 people
... ago
5.0(1)
flashcards Flashcard (31)
studied byStudied by 3 people
... ago
5.0(1)
flashcards Flashcard (71)
studied byStudied by 26 people
... ago
5.0(1)
flashcards Flashcard (230)
studied byStudied by 6 people
... ago
5.0(1)
flashcards Flashcard (82)
studied byStudied by 3 people
... ago
5.0(1)
flashcards Flashcard (20)
studied byStudied by 1 person
... ago
5.0(1)
flashcards Flashcard (23)
studied byStudied by 97 people
... ago
5.0(1)
flashcards Flashcard (404)
studied byStudied by 4 people
... ago
5.0(3)
robot