Home Robotics C++ Physics II AP Physics B Electronics AP Java Astronomy Independent Study Summer Session Contests  About
                                                       

Example 1

 

 

   The following code will run and print failure. Name the concept that is being illustrated.

 

      Int x = 0;

      Boolean y = true;

      If (y && (x!=0) && (2/x == 0)) Sys tem.out.println (“success”);

      Else System.out.printlon(“failure);

 

      _______________________________  short-circuit evaluation

 

   Name the 2 major categories of data types used by Java.

 

     _______________________________________________  primitive

 

     _______________________________________________  reference 

 

   What 2 things must you be able to accomplish in order to use recursion to solve a problem?

 

 

    a. A stopping condition - when will it terminate

   b. An algorithm that will head toward and reach the stopping condition

 

   List an advantage and a disadvantage of recursion.

 

     a. Advantage: Clearer (if you see it) and shorter

    b. Disadvantage: Takes a lot of memory, may not reach stopping condition

 

   List and explain the 3 types of classes  used by Java.

 

     a. Interface - no methods defined - a class can inherit from many interfacees

    b. Abstract - at least one method undefined - a class can inherit from only one abstract

       class

    c. Concrete - all methods defined - only one you can create an object of - class can

        inherit from only 1

 

   Unlike C++, Java does not allow multiple inheritance. Why? What is multiple inheritance?

 

     Multiple inheritance is when a class inherits directly from more than 1 class

    The problem is that you can have method conflicts - same name

 

   List 2 keywords used with Java exceptions.

 

    a. try

    b. throw

    c. throws            //not tested

    d. rethrows       //not tested

    e. catch

 

   What  is the purpose of the Java ArrayList class?

 

     It simplifies the use of arrays

     It is a data structure for storing a list of elements as does an array.

    It also provides methods to find, insert, and remove an element.

 

   In the code

 

      ArrayList  <className> theArrayList = new ArrayList <class name> ();

 

a.  What is the purpose of the item in < >?

 

      Constructs an ArrayList named theArrayList whose elements are of type className - where classname can be any class such as Comparable.

 

      NOTE: If this is used, then casting is not required - the compiler knows the data type.

 

