Warm-up Exercises
- Consider the following partial class definition:
- Write a statement that would create a
Book
object. - Write a “getter” and a “setter” for the
title
attribute. - Write a constructor for the
Book
class taking at least one argument.
Solution for Part 1
Solution for Part 2
Solution for Part 3
Questions
- How do you make reference to a public property Name outside of the class.
- *Name
- +Name
- .Name
- neither of these
- In C#, you should think of the class’s properties as the class’s attributes.
- Yes
- No
- The property notation allows the client to directly manipulate the private instance variable.
- Yes
- No
- Consider the code:
Which of the following statements is false? - [ ] The first line of each method declaration is the method header. - [ ] The method’s return type specifies the type of data the method returns to its caller after performing its task. - [ ] The return type void indicates that when SetName() completes its task, it does not return any information to its calling method. - [x] All methods require at least one parameter to provide data to perform tasks.
- A return type of _____ is specified for a method that does not return a value.
- int
- double
- void
- None of the above.
- Methods are called by writing the name of the method followed by _____ enclosed in parentheses.
- a condition
- argument(s)
- a counter
- None of the above.
- The parameter list in the method header and the arguments in the method call must agree in:
- Number
- Type
- Order
- All of the above
- Suppose method1 is declared as
Which of the following methods does not overload method1? - [x] void method2(int a, float b, char c) - [ ] int method1(float a, int b, string c) - [ ] float method1(int a, float b) - [ ] string method1(string a, float b, int c)
- Write a get method for an instance variable named
total
of type int.
Solution
- Write a getter for an attribute of type string named
myName
.
Solution
- Write a setter for an attribute of type int named
myAge
.
Solution
- Assuming name is a string instance variable, there is a problem with the following setter. Fix it.
Solution
- Is it possible to have more than one constructor defined for a class? If yes, how can C# know which one is called?
Solution
Yes, C# can identify which constructor is called based on that constructor’s method signature, that is, the combination of parameters associated with it.
- What is the name of a constructor method? What is the return type of a constructor?
Solution
The name of a constructor method is the name of the class that contains it, and a constructor’s return type is the class that contains it.
- Write a constructor for a
Soda
class with one string attribute calledname
.
Solution
- What is the “default” constructor? Do we always have the possibility of using it?
Solution
The default constructor is one without any parameters. The only case in which it may not be called is if it has not been explicitly defined while other constructors have been defined.
- Why would one want to define a constructor for a class?
Solution
By defining a constructor for a class, one can specify which values to assign to the instance variables upon instantiation.