1. What is the output for this program?

static public void main(String[] args) {
   a();
}//end of main

static void a() {
   b();
}//end of a()

static void b() {
   c();
}//end of b()

static void c() {
   d();
}//end of c()

static void d() {
   int i = 1;
   int j = 0;

System.out.println(i/j);
}//end of d()

A) i/j
B) 0
C) Exception in thread "main" java.lang.ArithmeticException: / by zero
D) 1
2. What is the output for this program?

static public void main(String[] args) {

   try {
        System.out.println("Before division");

         int i = Integer.parseInt(args[0]);
         int j = Integer.parseInt(args[1]);

   System.out.println(i/j);
   System.out.println("After division");
}

catch (ArithmeticException e ) {
    System.out.println("Arithmetic exception");
}

catch (ArrayIndexOutOfBoundsException e) {

   System.out.println("ArrayIndexOutOfBoundsException");
}


A) Before division 0/1
B) Before division ArrayIndexOutOfBoundsException
C) 0 After division
D) Arithmetic exception
3. What is the output for this program?

static public void main( String[] args ) {

    for(int port = 1; port <= 2; port++) {

try {
ServerSocket server = new ServerSocket(port);
}

       catch (IOException e) {
          System.out.print("There is a server on port " + port + ".");
       }//end of catch
    }//end of for
}//end of main

A) ArrayIndexOutOfBoundsException
B) There is a server on port 0, 1, 2.
C) There is a server on port 1.There is a server on port 2.
D) IOException e
4. What is the output for this program?

float array1[] = new float[3];
array1[0] = -3.45f;
array1[1] = 7.7f;
array1[2] = 101.56f;
float array2[] = new float[3];
array2 = array1;
array2[1] = 100;
System.out.print(array2[0]);
System.out.print(array2[1]);
System.out.print(array2[2]);

A) -3.45 100.0 101.56
B) ArrayIndexOutOfBoundsException
C) -3.45 7.7 101.56
D) array 0 array 1 array 2
5. What is the output for this program?

charArray[] = {'h','a','p','p','y',' ','b','i','r','t','h',' ','d','a','y'};

String s = new String("hello");
String s1 = new String(s);

String s2 = new String(charArray);

String s3 = new String(charArray,6, 5);

String output = "s1= " + s1 + "s2= "+ s2 + "s3= "+ s3;

A) happy bith day happy birth day
B) hello happy birthday
C) ArrayIndexOutOfBoundsException
D) hello happy birth day birth
6. Which of the following correctly initializes an array arr to contain four elements each which value 0?
I) int [] arr = {0, 0, 0, 0};
II) int [] arr = new int [4];
III) int [] arr = new int [4];
         for (int =0; i < arr.length; i++)
                arr[i] = 0;

A) I and III
B) I, II, and III
C) II and III
D) I and II
7. Refer to the following class:
public class Tester {
   private int[] testArray = {3, 4, 5};
    //add 1 to n
    public void increment(int n) {
      n++;
public void testMethod( )  {
   for (int i = 0; i < testArray.length; i++)  {
     increment(testArray[i]);
     System.out.print(testArray[i] + " ");
     }
   }
}
What output will be produced by invoking testMethod for a Tester object?

A) 4 5 6
B) 3 4 5
C) 5 6 7
D) No output will be produced.
8. What will be output from the following code segment, assuming it is in the same class as the doSomething method?

int [] arr = {1, 2, 3, 4};
doSomething(arr);
System.out.print(arr[1] + " ");
System.out.print(arr[3]);
...
public void doSomething(int[] list) {
   int[] b = list;
   for (int i=0; i < b.length; i++)
       b[i] = i;
}

A) 2 4
B) 0 2
C) 0 3
D) 1 3
9. Consider the getData method below:
public void getData(int[] list) {
   System.out.println("Enter 10 integers ");
   list = new int[10];
   for (int i=0; i < 10; i++)
      list[i] = IO.readInt(); //read user input
}
A method in the same class calls getData as follows:
int [] array = new int [10];
    getData(array);
    for (int i = 0; i < 10; i++)
        System.out.print(array[i] + " ");
What will be the effect of running this code?

A) A NullPointerException will be thrown.
B) Ten zeroes will be the output.
C) The ten integers entered by the user will be the output.
D) An ArrayIndexOutOfBoundsException will be thrown.
10. Consider this program segment:
   for ( int = 2; i < k; i++)
       if (arr[i] < someValue)
           System.out.print("SMALL");
What is the maximum number of times that SMALL can be printed?

A) 0
B) k
C) k-1
D) k-2