Question: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication22 { //an example introducing the application of this keyword class MyClass { int

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication22 { //an example introducing the application of "this" keyword class MyClass { int var1 = 10; //an instance field static

int var2=10; //a static field

string var3 = ""; /* * inside of a instance method or a static method, a statement can access either a static * field or an instance field, such as MaxVarA(int var1) and MaxVarB(int var2) */ public int MaxVarA(int var1) { //in a instance method, we need to //compare parameter var1 and the instance field var1 //in this case, we need to use the key word this if (var1 > this.var1) return var1; else return this.var1; } public int MaxVarB(int var2) { //in a instance method, we need to //compare parameter var2 and the static field var2. //in this case, we need to use class name, MyClass.var2 to access the global variable if (var2 > MyClass.var2) return var1; else return MyClass.var2; } /* * a static method can only access static fields. it cannot access an instance field */

public static int MaxVarC(int var1) { //in a static method, we cannot //compare parameter var1 and the instance field var1 //in this case, we cannoly compare the parameter var1 and the instance field var2 if (var1 > MyClass.var2) //it will not work like: if(var1>this.var2) return var1; else return MyClass.var2; //it will not work like: return this.var2 } public MyClass() { var1 = 12; var3 = "hello world!"; } public MyClass(string str) { var1 = 19; var3 = str; } public MyClass(int ivar1) { var1 = ivar1; var3 = "how are you?"; } public MyClass(int ivar1, string str) { var1 = ivar1; var3 = str; } ~MyClass() { Console.WriteLine("this is from destructor, var3={0}", var3); } } public class MyClassDemo { static void Main(string[] args) { int varM = 20; int maxM = 0;

1. How many instance constructors does it have? a. 1 b. 2 c. 3 d. 4

2. How many destructors does it have? a. 1 b. 2 c. 3 d. 4

3. If you want to create an instance of the class MyClass, which statement is correct? a. MyClass MA = new MyClass(2.5); b. MyClass MA = new MyClass(2.5, hello); c. MyClass MA = new MyClass(how are you?); d. MyClass MA = new MyClass(23, csc101);

4. Which statement is a correct statement about destructor? a. A destructor must have at least one parameter. b. A class has at least two destructors. One is a default destructor, and the other is defined by the programmer explicitly. c. A destructor has no access modifier. d. When an instance is done, its destructor must be called explicitly so that it can release the memory occupied by the instance. 5. Which statement is a correct statement calling the static method? a. maxM = MyClass.MaxVar(30); b. maxM = MyClass.MaxVar(hello); c. maxM = MyClass.MaxVar(); d. maxM = MyClass.MaxVar(30, hello); 6. Which statement is a correct statement about this keyword a. this keyword is a reference to the current instance. b. this keyword can be used in static methods. c. this keyword can only be used for a static class. d. this keyword can be used for instance methods.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!