Advanced Placement Computer Science Section 6 Quiz


  1. Which of the following is not a reserved word in Java? A) super B) extends C) void D) class E) none of the above

  2. Which of the following will return the phrase “bad quiz” from the string “what a bad quiz!” which is referenced by the variable Q A) Q.substring(8,16); B) Q.substring(7,16); C) Q.instr(7,15); D) Q.mid(7,15); E) none of the above

  3. Which of the following will return the length of a String g A) g.getlength(); B) g.len(); C) strlen(g); D) g.length(); E) none of the above

  4. To declare a method as a class method, you need to include which keyword in the declaration (or signature) of the method? A) void B) int C) static D) protected E) none of the above

  5. In order to declare a class variable one must include what keyword in the declaration of the variable A) void B) int C) static D) protected E) none of the above

  6. The default value of all references in Java is A) 0 B) undefined C) null D) void E) none of the above

  7. A variable declared in a method declaration (ie: heading, signature or prototype in C/C++ terms) is visible to A) only that method B) only that class C) the entire program D) only the method declaration E) actually, it can't be used at all

  8. In order to define a variable that cannot change its value, one need declare it as type A) stubborn B) const C) int D) final E) none of the above

  9. A default constructor for a class satchel would be started as follows: A) satchel { B) satchel() { C) satchel::satchel() { D) public satchel() { { E) none of the above

  10. What object can be used to display dialog boxes to the user? A) MessageBox B) System.cout C) MsgBox D) JOptionPane E) none of the above


For the following questions write on your answer sheet T if the statement is true, f otherwise

  1. Class methods require an instance of a class in order to execute.

  2. Class methods must be declared public.

  3. When extracting a substring from a string the index of the first parameter to substring are counted starting with 1

  4. Recursive methods should have a base case

  5. If no other object is specified when executing a method, the default executor (this) is used. (ie: if you run a line like this: dosomething(); Java will translate it to this.dosomething();)

  6. The following method is an example of tail recursion:

      public static void remainder(int x, int y) {

        if (x == 0) return y;

        else return x / y;

      }

  1. The following method will properly calculate the factorial of a number:

public static int factorial(int x) {

return x * factorial(x-1);

}

  1. The following is a proper example of tail recursion for a method that counts the number of times b occurs in a String

public static int countbhelp(String s, int currentcount) {

if (s.length() == 0) return currentcount;

else if (s.substring(0,1).equals(“b”))

return countbhelp(s.substring(1,s.length()), currentcount + 1);

else return countbhelp(s.substring(1,s.length(), currentcount);

}

public static int countb(String s) {

return countbhelp(s,0);

}

  1. Embedded recursion requires an extra parameter in order to calculate the result

  2. The second parameter of substring starts counting from 1

  3. I'm a lumberjack, and I'm ok. I sleep all night, and work all day!”