Q1. Give: Which is correct?
A. Employee takes advantage of composition.
B. Employee "has-an" Email.
C. Employee "is-a" LetterPrinter.
D. Employee has low cohesion.
Answer: D
Explanation:
The relationship between Employee and e-mail is poorly implemented here.
There is low cohesion.
Note:
Low cohesion is associated with undesirable traits such as being difficult to maintain,
difficult to test, difficult toreuse, and even difficult to understand.
Cohesion is decreased if:
The functionalities embedded in a class, accessed through its methods, have little in
common. Methods carryout many varied activities, often using coarsely-grained or
unrelated sets of data. Disadvantages of lowcohesion (or"weak cohesion") are:
Increased difficulty in understanding modules.
Increased difficulty in maintaining a system, because logical changes in the domain affect
multiple modules,and because changes in one module require changes in related modules.
Increased difficulty in reusing amodule because most applications won't need the random
set of operations provided by a module.Reference:Cohesion (computer science)
Q2. Given:
What is the result?
A. fast slow
B. fast goes
C. goes goes
D. fast fast
E. fast followed by an exception
F. Compilation fails
Answer: F
Explanation:
Line:Vehicle v = new Sportscar();
causes compilation failure:
error: cannot find symbol
Vehicle v = new Sportscar();
symbol: class Sportscar
location: class VehicleTest
Q3. To provide meaningful output for:
System.out.print( new Item ()):
A method with which signature should be added to the Item class?
A. public String asString()
B. public Object asString()
C. public Item asString()
D. public String toString()
E. public object toString()
F. public Item toString()
Answer: D
Explanation:
Implementing toString method in java is done by overriding the Object's toString method.
The javatoString() method is used when we need a string representation of an object. It is
defined in Object class. Thismethod can be overridden to customize the String
representation of the Object.
Note:
Below is an example shown of Overriding the default Object toString() method. The
toString() method must bedescriptive and should generally cover all the contents of the
object.
class PointCoordinates {
private int x, y;
public PointCoordinates(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
// Custom toString() Method.
public String toString() {
return "X=" + x + " " + "Y=" + y;
}}
Q4. Given:
Which statement, inserted at line 8, enables the code to compile?
A. new Task().new Counter().increment();
B. new Task().Counter().increment();
C. new Task.Counter().increment();
D. Task.Counter().increment();
E. Task.Counter.increment();
Answer: C
Q5. Given:
What is the result?
A. John Adams George Washington Thomas Jefferson
B. George Washington John Adams Thomas Jefferson
C. Thomas Jefferson John Adams George Washington
D. An exception is thrown at runtime
E. Compilation fails
Answer: B
Explanation: The program compiles and runs fine.
At runtime the NameList is built and then sorted by natural Order (String >> alphabetically).
Q6. Which two are valid initialization statements?
A. Map<String, String> m = new SortedMap<String, String>();
B. Collection m = new TreeMap<Object, Object>();
C. HashMap<Object, Object> m = new SortedMap<Object, Object>();
D. SortedMap<Object, Object> m = new TreeMap<Object, Object> ();
E. Hashtable m= new HashMap();
F. Map<List, ArrayList> m = new Hashtable<List, ArrayList>();
Answer: D,F
Q7. Given the code fragment:
What is the result when the result.txt file already exists in c:\student?
A. The program replaces the file contents and the file's attributes and prints Equal.
B. The program replaces the file contents as well as the file attributes and prints Not equal.
C. An UnsupportedOperationException is thrown at runtime.
D. The program replaces only the file attributes and prints Not equal.
Answer: B
Explanation:
Assuming there is a file D:\\faculty\\report.txt then this file will be copied and will be replacing C:\\student\\report.txt.
Q8. Given: Which three values will appear in the output?
A. 5
B. 7
C. a1
D. a2
E. b1
F. b2
Answer: A,D,E
Explanation:
Staticmethod of base class is invoked >>
A myA = new B();
System.out.print(myA.doA() + myA.doA2() + myA.a);
class B String doA() { return "b1 "; }
class A protected static String doA2 () { return "a2 "; }
class B int a = 7;
Q9. Given:
What is the result?
A. Event Quiz
B. Event Event
C. Quiz Quiz
D. Quiz Event
E. Compilation fails
Answer: E
Q10. The two methods of code reuse that aggregate the features located in multiple classes are ____________ ?
A. Inheritance
B. Copy and Paste
C. Composition
D. Refactoring
E. Virtual Method Invocation
Answer: A,C
Explanation:
A: Inheritance is a way of reusing code and building bigger more functional objects from a
basic object.
The original little object, the parent, is called the super-class. The more functional object
that inherits from it iscalled the sub-class .
C: When your goal is code reuse, composition provides an approach that yields easier-to-change code.