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.)  Write a recursive method to compute the sum of numbers from 1 to some N.

 

 

public static sum(int N) {

 

 

 

 

 

 

 

 

 

 

 

 

}