Arguments Question 1

  • Uploaded by: rahul rastogi
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Arguments Question 1 as PDF for free.

More details

  • Words: 1,948
  • Pages: 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. b. c. d. e. f. g.

Prints: 1,1 Prints: 1,2 Prints: 2,1 Prints: 2,2 Run-time error Compile-time error None of the above ANSWER Prints: 1 b 1,2

Primitive arguments are passed by value. The method m1 increments the parameter x, and the result is returned. The local variable x of main 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. b. c. d. e.

Prints: ABC Prints: BCD Prints: CDE Prints: A B C Prints: B C D

f. g. h. i.

Prints: C D E Compile-time error Run-time error None of the above ANSWER Prints: 2 b BCD

The index for the first element of an array is zero so the first argument printed by this program is the second argument on the command line 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. b. c. d. e. f. g.

Prints: 1,3 Prints: 2,3 Prints: 1,4 Prints: 2,4 Run-time error Compile-time error None of the above ANSWER 3 f

Method m1 is an instance method, and must be invoked with Compile-time 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. b. c. d. e. f. g.

Prints: 1,3 Prints: 2,3 Prints: 1,4 Prints: 2,4 Run-time error Compile-time error 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 reason, method m1 does not change the value of the variable y in the 2,3 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. b. c. d. e. f. g.

Prints: 1,3 Prints: 2,3 Prints: 1,4 Prints: 2,4 Run-time error Compile-time error None of the above ANSWER 5 a Prints: Variables of primitive type are passed to methods by value: Only a copy 1,3 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

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. b. c. d. e. f. g.

Prints: Dog,Cat Prints: Dog,Bird Prints: Bird,Cat Prints: Bird,Bird Run-time error Compile-time error None of the above ANSWER 6 c Prints: Bird,Cat

The method m1 is invoked by the method invocation expression 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. b. c. d. e. f. g.

Prints: Dog,Cat Prints: Dog,Fish Prints: Fish,Cat Prints: Fish,Fish Compile-time error Run-time error None of the above ANSWER 7 a Prints: Dog,Cat

The method m1 is invoked by the method invocation expression 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. b. c. d. e. f. g.

Prints: 1,1 Prints: 1,3 Prints: 3,1 Prints: 3,3 Run-time error Compile-time error 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 i1 of method m1. Inside the body of m1, the Prints: expression i1 = i2 sets the value of parameter i1 to the value of 8 b 1,3 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. b. c. d. e. f. g.

Prints: 1,1 Prints: 1,3 Prints: 3,1 Prints: 3,3 Run-time error Compile-time error 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. b. c. d. e. f. g.

Prints: 1,1 Prints: 1,3 Prints: 3,1 Prints: 3,3 Run-time error Compile-time error None of the above ANSWER 10 a Prints: The method m1 is invoked by the method invocation expression 1,1 m1(i1). 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. b. c. d. e. f. g. h.

Prints: 0,0 Prints: 1,1 Prints: 1,3 Prints: 3,1 Prints: null,null Run-time error Compile-time error None of the above ANSWER Although the reference parameters i1 and i2 are reassigned inside of Prints: m1, the change has no impact outside of m1. Array references are 11 c 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. b. c. d. e. f. g. h.

Prints: 0,0 Prints: 1,1 Prints: 1,3 Prints: 3,1 Prints: null,null Run-time error Compile-time error None of the above ANSWER Prints: 12 d 3,1

Inside of method m2, the local variables i1 and i2 remain unchanged while the shadowed instance variables are changed. .

Related Documents

Arguments Question 1
June 2020 22
Arguments
December 2019 28
Question 1
November 2019 23
Question 1
April 2020 13
Question 1
October 2019 19
Question 1
November 2019 21

More Documents from ""