- •Import java.Util.*;
 - •II and III only
 - •I and II only
 - •24) Consider the following uml class diagram.
 - •25) Consider the following uml class diagram.
 - •II only
 - •30) Consider the following class definitions.
 - •III only
 - •I and II only
 - •II only
 - •I and III only
 - •II and III only
 - •I and II only
 - •I and III only
 - •II only
 - •I and III only
 
I and II only
34) Consider the following Java class definitions.
public class Publication { private String author; public Publication() { this.author = "anonymous"; } public String getAuthor() { return author; } } public class Book extends Publication { private int edition; //No default constructor public Book(int edition) { this.edition = edition; } }
Based on these class definitions, which of the following program segments will compile?
Book book = new Book(2); System.out.println(book.author);
Book book = new Book(2); System.out.println(book.getAuthor());
Book book = new Book();
II only
35) Which of the following statements about Java constructors is (are) true for any given class?
Constructors can be overloaded.
If a constructor is not defined, then one will be automatically created.
One constructor can invoke another constructor using the keyword this.
I and III only
36) In Java, a(n) _____ is an instance of a(n) _____.
object, class
37) In Java, instance fields and methods of an object can be accessed with which of the following operators?
.
38) Consider the following Java program segment.
String s1 = new String("ABC"); String s2 = new String("ABC"); Boolean b1 = (s1 == s2); s2 = new String("XYZ"); s1 = s2; Boolean b2 = (s1 == s2);
At the end of execution of the segment, the value of b1 will be _____ and the value of b2 will be _____.
false, true
39) Consider the following Java program segment.
String s1 = new String("ABC"); String s2 = new String("ABC"); Boolean b1 = (s1 == s2); Boolean b2 = s1.equals(s2);
At the end of execution of the segment, the value of b1 will be _____ and the value of b2 will be _____.
false, true
40) When comparing for equality in Java, primitives are compared with the operator _____, and objects are compared with the operator _____.
==, .equals
41) In Java, which of the following modifiers indicates that a field cannot be accessed from outside its class?
private
42) Consider the following Java program segment.
public class Foo { public static int x; }
If two separate Foo objects are instantiated, x is created in memory _____ time(s) because it is a _____ variable.
one, class
43) In Java, class variables must be declared with which of the following modifiers?
static
44) Which of the following is (are) true of static class variables in Java?
They can be accessed regardless of whether or not a class object has been instantiated.
They must be initialized with constructors.
I only
45) In Java, which of the following is (are) true of constructors?
Only one constructor may be present in each class.
A constructor must have the same name as its class.
Argument lists can be passed to constructors.
II and III only
46) Consider the following Java program segment.
class Foo { private int x = 1; public Foo() {x = 2;} public Foo(int xVal) {x = xVal;} } Foo f = new Foo();
What will be the value of f.x at the end of execution of the segment?
2
47) Which of the following is (are) true of static methods in Java?
They can access non-static variables in their class.
They must be associated with an instantiated object.
None
48) Which of the following is an example of a method return-type in Java?
void
49) Which of the following Java method declarations does not require that the method return a value?
public void myMethod();
50) In Java, defining more than one method having the same name within a particular class is called
overloading
51) Of the following names, which best follows the Java naming convention for a read accessor for variable myVal?
getMyVal()
52) Which of the following Java program segments will compile and execute without error?
int[] numbers = {1, 2, 3}; for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }
int[] numbers = {1, 2, 3}; for (int i = 0; i < numbers.length; i--) { System.out.println(numbers[i]); }
I only
53) In Java, which of the following is (are) true of both arrays and ArrayList objects?
Both use the bracket operator and an index to access individual elements.
Both are resizable.
Elements are inserted into both with the method add.
None
54) What will be output when the following Java program segment is executed?
ArrayList<String> animals = new ArrayList<String>(); animals.add("cat"); animals.add("dog"); for (Iterator<String> i = animals.iterator(); i.hasNext(); ) { System.out.print(i.next().charAt(0)); }
cd
55) What will be output when the following Java program segment is executed?
ArrayList<String> animals = new ArrayList<String>(); animals.add("cat"); animals.add("dog"); animals.add("dog"); for (int i = 0; i < animals.size(); ++i) { if (animals.get(i).equals("dog")) { animals.remove(i); } } System.out.println(animals.size());
2
56) What will be output when the following Java program segment is executed?
String[] strings = {"bird", "dog"}; for (int i = strings.length - 1; i >= 0; --i) { System.out.print(strings[i].length() + " "); }
3 4
57) In Java, an individual component of an array can be accessed using an index and which of the following operators?
[ ]
58) Consider the following Java code segment.
int[] foo = new int[5];
At the end of execution of the segment, what will be the value of foo[2]?
0
59) A reference variable r in Java can hold a reference to which of the following?
An object having the same declared type as r
An object of any subclass of the declared type of r
An object of any superclass of the declared type of r