b.      What is the  purpose of the new keyword?

 

       This operator is used to create a new instance of a object - it allocates memory  

       from free storage.

 

   The Comparable class is  in Java.lang which is loaded automatically (it does not have to be

      imported). The following is an example line of code from Lesson 17 – where this class is

      discussed..

 

       If (list.get(inner).compareTo(list.get(inner+1) > 0 ……

 

a.      What is returned by the method compareTo of class Comparable?

 

         If inner is less than inner+1 then it will return -1

         If they are equal it will return 0

         If inner is greater than inner + 1 it will return +1

 

b.      What is the importance of the class Comparable?

 

         Allows comparisons

 

   Sorting Algorithms

 

       a. List and briefly describe the sorting algorithms that we have studied to date.

 

(1)Bubble: Go through the items and if two adjacent are out of order then swap them, repeat

         (2) Selection: Sweep through the items, find larges, swap with the last , repeat

         (3) Insertion: Used whe items mostly sorted, find where item belongs and insert it there

 

       b. They are called quadratic sorting algorithms. Why?

 

            Steps involved go as the square of the number of items (N) or N2

 

   Given a one-dimensional array whose indices go from 0 to n. How do you find the cell that is

      in approximately the middle of the array?   (0 + n)/2

 

   ArrayList class. Given that myList is a reference to an ArrayList. How do you

 

       a. Print the value of the element at index i?

 

            System.out.println (myList.get(i));

 

       b. Add a reference to an object to the end of the list?

 

            myList.add("name");

 

       c. Add a reference to an object to a given index?

 

            myList.set(indexnumber, "name");

 

       d. Remove a reference to an object at a given index?

 

            myList.remove(indexnumber); 

 

   Searching

 

       a. If an array is not sorted, how do you find an element?

 

            Sequentially - start at the beginning and go to the end

 

       b. If an array is sorted, you can use binary search. Explain the operation of this algorithm.

 

          Find the midpoint, it that is the item you are looking for, problem solved.

          If not the item, ask is target larger or smaller than midpoint

          Depending upon answer to above, 1/2 of array can be discarded

          Repeat

 

   Number Systems

 

       a. What base is used in each of the following?

 

            (1) Binary: ________________               2

            (2) Decimal: ______________           10

            (3) Hexadecimal: ______________    16

            (4) Octal: _______________               8

 

       b. What is a bit?

 

            Short for binary digit - smallest unit of information handled by a computer

         One bit expresses a 1 or a 0 in a binary numeral

 

       c. What is underflow?

 

            A condition in which a mathematical calculation produces a result too near to zero

          to be  represented by the range of binary digits available to the computer.

 

       d. What is overflow?

 

           When data requires more bits than are available - number too large to handle

 

       e. Convert the base 10 number 25 to 8 bit binary.      __________________ 0 0 0 1 1 0 0 1

       f. Convert the base 2 number 1 0 1 to base 10    __________________ 5

 

   Control Structures

 

       a. Where is the boolean in the following loop?  i for (int i = 0; i < 10; i++)

 

           i < 10 is the boolean

 

       b. What is the difference between a while and a do while loop?

 

            In a while loop the boolean is checked at the beginning - so it might not execute

          In a do while loop the boolean is checked at the end so it will execute at least once

 

   What is an initializer list?

 

        A way to initialize an array - enclosed in {  }


   If an initializer list or equivalent method is not used, what is contained in the

       elements of a two-dimensional array if it is declared to be of type

 

a.  integer:_________ 0

 

b.  boolean:_________ false

 

c.  object:__________ null

 

   Assume that an array named array1 has been declared and initialized.

      Write code to find the length of the first row.

 

       array1.length();

 

     Note: This problem was not clearly worded. I should have stated whether it is a 1 or 2D array.

     For a 1D array the above is correct. For a 2D array, the length (number of columns) of each row would be

     array1[i].length where i is the row subscript.

 

     For this reason, either answer is accepted

 

   Write code to declare a two-dimensional array of type integer.

 

        int [][] table;  or int table [][];  declares the array.

 

      int [][] table = new int [3][4]; assigns memory to the object  using the new keyword.

 

   List 3 ways to initialize a two-dimensional aray.

 

       a. Direct assignment

     b. Two for loops

     c. Initializer list

 

 

   When Java performs an illegal operation, a special event know as a(an) ________ occurs.

 

        Exception

 

   How do you find an item in a one-dimensional array

 

       a. If the elements are not sorted?

 

         Using sequential search - start at the beginning and go to the end.

 

       b. If the elements are sorted?

 

            Using binary search

      

   What is the basic idea behind quicksort - pseudocode?

 

        Find the mid point, roughly sort them (those larger than mid point on right of mid point, those smaller

      or equal to mid point on left of mid point. Do this recursively until down to 1 element which  is by

      definition sorted.

 

   Two-dimensional arrays.

 

       a. What is the syntax to print the number of rows?

 

            Observe the code listed below

 

       b. What is the syntax to print number of cols in a given row?

 

            Observe the code listed below

 

            int [][] arrayOne = new int [4][5];

            int row, col;

            for (row = 0; row < arrayOne.length; row++)            //number of rows in red

                for (col = 0; col < arrayOne[row].length;col++)   //number of columns in that row in red

                    //do something

 

   Order of algorithms.

 

        a. What is a linear algorithm?

 

            An algorithm where the number of steps required is directly proportional to the

          size of the data set (N). As N increases, the number of steps required increased

          linearly.

 

        b. Give an example of a linear algorithm.

 

            Searching for an item in a non-sorted array - sequential search

 

       c. Using "Big O" notation, we write O(N). What is N?  Amount of data or number of elements

 

       d. What does it mean to say that the order of an algorithm is quadratic?

 

           The number of steps or run time goes as N squared

 

        f.  What does log2N mean?

 

           The number to which 2 must be raised to equal N

 

   Strings

 

        Given the following code: String string1 = “AP Java”; String string2 = “A string”;

 

a.    Write code that will assign the length of string1 to a variable named theLength

 

int length = string1.length();

 

b.    Write code that will assign the character located at index 3 of string1 to the character variable theChar

   

             char theChar =    string1.charAt(3);

 

c.    Write code that will assign the index of the character P to the integer variable indexOf

  

            Int indexOf = string1.indexOf(‘P’); 

 

d.    Write code that will find which string (string1 or another string, string2 appears first in alphabetical order.

 

               int n = string1.compareTo(string2);

               returns: < 0 if string1 less than string2, 0 if equal, >0 if string 2 greater than string 1

 

         e.  How do you compare two strings to see if they are identical (are the same)?

 

              if (string1.equals(string2))....

 

   Describe the concept of garbage collection.

 

       If a reference to an object is no longer being used then there is no way to find it and

      it becomes garbage.  The garbage collector removes it to save memory.

 

  

 

   The LinkedList class is based on the ______________________________ data structure.

      Linked List

 

   The ArrayList class is based on the ______________________ data structure.  Array