1) Answer: a) int[] array = new int[10];
a) It is valid array declaration because it is the standard syntax to declare an array.
b)It is invalid because it has no new keyword
c) It is invalid because a string cannot be converted into a char array.
2) Answer: The program displays int followed by 10
Explanation: In the main when the xMethod(10) is called it invokes the function static int xMethod(int) because the argument passed in the function call is of type integer but not long.
3) Answer:d) invalid call
Explanation: It gives an error because the function call has float in the second argument where it should be an integer. This results in lossy conversion. And shows an error.
nPrint("a",3.0);
4) Answer: c.3
EXPLANATION: nPrint() function was called using pass by value. So this does not effect the value of k in main(). It's value remains the same.
nPrint("A message",k);
5) Answer: a) the code has syntax error because I is not define before or in System.out.println("i is: +i");
Explanation: The scope of i is limited to the for block. So, i is an unknown symbol when it is used in System.out.println();
6) Answer: d
Explanation: Because the xMethod () doesn't have a return statement in it's definition.
7) Answer: a) The code prints n is 2
Explanation: The xMethod () is called using pass by value. So the change of n in function definition doendoe reflect in the value of n in the main(). So, the value of n remains same, 2.
Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...
please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...
2. Write a counter controlled loop to solve the following problems. Each one will involve an array (MinMax.java) Read in 25 ints from the keyboard, and store them in an array. Then, find the maximum and minimum values in such an array, and display them on the screen. public class Array-Assignment { public static void main(String [] args) { int [] x = new int[3]; int [] y = {3, 5, 9, 2}; x[2] = y[3]; ...
Question 1 Consider the following small Java program. What will the contents of the array x be when the code finishes running? public class Driver { public static void main(String[ ] args) { int[ ] x = {3,1,5,2,4}; int a = 0; for (int index = 0; index < x.length - 1; index++) { if ( x[index] > x[index + 1]) { a = x[index]; x[index] = x[index + 1]; x[index + 1] = a; } } } } 1,2,4,3,5...
Write a program that stores a phrase as an array of words, and then prints it backwards. The main method calls method getInput , which asks the user how many words there are, stores them in an array, and returns this array. printBackwards then takes this array of words, and prints it in reverse. Code Example: import java.util.Scanner; public class L17Num1{ public static void main(String[] args) { String[] sArray=getInput(); printBackwards(sArray); } public static String[] getInput() { System.out.println("How many...
JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in); System.out.println("Seed:"); int seed = scanner.nextInt(); System.out.println("Length"); int length = scanner.nextInt(); Random random...
X. (10 points) In the box provided show the output for this program segment public static void main (String [) args) intl num-(1,2.3,4) System.out printinx+y+num12) tyit(y, x. num): System.out printin(x+ynum(2): public static void tryit(int a, int b, intl c) int x c[2]-20 System.out printin(x+bCI2) XI. (7 points) a. Write a method NEGATIVE that receives as parameters a one-dimensional array variable LIST of integers. The method should return the sum of the negative elements in the array. You may not assume...
Why float f = 10d will cause the compiler error/ and why int and double did not cause any compiler error ? public class Main { public static void main(String[] args) { // write your code here char c = 38; int i = 'a'; double d = 10f; double d2 = 10; float f = 10d; System.out.println(a + b); } }
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
7. Consider the following recursive function: package javaapplication293; public class JavaApplication293 public static void main Stringl args) recFun(7) public static void recFun int u) if (-= 1 ) System.out. print("Stop! "); else System.out.print("Go "); recFun(u-1);
I have the following java-program, that creates a random walk
starting at (0,0) and continuing until x or y is greater than
abs(n).
First: I get an error, when I try closing the Scanner by
inserting "reader.close(); " in line 28. It says "Unreachable
code". How can I fix that?
Second: Is it possible to create a different colour for the
StdDraw.point when it reaches a point on one of the edges "n+1" or
"n-1" ?
1 import java.util.*; 3...