Practice Test

 

 

1. What is wrong with the following code:

 

class Wrong {

     public static void main(String args[]) {

          int x = 3;

          printX();

     }

 

     public static void printX() {

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

     }

}

 

 

 

 

2.)  Fix the above using parameters

 

 

 

 

 

 

 

 

 

 

 

3.)  Why can’t you write a swap method in Java?

 

 

 

 

 

 

 

 

 

4.) Write a for loop that iterates 100 times.

 

 

 

 

 

 

 

 


5.) How would you, in Java, get a random number between 1 and 100?

 

 

 

 

 

 

 

 

6.)  Write a method called coinToss that returns 0 for heads and 1 for tails.

 

 

 

 

 

 

 

 

 

 

 

 

 

7.) For each of the following, provide the recursive definition and the base case:

 

            a.) Sum

 

 

 

 

 

 

            b.) Factorial

 

 

 

 

 

 

            c.)  Power

 

 

 

 

 

8.) Pick one of the above and write a recursive method in Java to calculate it.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

9.) What is an Object in Object Oriented Programming?  

 

 

 

 

 

 

 

 

 

 

 

 

 

10.) What does it mean for a program to be event-driven?

 

 

 

 

 

 

 

 

11.) Given the following method that you do not need to implement, but can use:

 

int min(int a[], int start);

 

that returns the position of smallest element in a[], starting at position a[start], write the code for Selection Sort.

 

 

void selectionSort(int[] data) {

 

 

 

 

 

 

 

 

 

 

 

 

 

}