What is printed by running the following code?
public static void main(String[] args) {
int[] nums = {2, 3, 4};
int n = 5;
changeMe1(n, nums);
System.out.print( n );
System.out.print(nums[0]);
changeMe2(n, nums);
System.out.print( n );
System.out.print(nums[0]);
}
public static void changeMe1(int number, int[] list) {
number++;
list[0]++;
}
public static void changeMe2(int number, int[] list) {
number = 9;
list = new int[1];
list[0] = 99;
}
class Outpt
{
public static void main(String[] args) {
int[] nums = {2, 3, 4};
int n = 5;
changeMe1(n, nums);
System.out.print( n ); // n still remains same after function call
System.out.print(nums[0]);
changeMe2(n, nums);
System.out.print( n ); // n still remains same after function call
System.out.print(nums[0]);
}
public static void changeMe1(int number, int[] list) {
number++; //value of local variable number incremented but not reflected in variable n in main since it is a pass by value call for changeMe1
list[0]++; ////array is passed by reference and first value incremented and is reflected in original array nums in main
}
public static void changeMe2(int number, int[] list) {
number = 9; //value of local variable number changed to 9 but not reflected in variable n in main since it is a pass by value call for changeMe1
list = new int[1]; //array is passed by reference but here it is locally defined as a single variable array and given value 99 which is not reflected in original array nums in main
list[0] = 99;
}
}
// Thus the output of program is : 5353
What is printed by running the following code? public static void main(String[] args) { int[] nums...
10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...
What is the Java output? Part One: class Driver { public static void main(String[] args) { int a = 5; int b = 3; if (a < b || a * 2 < b) System.out.print(a - b); System.out.print(b + a); } } Part Two: class Driver { public static void main(String[] args) { int a = 5; int b = 8; if (a < b) if (a * 2 < b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } }
1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...
1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...
Consider the following codes: public class TestThread extends Thread { public static void main(String[] args) { TestThread thread = new TestThread(); } @Override public void run() { printMyName(); } private void printMyName() { System.out.println("Thread is running"); } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...
What is wrong the following code? Explain. public class Test { public static void main(String[] args) { A a = new A(5.0); } class A { int value = 2; } }
10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println ()
For the below code, what will be printed? public class mathchar { public static void main(String[] args) { int i = 81, j = 3, k = 6; char w = 'f'; System.out.printf("%.2f\n", Math.sqrt(i)); System.out.printf("%.2f\n", Math.pow(j,k)); System.out.printf("%c\n", Character.toUpperCase(w)); System.out.printf("%c\n", Character.toLowerCase(w)); System.out.printf("%d\n", (int)(Math.random() * 21 + 6)); /* just tell range of possible values */ } }
what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int w 0; int x; int y 5; float z = 6.1 System.out.print("Enter y: "); x keyboard.nextint); System.out.println('y'); System.out.println(x); System.out.println(w*3); x- x+(int)z; System.out.println(x); 0 System.out.println(u); System.out.,println(u); System.out.println"x In" + y); System.out.print(y + z); ) liclosing main method 1 liclosing class header
how to change this code to string instead of int public static void main(String[] args) { int[] sample = { 212, 580, 6, 7, 28, 84, 112, 434}; Dictionary bst = new Dictionary(); for (int x : sample) { bst.insert(x); } System.out.println(bst.find(65)); System.out.println(bst.smallest()); System.out.println(bst.largest()); // bst.delete(84); System.out.println(bst.numOfLeafNodes()); System.out.println(bst.height()); bst.traverseInOrder(); } }