Java Review
Java Review
| Given the following program:
|
Answer the following questions:
| Q 1: |
What does this program do when executed from the following command line:
java echo
|
|
|
| Q 2: |
What does this program do when executed from the following command line:
java echo This old man, he played one
|
|
|
| Q 3: | What does this program do when executed from the following command line:
java echo "This old man, he played one"
|
|
|
|
| Given the following program:
| Source code: runTests.java
|
|
public class V2 {
public static int[] vowelCount(String s) {
int[] ret = new int[5];
int i=0;
String temp="";
temp = s.toUpperCase();
for(i=0; i<5; i++)
ret[i] = 0; // no vowels found
for (i = 0; i < s.length(); i++)
switch (temp.charAt(i)) {
case 'A': ret[0]++; break;
case 'E': ret[1]++; break;
case 'I': ret[2]++; break;
case 'O': ret[3]++; break;
case 'U': ret[4]++; break;
default:
}
return ret;
}
public static void main(String[] args) {
int i = 0;
int[] vc = new int[5];
vc = vowelCount("This old man, he played one.");
for (i=0; i<5; i++)
System.out.println("vowel count[" + i + "] = " + vc[i]);
}
}
|
|
Answer the folling questions:
| Q 4: | What is the output from this program when executed from the following command line: java V2
|
|
|
| Q 5: | What does the third line of output represent?
|
|
|
| Q 6: | What is the purpose of the folling line from the vowelCount method?
temp = s.toUpperCase()
Where is toUpperCase() defined?
|
|
|
|
| Given the following program that uses a recursive method guessMyFunction called from the main progam:
| Source code: runTests.java
|
|
import java.util.Scanner;
public class runTests {
public static int guessMyFunction (int n) {
if (n < 0)
return -guessMyFunction (-n);
else if (n < 10)
return (n + 1) % 10;
else
return 10 * guessMyFunction (n / 10) + (n + 1) % 10;
}
public static void main(String[] args) {
int[] testData = {7, 42, -790, 89294};
int val = 0;
for (int i=0; i
|
|
Answer the folling questions:
| Q 4: | When the program is executed from the following command line: java runTests
the main progam will loop through the array named testData, and call guessMyFunction with each element.
Trace the values of guessMyFunction when called with a 0, 1, 2, 3, 4 and -1 and then identify what the program output when the main program is executed.
|
|
|
|
|