Question

Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray Computer Science; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println(int); return n; public static long xMethod(long n) System.out.,println(long); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are based on the following method: e void nPrint(String message, int n) while (n> 0) System.out.print(message);
return n: The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. a. b. c. d. Note: Questions 3-4 are based on the following method static void nPrint(String message, int n) while (n> 0) System.out.print(message);
3.0? 3. What is the printout of the call nPrint a. aaasa b. aasa d. invalid call 4 What is k after invoking nPrintA message, k) in the following codes? int k -3 nPrint(A message, k); b. 2 d. Analyze the following code. None of the above. 5. public static void main(Stringl) args) for (int i ; i<10: i+) System.out.printini is nPrintA message, i a. The code has a syntax error because i is not defined before or in System.out-printiniisi) b. The code prints i is c. The code prints f is 1 d. The code prints i is 10 6. Analyze the following code. public class Test public static void main( Stringl) args) Int n-2; System.ut printinxMethod n). public static int xMethod int n) System.out.printin n is+) a. The code prints 3 b. The code prints n is 2 c. The code prints n is 3
d. The code has a syntax error because xMethod does not have a return statement Analyze the following code. public class Test public static void main(String) args) int n 2 xMethod(n); System.out,printinn is+n): public static void xMetho(int n) a. The code prints n is 2 b. The code prints n is 3. c. The code prints n is 6 d. None of the above. 8. Which of the following method returns the sine of 90 degree? a. Math.sine(90) b. Math sin(90) Math.sin(Pr 0.5) e Math sin(Math.Pi0.5) An array reference variable can be used in which of the following ways? a. As a local variable b. As a parameter of a method c. As a return value of a method d All of the above 10. Consider the following code fragment: int[] list-new int[ 1야 for (int i- 0; ilist length; i+) istfil (intX(Math.randomO *10) Which of the following statements is true? a. list.length must be replaced by 10 b. The loop body will execute 10 times, filling up the array with random numbers c. The loop body will execute 10 times, filling up the array with zeros. d. The code has a runtime error indicating that the array is out of bound.
11. Given the following statement int] list new int[10]: list.length has the value a. 10 b. c. The value depends on how many integers are stored in list. None of the above. d. Given the following statement int ] list- new int[10]: 12. The array variable list contains ten values of type int. The array variable list contains nine values of type int. b. variable list contains a memory address that refers to an array of 10 int values d. c. The array variable list contains a memory address that refers to an array of 9 int values None of the above. 13. In the following code, what is the printout for list ? class Test public static void main( Stringl] args) int] list1 -(3,2,13 for (int i -0, i list1 length-1++) System.out,print(list I[i]+ 1 23 3 2 1 012 b. d. 2 10 e. 013 Analyze the following code: public class Test 14. public static void main(StringlI args) lx-(0, I, 2,3, 4, 5); xMethod(x, 4) public static void xMethod(int]x, int length) for(int i-ti<length; i++) System.out.print+xiD a. The program displays 0 123.
b. The program displays 0123 and then raises a runtime exception. d. The program displays 0 123 4 and then raises a runtime exception. Analyze the following code: public class Test P 15. void maing(Stringl) arg) intx-new int 5] int i; for (İ-0; İ < x.length, iH) x]-i System.out,println(i-) a. The program displays 0 1 234 b. The program displays 4. c. The program has a runtime error because the last statement in the main method causes ArraylndexOutOfBounds exception. d. method The program has syntax error because i is not defined in the last statement in the main Identify and correct the errors in the following program. (10 pts) public class Test Part II public static void main(String [Jargs) floatx - float [10]: getlnput(x) nPrint(x): System-out.println(The summation İs + process(x)); nPrintx) public static int getinput(float x) for(i=0, i ◇. x.length; İH) il input.nextint0 public static void nPrint (float x) forint i-0,i<x.length; i+) System-out.printf(%d + x[i): public static process(float x) float sum-0 forint į-0; İ < x.length; it+)
xli]*2 (10 pts each) III. Show the printout of the following code: public class Testl t public static void main(Stringll args) int i-1; hi5) xMethod() 计+, public static void xMethod(int i) for (int j- 1;jij++) System.out.printinO: b. public class Test2 public static void main String [] args) int x-1 int O array new int [5); fortint i=0;沁amay length, i++) array i)-i xMethod(x, array): System.outprintIn(一primitive variable x- + x); System.out printinf array element array11)- *+ array[ID:
public static void xMethod(int x, int [Iy) X-2x for(int i 0:i < y.length, it+) public class Test3 public static void main String 0 args) int x-1 int [ array new int [5]; for int i-0; i< array.length; i+) xMethodlx, array): System.out.printin array element array[l]-+array[ID array[il- i evariable x=-+x); public static void xMethod(int x, int [Dy) x2 x; for(int i 0; i < y.length; İH) d. Given the follwing program, show the values of the aray in the folowing figure public class Test4 publie static void main(Stringl] args) intl values-new int 5]: for (int i l; i<5; i++) values[i]-i+2* values i-1) values[0]- values1]+ values4)
After the first iteration in the loop is done After the last statement in the main method is executed After the loop is After the array is created Part IV: Write programs (10 pts each) a. Write a program that uses a loop to compute sum = 1 +-+-+-+-+-+--,..+
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    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...

  • Question 1 Consider the following small Java program. What will the contents of the array x...

    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...

  • 2. Write a counter controlled loop to solve the following problems. Each one will involve an...

    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];    ...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    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...

    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...

  • what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int...

    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...

    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...

    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...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • With a Scanner object created as follows, what method do you use to read an int...

    With a Scanner object created as follows, what method do you use to read an int value? Scanner input = new Scanner(System.in); A. input.int(); B. input.nextInt(); C. input.integer(); D. input.nextInteger(); What is the output of the following code? x = 0; if (x > 0) System.out.println("x is greater than 0"); else if (x < 0) System.out.println("x is less than 0"); else System.out.println("x equals 0"); A. x is greater than 0 B. x is less than 0 C. x equals 0...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT