Module 02: Using Objects Flashcards

1
Q

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.
A
  1. The formal names given to the data that gets passed into a method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A procedure that is defined by the user is called a

  1. method.
  2. keyword.
  3. variable.
  4. variable type.
A
  1. method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The value that a non-void method outputs is called

  1. a print statement.
  2. an argument.
  3. a parameter.
  4. a return value.
A
  1. a return value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.
A
  1. It returns no value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

  • public double doubleNumber(double myNumber)*
  • {*
  • return (double) (int) myNumber * 2;*
  • }*
A

14.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the method call tripleString(“APCSA”); return for the following method?

  • public String tripleString(String x)*
  • {*
  • return x * 3;*
  • }*
  1. APCSAAPCSAAPCSA
  2. APCSA APCSA APCSA
  3. APCSA3
  4. This method is improperly written.
  5. APCSA 3
A
  1. This method is improperly written
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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;*
  • }*
A

122

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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.
A
  1. “okemonpay”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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
A
  1. 17 Dec, 2018
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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
A
  1. Nothing is returned from this method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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
A
  1. I, III, and IV
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.
A
  1. An instance of the DrinkOrder class is latte
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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));

A

10

4

-1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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;
A
  1. double coinWorth = quarter.getValue();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Consider the following class:

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()
A
  1. Insect(String theName, int legNumber, boolean isExoskeleton)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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));
A
  1. Math.pow(Math.min(x, y), 2);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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
A
  1. null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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?

A

13.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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);
A
  1. return (int) (Math.random() * 100 +1);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Consider the following class:

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));

A

5

9

11.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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
A
  1. I am13years old
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

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());*

I only

III only

I and II only

I and III only

I, II, & III

A

I and III only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Consider the following class:

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the difference between a primitive type and a reference type?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are objects?

A

All reference types hold references to objects and provide a means of accessing those objects in memory:

  • Sting name = “My name is Karel.”;*
  • // name = object*
  • Object variable of data type that is used defined
  • The object has both state and behavior
  • The state is data about the object
  • The behavior of an object is the action that can be performed on that object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What are classes?

A

Objects are created from classes

A class is a template for creating an object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What are instance variables?

A

Used to store the state or data of the object instances

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is a constructor/signature?

A

Allows for the creation of a new object. Consists of the constructor name and parameter list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

what is the “new” keyword?

A

Keyword for instantiating a class object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is instantiate?

A

Create an instance of a class object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What are formal and actual parameters?

A
  • Formal parameters are the parameters outlined in the parameter list in the constructor
  • Actual parameters are the parameters that are input when a new instance of a clas object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is a constructor?

A

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is overloading?

A

When a class has more than one constructor with the same name, but different parameter lists

Overloading: (Java looks for constructor call with the appropriate parameter) Has the same name and different parameters

Purpose: It allows the user to set the values of different combinations of the instance variables when the object is created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is the reference variable?

A

A reference variable holds the address of an object, not the name of the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What are objects in memory?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What are scanner constructors and classes?

A

The scanner class is an example of a preexisting class that we have utilized for our own benefit When we use a class that someone has created, we are a client of that class.

Documentation for the Scanner constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is an instance method?

A

Instance Method: method that belongs to instances of a class, not to the class itself. An instance method is a piece of code called on a specific instance of the class. It is called a receiver object.

38
Q

What are methods?

A

Methods are procedures that allow us to control and define the behavior of an object. (Change behavior)

39
Q

How do you call methods?

A
  • To use a method = must call it
  • To call a method: objectName.method()
40
Q

What is procedural abstraction?

A

