CSCI 3328 Object Oriented Programming in C Chapter
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa. edu 1
Multiple Choices • A method is invoked with a(n) _____. – A. method call B. method declaration C. class declaration call D. class • A variable known only within the method in which it is declared is called a(n)_____. – A. global variable B. local variable C. random variable D. private • The statement of _______ in a called method can be used to pass the value of an expression back to the calling method. – A. assigning value to parameters passing by values B. break C. return D. continue • The keyword ____ indicates that a method does not return a value. – A. return B. public C. private D. void • Data can be added to or removed from the ______ of a stack. – A. bottom B. top C. middle D. random position 2
Multiple Choices (cont'd) • An object of class ______ produces pseudorandom numbers. – A. Math B. random. Num C. Random D. random. Number • Assuming random. Numbers is an object for generating pseudorandom numbers, which of the following statements generates a random number between 1 and 4 (inclusive)? – – A. random. Numbers. Next(4)+1; B. random. Numbers. Next(0, 5); C. random. Numbers. Next(1, 4); D. random. Numbers. Next(); • A method that calls itself either directly or indirectly is a(n) _____method. – A. static B. dynamic C. public D. recursive 3
True/False Statements • The following two methods can exist in the same class: – void Method 1(int x, float y); – int Method 1(int x, float y); • If we want to use the constant p, we can write Math. pi in the C# program. • It holds that method Math. Floor(4. 5) returns value 4. • It holds that method Math. Ceiling(4. 0) returns value 4. 4
Debugging Errors int G() { Console. Write. Line("Inside method G"); void H() { Console. Write. Line("Inside method H"); } } 5
Debugging Errors (cont'd) void F(float a) { float a; Console. Write. Line(a); return a; } int G(int x) { return x*G(x-1); } 6
Write a Program • Write a method in C# to return the larger value of two decimal numbers. • Write another program in C# to return the largest value among 3 decimal numbers, by invoking the method above. 7
Write a Program (cont'd) • Write a recursive method in C# to compute the Fibonacci number: – 1, 1, 2, 3, 5, 8, 13, 21, 34, . . – Given the first two numbers of the sequence (say, a 1 and a 2) – n-th number an, n >= 3, of this sequence is given by: an = an-1 + an-2 8
- Slides: 8