Question

In: Computer Science

Question 1: What does the following code print to the console when the main method is...

Question 1:

What does the following code print to the console when the main method is run?

public static void referenceMystery(int x, int[] a) {
  x = 3;
  a[0] = 4;
  System.out.println(x+" "+a[0]);
}
 
public static void main(String[] args) {
 int x = 1, y = 2;
 int[] a = new int[1];
 int[] b = new int[1];
 referenceMystery(y, b);
}

1. x a[0]

2. 1

3. 2 0

4. 3 4

Question 2:

What does the following code print out when the main method is run?

public static void referenceMystery(int x, int[] a) {
  x = 3;
  a[0] = 4;
}
 
public static void main(String[] args) {
 int x = 1, y = 2;
 int[] a = new int[1];
 int[] b = new int[1];
 referenceMystery(y, b);
 System.out.println(x+" "+a[0]);
}

1. x a[0]

2. 1 0

3. 2 1

4. 3 4

Solutions

Expert Solution


Related Solutions

3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
1) What are the main differences between the NPV method and the IRR? 2) When does...
1) What are the main differences between the NPV method and the IRR? 2) When does IRR give you the wrong answer? 3) How does the MIRR avoid the IRR shortcomings? Please answer all sections with 5-7 sentences for each question.
Write the code in Java: 1. Create a method that displays your name in the console....
Write the code in Java: 1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press. 2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure...
Please write the following swap functions and print code in main to show that the functions...
Please write the following swap functions and print code in main to show that the functions have adequately . Your code should, in main(), print the values prior to being sent to the swap function. In the swap function, the values should be swapped and then in main(), please print the newly swapped values. 1) swap integer values using reference parameters 2) swap integer values using pointer parameters 3) swap pointers to integers - you need to print the addresses,...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the error message “file name expected” is printed out instead of the "Opening file " message. Hint: If no parameter values are specified for method “main”, then the length of the array “args” will be zero. If that error message “file name expected” is printed out, the program should stop. You can make any Java program stop using this line … System.exit(0); skeleton code: /**...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) {...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) { new MyFrame(); } } import javax.swing.*; public class MyFrame extends JFrame{ MyPanel panel; MyFrame(){ panel = new MyPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ //Image image; MyPanel(){ //image = new ImageIcon("sky.png").getImage(); this.setPreferredSize(new Dimension(500,500)); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; //g2D.drawImage(image, 0, 0, null); g2D.setPaint(Color.blue); g2D.setStroke(new BasicStroke(5)); g2D.drawLine(0, 0, 500,...
In Java: Write a complete main method that does the following: 1. Takes any number, but...
In Java: Write a complete main method that does the following: 1. Takes any number, but at least two, command line arguments which are words (represented as strings) and will print to the console the number of words of that end with a digit. (Hint: loop through the args array) 2. If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message.
Consider the following class and the main method below. Trace the code, then answer the questions...
Consider the following class and the main method below. Trace the code, then answer the questions on the right. public class SomeClass { private String aName; private int aNumber; private boolean amAwesome; public SomeClass(String name, int number){ aName = name; aNumber = number; amAwesome = true; } public SomeClass(String name, int number, boolean awesome){ aName = name; aNumber = number; amAwesome = awesome; } public void methodAwesome(int number){ if(amAwesome) aNumber += number - 5; amAwesome = !amAwesome; } public int...
Question 1. // In method main    // In order to make the process of populating...
Question 1. // In method main    // In order to make the process of populating this array easier, we have the following local arrays defined:    String names[] = { "Smith", "Johnson", "Hill"}; String months[] = { "May", "August", "June"}; int days[] = { 12, 4, 28};    // SET2. You do this one! Declare an array of type Event_Type (that is defined in class SummerEvent) and initialize // it so that the first element is constant SOCIAL, the...
// 5. Predict what the following code will print (write it down), then try it. var...
// 5. Predict what the following code will print (write it down), then try it. var myVariable = "global"; var myOtherVariable = "global"; function myFunction() { var myVariable = "local"; return myVariable; } function myOtherFunction() { myOtherVariable = "local"; return myOtherVariable; javascript
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT