0% found this document useful (0 votes)
183 views

Practice For The Final Exam (Computer Science 1)

The document contains a practice exam for a CS 172 final with 37 multiple choice and programming questions covering topics like method definitions, parameters, return types, recursion, arrays, and classes. It provides sample code and asks the reader to analyze, define, call, modify and write methods to test their understanding of fundamental Java concepts.

Uploaded by

Micah Villasana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
183 views

Practice For The Final Exam (Computer Science 1)

The document contains a practice exam for a CS 172 final with 37 multiple choice and programming questions covering topics like method definitions, parameters, return types, recursion, arrays, and classes. It provides sample code and asks the reader to analyze, define, call, modify and write methods to test their understanding of fundamental Java concepts.

Uploaded by

Micah Villasana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Name ____________________________________________

CS 172 - Practice for the final exam

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

2. _____________________ is used in a recursive method to stop the recursion.


a. A base case
b. A recursive step
c. An iterative step
d. A method call

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

4. Method overloading is ________________________.


a. writing two or more versions of a method with the same name but different return types
b. writing two or more versions of a method with the same name but different parameters
c. writing two more more versions of a method in two different classes
d. writing a method in a child class with the same header as a method in the parent class

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:

public static int calc( int k ) {


int n = 2;
for (int i = 1; i <= k; i++) {
n = n + 5;
} // end for
return n;
} // end method

7. What is the valueof Store.calc(4)?


a. 5
b. 12
c. 17
d. 22

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:

public static int getValue ( int x ) {


// base case
if (x <= 0)
return 0;
// recursive step
return 2 + getValue(x – 2);
}

9. What is the value of MyFunctions.getValue(3)?


a. 0
b. 2
c. 4
d. no return value because the method has infinite recursion

10. A non-static method is called using ___________ followed by a period, the name of the method and a
parameter list.

a. the class name


b. the word “call”
c. System.out.println
d. an object name

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

13. A method that is accessible from any program is a _________ method.


a. void
b. private
c. public
d. generic

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

15. A void method ________________________.


a. doesn't have any parameters
b. must be static
c. doesn't return a value
d. must have a return statement in the body of the method

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?

int y = (int) (Math.random() * 13) + 10;

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.

Clearly comment the base case and recursive steps.

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.

public static void main ( String args[ ] ) {

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.

 Write an accessor for each instance variable.

 Write a mutator for each instance variable.

 Write a toString method that returns a String in the format:

title by author, year

23. What is the output of the following program?

Output
public class FE2 {

public static int f( int x[] ) {


int s = 0 ;
for (int i = 0; i < x.length; i++)
s = s + x[i];
return s / x.length;
}

public static void main (String args[]) {


int[] a = {2, 1, 8, 9, 6};
int x1 = f(a);
System.out.println("x1: " + x1);
}
}

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:

int y = (int) (Math.random() * 8) + 7;

Minimum = ____________ Maximum = ____________

28. Java has eight _______________________ types including int, float, and boolean.

29. A _____________________ is used in a recursive method to stop the recursion.

Output
30. What is printed by the following code segment?

int[ ] array = {10, 11, 8, 3, 4};

for (int k = 0; k <= array.length / 2; k++)


System.out.print( array[array.length - k - 1] + " " );

Assume you have the following method defined in a class called MyFunctions:

public static int nofun ( int r ) {


if ( r <= 0 )
return 0;
return 1 + (r - 2);
}

31. What is the value of MyFunctions.nofun( 3 )?

32. What is the value of MyFunctions.nofun( -5 )?

Output
33. What is the output of the following code segment?
int [] array = { 7, 5, 3, 1, 4, 8 };

for (int p = 0; p < 3; p++) {

System.out.print( " " + array[p * 2] );

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:

Scanner scan = new Scanner(System.in);

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.

public class OrthopedicShoe {


private float size;
private String 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.

Examples: reverse( 18 ) should print 81


reverse ( -30 ) should print -03

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.

43. Instantiation is the process of creating a(n) __________________.

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?

String sent = "shetland pony";


char x = sent.charAt ( 3 );

48. Write one statement that will declare and allocate space for an array of 5 String references.

49. What is the output of the program shown below?


Output
public class FExam {

public static int find ( int x, int y, int z ) {

int a = x;
if (a > y) a = y;
if (a > z) a = z;
return a;
}

public static void main ( String args[ ] ) {

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

You might also like