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

Compile Error - Data Structures - Quiz, Exercises of Data Structures and Algorithms

Main points of this past exam are: Compile Error, Public Class, Compile Error, Declare an Array, Program Compiles, Output Value, Binary Search

Typology: Exercises

2012/2013

Uploaded on 04/07/2013

seshu_lin3
seshu_lin3 🇮🇳

4

(3)

59 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Pleasewriteryouronthetopofthepage.
22c:21ComputerScienceII:DataStructures
Quiz2A(openbookandnotes;pleasecircleallthecorrectsolutions)
1.Ifyoudeclareanarraydouble[]list={3.4,2.0,3.5,
5.5},list[1]is________.
A.3.4
B.2.0
C.3.4
D.5.5
E.undefined
2.Ifyoudeclareanarraydouble[]list={3.4,2.0,3.5,
5.5},thehighestindexinarraylistis__________.
A.0
B.1
C.2
D.3
E.4
3.Supposeinti=5,whichofthefollowingcanbeused
asanindexforarraydouble[]t=newdouble[100]?
A.i
B.(int)(Math.random()*100))
C.i+10
D.i+6.5
E.Math.random()*100
4.Whichofthefollowingstatementsisvalid?
A.inti=newint(30);
B.doubled[]=newdouble[30];
C.int[]i={3,4,3,2};
D.char[]c=newchar();
E.char[]c=newchar[4]{'a','b','c','d'};
5.Whatwouldbetheresultofattemptingtocompile
andrunthefollowingcode?
publicclassTest{
publicstaticvoidmain(String[]args){
double[]x=newdouble[]{1,2,3};
System.out.println("Valueis"+x[1]);
}
}
A.Theprogramhasacompileerrorbecausethesyntax
newdouble[]{1,2,3}iswronganditshouldbereplaced
by{1,2,3}.
B.Theprogramhasacompileerrorbecausethesyntax
newdouble[]{1,2,3}iswronganditshouldbereplaced
bynewdouble[3]{1,2,3};
C.Theprogramhasacompileerrorbecausethesyntax
newdouble[]{1,2,3}iswronganditshouldbereplaced
bynewdouble[]{1.0,2.0,3.0};
D.Theprogramcompilesandrunsfineandtheoutput
"Valueis1.0"isprinted.
E.Theprogramcompilesandrunsfineandtheoutput
"Valueis2.0"isprinted.
6.Assumeint[]t={1,2,3,4}.Whatist.length?
A.0
B.3
C.4
D.5
pf3

Partial preview of the text

Download Compile Error - Data Structures - Quiz and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Please writer your on the top of the page.

22c:21 Computer Science II: Data Structures

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

  1. If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________. A. 3. B. 2. C. 3. D. 5. E. undefined
  2. If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________. A. 0 B. 1 C. 2 D. 3 E. 4
  3. Suppose int i = 5, which of the following can be used as an index for array double[] t = new double[100]? A. i B. (int)(Math.random() * 100)) C. i + 10 D. i + 6. E. Math.random() * 100
  4. Which of the following statements is valid? A. int i = new int(30); B. double d[] = new double[30]; C. int[] i = {3, 4, 3, 2}; D. char[] c = new char(); E. char[] c = new char[4]{'a', 'b', 'c', 'd'};
    1. What would be the result of attempting to compile and run the following code? public class Test { public static void main(String[] args) { double[] x = new double[]{1, 2, 3}; System.out.println("Value is " + x[1]); } } A. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}. B. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3}; C. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0}; D. The program compiles and runs fine and the output "Value is 1.0" is printed. E. The program compiles and runs fine and the output "Value is 2.0" is printed.
    2. Assume int[] t = {1, 2, 3, 4}. What is t.length? A. 0 B. 3 C. 4 D. 5
  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
    1. Analyze the following code: public class Test { public static void main(String[] args) { final 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 has a compile error on the statement x = new int[2], because x is final and cannot be changed. D. The elements in the array x cannot be changed, because x is final.
    2. When you pass an array to a method, the method receives __________. A. a copy of the array B. a copy of the first element C. the reference of the array D. the length of the array
    3. Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return? A. 0 B. ‐ 1 C. 1 D. 2 E. ‐ 2