Download presentation
Presentation is loading. Please wait.
1
Methods and Parameters
Peer Instruction 5 Methods and Parameters
2
Which of the method declarations shown below will compile ?
public static void method1(int i0, i1, i2, char c0) { ... } public static void method2(int i0, String s0, char c0, int i1) { ... } public static void method3(String s0, void, int i0, char c0) { ... } public static void method4(int, char, String, double) { ... } public static void method5(int i, int j = 10) { ... } must specify a data type per parameter is correct void is not a data type, used for no return value must have parameter names, otherwise how to access, however see this sometimes no way to make a default value for parameter, unlike C++ Formal Parameters cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
3
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
What can you infer about the data types of the formal parameters of someMethod ? someMethod(‘c’, 123, (float)2.5, "Hello", false, L); Parameters seem to be char, int, float, String, boolean, long. First parameter may be int, long, float, double instead. Second parameter may be long, float, double instead. Third parameter may be a double instead of a float. Last parameter may actually be a float or double! All of the above E) is correct, all the others are correct because actual parameters might get promoted! NOTE: short and char are not compatible, because short is signed and char is unsigned Formal Parameters cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
4
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
Which statement is a valid invocation of a method with and int and float parameter? myMethod(int i = 12, float f = 2.3); myMethod((int) 12, (float) 2.3f); myMethod(int i, float f); myMethod(int, float); myMethod(12.0, 2.3f); cannot specify data type, cannot have initializer is correct, but type casts are redundant cannot specify data type, compiler already knows is ridiculous, not actual parameters, just data types first parameter cannot be double, second is okay NOTE: actual parameters are those used to invoke method, also called arguments Actual Paramaters cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
5
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
Which line of code uses the integer return value from the method correctly? System.out.println(calculateInteger()); int myInteger = calculateInteger(); double myDouble = (5.0 * calculateInteger()) / ; double myDouble = Math.max(calculateInteger(), 1234); All of the above Answer is E), anywhere a literal or variable of type int can be used you can have an int return value. Return Values cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
6
What are the limitations of single return value from a method?
A single primitive (byte, int, float, double, char, boolean, ...) A single primitive or an array of primitives A single class, (String, Scanner, ...) A single class or an array of classes All of the above Answer is E), thus it’s not much of a limitation! Return Values cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
7
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
How can two different methods in a class read and write the same variable? Allow one method to reference a local variable of the other Declare a variable of the same name in both methods Add the variable as a class or instance variable Pass the variable as a parameter between methods None of the above exactly what you cannot do fine, but they totally different variables is correct, that works D) would have to return the value as well E) not an option Return Values cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
8
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
Given the code below, what is output by the two print statements, in order of execution? // Code fragment int value = 6; printSquare(value); System.out.println(value); public static void printSquare(int value) { value = value * value; } 6, 6 36, 6 36, 36 6, 36 None of the above Answer is B), value variables are completely difference memory locations, parameter is squared but not returned NOTE: Naming the actual and formal parameters the same is misleading! Pass by Value cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
9
cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
How many activation records are on the stack when executing code in Math.sin? // code fragment in main foo(1.0); public static void foo(double d) { d += bar(d * d) ; } public static void bar(double d) { d *= Math.sin(Math.PI); 1 2 3 4 E) is correct: main, myMethod0, myMethod1, Math.sin What if bar called foo? What of bar called bar? Pass by Value cs163/164: Peer 5 - Methods and Parameters - Fall Semester 2016
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.