Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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)

59 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
_________________________________________________๎˜ƒ
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.๎˜ƒ
๎˜ƒ
4.๎˜ƒ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'};๎˜ƒ
๎˜ƒ
5.๎˜ƒ๎˜ƒ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๎˜ƒ
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