Practice For The Final Exam (Computer Science 1)
Practice For The Final Exam (Computer Science 1)
1. Which special type of method has no return type (not even void)?
a. an accessor
b. a mutator
c. the toString method
d. a constructor
3. A method that is used to assign a new value to an instance variable is called a(n) ______________.
a. mutator
b. accessor
c. constructor
d. object modifier
5. Which of the following is a correct header for a static Java method that will accept an array of integer
values and return the number of positive values in the array.
a. public static int positive ( int array )
b. public static positive ( int array[ ] )
c. public static pos ( int [ ] array ) : int
d. public static int count ( int x [ ] )
6. Which of the following import statements should be included in a program that uses the Scanner class?
a. import java.util.*;
b. import java.Scanner.*;
c. import javax.swing.*;
d. import java.awt.*;
Page 1
Assume you have the following method defined in a class called Store:
8. ___________ methods are accessible only within the class where they are declared.
a. public
b. static
c. private
d. protected
Assume you have the following method defined in a class called MyFunctions:
10. A non-static method is called using ___________ followed by a period, the name of the method and a
parameter list.
Page 2
11. Instantiation is the process of creating a(n) __________________.
a. class
b. object
c. Scanner
d. method
12. A method that uses a loop, instead of recursion, to solve a problem is called a(n) ____________ method.
a. redundant
b. conditional
c. iterative
d. encapsulated
14. The following method is defined in a class called Carton. What type of method is this?
public int getSize ( ) {
return size;
}
a. accessor
b. mutator
c. constructor
d. protected
16. Math.random returns a(n) __________________ value greater than or equal to 0.0 and less than 1.0.
a. integer
b. float
c. double
d. String
Page 3
17. Which of the following will declare and allocate space for an array of 20 double elements?
a. double sum[ ] = new double[ 20 ] ;
b. double sum= new double [ 20 ];
c. double [ ] sum = new [ 20 ];
d. double [ ] sum = double [ 20 ];
18. When a method calls itself, the method is said to be a(n) ________________ method.
a. iterative
b. circular
c. reflexive
d. recursive
19. What are the minimum and maximum values of integers generated by the following statement?
a. min = 10 max = 23
b. min = 10 max = 13
c. min = 10 max = 22
d. min = 13 max = 23
20. Write a recursive Java method called length that will accept an integer parameter named k. The method
should find and return the length (the number of digits) of the parameter. Your method must work for all
integers. For negative values of k, the negative sign doesn't count as part of the length.
21. Write a print statement inside the following main method that will call the length method above and send it
the number 3856. Assume that the main method is in the same class as the length method.
Page 4
22. Write the Java program for a class named Textbook. The Textbook class should have three
instance variables: title (String), author (String), and year (int).
Write a default constructor that initializes title and author to the empty String and year to 0.
Output
public class FE2 {
24. Write a statement that will generate a random integer between 12 and 20 (inclusive) and store it in a
variable called p.
25. Programming: Write a public, static, recursive method called twoToThe that takes an integer argument
named n and returns 2 to the nth power. The method should work for any value of n.
26. Programming: Write a public, static, Java method named noSpaces that will accept an array of characters
as its parameter. The method should print all of the array elements that are not spaces, all on one line.
Do not ask the user for input and do not return anything.
Page 5
27. What are the minimum and maximum values of integers generated by the statement:
28. Java has eight _______________________ types including int, float, and boolean.
Output
30. What is printed by the following code segment?
Assume you have the following method defined in a class called MyFunctions:
Output
33. What is the output of the following code segment?
int [] array = { 7, 5, 3, 1, 4, 8 };
Page 6
34. A set of step-by-step instructions used to solve a problem or perform a task is called
a(n) _________________________.
35. Java allows multiple methods to be written with the same name as long as the different
versions of the method have different __________________________________.
36. Assuming that the appropriate import statement has been used and a Scanner object has been declared
with:
Write a Java statement will input a line of text (may include spaces) from the user and store it in a String
variable called sentence.
37. Using what you know about how the method is called, write the header for the random method from the
Math class.
38. Programming: Given the beginning of a class with its instance variables as shown below, write the
accessor method and the mutator method for lift.
39. Programming: Write a public, static, non-recursive method called reverse that will accept an integer
parameter named value. The method should print all of the digits of the value in reverse. If the value is
negative, the minus sign should appear in the front of the digits.
40. A(n) __________________________ method is designed to return the value of a private instance variable.
41. A(n) __________________________ method is designed to change the value of a private instance
variable.
42. A(n) __________________________ method is designed to initialize the values of the instance variables
in a class.
44. Non-static methods are called using _______________________________ followed by a period, the
method name, and a list of parameters.
Page 7
45. Static methods are called using _______________________________ followed by a period, the method
name, and a list of parameters.
46. A method that uses a loop (instead of recursion) to solve a problem is called a(n)
_________________________ method.
47. What is the value of x after the following code segment has executed?
48. Write one statement that will declare and allocate space for an array of 5 String references.
int a = x;
if (a > y) a = y;
if (a > z) a = z;
return a;
}
int d = find ( 5, 6, 0 );
System.out.println( d );
} // end main
} // end class
50. In one brief sentence, describe the purpose of the find method.
Page 8