Docsity

5 Unsolved Problems on Java Programming - Quiz 9 | COP 2250, Quizzes of Javascript programming

Toggle sidebar
bg1
Quiz 9
Name: Panther ID:
Please use 10 minutes to finish this quiz.
1. Consider the following program.
public class Utility
{
public static void main(String []args)
{
System.out.println(Utility.average(7, 8));
System.out.println(Utility.average(7.0, 8));//7.0f
}
public static float average(int num1, int num2)
{
return (num1 + num2)/2.0f;
}
public static float average(float num1, int num2)
{
return (num1 + num2)/2.0f;
}
public static int average(int num1, int num2)
{
return (num1 + num2)/2;
}
// Confilict: The above method has the same signature as the first
// average
}
[3 pts] Is this program correct? If not, please explain.
Not Correct.
[2 pts] What is the output?
7.5
7.5
2. Consider the following three classes: Num, ParameterModifier and
ParameterTester
class Num
{
private int value;
public Num (int update)
{
value = update;
}
public void setValue (int update)
{
value = update;
}
public String toString ()
{
return value + "";
}
}
pf2

Partial preview of the text

Download 5 Unsolved Problems on Java Programming - Quiz 9 | COP 2250 and more Quizzes Javascript programming in PDF only on Docsity!

Quiz 9

Name: Panther ID:

Please use 10 minutes to finish this quiz.

1. Consider the following program.

public class Utility { public static void main(String []args) { System.out.println(Utility.average(7, 8)); System.out.println(Utility.average(7.0, 8));//7.0f } public static float average(int num1, int num2) { return (num1 + num2)/2.0f; } public static float average(float num1, int num2) { return (num1 + num2)/2.0f; } public static int average(int num1, int num2) { return (num1 + num2)/2; } // Confilict: The above method has the same signature as the first // average }

[3 pts] Is this program correct? If not, please explain.

Not Correct.

[2 pts] What is the output?

2. Consider the following three classes: Num, ParameterModifier and

ParameterTester

class Num { private int value; public Num (int update) { value = update; } public void setValue (int update) { value = update; } public String toString () { return value + ""; } }

class ParameterModifier { public void changeValues (int f1, Num f2, Num f3) { f1 = 999; f2.setValue(888); f3 = new Num (777); System.out.println ("After changing the values:"); System.out.println ("f1\tf2\tf3"); System.out.println (f1 + "\t" + f2 + "\t" + f3 + "
n"); } } public class ParameterTester { public static void main (String[] args) { ParameterModifier modifier = new ParameterModifier(); int a1 = 111; Num a2 = new Num (222); Num a3 = new Num (333); modifier.changeValues (a1, a2, a3); System.out.println ("After calling changeValues:"); System.out.println ("a1\ta2\ta3"); System.out.println (a1 + "\t" + a2 + "\t" + a3 + "
n"); } }

[5 pts] What is the output produced by the program?

After changing the values:

After calling changeValues:

Bonus

[5pts] Can you draw the diagram to illustrate the parameter passing throughout the

method call?

Please refer the lecture given in class.