Java Exercise
Java Exercise
Java Exercise
2. A selection sort searches an array looking for the smallest element in the array, then
swaps that element with the first element of the array. The process is repeated for the
subarray beginning with the second element. Each pass of the array places one element
in its proper location. For an array of n elements, n - 1 passes must be made, and for
each subarray, n - 1 comparisons must be made to find the smallest value. When the
subarray being processed contains one element, the array is sorted. Write recursive
method selectionSort to perform this algorithm.
5. Write a Java program that prompts the user to enter a name. The program will compare
the name with a list of names from a file named Names.txt. The program will then
display the total number of names in the file that matches with the name given by user ?
6. Write a complete Java program that prompt the user to enter TEN (10) integers and keep
these integers in an array named num[]. Next display in reverse order all the integers that
are greater than 15, and calculate and display the total of all integers that are smaller
than 15.
7. Create a class called Complex to perform arithmetic operations with complex numbers.
a. Use double variables to represent the fields of the class.
b. Provide a no-argument constructor with default values in case no initializers are
provided.
c. Provide a constructor that enables an object of this class to be initialized when it
is declared.
d. Provide public operations that perform the following :
i. Add two Complex numbers.
ii. Subtract two Complex numbers.
iii. Multiply two Complex numbers.
iv. Divide two Complex numbers.
v. Print Complex numbers in the form (a + i b), where a is the real part and b
is the imaginary part.
8. Name ______________________
Assume the following:
String s, t, h, a;
String n, e;
int i;
h = "Hello";
s = " How are you? ";
a = "abc";
n = null;
e = "";
Give the values of the following expressions, or illegal.
1 __________ h.length()
2 __________ h.substring(1)
3 __________ h.toUpperCase()
4 __________ h.toUpperCase().toLowerCase()
5 __________ h.indexOf("H")
6 __________ h.startsWith("ell")
7 __________ "Tomorrow".indexOf("o")
8 __________ "Tomorrow".indexOf("o", 3)
9 __________ "Tomorrow".lastIndexOf('o')
10 __________ "Tomorrow".substring(2,4)
11 __________ a.length() + a
12 __________ "a = \"" + a + "\""
13 __________ (a.length() + a).startsWith("a")
14 __________ a.length() + a.startsWith("a")
15 __________ ">>>" + a.length() + a.startsWith("a")
16 __________ a.substring(1,3).equals("bc")
17 __________ a.substring(1,3) == "bc"
18 __________ "a".compareTo("c")
19 __________ "a".compareTo("A")
20 __________ s.trim().charAt(2)
10. What is the range of values that can be assigned to a variable of type short?
A. Depends on the underlying hardware
B. 0 through 216 − 1
C. 0 through 232 − 1
D. −215 through 215 − 1
E. −231 through 231 − 1
Choose the valid identifiers from those listed here. (Choose all that apply.)
A. BigOlLongStringWithMeaninglessName
B. $int
C. bytes
D. $1
E. finalist
14. Which of the following may appear on the left-hand side of an instanceof operator?
A. A reference
B. A class
C. An interface
D. A variable of primitive type
16. When a byte is added to a char, what is the type of the result?
A. byte
B. char
C. int
D. short
E. You can’t add a byte to a char.
17. Which of the following may follow the static keyword? (Choose all that apply.)
A. Class definitions
B. Data
C. Methods
D. Code blocks enclosed in curly brackets
24. Given arrays a1 and a2, which call returns true if a1 and a2 have the same length, and
a1[i].equals(a2[i]) for every legal index i?
A. java.util.Arrays.equals(a1, a2);
B. java.util.Arrays.compare(a1, a2);
C. java.util.List.compare(a1, a2);
D. java.util.List.compare(a1, a2);
25. Which of the statements below are true? (Choose all that apply.)
A. When you construct an instance of File, if you do not use the file-naming
semantics of the
local machine, the constructor will throw an IOException.
B. When you construct an instance of File, if the corresponding file does not exist
on the local
file system, one will be created.
C. When an instance of File is garbage collected, the corresponding file on the
local file system
is deleted.
D. None of the above.
28. Suppose prim is an int and wrapped is an Integer. Which of the following are legal Java
statements? (Choose all that apply.)
A. prim = wrapped;
B. wrapped = prim;
C. prim = new Integer(9);
D. wrapped = 9;
In the following code, what are the possible types for variable result? (Choose the
most complete
true answer.)
1. byte b = 11;
2. short s = 13;
3. result = b * ++s;
A. byte, short, int, long, float, double
B. boolean, byte, short, char, int, long, float, double
C. byte, short, char, int, long, float, double
D. byte, short, char
E. int, long, float, double