Unit 5 Exam – Alternative Version – Solutions Quizlet

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

17. Which of the following is true about a constructor? a. It has no return type. b. It must have a different name than the class. c. It must be declared private. d. It cannot initialize the instance variables of the class. e. Classes can only have one constructor - the default constructor.

a. It has no return type. ➜ Unlike any other method, constructors should not have any return type specified.

20. Consider the following class. public class myClass { public myClass(int arg1) { /* implementation not shown */ } public myClass(String arg1) { /* implementation not shown */ } public myClass(String arg1, int arg2) { /* implementation not shown */ } } Which of the following additional constructors can be added to myClass without causing a compile-time error? I. public myClass(String alt1) { /* implementation not shown */ } II. public myClass(String alt1, int alt2) { /* implementation not shown */ } III. public myClass(int alt1) { /* implementation not shown */ } a. None of these additional constructors can be added without causing an error b. I only c. II only d. III only e. I, II and III

a. None of these additional constructors can be added without causing an error ➜ This is correct. All of these have the same number, type and order of arguments of constructors that already exist.

16. Consider the following methods. public int manipulate(boolean b, int n) { if (!b) { return n + 1; } else { return n - 1; } } public void tester() { boolean a = true; int x = 17; x = manipulate(a, x); System.out.println("a = " + a + ", x = " + x); } What is printed by the call tester()? a. a = true, x = 16 b. a = true, x = 18 c. a = false, x = 16 d. a = false, x = 18 e. a = true, x = 17

a. a = true, x = 16 ➜ This is correct. The values of a and x are passed to the method and if !b is true (b is false) then the if runs, or else the else runs. Since b is true, !true is false and so else runs and 1 is subtracted from n and returned back to the tester () method and stored to x. Throughout this, a never changes and x is decreased by one. So a = true and x = 16

