You are on page 1of 9

Arguments

Question 1
class GFC401 {
static int m1(int x) {return ++x;}
public static void main (String[] args) {
int x = 1;
int y = m1(x);
System.out.println(x + "," + y);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 1,1


b.  Prints: 1,2
c.  Prints: 2,1
d.  Prints: 2,2
e.  Run-time error
f.  Compile-time error
g.  None of the above

ANSWER
Primitive arguments are passed by value. The method m1 increments
Prints:
1 b  the parameter x, and the result is returned. The local variable x of main
1,2 
remains unchanged.

Question 2

class GRC10 {
public static void main (String[] s) {
System.out.print(s[1] + s[2] + s[3]);
}}
java GRC10 A B C D E F

What is the result of attempting to compile and run the program using the specified
command line?

a.  Prints: ABC


b.  Prints: BCD
c.  Prints: CDE
d.  Prints: A B C
e.  Prints: B C D
f.  Prints: C D E
g.  Compile-time error
h.  Run-time error
i.  None of the above

ANSWER
The index for the first element of an array is zero so the first argument
Prints:
2 b  printed by this program is the second argument on the command line
BCD 
following the name of the class.  

Question 3
class GFC402 {
static int x=1;
void m1(int i) {x++; i++;}
public static void main (String[] args) {
int y=3; m1(y);
System.out.println(x + "," + y);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 1,3


b.  Prints: 2,3
c.  Prints: 1,4
d.  Prints: 2,4
e.  Run-time error
f.  Compile-time error
g.  None of the above

ANSWER
Method m1 is an instance method, and must be invoked with
Compile-time
3 f  reference to an instance of type GFC402. Method m1 can not be
error 
invoked from a static context.  

Question 4
class GFC403 {
private static int x=1;
static void m1(int i) {x++; i++;}
public static void main (String[] args) {
int y=3; m1(y);
System.out.println(x + "," + y);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 1,3


b.  Prints: 2,3
c.  Prints: 1,4
d.  Prints: 2,4
e.  Run-time error
f.  Compile-time error
g.  None of the above

ANSWER
Variables of primitive type are passed to methods by value: Only a copy
of the value of the variable is passed to the method. While the method
works with a local copy of the variable, the original variable remains
Prints: unchanged by any actions performed on the method parameter. For that
4 b 
2,3  reason, method m1 does not change the value of the variable y in the
main method. However, method m1 does have direct access to the class
variable x and the content of the class variable is modified by method
m1.  

Question 5
class GFC404 {
private static int x=1;
static void m1(int x, int y) {x++; y++;}
public static void main (String[] args) {
int y=3; m1(x, y);
System.out.println(x + "," + y);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 1,3


b.  Prints: 2,3
c.  Prints: 1,4
d.  Prints: 2,4
e.  Run-time error
f.  Compile-time error
g.  None of the above

ANSWER
Variables of primitive type are passed to methods by value: Only a copy
of the value of the variable is passed to the method. While the method
Prints: works with a local copy of the variable, the original variable remains
5 a 
1,3  unchanged by any actions performed on the method parameter. For that
reason, method m1 does not change the contents of the variable y in the
main method or the class variable x.  

Question 6
class GFC301 {
private String name;
public GFC301(String name) {this.name = name;}
public void setName(String name) {this.name = name;}
public String getName() {return name;}
public static void m1(GFC301 r1, GFC301 r2) {
r1.setName("Bird");
r2 = r1;
}
public static void main (String[] args) {
GFC301 pet1 = new GFC301("Dog");
GFC301 pet2 = new GFC301("Cat");
m1(pet1,pet2);
System.out.println(pet1.getName() + "," + pet2.getName());
}}

What is the result of attempting to compile and run the program?

a.  Prints: Dog,Cat


b.  Prints: Dog,Bird
c.  Prints: Bird,Cat
d.  Prints: Bird,Bird
e.  Run-time error
f.  Compile-time error
g.  None of the above

ANSWER
6 c  Prints: The method m1 is invoked by the method invocation expression
Bird,Cat  m1(pet1,pet2). The value of the reference variable denoted by the
argument pet1 is used to initialize the method parameter r1. Inside of
method m1, the method invocation expression r1.setName("Bird")
uses the copy of the value of the argument pet1 to assign a new name
to the instance of GFC301 that is referenced by the local variable pet1
in the main method. Generally speaking, a reference parameter can be
used to invoke methods on the referenced object and change the state
of the object to the extent provided by the object's methods. The
method invocation expression m1(pet1,pet2) has a second argument
pet2, and the value of pet2 is used to initialize the method parameter
r2. Inside of method m1, the assignment expression r2 = r1 changes
the value of the method parameter r2; but the local variable of the
main method denoted by the argument pet2 appearing in the method
invocation expression m1(pet1,pet2) remains unchanged.  

Question 7
class GFC303 {
private String name;
public GFC303(String name) {this.name = name;}
public void setName(String name) {this.name = name;}
public String getName() {return name;}
public static void m1(GFC303 pet1, GFC303 pet2) {
pet1 = new GFC303("Fish");
pet2 = null;
}
public static void main (String[] args) {
GFC303 pet1 = new GFC303("Dog");
GFC303 pet2 = new GFC303("Cat");
m1(pet1,pet2);
System.out.println(pet1.getName() + "," + pet2.getName());
}}

What is the result of attempting to compile and run the program?

a.  Prints: Dog,Cat


b.  Prints: Dog,Fish
c.  Prints: Fish,Cat
d.  Prints: Fish,Fish
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
7 a  Prints: The method m1 is invoked by the method invocation expression
Dog,Cat  m1(pet1,pet2). A copy of the reference argument pet1 is assigned to
the method parameter pet1. Inside the body of method m1, the
assignment expression pet1 = new GFC303("Fish") assigns a
reference to a new instance of GFC303 to the method parameter pet1;
but the argument pet1 that appears in the method invocation
expression m1(pet1,pet2) and the local variable pet1 that is declared in
the main method remain unchanged. The method invocation
expression m1(pet1,pet2) has a second argument pet2, and a copy of
pet2 is assigned to the method parameter pet2. Inside of method m1,
the assignment expression pet2 = null changes the value of the method
parameter pet2; but the argument pet2 appearing in the method
invocation expression remains unchanged in the main method.  

Question 8
class GFC304 {
static void m1(int[] i1, int[] i2) {
int[] i3 = i1; i1 = i2; i2 = i3;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 1,1


b.  Prints: 1,3
c.  Prints: 3,1
d.  Prints: 3,3
e.  Run-time error
f.  Compile-time error
g.  None of the above

ANSWER
The method m1 is invoked by the method invocation expression m1(i1,
i2). The argument i1 denotes a local variable of type int[] that is declared
in the main method. The value of the argument is a reference to the
array, and the argument value is used to initialize the method parameter
Prints: i1 of method m1. Inside the body of m1, the expression i1 = i2 sets the
8 b 
1,3  value of parameter i1 to the value of parameter i2, but the change in the
value of the parameter i1 does not change the original argument value or
the local variable i1 of the main method that the argument denotes.
Similarly, the assignment expression i2 = i3 in method m1 does not
change the value of the local variable i1 declared in the main method.  

Question 9
class GFC305 {
static void m1(int[] i1, int[] i2) {
int i = i1[0]; i1[0] = i2[0]; i2[0] = i;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 1,1


b.  Prints: 1,3
c.  Prints: 3,1
d.  Prints: 3,3
e.  Run-time error
f.  Compile-time error
g.  None of the above

ANSWER
Method m1 is not able to change the value of the local variables that are
Prints: declared in the main method and serve as the arguments in the method
9 c 
3,1  invocation expression. However, method m1 is able to modify the
contents of the arrays that are referenced by the method parameters.

Question 10
class GFC306 {
static int[] i1 = {1}, i2 = {3};
static void m1(int[] i1) {
int[] i3 = i1; i1 = i2; i2 = i3;
}
public static void main (String[] args) {
m1(i1);
System.out.print(i1[0] + "," + i2[0]);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 1,1


b.  Prints: 1,3
c.  Prints: 3,1
d.  Prints: 3,3
e.  Run-time error
f.  Compile-time error
g.  None of the above

ANSWER
10 a  Prints: The method m1 is invoked by the method invocation expression m1(i1).
1,1  The argument i1 denotes the static member variable i1. Inside the
declaration of method m1, the method parameter i1 shadows the static
member variable i1. The assignment expression i1 = i2 assigns the
value of the member variable i2 to the method parameter i1, but the
member variable i1 remains unchanged. Inside of method m1, the
member variable i2 is not shadowed; so the assignment expression i2 =
i3 assigns the reference value contained by the method local variable i3
to the member variable i2. This question demonstrates that argument
values are passed to method parameters by value, and the method
parameter is only a copy of the argument value. A change made to the
method parameter does not change the value of any variable that is
shadowed by the parameter and does not change the value of the
argument appearing in the method invocation expression.  

Question 11
class GFC307 {
static void m1(int[] i1, int[] i2) {
i1 = i2 = null;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 0,0


b.  Prints: 1,1
c.  Prints: 1,3
d.  Prints: 3,1
e.  Prints: null,null
f.  Run-time error
g.  Compile-time error
h.  None of the above

ANSWER
Although the reference parameters i1 and i2 are reassigned inside of
Prints:
11 c  m1, the change has no impact outside of m1. Array references are
1,3 
passed by value: the invoked method gets a copy of the array reference.

Question 12
class GFC308 {
int[] i1 = {1}, i2 = {3};
void m1() {
m2(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}
void m2(int[] i1, int[] i2) {
int[] i3 = i1;
this.i1 = i2;
this.i2 = i3;
}
public static void main (String[] args) {
new GFC308().m1();
}}

What is the result of attempting to compile and run the program?

a.  Prints: 0,0


b.  Prints: 1,1
c.  Prints: 1,3
d.  Prints: 3,1
e.  Prints: null,null
f.  Run-time error
g.  Compile-time error
h.  None of the above

ANSWER
Prints: Inside of method m2, the local variables i1 and i2 remain unchanged
12 d 
3,1  while the shadowed instance variables are changed.  

You might also like