Method Returns - Data Structures - Quiz, Exercises of Data Structures and Algorithms

Main points of this past exam are: Method Returns, Runtime Error, Many Elements, Variable List, Different Size Array, Method Returns, Last Statement

Typology: Exercises

2012/2013

Uploaded on 04/07/2013

seshu_lin3
seshu_lin3 🇮🇳

4

(3)

54 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
_________________________________________________
Pleasewriteyouronthetopofthepage.
22c:21ComputerScienceII:DataStructures
Quiz2B(openbookandnotes;pleasecircleallthecorrectsolutions)
1.Whichofthefollowingisincorrect?
A.int[]a=newint[2];
B.inta[]=newint[2];
C.int[]a=newint(2);
D.inta=newint[2];
E.inta()=newint[2];
2.Howmanyelementsareinarraydouble[]list=new
double[5]?
A.4
B.5
C.6
D.0
3.Analyzethefollowingcode.
publicclassTest{
publicstaticvoidmain(String[]args){
int[]x=newint[3];
System.out.println("x[0]is"+x[0]);
}
}
A.Theprogramhasacompileerrorbecausethesizeof
thearraywasn'tspecifiedwhendeclaringthearray.
B.Theprogramhasaruntimeerrorbecausethearray
elementsarenotinitialized.
C.Theprogramrunsfineanddisplaysx[0]is0.
D.Theprogramhasaruntimeerrorbecausethearray
elementx[0]isnotdefined.
4.Howcanyouinitializeanarrayoftwocharactersto'a'
and'b'?
A.char[]charArray=newchar[2];charArray={'a','b'};
B.char[2]charArray={'a','b'};
C.char[]charArray={'a','b'};
D.char[]charArray=newchar[]{'a','b'};
5.Assumeint[]scores={1,20,30,40,50},whatvalue
doesjava.util.Arrays.binarySearch(scores,3)return?
A.0
B.‐1
C.1
D.2
E.‐2
pf3
pf4

Partial preview of the text

Download Method Returns - Data Structures - Quiz and more Exercises Data Structures and Algorithms in PDF only on Docsity!

_________________________________________________

Please write your on the top of the page.

22c:21 Computer Science II: Data Structures

Quiz 2B (open book and notes; please circle all the correct solutions)

  1. Which of the following is incorrect? A. int[] a = new int[2]; B. int a[] = new int[2]; C. int[] a = new int(2); D. int a = new int[2]; E. int a() = new int[2];
  2. How many elements are in array double[] list = new double[5]? A. 4 B. 5 C. 6 D. 0
  3. Analyze the following code. public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } } A. The program has a compile error because the size of the array wasn't specified when declaring the array. B. The program has a runtime error because the array elements are not initialized. C. The program runs fine and displays x[0] is 0. D. The program has a runtime error because the array element x[0] is not defined.
    1. How can you initialize an array of two characters to 'a' and 'b'? A. char[] charArray = new char[2]; charArray = {'a', 'b'}; B. char[2] charArray = {'a', 'b'}; C. char[] charArray = {'a', 'b'}; D. char[] charArray = new char[]{'a', 'b'};
    2. Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return? A. 0 B. ‐ 1 C. 1 D. 2 E. ‐ 2

6. Analyze the following code.

int[] list = new int[5];

list = new int[6];

A. The code has compile errors because the variable

list cannot be changed once it is assigned.

B. The code has runtime errors because the variable

list cannot be changed once it is assigned.

C. The code can compile and run fine. The second

line assigns a new array to list.

D. The code has compile errors because you cannot

assign a different size array to list.

  1. What is output of the following code: public class Test { public static void main(String[] args) { int[] x = {120, 200, 016}; for (int i = 0; i < x.length; i++) System.out.print(x[i] + " "); } } A. 120 200 16 B. 120 200 14 C. 120 200 20 D. 016 is a compile error. It should be written as 16.
  2. In the following code, what is the printout for list2? class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list2.length; i++) System.out.print(list2[i] + " "); } } A. 1 2 3 B. 1 1 1 C. 0 1 2 D. 0 1 3
  3. Analyze the following code: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for (int i = 0; i < y.length; i++) System.out.print(y[i] + " "); } } A. The program displays 1 2 3 4 B. The program displays 0 0 C. The program displays 0 0 3 4 D. The program displays 0 0 0 0
  1. Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are x.length are x[0].length? A. 2 and 1 B. 2 and 2 C. 3 and 2 D. 2 and 3 E. 3 and 3
  2. What is the output of the following code? public class Test { public static void main(String[] args) { int[][] matrix = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; for (int i = 0; i < 4; i++) System.out.print(matrix[i][1] + " "); } } A. 1 2 3 4 B. 4 5 6 7 C. 1 3 8 12 D. 2 5 9 13 E. 3 6 10 14