Chapter 5

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

T/F: Variables that are declared as static are shared among all instances of a class.

Answer: True Explanation: Static variables are sometimes called class variables because they are shared among all instances of a class.

Explain the difference between actual parameters and formal parameters.

Formal parameters are used in the definition of a method, while actual parameters are the values that are used in the method call.

Write a method called threeOfAKind that takes in three integer parameters and returns true if all three of them are the same, false otherwise.

public boolean containsPair(int a, int b, int c) { return (a == b && a == c && b == c); }

T/F: 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.

Answer: False Explanation: The value returned from a method must be consistent with the return type specified in the method header.

T/F: In a class that has variables called height and width, methods called getHeight() and getWidth() are examples of accessor methods.

Answer: True Explanation: Accessor methods get the value of instance variables. In contrast, mutators set the values.

What is the difference between an object and a class?

Answer: A class can be thought of as the blueprint for an object. In other words, the object is the embodiment of a class.

T/F: A main method can only access static or local variables.

Answer: True Explanation: A main method cannot access non-static and non-local variables because it is a static method. In particular, it cannot access any variables declared at the class level.

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.

public double randomAverage() { int sum = 0; for(int i = 0; i < 100; i++) sum += generator.nextInt(100) + 1; return (double) sum/100.0; }

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..

public int countSpaces(String s) { int count = 0; for(int i = 0; i < s.length(); i++) if(s.charAt(i) == ' ') count++; return count; }

What is encapsulation? How can it be enforced in Java?

Answer: Encapsulation is the principle that objects should be self-governing. In other words, an object's internal data should be protected from outside access. Encapsulation can be enforced in Java by making instance variables private.

T/F: An object can be thought of as a blueprint for a set of classes.

Answer: False Explanation: A class can be thought of as a blueprint for a set of objects; not the other way around.

__________________ parameters are the values that are used when calling a method. a) formal b) actual c) useful d) informal e) none of the above

Answer: b Explanation: Actual parameters are sent in when calling a method. Formal parameters are used when defining a method.

Write a method called containsPair that takes in three integer parameters and returns true if any two of the input parameters are the same.

public boolean containsPair(int a, int b, int c) { return (a == b || a == c || b == c); }

Write a method called square that takes in an integer value and returns the integer squared. Your method should use the Math.pow() method.

public int square(int a) { return Math.pow(a, 2); }

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?

Answer: Before method call: 12 Spades After method call: 12 Clubs

T/F: A programmer is required to define a constructor for every class.

Answer: False Explanation: Every class automatically has a default constructor that doesn't take any parameters.

T/F: A variable can always be referenced anywhere in a program.

Answer: False Explanation: The scope of a variable determines where it can be referenced. It depends on where the variable is declared.

Explain why method overloading is useful.

Answer: Method overloading is useful because it allows multiple methods to have the same name as long as they have different parameter types. This allows for more flexibility in choosing identifiers for methods, since the alternative would require the programmer to have different method identifiers for every method that took in a different type of parameter.

Explain why a static method cannot refer to an instance variable.

Answer: Static methods may be called without instantiating an object. Therefore, an instance variable may not have an assigned value when a static method is called, so a static method cannot reference it.

T/F: A return statement is not required at the end of every method.

Answer: True Explanation: A return statement is not required at the end of a constructor or any method declared with the return type void.

T/F: When an object is passed to a method, the actual and formal parameters become aliases.

Answer: True Explanation: Actual parameters are the datum that are sent into the method. The formal parameters are used in the method definition. When objects are sent to a method, both of these values are references, and they become aliases of one another.

T/F: Aggregation is sometimes described as a has-a relationship.

Answer: True Explanation: Aggregate objects are made up of other objects. Therefore the relationship between an object that is an aggregate of other objects is often described as a has-a relationship.

A method that has multiple definitions is an __________________ method. a) overloaded b) overridden c) overlooked d) overclocked e) none of the above

Answer: a Explanation: A method that has multiple definitions is an overloaded method. The versions of an overloaded method are distinguished by the number, type and order of their parameters. Overridden methods are methods that have been redefined later in an inheritance hierarchy. They will be studied in more detail later. Choice c and d are not types of methods in Java.

A _______________ variable is shared among all instances of a class. a) static b) final c) public d) private e) none of the above

Answer: a Explanation: A static variable is shared among all instances of a class. A final variable is a constant, a public variable is one that is accessible from outside the object and a private variable is one that cannot be accessed outside of the object.

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

Answer: a Explanation: Encapsulation is the object-oriented principle that specifies that an objects data should be guarded from inappropriate access. Therefore choice a is correct. Inheritance and polymorphism are features of object-oriented programming that allow for class flexibility and re-use. Instance variables and methods play important roles in object-oriented programming, but are not fundamental principles.

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

Answer: 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.

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

Answer: b Explanation: A UML diagram helps us visualize the contents and relationships among the classes of a program. The other choices do not refer to any type of diagram.

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

Answer: b Explanation: An aggregate object is one that is made up of other objects. Choice a, d and e do not refer to types of objects. Encapsulated objects may be made up of primitive types or object types

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

Answer: c Explanation: Methods that can be called directly through the class name must be declared as static. Choice b and choice d are visibility modifiers for a method. Methods declared as final cannot be overridden.

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

Answer: c Explanation: Private support methods are useful when a service is too complex to be defined in a single method. Therefore choice c is correct.

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

Answer: d Explanation: An algorithm is a step-by-step solution for solving a problem. A UML diagram is a way of visually representing how classes and objects interact. An aggregate object is an object that is composed, in part, of other objects. A class can be thought of as a blueprint for a set of objects.

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

Answer: 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.

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

Answer: d Explanation: Methods that do not need to return any data should have void specified as the return type. A method cannot have public specified as its return type, so choice b is incorrect. Choice a and choice c specify a return type, and therefore they must return data of that type.

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

Answer: 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.

When applied to instance variables, the ________________ visibility modifier enforces encapsulation. a) static b) final c) public d) private e) none of the above

Answer: d Explanation: The private visibility modifier guards against inappropriate data access, and therefore promotes encapsulation. Choices a and b are not visibility modifiers, and choice c is a visibility modifier that allows public access to an objects data, which violates the principle of encapsulation.

The ________________ reference always refers to the currently executing object. a) null b) static c) final d) actual e) this

Answer: e Explanation: The this reference always refers to the currently executing object. A null reference is a reference that is not pointing to any object. The other three choices are not special references in Java.

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.

public boolean isPalindrom(String shit){ String shit2 = ""; for (int i=shit.length(); i>0; i--){ shit2 += shit.charAt(i-1); } return shit.equals(shit2); }

Write a method called maxOfThree that accepts three integer parameters and returns the largest of the three.

public int maxOfThree (int a, int b, int c) { int largest = a; if (largest < b) largest = b; if (largest < c) largest = c; return largest; }

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.

public int randomInRange(int a, int b) { if(b < a) { System.out.println("Error, invalid range!"); return 0; } return generator.nextInt(b - a + 1) + a; }

Write a method that prints your name, age, and major. The method should accept no parameters and return no value.

public void myInfo() { System.out.println("Name:\tJohn Smith"); System.out.println("Age:\t30"); System.out.println("Major:\tBasket Weaving"); }


Ensembles d'études connexes

Chapter 16 - Nursing Management During the Postpartum Period

View Set

Health Insurance Policy Provisions CH. 6

View Set

Chapter 2 - Population and Health

View Set

Chapter 32 ebook quiz, Module 7 Review (eBook Quizzes)

View Set

Reading 4: Introduction to the Global Investment Performance Standards (GIPS)

View Set

Chapter 7: Innovation and Entrepreneurship

View Set