Name_______________________________ Your final answer to each question should appear on this sheet.
v22.0002, Sample questions for the final
For the String problems, you may use the charAt(j), substring()
and length() methods.
None of the methods you write should read from the keyboard
or write to the screen. Don't call System.out
1a Given the following method heading
public static int firstOccur(char ch, String s)
write a method that returns the first occurrence of the character stored in ch in the string s. If the character is found in the string,
your method should return its position. Thus if s is “abcdefg” and
the value of ch is d, your program would return 3. If
character is not found in the string, your program should return -1.
2. Write a method with the heading public static Boolean primeness(int number) that determines if number is a prime number.
3.. Complete the following method that adds the odd numbers from 1 to
public static int add(int num)
//adds odd numbers from 1 to num
int sum = __________________________;
for(int j = 1; ___________________; j++)
sum = _______________________;
4. Complete the following method that places odd numbers found in the
in consecutive elements of the array x.
public static void assign(String s)
//places odd numbers in consecutive elements of array
int len = __________;
int x = new int[_________________];
int index = 0;
for(int j = 0; j < len; j++)
digit = s.charAt(j) - '0';//convert to int digit
if( ______________________)//test for odd #
x[____________] = digit;
System.out.println("The odd numbers are");
for(int j = 0; j < index; j++)
5. Write a method public static void eliminate(int x, int y)
that copies all the elements of the array x except the largest value
into the array y.