For questions 1-4, assume x and y are String variables with x = "Hello" and y = null.
1) The result of (x = = y) is
a) true
b) false
c) a syntax error
d) a run-time error
e) x being set to the value null
Answer: b. Explanation: x is a String instantiated to the value "Hello" and y is a String that has not yet been instantiated, so they are not the same String. (x = = y) is a condition, testing to see if x and y are the same String (that is, x and y reference the same item in memory), which they don't, so the result is false.
2) The result of x.length( ) + y.length( ) is
a) 0
b) 5
c) 6
d) 10
e) a thrown exception
Answer: e. Explanation: The statement y.length( ) results in a thrown NullPointException because it is not possible to pass a message to an object that is not currently instantiated (equal to null).
3) If the operation y = "Hello"; is performed, then the result of (x = = y) is
a) true
b) false
c) x and y becoming aliases
d) x being set to the value null
e) a run-time error
Answer: b. Explanation: While x and y now store the same value, they are not the same String, that is, x and y reference different objects in memory, so the result of the condition (x = = y) is false.
4) If the operation y = x; is performed, then the result of (x = = y) is
a) true
b) false
c) x being set to the value null while y retains the value "Hello"
d) y being set to the value null while x retains the value "Hello"
e) x being set to y, which it is already since y = x; was already performed
Answer: a. Explanation: When y = x; was performed, the two variables are now aliases, that is, they reference the same thing in memory. So, (x = = y) is now true.
Java
- In Java, the mechanism that allows you to combine data and operations on the data into a single unit is called a(n)
- Which of the following is a legal way to declare and instantiate an array of 10 Strings?
- The statement System.out.println(values[7]); will
- Which of the following loops would adequately add 1 to each element stored in values?
- What is the value of values.length?
- What is returned by values[3]?
- A listener is an object that
- JOptionPane is a class that provides GUI
- In order to have some code throw an exception, you would use which of the following reserved words?
- An exception can produce a “call stack trace” which lists
- A Java program can handle an exception in several different ways. Which of the following is not a way that a Java program could handle an exception?
- Assume that you are defining a class and you want to implement an ActionListener. You state addActionListener(this); in your class’ constructor. What does this mean?
- In order to implement Comparable in a class, what method(s) must be defined in that class?
- Which of the following interfaces would be used to implement a class that represents a group (or collection) of objects?
- An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship?
- Static methods cannot
- Which of the following methods is a static method? The class in which the method is defined is given in parentheses following the method name.
- An object may be made up of other objects.
- Method decomposition is the process of creating overloaded versions of a method that do the same thing, but operate on different data types.
- The println method on System.out is overloaded.
- A method defined without a return statement will cause a compile error.
- If a method takes a double as a parameter, you could pass it an int as the actual parameter.
- The different versions of an overloaded method are differentiated by their signatures.
- The number and types of the actual parameters must match the number and types of the formal parameters.
- The return statement must be followed a single variable that contains the value to be returned.