AP Computer Science - Semester 1 Review

Name:

For the following program:

public class CodeNinjas {

public static void main(String[] args) {

System.out.println("let's code...");

codeLikeAPro();

System.out.println("we rule!");

}

public static void codeLikeABeginner() {

System.out.println("I can make methods!");

}

public static void codeLikeAPro() {

codeLikeABeginner();

System.out.println("I can make whole classes!");

}

}

(2pts) 1. What are the names of all the methods defined or called in this code?

(4pts) 2. What is the console output?

What do the following Java expressions evaluate to, and what is the data type of the final value? Use the lines below each expression to show each step (operation) you take in evaluating the expression.

(4pts) 3. 7 / 2 + 8.4 / 2 + 1 * 0.8 (4pts) 4. 5 - 3 + "6 - 1" + (7 - 4) + 10 % 4 + 1

______

______

______

______

______

______

______

______

Final value: ______Final value: ______

Final data type: ______Final data type: ______

(4pts) 5. What is the console output of the following sequence of loops?

for (int outerCount = 1; outerCount <= 4; outerCount++) {

for (int innerCount = 1; innerCount <= outerCount; innerCount++) {

System.out.print('*');

}

System.out.println();

}

(8 pts.) 6. Write nested for loops to produce the following output:

12345

1234

123

12

1

(1pt) 7. Index values used with strings and arrays are… (circle one)

  1. generally zero-based, where 0 is the first value, and is followed by 1, 2, 3, and so on.
  2. found at the back of the book, and show the page number on which a term appears.
  3. the current market price for an index fund on a stock exchange.
  4. generally one-based, where 1 is the first value, and is followed by 2, 3, 4, and so on.

(1pt) 8. A constructor… (circle one)

  1. is needed for doubles and floats, but not ints or longs.
  2. is a request to access a specific Java package.
  3. is called with the keyword new followed by the class name, and creates and initializes an object before it can be used.
  4. must wear a hard-hat in work areas, according to state and federal safety guidelines.

Write down the method signatures for the following methods?

(2pts) 9. public static void aceTheFinal(String whichClass)

Answer:

(2pts) 10. public double catchThatDino(

int dimetrodon,

String gonna,

double pay)

Answer:

(6pts) 11. What is the output of the following program?

public class AnalyzeMe {

public static void main(String[] args) {

int number = change(5, 2, "first time");

change(getFavoriteNumber(), number, "second time");

System.out.println(number);

}

public static int getFavoriteNumber() {

return 8;

}

public static int change(int input, int delta, String message) {

int value = input + delta;

System.out.println(message);

System.out.println("updating " + input + " to " + value);

return value;

}

}

Given the variable declarations:

int a = 5;

int b = -2;

int c = 10;

Scanner s = new Scanner("10 types of people exist in the world... those who know binary"

+ " and those who don't");

What do the following expressions evaluate to? (1pt each)

12. a > 0 à ______16. a < 5 & b * 3 <= 0 à ______

13. b <= 4 à ______17. a == 5 || b == -2 || c == 25 à ______

14. (c != 10) à ______18. false & isLoneliestNumber(1) à ______

15. !(a + b <= 8) à ______19. s.hasNextInt() & c == s.nextInt() à ______

For the following questions, you may assume the isSpecial method is defined as follows to return true if letter is “special” and false if not:

public static boolean isSpecial(char letter) {

char lowercase = Character.toLowerCase(letter);

return lowercase == 'l' || lowercase == 'c' || lowercase == 'h'

|| lowercase == 's';

}

(8pts) 20. Fill in the body of the getSpecialCount method. It must return the total number of “special” letters contained in its String parameter. You may use the isSpecial method from above in your solution for getSpecialCount if you like.

public static int getSpecialCount(String text) {

}

(4pts) 21. Fill in the body of the startsWithTwoSpecials method. It must return true if its String parameter starts with two or more “special” letters. For example, it would return true for “Zero” and “Namesake” but false for “Z” and “none”. You may use the isSpecial method from above in your solution for startsWithTwoSpecials if you like. Try to do it using a single return statement.

public static boolean startsWithTwoSpecials(String text) {

}

Given the following array definitions:

int[] nums = {-4, -2, 0, 2, 4, 6};

int[][] moreNums = new int[3][8];

What do the following expressions evaluate to? (1pt each)

22. nums[0] ______25. moreNums[0][0] ______

23. nums.length ______26. moreNums.length ______

24. nums[nums.length – 1] ______27. moreNums[0].length ______

(3pts) 28. Consider the following method.

public static int mystery(int[] arr) {

int x = 0;

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

x += arr[k];

}

return x;

}

Assume that the array nums has been declared and initialized as follows.

int[] nums = {3, 6, 1, 5, 1, 4, 2};

What value will be returned as a result of the call mystery(nums) ? Answer: ______

(8pts) 29. Fill in the method body so that it returns the sum of values in the 2D array.

public static int getSum(int[][] moreNums) {

}

