80 likes | 107 Views
Test your knowledge of C# methods with multiple choice questions and true/false statements in this exercise chapter. Practice method invocation, declaration, passing parameters, and more.
E N D
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
Multiple Choices • A method is invoked with a(n) __________. • A. method call B. method declaration C. class declaration D. class call • 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 variable • 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
Multiple Choices (cont'd) • An object of class ______ produces pseudorandom numbers. • A. Math B. randomNum C. Random D. randomNumber • Assuming randomNumbers is an object for generating pseudorandom numbers, which of the following statements generates a random number between 1 and 4 (inclusive)? • A. randomNumbers.Next(4)+1; • B. randomNumbers.Next(0, 5); • C. randomNumbers.Next(1, 4); • D. randomNumbers.Next(); • A method that calls itself either directly or indirectly is a(n) __________method. • A. static B. dynamic C. public D. recursive
True/False Statements • The following two methods can exist in the same class: • void Method1(int x, float y); • int Method1(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.
Debugging Errors int G() { Console.WriteLine("Inside method G"); void H() { Console.WriteLine("Inside method H"); } }
Debugging Errors (cont'd) void F(float a) { float a; Console.WriteLine(a); return a; } int G(int x) { return x*G(x-1); }
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.
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, a1 and a2) • n-th number an, n >= 3, of this sequence is given by: an = an-1 + an-2