DOC

Java Review Questions

By Rita Harper,2014-04-24 12:32
14 views 0
Java Review Questions

Name:_________________________

    AP SUMMER REVIEW -Java Review Questions

You should be familiar with the following topics:

    Objects and Classes

    Fundamental Data Types

    Decision if/if-else

    Iteration- while and for loops

    Some knowledge of hardware and software

You will find many useful links, quizzes, flashcards, etc. on the following

    website. Browse through and try them.

    http://chortle.ccsu.edu/CS151/cs151java.html

Part I: For Loop Review

    Determine the output for the following without the use of a computer:

1)

    for(int x=4; x=0; x--)

     System.out.println(x*x);

2)

    for(int x=1; x<=3; x++)

    {

    for (int y=1; y<=2; y++)

     {

     System.out.println(x +" " +y);

     }

    }

3)

    for(int x=1; x<=3; x++)

    {

     for (int y=1; y<=2; y++)

     {

     System.out.println(x +" " +y);

     }

     System.out.println();

    }

    Part II: Array Review Section

4) Suppose an array numbers was declared, created and filled as follows:

    -2 -9 10 11 12

    a.) What is the length of this array?

    b.) What element is in numbers[2]?

5) Which of the following is invalid?

    (A) double[] rain[12];

    (B) double[] rain;

    (C) double rain[]= new double[12];

    (D) double[] rain= new double[12];

6) Given the declarations

    int[] a = new int[10];

     int[] b;

which of the following will cause an

    ArrayIndexOutOfBoundsException?

(A) a[0]=10;

    (B) a[6]=10;

    (C) a[10]=10;

7) fun is defined as follows:

public int fun(int[ ] v)

    {

     v[0]--;

     return v[0] +2;

    }

What is the value of v[0] after the following code is executed?

     int[] v={3,4,5};

     v[0]=fun(v);

    a) 1

    b) 2

    c) 3

    d) 4

    e) 5

Part III: String Review Section

8) Indicate the output of the following code segments.

    Refer to the following declarations:

    String str = “Please ”; String str2= “”;

    f) System.out.println(str.substring(4,8));

    g) System.out.print(str.indexOf(„ „));

    h) System.out.println(str2.toUpperCase());

    i) System.out.print(str.charAt ( 7 ));

    j) System.out.print(str2.length( ));

    Part IV: While Review

9) Give the output for the following program segments:

a) x = 0;

     do

     {

     y = x*x + 1;

     System.out.println(x+” “+y);

     x++;

     }

     while (x <= 2);

b) x = -2;

     do

     {

     y = x*x -1;

     System.out.println(x+” “ +y);

     x++;

     }

     while (x >= 2);

c) x= 0;

     do

     {

     y=x*x*x;

     System.out.println(x);

     while (y <= 10)

     System.out.println(y);

     x++;

     }

     while (y <=5);

d) Write the shortcut for : a = a + 1

e) Give the output of the following program segment.

    z = 3;

    x = 5;

    y = 2;

    if (x < y)

     if (x < z)

     System.out.println( “tricky”);

    else

     Sysytem.out.println(“watch out”);

    System.out.println(“misleading question”);

    10) The method xProperty is defined as follows:

     public boolean xProperty(int a)

     {

     return a == 2*(a/10 + a %10);

     }

     For which of the following values of a does xProperty(a) return true?

    (a) 2

    (b) 8

    (c) 18

    (d) 28

    (e) 128

Part V: Mixed Review

    True/False

T F 11) An array is an object.

T F 12) The expression x>0 is the same as the expression 0<=x.

T F 13) The expression a || b will short-circuit if a is false.

T F 14) An algorithm is a step-by-step process for solving a problem.

T F 15) The expression a && !b is the same as a || b.

T F 16) In the declaration int num =2.4; The 2.4 will automatically be

    converted to an int and the num will get the value of 2.

T F 17) The following two declarations are both valid.

    double temp[];

    double[] temp;

    T F 18) A single array can contain elements of different data

    types.

T F 19) In an arraylist you can add an object into a specified position

    of the sequence.

T F 20) You must declare an object before using it.

    T F 21) The following declaration is valid:

     Customer john, JOHN, John;

PART VI: Multiple Choice: Mixed Review

    22) Compiling a Java Source file will produce

    a) an executable file

    b) a bytecode file

    c) a bytecode file only if its an application

    d) a bytecode file only if its an applet

23) The expression 5 + 6 * 3 % 2 -1 evaluates to

    a) 0

    b) 1

    c) 4

    d) 10

    e) 13

24) Which of the following statements DOES NOT display 9.95?

    (A) System.out.println(9 + 0.95);

    (B) System.out.println(995/100.0);

    (C) System.out.println(9. + 95/100);

    (D) System.out.println(9 + 95.0/100);

    (E) System.out.println(9 + “.” + 95);

Report this document

For any questions or suggestions please email
cust-service@docsford.com