C# Chapter 4

Ace your homework & exams now with Quizwiz!

(True/False) You should think of a C# class's attributes as instance variables.

False

If the following is a constructor public Account(string accountName) { Name = accountName; } Then what's the name of the class? a) Account b) accountName c) Name d) None of the above.

a) Account

Which of the following statements is false? a) Each class you declare must provide a constructor with parameters that can be used to initialize an object when it's created. b) C# requires a constructor call for every object that's created, so this is the ideal point to initialize an object's instance variables. c) A constructor's identifier must be the class's name. d) When you declare a class, you can provide your own constructor to specify custom initialization for objects of your class.

a) Each class you declare must provide a constructor with parameters that can be used to initialize an object when it's created.

Which of the following statements is false? a) Each object of a class shares one copy of the class's instance variables. b) Each class declaration is typically stored in a file having the same name as the class and ending with the .cs filename extension c) Class, property and method names begin with an initial uppercase letter (i.e., Pascal case); variable names begin with an initial lowercase letter (i.e., camel case). d) A class has attributes, implemented as instance variables. Objects of the class carry these instance variables with them throughout their lifetimes.

a) Each object of a class shares one copy of the class's instance variables.

Which of the following statements is false? a) Set methods can be programmed to validate their arguments and reject any attempts to set the data to bad values b) A Get method always presents the data in the form in which it's stored. c) Tightly controlling the access to and presentation of private data can greatly reduce errors, while increasing the robustness, security and usability of your programs. d) All of the above are true.

b) A Get method always presents the data in the form in which it's stored.

Which of the following statements is false? a) Variables declared in a particular method's body (such as Main) are local variables, which can be used only in that method. b) Each method can access its own local variables and those of other methods. c) When a method terminates, the values of its local variables are lost. d) A method's parameters also are local variables of the method.

b) Each method can access its own local variables and those of other methods.

Consider the code: Console.WriteLine($"Initial name is: {myAccount.GetName()}"); Which method is called first? a) WriteLine b) GetName c) They execute at the same time. d) None of the above.

b) GetName

Which of the following statements is false? a) Normally, a class also contains methods and properties. These manipulate the instance variables belonging to particular objects of the class. b) Instance variables are declared inside the bodies of the class's methods and properties. c) Clients of a class cannot access the class's private instance variables. d) Clients of a class can access the class's public method

b) Instance variables are declared inside the bodies of the class's methods and properties.

Which of the following statements is false? a) It's common to see property definitions where the get accessor simply returns private instance variable name's value and the set accessor simply assigns a value to the instance variable—no other logic appears in the accessors. For such simple cases, C# provides auto- implemented properties. b) With an auto-implemented property, the C# compiler automatically creates a public instance variable, and the get and set accessors for getting and setting that instance variable c) With an auto-implemented property you can implement the property trivially, which is handy when you're first designing a class. d) If you later decide to include other logic in the get or set accessors, you can simply implement the property and an instance variable.

b) With an auto-implemented property, the C# compiler automatically creates a public instance variable, and the get and set accessors for getting and setting that instance variable

Type ________ is designed to precisely represent numbers with decimal points, especially monetary amounts. a) digit b) decimal c) float d) double

b) decimal

By default, everything in a class is ________, unless you specify otherwise by providing access modifiers. a) public b) private c) protected d) None of the above.

b) private

Which of the following statements is false? a) A decimal instance variable is initialized to zero by default. b) By default, a property's get and set accessors have the same access as the property. c) A property's get and set accessors must have the same access modifiers. d) We can declare a Balance property's set accessor private to indicate that it may be used only within its class, but not by the class's clients.

c) A property's get and set accessors must have the same access modifiers.

Which of the following statements about the C format specifier is false. a) It formats the string as currency. b) It includes an appropriate currency symbol ($ in the U.S.) next to the number. c) It separates digits with an appropriate separator character (in the U.S. its a comma between every digit.) d) It sets the number of decimal places to two by default.

c) It separates digits with an appropriate separator character (in the U.S. its a comma between every digit.)

Keyword get is a ________ keyword, because it's a keyword only in a property's body—elsewhere, get can be used as an identifier. a) restricted b) dependent c) contextual d) contingent

c) contextual

Most instance variables are declared ________. a) public b) protected c) private d) None of the above.

