Monday, June 29, 2015

java review test college exam


 (Assume all variable are properly declared and all programs are complete).  
  (You are not allowed to use computer to run the programs.)      

1.      Circle the correct answers?             
                                                                    

a). __________ enable programmers to specify, with a single method declaration, a set of related methods.

a. Overloaded methods.
b. Overriden methods.
c. Generic methods.


b). All generic method declarations have a type parameter section delimited by __________.

a. curly brackets ({ and }).
b. angle brackets (< and >).
c. square brackets ([ and ] ).





 c). A recursive method _______________________.
a. is a method containing a loop.
b. calls itself.
c. is part of the java.recursion package.







d).  When formulating a recursive solution, what should you consider?
a. base cases and general case
b. base cases only
c. general case only








 2. What does the following statements display?                               

public class MysteryClass
{
   public static int mystery( int[] array2, int size )
   {
      if ( size == 1 )
         return array2[ 0 ];
      else
         return array2[ size - 1 ] + mystery( array2, size - 1 );
   } // end method mystery

   public static void main( String[] args )
   {
      int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

      int result = mystery( array, array.length );
      System.out.printf( "Result is: %d\n", result );
   } // end method main
} // end class MysteryClass
                               


//answer for #2

M(a,10) = a[9] + M (a,9) 45+10 = 55
M(a,9) = a[8] + M (a,8)36+9 45
M(a,8) = a[7] + M (a,7)28+8
M(a,7) = a[6] + M (a,6) 21+7
M(a,6) = a[4] + M (a,4)15+6
M(a,5) = a[4] + M (a,4)10+5
M(a,4) = a[3] + M (a,3)6+4
M(a,3) = a[2] + M (a,2) 3+3
M(a,2) = a[1] + M (a,1) 2+1
M(a,1) = a[0] + M (a,0)


 Result is : 55


















  3.  Show the output of the following program:       

public class RangeSum
{

   public static void main(String[] args)
   {
      int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
     
      System.out.print("The sum of elements 2 through " +
                       "5 is "+ rangeSum(numbers, 2, 5));
   }
  

   public static int rangeSum(int[] array, int start, int end)
   {
      if (start > end)
         return 0;
      else
         return array[start] + rangeSum(array, start + 1, end);
                
   }
}    
  
//answer for #3


Rangesum (2,5) = array[start] + rangesum (a, start +1, end)
Rs(2,5) array[2]  + rs (a, 2+1, 5)
Rs(2,5) 3+ rs (3,5) 15+3 = 18
Rs(3,5) 4 + rs(4,5) 11+4
Rs(4,5) 5 + rs(5,5) 6+5
Rs(5,5) 6 + rs(6,5)


The sum of elements 2 through 5 is 18

















4. Show the output of following program:

//  MaximumTest.java
// Generic method maximum returns the largest of three objects.

public class MaximumTest
{
   public static void main( String[] args )
   {
      System.out.printf( "Maximum of %d, %d and %d is %d\n\n", 3, 4, 5,
         maximum( 3, 4, 5 ) );
      System.out.printf( "Maximum of %.1f, %.1f and %.1f is %.1f\n\n",
         6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );
      System.out.printf( "Maximum of %s, %s and %s is %s\n", "pear",
         "apple", "orange", maximum( "pear", "apple", "orange" ) );
   } // end main

   // determines the largest of three Comparable objects
   public static < T extends Comparable< T > > T maximum( T x, T y, T z )
   {
      T max = x; // assume x is initially the largest

      if ( y.compareTo( max ) > 0 )
         max = y; // y is the largest so far

      if ( z.compareTo( max ) > 0 )
         max = z; // z is the largest

      return max; // returns the largest object
   } // end method maximum
} // end class MaximumTest



//answer #4
Maximum of 3, 4 and 5 is 5
 
Maximum of 6.6, 8.8 and 7.7 is 8.8
 
Maximum of pear, apple and orange is pear


     









5. Show the output of running the class Test in the following code:

import java.util.ArrayList; // Needed for ArrayList class

