Está en la página 1de 7

1.

The Fibonacci series


0, 1, 1, 2, 3, 5, 8, 13, 21, …
begins with the terms 0 and 1 and has the property that each succeeding term is the sum
of the two preceding terms.
a) Write a nonrecursive method fibonacci( n ) that calculates the nth Fibonacci number.
Incorporate this method into an applet that enables the user to enter the value of n.
b) Determine the largest Fibonacci number that can be printed on your system.
c) Modify the program of part a) to use double instead of int to calculate and return
Fibonacci numbers and use this modified program to repeat part b).

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.

3. Write a program for Shell Sort


4. For each of the following, write a single statement that performs the indicated task.
a) Compare the string in s1 to the string in s2 for equality of contents.
b) Append the string s2 to the string s1, using +=.
c) Determine the length of the string in s1.

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)

9. What is the difference between an Abstract class and Interface in Java ? or


can you explain when you use Abstract classes ?

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

11. Consider the following application:


1. class Q6 {
2. public static void main(String args[]) {
3. Holder h = new Holder();
4. h.held = 100;
5. h.bump(h);
6. System.out.println(h.held);
7. }
8. }
9.
10. class Holder {
11. public int held;
12. public void bump(Holder theHolder) {
13. theHolder.held++; }
14. }
15. }
What value is printed out at line 6?
A. 0
B. 1
C. 100
D. 101

Choose the valid identifiers from those listed here. (Choose all that apply.)
A. BigOlLongStringWithMeaninglessName
B. $int
C. bytes
D. $1
E. finalist

12. Which of the following code snippets compile?


A. Integer i = 7;
B. Integer i = new Integer(5); int j = i;
C. byte b = 7;
D. int i = 7; byte b = i;
E. None of the above

13. What is the value of x after the following line is executed?


x = 32 * (31 - 10 * 3);
A. 32
B. 31
C. 3
D. 704
E. None of the above

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

15. Which of the following operations might throw an ArithmeticException?


A. +
B. -
C. *
D. /
E. None of these

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

18. Which of the following statements are true?


A. An abstract class may be instantiated.
B. An abstract class must contain at least one abstract method.
C. An abstract class must contain at least one abstract data field.
D. An abstract class must be overridden.
E. An abstract class must declare that it implements an interface.
F. None of the above.

19. This question concerns the following class definition:


1. package abcde;
2.
3. public class Bird {
4. protected static int referenceCount = 0;
5. public Bird() { referenceCount++; }
6. protected void fly() { /* Flap wings, etc. */ }
7. static int getRefCount() { return referenceCount; }
8. }
20. Which statement is true about class Bird and the following class Parrot?
1. package abcde;
2.
3. class Parrot extends abcde.Bird {
4. public void fly() {
5. /* Parrot-specific flight code. */
6. }
7. public int getRefCount() {
8. return referenceCount;
9. }
10. }
A. Compilation of Parrot.java fails at line 4 because method fly() is protected in the
superclass, and classes Bird and Parrot are in the same package.
B. Compilation of Parrot.java fails at line 4 because method fly() is protected in the
superclass and public in the subclass, and methods may not be overridden to be more
public.
C. Compilation of Parrot.java fails at line 7 because method getRefCount() is static in the
superclass, and static methods may not be overridden to be nonstatic.
D. Compilation of Parrot.java succeeds, but a runtime exception is thrown if method fly()
is ever called on an instance of class Parrot.
E. Compilation of Parrot.java succeeds, but a runtime exception is thrown if method
getRefCount() is ever called on an instance of class Parrot.

21. What would be the output from this code fragment?


1. int x = 0, y = 4, z = 5;
2. if (x > 2) {
3. if (y < 5) {
4. System.out.println("message one");
5. }
6. else {
7. System.out.println("message two");
8. }
9. }
10. else if (z > 5) {
11. System.out.println("message three");
12. }
13. else {
14. System.out.println("message four");
15. }
A. message one
B. message two
C. message three
D. message four
22. Consider these classes, defined in separate source files:
1. public class Test1 {
2. public float aMethod(float a, float b)
3. throws IOException {...
4. }
5. }
1. public class Test2 extends Test1 {
2.
3. }
Which of the following methods would be legal (individually) at line 2 in class
Test2? (Choose
all that apply.)
A. float aMethod(float a, float b) {...}
B. public int aMethod(int a, int b) throws Exception {...}
C. public float aMethod(float a, float b) throws Exception {...}
D. public float aMethod(float p, float q) {...}

23. Consider the following class definition:


1. public class Test extends Base {
2. public Test(int j) {
3. }
4. public Test(int j, int k) {
5. super(j, k);
6. }
7. }
Which of the following are legitimate calls to construct instances of the Test
class? (Choose all
that apply.)
A. Test t = new Test();
B. Test t = new Test(1);
C. Test t = new Test(1, 2);
D. Test t = new Test(1, 2, 3);
E. Test t = (new Base()).new Test(1);

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.

26. Suppose class X contains the following method:


void doSomething(int a, float b) { … }
Which of the following methods may appear in class Y, which extends X?
A. public void doSomething(int a, float b) { … }
B. private void doSomething(int a, float b) { … }
C. public void doSomething(int a, float b)
throws java.io.IOException { … }
D. private void doSomething(int a, float b)
throws java.io.IOException { … }

27. Which of the following classes implement java.util.List?


A. java.util.ArrayList
B. java.util.HashList
C. java.util.StackList
D. java.util.Stack

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