by
PRO
FE S
S OR

QUIZ for SECTION VIII
To find an answer, click on the desired question.
QUESTION 1:
Write a program to display the first String array element passed from the Command Line Window.
(Note: Let's say that the class where the 'main' is contained is named "Quiz". Then when you write "java Quiz message" from the Command Line
Window, the String Object "message" is the first element of the String array that is passed
to the program. If you write "java Quiz message two", then "two" is the second element of
the String array that is passed to the program.)
QUESTION 2:
QUESTION 3:
In the 'main' instantiate the Date class;
assign values for each of its Instance variable to indicate the date: April 4th, 2002.
Then, display the 3 values in a date format.
Note: In this exercise, the 'main' method can be contained, say in the 'Quiz'
class, in which case there are two classes defined: Quiz and Date.
But, as an alternate way of writing the program, the 'main' method can be contained within the Date
class, in which case we only need one class defined: Date.
In our Answer we provide both options.
QUESTION 4:
Note: In order to invoke an Instance method, you need to instantiate the class where that method is contained.
In the answer provided, the Instance method as well as the 'main' Method
are both contained within the 'Quiz' class.
QUESTION 5:
The StringBuffer Object is modified by the Method. It is also affected outside the Method, as it was passed as Reference to
the Method. Say, that the StringBuffer argument passed from the 'main' is "Mike", then after the method is invoked,
the StringBuffer Object becomes "Hello Mike".
In Java all Objects are passed as Reference. If the StringBuffer Object were a Primitive Data type instead, then
it would have been passed as a value, and it would not have been affected outside of the Method.
QUESTION 6:
In this Exercise you are asked to write a program containing three overloaded class methods. All three methods have different
return types, and different arguments (not same signature).
The arguments are: double, double, and the return type is double.
The arguments are: int, double, and the return type is double.
The argument are: int, int, and the return type is int.
In the 'main' invoke each of these methods and pass argument values accordingly.
Also in the 'main' display each of the values returned by each method.
QUESTION 7:
Then, what is the outcome if the following line of code is included in the 'main' ?
double sum = add(34.567, 10); // passing a 'double' and an 'int'
in that order.
QUESTION 8:
What is the outcome if the following line of code is included in the 'main' ?
int sum = add(34.567, 10); // passing a 'double' and an 'int' in
that order.
ANSWER to QUESTION 1:
{
{
Note: When executing this program, if you enter "java Quiz message" from the Command Prompt Window, it displays "message".
However, if you enter "java Quiz" a run time error would occur, as there is no args[0].
To avoid that you can write
if (args.length > 0)
preceding the display line System.out.println(args[0]);. Then the display is implemented only
if an argument exists. If there is no args[0], then no display is implemented, and there will not be any run time
error.
ANSWER to QUESTION 2:
{
{
double a = Input.enterDouble(); // Input.class needed to enter a 'double'
System.out.print("Enter the second number (double) : ");
double b = Input.enterDouble(); // Input.class needed to enter a 'double'
double sum = add(a, b); // method is invoked here
System.out.println(sum); // display result here.
static double add(double a, double b)
{
return sum;
This program requires Input.class in same directory to enter 'double'.
Use right mouse button and click on Input.class to download it.
If you do not want to use the Input.class to enter the doubles from the keyboard, you can hard code them:
double a = < a value >; and double b = < a value >;
ANSWER to QUESTION 3:
ANSWER to QUESTION 4:
ANSWER to QUESTION 5:
ANSWER to QUESTION 6:
{
{
System.out.println(sum1);
double sum2 = add(10, 25.67);
System.out.println(sum2);
int sum3 = add(41, 135);
System.out.println(sum3);
static double add(double a, double b)
{
static double add(int a, double b)
{
static int add(int a, int b)
{
ANSWER to QUESTION 7:
The method that would be implemented is 'static double add(double a, double b)'.
The 'int' value 10 of the argument is implicitly casted into a double.
ANSWER to QUESTION 8:
The existing method that returns the 'int' does not implicitly cast a double into an int.
You would need to define another 'add' method with 'double', and 'int' as arguments, in that order,
and a 'int' as the returned value. Or you could explicitly cast the returned value when invoking the method, as this:
int sum = (int)add(34.567, 10);
in which case, the method that would be implemented is 'static double add(double a, double b)'.