c) private

Which of the following properly declares an auto-implemented Name property of type string? a) public string Name { get, set } b) public string Name { get, set, } c) public string Name { get; set; } d) public string Name { get: set: }

c) public string Name { get; set; }

Consider the code: public void SetName(string accountName) { name = accountName; // store the account name } Which of the following statements is false? a) The first line of each method declaration is the method header. b) The method's return type (which appears to the left of the method's name) specifies the type of data the method returns to its caller after performing its task. c) The return type void indicates that when SetName completes its task, it does not return any information to its calling method. d) A method requires one or more parameters that represent the data it needs to perform its task.

d) A method requires one or more parameters that represent the data it needs to perform its task.

Consider the code (in class Account): public string GetName() { return name; } Which of the following statements is false? a) The method returns a particular Account object's name to the caller— a string, as specified by the method's return type. b) The method has an empty parameter list, so it does not require additional information to perform its task. c) When a method with a return type other than void is called and completes its task, it must return a result to its caller. d) All of the above are true.

d) All of the above are true.

Consider the code: myAccount.Name = theName; which assigns the string theName to myAccounts's Name property. Which of the following is false regarding when property Name is invoked by the expression myAccount.Name on the left of an assignment? a) The app transfers program execution to Name's set accessor. b) Property Name's set accessor performs its task—that is, it stores in the myAccount object's name instance variable the string value that was assigned to property Name. c) When Name's set accessor completes execution, program control returns to where the Name property was accessed, then execution continues at the next statement. d) All of the above are true.

d) All of the above are true.

Consider the code: myAccount.SetName(theName); Which of the following statements is false? a) When this method executes, the argument value in the call's parentheses (i.e., the value stored in theName) is copied into the corresponding parameter in the method's header. b) Each parameter must specify a type followed by a parameter name. When there are multiple parameters, they are placed in a comma- separated list. c) The number and order of arguments in a method call must match the number and order of parameters in the method declaration's parameter list. d) All of the above are true.

d) All of the above are true.

Which of the following statements is false? a) Set and Get methods can validate attempts to modify private data and control how that data is presented to the caller, respectively. b) If an instance variable were public, any client of the class could see the data and modify it, including setting it to an invalid value. c) public data allows client-code programmers to write code that depends on the class's data format. If the class's owner changes that format, any client code dependent on it would "break" and would need to be adjusted to the new format, making it subject to break again. d) All of the above are true.

d) All of the above are true.

Which of the following statements is false? a) In any class that does not explicitly declare a constructor, the compiler provides a public default constructor (which always has no parameters). b) When a class has only the default constructor, the class's instance variables are initialized to their default values: 0 for numeric simple types, false for simple type bool and null for all other types. c) If you declare one or more constructors for a class, the compiler will not create a default constructor for that class. d) If the compiler does not create a default constructor for an Account class, you will not be able to create an Account object with the expression new Account().

d) If the compiler does not create a default constructor for an Account class, you will not be able to create an Account object with the expression new Account().

Which of the following statements about creating, compiling and running a Visual C# project with two classes is false? a) The IDE automatically recognizes as the app's entry point the class that contains Main. b) When you select Build > Build Solution in Visual Studio, the IDE compiles all the files in the project to create the executable app. c) If you do not build the app before running it, typing Ctrl + F5 will build the app first and run the app only if there are no compilation errors. d) In a given project, declaring a Main method in more than exactly one class results in a runtime error.

d) In a given project, declaring a Main method in more than exactly one class results in a runtime error.

The fact that we could create and manipulate an Account object without knowing its implementation details is called ________. This is one of the most powerful software-engineering benefits of object-oriented programming. a) inheritance b) shadowing c) overriding d) abstraction

d) abstraction

Each class you create becomes a new type you can use to create objects, so C# is a(n) ________ programming language. a) type sensitive b) flexible c) elastic d) extensible

d) extensible


Related study sets

Cloud & IOT Fundamentals: Chapter 1

View Set

Genetics and Inheritance Final Review

View Set

Bloque III: Bioética y su relación con la vida humana

View Set

Sole - Ch 13 Cardiovascular Alterations

View Set

Chapter 1 Study Guide - Political Culture, People & Economy of Texas

View Set

Educational Psychology Test 3 (Ch. 7, 8, 9)

View Set