Questions 30-35 refer to the classes declared below:

public class Book {

public String title;

public String author;

public int year;

}

public class BookShelf {

public String shelfID;

public Book[] books;

}

public class Library {

public String address;

public BookShelf[] shelves;

}

Assume you have an instance of BookShelf called shelf, and write the expressions to refer to:

(example) The ID of the bookshelf: shelf.shelfID

(1pt) 30. The number of books on the bookshelf:

(1pt) 31. The title of the first book on the bookshelf:

(1pt) 32. The author of the last book on the bookshelf:

Assume you have an instance of Library called library, and write the expressions to refer to:

(1pt) 33. The number of shelves in the library:

(1pt) 34. The number of books on the third shelf in the library:

(6pts) 35. Write the body for getBookCount(Library) that will return the total number of books in the library:

public int getBookCount(Library library) {

}

Questions 36-39 refer to the Student class declared below:

public class Student {

private String firstName;

private String lastName;

private int age;

public Student(String firstName, String lastName) { /* implementation not shown */ }

public Student (String firstName, String lastName, int age) {

/* implementation not shown */

}

// Sets this student’s age.

public void setAge(int age) { /* implementation not shown */ }

// Increases this student’s age by one.

public void happyBirthday() { /* implementation not shown */ }

// Returns true if this student equals s, false otherwise.

public boolean equals(Student s) { /* implementation not shown */ }

// Returns true if this student is older than s, false otherwise.

public boolean olderThan(Student s) { /* implementation not shown */ }

// Returns this student as a String in the form "firstName lastName (age)".

public String toString() { /* implementation not shown */ }

}

(1pt) 36. Which of the following is a false statement about the methods?

(A)  setAge(int) and toString() are both accessor methods.

(B)  happyBirthday() is a mutator method.

(C)  The Student class is a good example of encapsulation.

(D)  Student(String, String) is a constructor.

(E)  olderThan(Student) is an accessor method.

(1pt) 37. Which of the following represents correct implementation code for the first constructor above?

(A) firstName = this.firstName;
lastName = this.lastName;

(B) firstName = firstName;
lastName = lastName;

(C) Student = new Student(firstName, lastName);

(D) this.firstName = firstName;
this.lastName = lastName;

(E) firstName = "Bob";
lastName = "The Builder";

(3pts) 38. Write the implementation for the equals(Student) method in the Student class:

// Returns true if this student has exactly the same first name, last name, and age as

// student s, false otherwise.

public boolean equals(Student s) {

}

(1pt) 39. A client class has a display method that writes the student represented by its parameter:

// Outputs student s in the form "firstName lastName (age)".

public void display(Student s) {

/* method body */

}

Which of the following are correct replacements for /* method body */?

I.  Student s2 = new Student(s);
System.out.println(s2);

II.  System.out.println(s.toString());

III.  System.out.println(s.firstName + " " + s.lastName + " (" + s.age + ")");

IV.  System.out.println(s);

(A) I only

(B) II only

(C) III only

(D) IV only

(E) II and III

(F)  II and IV

(G) I, II, and III

(H) none of the above

Question 40 refers to the TextExcel project we worked on this past quarter, but you must assume you’re working with the code presented here rather than the actual code you turned in. Assume you have the following Cell class:

public class Cell {

private String originalInput;

public Cell(String originalInput) {

this.originalInput = originalInput;

}

public String toString() {

// This is the string that will be displayed in the spreadsheet. It does not

// need to be centered or truncated, because the spreadsheet will take care of

// formatting. Subclasses of Cell should override this method to return proper

// strings depending on their data.

return "";

}

public String getOriginalInput() {

return originalInput;

}

public double getValue() {

// This is the value that FormulaCell will use when calculating formulas that

// reference this cell. Subclasses of Cell should override this method to

// return proper values depending on their data.

return 0.0;

}

}

And assume that your spreadsheet class uses the following method to create cells:

public Cell createCell(String originalInput) {

Cell newCell;

if (isText(originalInput)) {

newCell = new TextCell(originalInput);

} else if (isDate(originalInput)) {

newCell = new DateCell(originalInput);

} else if (isNumber(originalInput)) {

newCell = new NumberCell(originalInput);

} else if (isFormula(originalInput)) {

newCell = new FormulaCell(originalInput, this);

} else if (isBoolean(originalInput)) {

newCell = new BooleanCell(originalInput);

} else {

throw new IllegalArgumentException("Unknown input: " + originalInput);

}

return newCell;

}

(12pts) 40. Define the BooleanCell class. Here are its requirements and characteristics:

●  It is created when the user enters yes, no, true, or false to set a cell. For example: D3 = yes

●  If the user entered yes or true, the cell will display in the spreadsheet as the String "yes". If the user entered no or false, the cell will display in the spreadsheet as the String "no".

●  If the user entered yes or true, the cell’s value in formulas must be 1. If the user entered no or false, the cell’s value in formulas must be 0.

Write the complete code for BooleanCell below: