What can you say about the name of a class?
# It must start with an uppercase letter.
# The convention is to start with an uppercase letter.
` # The convention is to start with an uppercase letter.
1/37
Term | Definition |
---|---|
What can you say about the name of a class? # It must start with an uppercase letter. # The convention is to start with an uppercase letter. | ` # The convention is to start with an uppercase letter. |
` What can you say about the name of constructors? # They must be the same name as the class name. # They can be any name, just like other methods. | # They must be the same name as the class name. |
` What is a constructor’s return type? # void # Object # The class name # A constructor does not have a return type. | # A constructor does not have a return type. |
` It is legal to have more than one constructor in a given class. # true # false | # true |
` In a class, if a field is private # it can be accessed directly from any class. # it can be accessed directly only from inside its class | # it can be accessed directly only from inside its class |
` In a typical class, what is the general recommendation for access modifiers? # Instance variables are private and methods are private. # Instance variables are private and methods are public. # Instance variables are public and methods are private. # Instance variables are public and methods are public. | # Instance variables are private and methods are public. |
` In a class, fields # can only be basic data types. # can only be basic data types or existing Java types (from existing classes). # can be basic data types, existing Java types, or user-defined types (from user-defined classes). | # can be basic data types, existing Java types, or user-defined types (from user-defined classes). |
8. Accessors and mutators are # instance variables of a class. # used to access and modify field variables of a class from outside the class. # constructor methods | # used to access and modify field variables of a class from outside the class. |
9. Accessor methods typically take # no parameter. # one parameter, of the same type as the corresponding field. | # no parameter. |
Mutator methods typically take # no parameter. # one parameter, of the same type as the corresponding field | # one parameter, of the same type as the corresponding field |
Accessor methods typically # are void methods. # return the same type as the corresponding field. | # return the same type as the corresponding field. |
To enable method chaining, mutator methods # return a reference to this object. # return the same type as the corresponding field. | # return the same type as the corresponding field. |
When coding a method that performs calculations on fields of that class, # these fields must be passed as parameters to the method. # these fields do not need to be passed as parameters to the methods because the class methods have direct access to them. | # these fields do not need to be passed as parameters to the methods because the class methods have direct access to them. |
What is the keyword used for declaring a constant? # static # final # constant | # final |
15. What is the keyword used for declaring a class variable or method? # static # final # class | # class |
What can you say about enum # It is part of the package java.lang # It can be used for self-documentation, improving the readability of your code. # An enum object is a constant object. # All of the above | # All of the above |
public class Sky { private Color color; public Sky ( Color c ) { color = c; } } 17. Consider the following method header: public Color getColor( ) Is this method a constructor, mutator, or accessor? | accessor |
public class Sky { private Color color; public Sky ( Color c ) { color = c; } } 18. Consider the following method header: public void setColor ( Color c ) Is this method a constructor, mutator, or accessor? | mutator |
public static double foo1(String s) public String foo2(char c) 19. What is the return type of method foo1 | double |
public static double foo1(String s) public String foo2(char c) 20. What is the return type of method foo2 | String |
public static double foo1(String s) public String foo2(char c) 21. Is method foo1 a class or instance method? Explain. | class, you need to use th class name to use this method |
public static double foo1(String s) public String foo2(char c) 22. Is method foo2 a class or instance method? Explain. | instance, you will need to reference an instantaited object to use this class |
Airplane class public static double foo1(String s) public String foo2(char c) 23. Write a line or two of code to call method foo1 from a client class. | Airplane.foo1("word"); |
Airplane class public static double foo1(String s) public String foo2(char c) 24. Write a line or two of code to call method foo2 from a client class. Assume we have instantiated an object named a1. | a1.foo2('f'); |
25. Inside method main, we see code like Airplane.foo3 ( 34.6 ); From this, reconstruct the header of method foo3 (which belongs to the class Airplane); make appropriate assumptions if necessary. | public static double setFoo3(double number){ return this.number = number; } |
26. Inside method main, we see code lik Airplane a = new Airplane( ); int n = a.foo4 ("Hello"); From this, reconstruct the header of method foo4 (which belongs to class Airplane). | public int foo4(String word) { return word.length(); } |
27. If you have defined the following enum constants enum Seasons { Winter, Spring, Summer, Fall }; what is the output of the following code sequence? System.out.println(Seasons.Spring.ordinal()); | 1 |
28. Declare two instance variables: grade, which is an integer, and letterGrad, which is a char. | - int grade - char letterGrade private int grade = 0; private char letterGrade; |
29. Declare a class field for a federal tax rate, a constant, with value .07. // declare federal tax rate constant; value is 0.07 | static final double FED_TAX_RATE = 0.07; |
Assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel. 30. Code a default constructor for that class: initialize the fields to an empty string, 0, and false respectively. | public TelevisionChannel() {} |
Assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel. 31. Code a constructor for that class that takes three parameters. // your constructor code goes here | public TelevisionChannel(String name, int number, boolean cable) { //ideally I would also validate this.name = name; this.number = number this.boolean = cable; } |
Assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel. 32. Code the three accessors for that class. | public getName(){ return name; } public getNumber(){ return number; } public getCable(){ return cable; } |
Assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel. 33. Code the three mutators for that class. | public setName(String name){ this.name = name; } public setNumber(int number){ this.number=number; } public setCable(boolean cable){ this.cable = cable; } |
Assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel. 34. Code the toString method. | public toString(){ return name + ":" + number + " Cable: " + cable; } |
Assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel. 35. Code the equals method. | public equals(TelevisionChannel object){ if((this.name == object.name)&&(this.number == object.number) && (this.cable == object.cable)){return true;} else{return false;} } |
Assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel. 36. Code a method returning the number of digits in the channel number. For instance, if the channel number is 21, the method returns 2; if the channel number is 412, the method returns 3. | public channelNumberLength(){ int length = String.valueOf(this.number); return length; } |
Assume that class TelevisionChannel has three fields: name, a String; number, an integer; and cable, a boolean, which represents whether the channel is a cable channel. 37. Code a method returning the word cable if the current object represents a cable channel and returning the word network if the current object does not represent a cable channel. | public channelOrNetwork(){ if(this.cabel = true) return "Cable"; else if(this.cable = false) return "Network"; } |