Procedural Abstraction: Even though we don’t know how nextLine(() was written, we can still use it effectively in our programs as long as we know what is does

41
Q

What are the different parts of a writing method declaration?

A
42
Q

What is method overloading?

A

Method Overloading: Methods can have multiple signatures. Java will use the correct signature based on the actual parameters used in a program

43
Q

What are parameters and why is parameter order important?

A

🖊️ Parameters: A value that you can pass to a method in Java. Parameters are the names of the variables in the method signature.

When there are multiple parameters, the order that parameters are input matters:

  • public void setWidthAndHeight(int rectWidth, int rectHeight)*
  • {*
  • width = rectWidth;*
  • height = rectHeight;*
  • }*
44
Q

What are return types?

A

The return type determines the type that will be returned from a method

We can return a value from a method by using the keyword return

public void printArea() //Can only print the value of area

{

int area = width * height; System.out.print(area);

}

public int getArea() //Returns the value of area which can be used in a variety of ways

{

int area = width * height; return area;

}

45
Q

Define immutability:

A

Unable to be changed or manipulated. String are immutable

46
Q

Define concatenation:

A

The process of adding two Strings values together.

This creates a new String object. Primitives can be concatenated with String objects

  • Strings can be concatenated with one another to create a new String value:
  • String firstName = “Karel”;*
  • String lastName = “ The Dog”;*
  • String fullname = firstName + last Name;*
  • System.out.println(fullName); // → Karel The Dog*
  • We use the += shortcut on String values:
  • String firstName = “Karel”;*
  • firstName += “!”;*
  • System.out.println(firstName); // → Karel!*
47
Q

Define Implicit Conversation:

A

Automatic process of transforming a variable data type. This occurs when a primitive and String object are concatenated by changing the primitive value to a String object type

48
Q

What is an escape sequences?

A

Enables users to use special characters and actions within String objects

49
Q

How do you indicate that something is a reference type?

A

Starting a data type with a capital letter indicates that it’s a reference data type!

50
Q

What does the method do?

name.concat(“value”)

A

Returns a new String with “value” appended to the end of the name

51
Q

What does the method do?

name.replace(“a”, “p”)

A

Returns a new String with all instances of “a” in name replaced with “p”

52
Q

What does the method do?

name.toUpperCase()

A

Returns name as Upper Case letters (same can be done to lower case letters)

53
Q

How can primitive types can be concatenated with String objects?

A
  • Implicit Conversion converts int age to a String, and concatenates it to the end of currentAge!

int currentAge = 20;

int age = 10;

System.out.println(“In ten years, I will be” + currentAge + age);

// → In ten years, I will be 2020

  • Putting the equation within the parenthesis will evaluate it before converting it to String:

int currentAge = 20;

int age = 10;

System.out.println(“In ten years, I will be” + (currentAge + age));

// → In ten years, I will be 30

54
Q

What are escape sequences?

A
55
Q

What does String(String, str) constructor do?

A

Constructs a new String object that represents the same sequence of characters as str

56
Q

int length ()

A

Returns the number of characters ins a String objects

57
Q

String substring (int from, int to)

A

Returns the substring beginning at index from and ending at index to -1

58
Q

String substring (int from):

A

Returns substring(from, substring())

59
Q

IndexOutOfBoundsException

A

A String object has index values from 0 to length -1.

Attempting to access indicates outsides this range will result in this error

60
Q

int indexOf(String str)

A

Returns the index of the first occurrence of str: returns -1 if not found

61
Q

int compareTo(String other)

A

Returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other

62
Q

Application Programming Interference:

A

APIs and libraries simplify complex programming tasks by providing sets of clearly defined methods of communications among various computing components

63
Q

What is Interning?

A

Interning: When a String is created without the new operator, a single copy of the String is allocated into memory

64
Q

Why are these not the same?

String s2 = new String(“Karel);

String s4 = new String(“Karel);

A

Not the same because they refer to a DIFFERENT location in memory.

The JVM (Java Virtual Machine) is forced to create a new string reference, even if “karel” is in the reference pool

s2 and s4 refer to two different objects in the heap, neither of which have been interned as is the case with string literals
Different objects always have different memory references

65
Q

What is an index?

A
  • Each character in a string has an index.
  • *INDEX**: Position of the character in the String
  • Each String starts with index 0. The final index of a String is one less than the number of the characters in the String
66
Q

charAT(int index)

A

Method charAT(int index) to access specific characters in a String:

String str = “hello”;

System.out.println(str.charAt(1);

//→ 1 e

67
Q

name.length( )

A

Returns the number of characters in a String object

String str = new String(“Good day!”);

// the number of characters includes the spaces and exclamation point

int length = str.lenght(); System.out.println(length); // → 9

68
Q

name.substring(2, 6)

A

Returns the substring beginning at index 2 and ending at index 5

  • 2 is inclusive
  • 5 is excluding
69
Q

name.indexOf(“c”)

A

Returns the index of the first occurrence of d; returns -1 if not found

70
Q

name.equals(“karel”)

A

Returns true if the name is equal to Karel; returns false otherwise

  • We can compare two Strings using one.equals(two).
  • Method returns true if both Strings have the same characters
71
Q

name.compareTo(“Karel”)

A

Returns a value < 0 if name is less than Karel; returns zero if name is equal to Karel; returns a value > 0 if name is grater than Karel

  • Compares the value of the first character that is NOT equal in both strings
  • Its returns a numerical value based on the comparison because character values are actually numerical values
  • Negative value means that the original String in the comparison is lexicographically lesser than the second
  • Compares the first two characters that are not equal
72
Q

What is Java.lang and packages?

A
  • String class and all associated String methods are part of the java.lang package.
  • Package is a group of related classes, along with their methods, fields, and constructors
    • part of a programming library
  • Libraries: Collection of written works make by other authors
    • collection of packages and classes (packages are made up of classes)
    • Classes has an Application Programming Interface (API) or a set of instructions on how to use it.
    • API’s allow us to use methods and classes without knowing how they work through documentation
    • Documentation is the reference for how to use different methods and classes
    • Oracle Documetation: Google Java
73
Q

What are integer and double classes?

A

The classes are part of the java.lang package and Object class and have a number of useful methods

74
Q

Integer(int value) and Double(double value)

A

Constructs a new Integer object or a new double object that represents the specified int or doubles

75
Q

Autoboxing?

A

Automatic conversion between primitive types and their corresponding objective wrapper classes

76
Q

Unboxing :

A

Reverse of autoboxing; automatic conversion from the wrapper class to the primitive type

77
Q

What are wrapper class?

A

Convert primitive types to object types using wrapper classes

A Wrapper class in Java is a class that contains or “wraps” primitive data types as an object

How do Wrapper Classes Help?

  • Each wrapper class contains special values (like the minimum and maximum values of the type) and methods that are useful for converting between types.
78
Q
A
79
Q

Integer.Min_VALUE and Integer.Max_VALUE

A

Returns the minimum and maximum possible int or Integer value

80
Q

Integer.intValue()

A

Converts integer value into an int value

intValue() allows us to extract the primitive value from the object Integer value

81
Q

Double.doubleValue()

A

Converts Double value into a double value

82
Q

What are the advantages of autoboxing and unboxing?

A

Autoboxing and unboxing lets programmers write clearer code, so its easier to read

Allows us to use primitive types and wrapper class objects interchangeably without having to perform any casting explicitly

83
Q

What is autoboxing? Autoboxing: Converting a primitive value into an object of the corresponding wrapper class

A

Autoboxing: Converting a primitive value into an object of the corresponding wrapper class

  • Instead of writing:
  • Integer myInt = new Integer(53);*
  • Double myDouble = new Double(7.3);*
  • We can write:
  • Integer myInt = 53;*
  • Double myDouble = 7.3;*

Java automatically converts primitive type values to integers and double objects if values are declared as Wrapper class objects

Complier will also apply autoboxing when a primitive value is passed as a parameter to a method that expects an object of the corresponding wrapper class

84
Q

What is unboxing?

A

📌 Unboxing: Converting an object of a wrapper type to its corresponding primitive value

Write: (Shows unboxing when an int object is assigned back to a primitive int type. The Java compiler takes care of the unboxing conversation at runtime)

/* autoboxing - wrap 53 */

Integer myInt = 53;

/* unboxing the object - back to primitve type */

int myInt2 = myInt;

85
Q

What are instance methods?

A
  • Instance (non-static) methods need to be called with an object of the class as a receiver
  • Instance methods are also non-static methods

Ex:

  • Rectangle is an instance of the Rectangle Class*
  • getArea( ) is an instance method*
  • rectangle.getArea()*
86
Q

What are static methods?

A

Static Methods: Methods in Java that can be called without creating an object of a class. Referenced by the class name itself.

  • public static void rectEquation ();*
  • /* When a method is declared with “static keyword” it is a known static method */*
  • {*
  • System.out.println(“The formula for area is: L * W”);*
  • System.out.println(“The formula for perimeter is: 2(L + W)”);*
  • }*
87
Q

Math.abs(x)

A

Returns the absolute value of x

88
Q

Math.pow(base, exponent)

A

Returns the value of baise raised to the power of exponent

89
Q

Math.sqrt(x)

A

Return the positive square root of x

90
Q

Math.random()

A

Returns a double value greater than or equal to 0.0 and less than 1.0

General Formula from Generating any range from (start to start + range) is:

int rangeInteger = (int)(Math.random() * (range + 1) + start);