8. Consider the following method maximum, which is intended to return the largest of three integers. public static int maximum(int a, int b, int c) { if ((a > b) && (a > c)) { return a; } else if (/* missing condition * ) { return b; } else { return c; } }

a. b > c This is correct. If the condition (a > b) && (a > c) is not satisfied then a <= b or a <= c must be true. To check which one is the larger one, we can use b > c. Now if this is true, the code returns b or else c.

19. Consider the following class declaration. public class Car { private String year; private String color; public Car(String y, String c) { year = y; color = c; } public String getYear() { return year; } public void setYear(String y) { year = y; } // There may be instance variables, constructors, // and methods that are not shown. } Assume that the following declaration has been made. Car myRide = new Car("2016", "Blue"); Which of the following statements is the most appropriate for changing the year of myRide from "2016" to "2020"? a. myRide.getYear("2020"); b. myRide.setYear("2020"); c. myRide.year = "2020"; d. Car.setYear("2020"); e. Car.year = "2020";

b. myRide.setYear("2020"); ➜ This is correct. This statement calls the setYear procedure on myRide with a value of "2020". The setYear procedure can be accessed as it is public, and its effect is to set the instance variable year for that object to whatever value is passed as a parameter. Therefore this command will set the value of year for myRide to "2020" as desired.

18. You have written a class called House. In a second program you have the line: House.printType(); Which of the following must be true about the method printType() for the method call to work? a. printType() must be void b. printType() must be declared static. c. printType() must return a value. d. printType() must be declared private. e. Nothing, this method call is not legal and will cause an error.

b. printType() must be declared static. ➜ As this method is called on the class directly and not an object, it must be static.

10. The default constructor is meant to set x and y to 0 and 0 by calling the second constructor. What could be used to replace /* missing code */ so that this works as intended? a. this(x, y); b. this(0, 0); c. this(a, b); d. a = x; b = y; e. x = a; y = b;

b. this(0, 0); ➜ This will properly call the constructor and set x and y to 0

6. Consider the class below: public class Stuff { public Stuff() { System.out.print("one"); } public Stuff(int x) { System.out.print("two"); } public Stuff(double x) { System.out.print("three"); } }

b. two The constructor with an int argument is called and no others. Therefore "two" is printed.

4. Consider the complete class definition below: public class Dice { public static void rollIt() { /* Missing Code */ } } Which of the following is the correct way to call the function rollIt() from another class? a. Dice d = new Dice(); d.rollIt(); b. Dice d = new Dice(); rollIt(d); c. Dice.rollIt(); d. rollIt(); e. None of the options listed.

c. Dice.rollIt(); Correct; rollIt is a static method, so we should call it on the class directly. It has no parameters.

11. Which of the following correctly implements a mutator method for point? a. public int getX() { return x; } b. public int getX() { return a; } c. public void setCoordinates(int a, int b) { x = a; y = b; } d. public String getCoordinates(int a, int b) { return x + " " + y } e. None of these are mutator methods

c. public void setCoordinates(int a, int b) { x = a; y = b; } This sets the values of the instance variables x and y to a and b respectively.

3. Consider the following class declaration. public class Address { private int myNumber; private String myStreet; private int myZip; public Address() { /* implementation not shown */ } public Address(int zip) { /* implementation not shown */ } public Address(int number, String street) { /* implementation not shown */ } public Address(int number, String street, int zip) { /* implementation not shown */ } // No other constructors } Which of the following declarations will cause a compilation error? a. Address location = new Address(); b. Address location = new Address(90210); c. Address location = new Address(9147, "East Springfield Road"); d. Address location = new Address(90210, 9147, "East Springfield Road"); e. Address location = new Address(9147, "East Springfield Road", 90210);

d. Address location = new Address(90210, 9147, "East Springfield Road"); This is correct. This declaration has two int arguments and then a String argument. None of the constructor methods for the class matches this format (the order of the arguments is important). Therefore this will cause an error when it attempts to compile.

13. The class PickColor is defined as shown below. public class PickColor { private String color; private int num; public PickColor(String c, int n) { color = c; num = n; } public void setColor(String c) { color = c; } public void setNum int n) { num = n; } public String toString() { return c + " " + n; } } The following methods appear in a separate class. public static void changeAll(Pickcolor x) { x.setColor("Green"); x.setNum(255); } public static void main(String[] args) { PickColor col = new PickColor("Blue", 53); changeAll(col); System.out.println(col); } What is printed when the main method is run? a. Blue 53 b. Blue 255 c. Green 53 d. Green 255 e. Nothing is printed, an error occurs.

d. Green 255 ➜ The variable col refers to an object of the PickColor class data type. So the reference to the object is passed to the parameter x. Changes made to x in the method (i.e. changes to the member variables) are made to the same object referred to by col.

5. Consider the following variables and method representing a student. public class Student { private double gpa; private int gradeLevel; public boolean nhs() { /* Missing Code */ } } A student can apply for the National Honor Society if their GPA is 3.75 or above, and they are in the 11th or 12th grade. Which of the following correctly replaces /* missing code */ so that the method works as intended? I. if ((gpa >= 3.75) && ((gradeLevel == 11) || (gradeLevel == 12))) { return true; } return false; II. boolean pass = false; if (gpa >= 3.75) { if ((gradeLevel == 11) || (gradeLevel == 12)) { pass = true; } return pass; III. if ((gpa >= 3.75) || ((gradeLevel == 11) || (gradeLevel == 12))) { return true; } return false;

d. I & II This is correct - both code segments will do what is intended.

14. Consider the following code: public static int number(int a) { int x = 1; while (x < a) { x = -2 * x; a++; } return x; } Which of the following is true about the behavior of method number? a. The method will never return a number as it always results in an infinite loop. b. The method will result in an infinite loop for argument values larger than 1000000, but when a in smaller than that, the loop does return a number c. The method will result in an infinite loop for arguments that are negative, but when a is originally positive, the loop does end and a value is returned d. The method will always return a number and never result in an infinite loop. e. The method will always return the same number, regardless of the argument passed to it.

d. The method will always return a number and never result in an infinite loop. ➜ Eventually, the value of x does become greater than a regardless of how large it is

15. Consider the following method that is intended to determine if the int values num1 and num2 are farther than a distance of 10 from each other. /** * @param num1 an int to be tested * @param num2 another int to be tested * @return true if the difference between num1 and num2 is larger than 10, and * false otherwise */ public boolean farEnough(int num1, int num2); { return (num1 - num2) > 10; } Which, if any, of the following procedure calls will demonstrate that this procedure is not working as intended? a. farEnough(100, 90) b. farEnough(200, 10) c. farEnough(100, 110) d. farEnough(50, 70) e. The procedure works exactly as intended. None of these will demonstrate that it is not working.

d. farEnough(50, 70) ➜ These two ints are farther than 10 from each other, but the return value is false, which is incorrect. The issue is when num2 is larger than num1, sometimes there is an error. Taking the abs() of num1 - num2 would have solved this problem.

1. What would be the header for the default constructor for a class called Fruit. a. private void Fruit() b. public int Fruit() c. private Fruit() d. public Fruit() e. public void Fruit()

d. public Fruit() The constructor is public, has the same name as the class and has no return type

9. Which of the following correctly implements the equals method, which is to check to make sure that each Point has the same x and y values? public class Point { private int x; private int y; public Point() { /* missing code */ } public Point(int a, int b) { x = a; y = b; } public int getX() { return x; } public int getY() { return y; } // ... other methods not shown }

d. public boolean equals(Point p) { return (x == p.getX() && y == p.getY()); } This method has a boolean return type and returns true if the x and y variables are the same for this point as they are for point p, false otherwise.

12. When defining a class, which of the following should NOT be declared as public? a. constructors b. accessors c. mutators d. variables

d. variables ➜ Variables in a class should be set to private so access to them can be controlled through the use of accessor and mutator methods

2. Which of the following is true about designing classes. a. All methods should be declared private. b. There is no need to create an equals() method, because by default one is provided. c. There is no need to create a toString method, because by default Java knows what to print and in the correct format d. All class variables should be declared as public. e. There is no need to create a default constructor, because by Java automatically creates one, which declares all class variables as either Null, 0, 0.0 or false, depending on the type of variable.

e. There is no need to create a default constructor, because by Java automatically creates one, which declares all class variables as either Null, 0, 0.0 or false, depending on the type of variable. This is true although most of the time we want to write default constructor anyways so we can better specify the default values of each variable

7. Which of the following is the correct way to declare a static boolean variable named answer? a. boolean answer; b. public boolean answer; c. answer; d. public constant boolean answer; e. public static boolean answer;

e. public static boolean answer; Two modifiers are required before the variable type - one to state whether the variable is private or public, and one to declare it as static.


Kaugnay na mga set ng pag-aaral

Chapter 7: Life Span Development

View Set

Evolutionary Biology Terms Lectures 2-5 (Poulin), Evolutionary Biology Terms Lectures 11-14, Evolutionary Biology Terms Lectures 6-10, Evolutionary Biology Lectures 15-18, Evolutionary Biology Lectures 19-22, Evolutionary Biology Lectures 25-28, Evol...

View Set

Chapter 8: lesson 4 Indiana laws and rules

View Set

This set's title has been removed

View Set

Theory of Recombinant DNA Techniques

View Set

CYB 600 CHAPTER 1: Information Systems Security

View Set