public class ArrayListDemo3
{
   public static void main(String[] args)
   {
      // Create an ArrayList to hold some names.
      ArrayList nameList = new ArrayList();
     
      // Add some names to the ArrayList.
      nameList.add("James");
      nameList.add("Catherine");
      nameList.add("Bill");
     
      // Display the items in nameList and their indices.
      for (int index = 0; index < nameList.size(); index++)
      {
         System.out.println("Index: " + index + " Name: " +
                            nameList.get(index));
      }
     
      // Now remove the item at index 1.
      nameList.remove(1);
     
      System.out.println("The item at index 1 is removed. " +
                         "Here are the items now.");
                        
      // Display the items in nameList and their indices.
      for (int index = 0; index < nameList.size(); index++)
      {
         System.out.println("Index: " + index + " Name: " +
                            nameList.get(index));
      }
   }
}

//answer #5
Index: 0 Name: James
Index: 1 Name: Catherine
Index: 2 Name: Bill
The item at index 1 is removed. Here are the items now.
Index: 0 Name: James
Index: 1 Name: Bill















6. What is the output of running class C?

import java.util.ArrayList; // Needed for ArrayList class


public class ArrayListDemo1
{
   public static void main(String[] args)
   {
      // Create an ArrayList to hold some names.
      ArrayList nameList = new ArrayList();
     
      // Add some names to the ArrayList.
      nameList.add("James");
      nameList.add("Catherine");
      nameList.add("Bill");
     
      // Display the size of the ArrayList.
      System.out.println("The ArrayList has " +
                         nameList.size() +
                         " objects stored in it.");

      // Now display the items in nameList.
      for (int index = 0; index < nameList.size(); index++)
         System.out.println(nameList.get(index));
   }
}


// Answer # 6

The ArrayList has 3 objects stored in it.
James
Catherine
Bill




















7. What is the output of running the class C.

public class C {
  public static void main(String[] args) {
    Object[] o = {new A(), new B()};
    System.out.print(o[0]);
    System.out.print(o[1]);  
  }
}

class A extends B {
  public String toString() {
    return "A";
  }
}

class B {
  public String toString() {
    return "B";
  } 
}

a. AB
b. BA
c. AA
d. BB
e. None of above
















8. What is the output of running class C?

class A {
  public A() {
    System.out.println(
      "The default constructor of A is invoked");
  }
}

class B extends A {
  public B(String s) {
    System.out.println(s);
  }
}

public class C {
  public static void main(String[] args) {
    B b = new B("The constructor of B is invoked");
  }
}
a.         none
b.         "The constructor of B is invoked"
c.         "The default constructor of A is invoked" "The constructor of B is invoked"
d.         "The default constructor of A is invoked"






























9. Analyze the following code:

public class Test1 {
  public Object max(Object o1, Object o2) {
    if ((Comparable)o1.compareTo(o2) >= 0) {
      return o1;
    }
    else {
      return o2;
    }
  }
}

a.         The program has a syntax error because Test1 does not have a main method.
b.         The program has a syntax error because o1 is an Object instance and it does not have the compareTo method.
c.         The program would compile if ((Comparable)o1.compareTo(o2) >= 0)
is replaced by ((ComparableTo(o1).compareTo(o2) >= 0).
d.         b and d are both correct.

10.a). Complete the code:
public class Test {
  public static void main(String[] args) {
    int i = 4; int j = 5;

    // Fill in the code to invoke the sum method and display the sum of i and j.


}
Sysem.out.println(sum(I,j));
}

Public static int sum ( int I, int j )
Return (I,j)

}




  }

  public static int sum(int i, int j) {
    // Fill in the code here to return the sum of i and j.
    
  }
}

b). Complete the code:
public class Test {
  public static void main(String[] args) {
    int i = 4; int j = 5;

    // Fill in the code to invoke the printSum method to display the sum of i and j.

10 b

Public static void main (string [] args){

Int I = 4 j = 5

printSum(i.j)

}

Public static void printSum(int I, int j)
{
System.out.println(i+j)



  }

  public static void printSum(int i, int j) {
    // Fill in the code here to display the sum of i and j.
    
  }
}




10 a


}
Sysem.out.println(sum(I,j));
}

Public static int sum ( int I, int j )
Return (I,j)

}



10 b

Public static void main (string [] args){

Int I = 4 j = 5

printSum(i.j)

}

Public static void printSum(int I, int j)
{
System.out.println(i+j)


No comments:

Post a Comment