Download presentation
Presentation is loading. Please wait.
Published byErnest Sherman Modified over 8 years ago
1
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539 xiang.lian@utrgv.edu
2
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 2
3
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 3
4
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 , 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
5
Debugging Errors int G() { Console.WriteLine("Inside method G"); void H() { Console.WriteLine("Inside method H"); } 5
6
Debugging Errors (cont'd) void F(float a) { float a; Console.WriteLine(a); return a; } int G(int x) { return x*G(x-1); } 6
7
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
8
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 a n, n >= 3, of this sequence is given by: a n = a n-1 + a n-2 8
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.