Deck 5: Writing Classes
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
Play
Full screen (f)
Deck 5: Writing Classes
1
Which of the following method headers is most likely a header for a mutator method?
A) public int getAge()
B) public double computeSalary()
C) public Person()
D) public void setAge(int newAge)
E) none of these are headers for a mutator method
A) public int getAge()
B) public double computeSalary()
C) public Person()
D) public void setAge(int newAge)
E) none of these are headers for a mutator method
D
Explanation: Mutators are methods that change the value of an instance variable, and are often referred to as "setters." Therefore, choice d is the correct answer. Choice a is an example of a header for a accessor method, often referred to as a "getter." Choice c is a constructor, and choice b is a class method.
Explanation: Mutators are methods that change the value of an instance variable, and are often referred to as "setters." Therefore, choice d is the correct answer. Choice a is an example of a header for a accessor method, often referred to as a "getter." Choice c is a constructor, and choice b is a class method.
2
Which of the following types of methods do not have any return type (not even a void return type)?
A) methods declared as static
B) methods declared as public
C) methods declared as private
D) constructors
E) all of the above have return types
A) methods declared as static
B) methods declared as public
C) methods declared as private
D) constructors
E) all of the above have return types
D
Explanation: Constructors are the only methods that do not have any return type. They do not even have a void return type. All of the other methods must specify a return type or be declared as void.
Explanation: Constructors are the only methods that do not have any return type. They do not even have a void return type. All of the other methods must specify a return type or be declared as void.
3
The versions of an overloaded method are distinguished by ___________________________.
A) the number, type and order of their parameters
B) their identifiers
C) their classes
D) the number and type of their parameters
E) the number of their parameters
A) the number, type and order of their parameters
B) their identifiers
C) their classes
D) the number and type of their parameters
E) the number of their parameters
A
Explanation: Overloaded methods are two methods in the same class that have the same identifier, but a different number, type or order of parameters. Therefore, choice a is correct and the rest are incorrect.
Explanation: Overloaded methods are two methods in the same class that have the same identifier, but a different number, type or order of parameters. Therefore, choice a is correct and the rest are incorrect.
4
Which of the following object-oriented principles refers to the fact that an object should have its data guarded from inappropriate access?
A) encapsulation
B) inheritance
C) polymorphism
D) instance variables
E) methods
A) encapsulation
B) inheritance
C) polymorphism
D) instance variables
E) methods
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
All methods (with the exception of constructors) must specify a return type. What is the return type for a method that does not return any values?
A) int
B) public
C) double
D) void
E) none of the above
A) int
B) public
C) double
D) void
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
The ________________ reference always refers to the currently executing object.
A) null
B) static
C) final
D) actual
E) this
A) null
B) static
C) final
D) actual
E) this
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
When applied to instance variables, the ________________ visibility modifier enforces encapsulation.
A) static
B) final
C) public
D) private
E) none of the above
A) static
B) final
C) public
D) private
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
There are times when it is appropriate to return data from a method of a type that is inconsistent with the return type specified in the method header.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
A return statement is not required at the end of every method.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
An object can be thought of as a blueprint for a set of classes.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
A(n) ___________________ object is one that is made up, at least in part, of other objects.
A) static
B) aggregate
C) encapsulated
D) private
E) public
A) static
B) aggregate
C) encapsulated
D) private
E) public
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
Methods that can be called directly through the class name and do not need to have an object instantiated are called _________________.
A) final
B) public
C) static
D) private
E) none of the above
A) final
B) public
C) static
D) private
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
If a service is so complex that it cannot be reasonably be implemented using one method, it is often helpful to decompose it to make use of ________________ support methods.
A) static
B) aggregate
C) private
D) public
E) final
A) static
B) aggregate
C) private
D) public
E) final
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
A ________________ diagram helps us visualize the contents of and relationships among the classes of a program.
A) class and object
B) UML
C) object-oriented
D) public
E) private
A) class and object
B) UML
C) object-oriented
D) public
E) private
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
A _______________ variable is shared among all instances of a class.
A) static
B) final
C) public
D) private
E) none of the above
A) static
B) final
C) public
D) private
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
A(n) ________________ is a step-by-step process for solving a problem.
A) UML diagram
B) aggregate object
C) class
D) algorithm
E) none of the above
A) UML diagram
B) aggregate object
C) class
D) algorithm
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
A method that has multiple definitions is an __________________ method.
A) overloaded
B) overridden
C) overlooked
D) overclocked
E) none of the above
A) overloaded
B) overridden
C) overlooked
D) overclocked
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
A variable can always be referenced anywhere in a program.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
When an object is passed to a method, the actual and formal parameters become aliases.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
__________________ parameters are the values that are used when calling a method.
A) formal
B) actual
C) useful
D) informal
E) none of the above
A) formal
B) actual
C) useful
D) informal
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
Write a method called containsPair that takes in three integer parameters and returns true if any two of the input parameters are the same.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
Consider the following method.
public void changeValues(int i, Card
c) {
i = 15;
c.setSuit("Clubs");
}
Now consider the following snippet of code that calls the method defined above.
int num = 12;
Card fiveOfSpades = new Card(5, "Spades");
System.out.println("Before method call:");
System.out.println(num);
System.out.println(fiveOfSpades.getSuit());
changeValues(num, fiveOfSpades);
System.out.println("After method call:");
System.out.println(num);
System.out.println(fiveOfSpades);
What is the output of this code?
public void changeValues(int i, Card
c) {
i = 15;
c.setSuit("Clubs");
}
Now consider the following snippet of code that calls the method defined above.
int num = 12;
Card fiveOfSpades = new Card(5, "Spades");
System.out.println("Before method call:");
System.out.println(num);
System.out.println(fiveOfSpades.getSuit());
changeValues(num, fiveOfSpades);
System.out.println("After method call:");
System.out.println(num);
System.out.println(fiveOfSpades);
What is the output of this code?
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
Write a method called randomAverage that generates 100 random integers in the range 1 to 100 (inclusive) and returns their average. You may assume that the class has a static Random object called generator already declared and instantiated.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
Write a method called square that takes in an integer value and returns the integer squared. Your method should use the Math.pow() method.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
What is the difference between an object and a class?
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
Write a method called threeOfAKind that takes in three integer parameters and returns true if all three of them are the same, false otherwise.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
Explain why method overloading is useful.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
Variables that are declared as static are shared among all instances of a class.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
A main method can only access static or local variables.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
Explain the difference between actual parameters and formal parameters.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
Write a method that prints your name, age, and major. The method should accept no parameters and return no value.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
Write a method called isPalindrome that accepts a String as a parameter and returns true if the String is a palindrome, and false otherwise. You may assume that the entered String consists entirely of lowercase letters (meaning it contains no numbers, spaces, punctuation, etc). Hint: write code that creates a new string that is the original string reversed, and then check to see if the two strings are equal.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
A programmer is required to define a constructor for every class.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
What is encapsulation? How can it be enforced in Java?
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
Explain why a static method cannot refer to an instance variable.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
In a class that has variables called height and width, methods called getHeight() and getWidth() are examples of accessor methods.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
Write a method called randomInRange that takes in two numbers representing a range. Print an error message and return zero if the second parameter is less than the first. Otherwise, the method should return a randomly generated integer in that range (inclusive). You may assume that the class has a static Random object called generator already declared and instantiated.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
Write a method called countSpaces that takes in a String as a parameter and returns an integer that represents the number of space characters (' ') contained in the string, false otherwise..
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
Write a method called maxOfThree that accepts three integer parameters and returns the largest of the three.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
Aggregation is sometimes described as a has-a relationship.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck