The hashCode method is not correct because it is inconsistant with the equals method

The code will not compile because there is a circular reference to the hashCode method

The code will not compile because the return value of this.hashCode is not an int value

The code will not compile because it does not implement the comparable interface

29 . Which of the following statements are true?

A higher priority Thread will prevent a lower priorty Thread from getting any access to the CPU

The yield method only allows any higher priority priority thread to execute

The Thread class has a static method called yield

Calling yield with an integer parameter causes it to yield for a specific time

30 . How do you set the priority of the thread “newThread” to the minimum of two values: maximum priority and current priority incremented to the next level? [Use the Thread priority constants and do not insert any extra characters or leave any blank spaces]

33 . What is the capacity of strBuf object after executing the following statement?

StringBuffer strBuf = new StringBuffer("Technopark");

Choose the one below:

0

6

25

255

-1

None of the above

34 . How can you remove the superfluous capacity of a StringBuffer object say strBuf (if there is any)? (Hint: Use SetLength() and length() methods. Please do not include any extra characters Or spaces)

Which of the following statements about the relationship between these tests are true?

If equalsFlag is true, then hashFlag must be true

If hashFlag is true, then eqFlag must be true

If equalsFlag is false, then eqFlag must be false

if hashFlag is false, then eqFlag must be false

Answers

1 : 2 & 3 is correct. Explanation: pause() and halt() are not methods of the Thread class.

2 : 2 is correct. Explanation: Please see the Java 1.4 documentation.

3 : 2 is correct. Explanation: Although bContinue does get set to true, the setPrice call is blocked because setPrice and run are both synchronized.

4 : 4 is correct. Explanation: If that seems a vauge answer it is because you cannot be certain of the system that the underlying OS uses for allocating cycles for a Thread. The chances are that once the thread has been spun off in the call to start in the methodname() the main method will run to completion and the value of sName will still be vandeluer before the Thread modifies it. You cannot be certain of this though.

Just because sName is static does not mean that passing it to a method gives the method the original copy. The method only sees a locally created copy and any changes to it will not be reflected on return to the calling method.

5 : 2 & 3 is correct. Explanation: Generally speaking, all I/O operations require explicit exception handling with try/catch blocks. The JDK 1.4 exams does not explicitly cover I/O but it may be referred to in the context of exception handling.

6 : 1 is correct. Explanation: This is a bit of a sneaky one as the Math.random method returns a pseudo random number between 0 and 1, and thus option 3 is a plausible Answer. However the number returned is a double and so the compiler will complain that a cast isneeded to convert a double to an int.

8 : 2 is correct. Explanation: The TreeMap class stores the keys rather than the values in sorted order.

9 : 3 is correct. Explanation: Constructors are executed in order of the oldest ancester down, so the output from Biddy is seen before Val. Although the the paremeter for shep has no floating point component, a widening conversion involves no loss of precisionand thus the value is cast to a float by the method shep. By default a float will have a decimal component and thus it has a trailing zero in this example.

12 : 2 & 3 is correct. Explanation: A variable declared as public within a method is only visible within the method. They are sometimes called automatic variables as they automatically come into scope when the method runs and are out of scope when the method hasfinished execution. Variables declared within methods cannot be marked as static. A method declared with no visibility modifier is accessible anywhere from within the current package.A method declared with the protected modifier is also accessible in child classes that are not in the current package.

13 : 3 is correct. Explanation: Option 4 is incorrect because an assert statment must make a comparison and the single = sign indicates an assignment.The assert statement does not use curly braces and assert cannot be used like a parameterless method call as in assert() in option 1.

14 : 4 is correct. Explanation: On line 9 the object created on line 8 has the reference sb2 pointed to it. Until something happens to make that reference unable to reach the object it will not be eligible for garbage collection.

15 : 3 is correct. Explanation: The == operator should never be used to test the equivalence of strings as it will only test the reference, not the sequence of characters. To test if the Strings match use the String equals() method.

16 : 1 is correct. Explanation: There is no rule against constructors being marked as private.

17 : 2 is correct. Explanation: This code will produce a java.lang.IllegalMonitorStateException at runtime because the wait/notify code is not within synchronized code.

18 : 1,2 & 4 is correct. Explanation: The LinkedHashMap was added with JDK1.4. Option 3 is nonsense because if something is sorted that imposes an order (however something may be ordered without being sorted).

19 : 1 is correct. Explanation: No problem here, the code compiles fine. One thing to be aware of is that i+++ ++i compiles fine because of the way Java code is actually parsed. The parser tokenizes the source in bunches of longest valid character sequences. i+++ ++i will betokenized as i,++,+,++,i and interpreted as i++ + ++i, that is i post-incremented plus i pre-incremented. Then you have to know how the ternary conditional ?: operator works, that is, it always evaluates its first operand and then depending onthe results, evaluates the second OR the third operand (never both). So getBoolean() returns either true or false, thus the code either prints i=2*i++ or i+++ ++i. The former is equivalent to i=2*i and the latter to 2*i+2.

20 : 2 is correct. Explanation: ArithmeticExceptions are considered programmer problems, and are not checked by the compiler. A divide-by-zero error, for example, should be checked for by your own code. (from Java Ranch round up game)

21 : 2 is correct. Explanation: The right shift operator fills all bits from the left with the same value as the original sign bit — in this case 1. (from Java Ranch round up game)

22 : 2 is correct. Explanation: The case argument must be either an int literal, or an int-compatible variable which is a constant ie., static final.

23 : 2 is correct. Explanation: This would be true for overriding methods, but overloaded methods are free to throw new checked exceptions. (from Java Ranch)

24 : 3 is correct. Explanation: Assigning null to a reference will only cause the object it references to be ready for garbage collection if no other reference points to it.It is possible to create an object inside a method and to also assign a reference created outside the method to the same object,so the completion of a method is no guarantee that an object is eligable for garbage collection.

28 : 1 is correct. Explanation: This represents a fairly useless but correct implementation of hashCode by using the version inherited from Object which uses the memory address of the object to generatethe hashCode value. (from www.examulator.com)

29 : 3 is correct. Explanation: A call to yield can allow any waiting thread to execute depending on the scheduling system of the underlying operating system. There is no version of yield that takes aninteger parameter.Weather a higher priority thread gets CPU time than a lower priorty thread is platform dependent and cannot be certain. (from www.examulator.com)

32 : 16. Explanation: The default capacity for any StringBuffer object is 16 + the length of the String specified as the argument. If none specified, it will be 16. Capacity can also be specifiedusing the other constructor StringBuffer(int capacity).

33 : 3 is correct. Explanation: Capacity of a StringBuffer object is always the length of the string + 16 (unless the capacity is specified using StringBuffer(int capacity))

35 : 2 is correct. Explanation: The expression tries to compare two references and will fail, since neither String nor StringBuffer is a super class of the other [from Khalid’s book]

36 : 4 is correct. Explanation: The instance of the Harpic class will probably not be garbage collected at all, and thus the finalize method will not be run (but it could be).(from www.examulator.com)

37 : 4 is correct. Explanation: The TSamp has no constructor defined that takes a class of type Runnable, and as constructors are not inherited the code will not compile. (from www.examulator.com)

38 : 3 & 4 is correct.

39 : 4 is correct. Explanation: Because the code attempts to insert the string “one” twice, the HashSet hs has only two elements. It is a good idea when using the add method of HashSet to check the boolean value returned to ensure that the new element wassuccessfully added.

40 : 2 is correct. Explanation: java.lang.AssertionError class is a subclass of java.lang.Error and that makes it unchecked.

41 : 1 is correct. Explanation: Note that n is decremented after the value is printed. (from LanWrights exam)

42 : 3 is correct. Explanation: The result is “: Arg betagamma:” because the call to trim() does not change s1. (from LanWrights exam)

43 : 6 is correct. Explanation: Choice 1 & 2 are not correct, ’cause WornPadException extends RuntimeException it is not mandatory to surround calls to it with a try/catch block. Choice 3 is not correct since thecompiler checks the need for try/catch, not the runtime. Choice 4 is not correct, because a finally clause can have any code in it. Choice 5 is not correct, since the final valuereturned is 1. Choice 6 is correct because WornPadException extends RuntimeException it is not mandatory to surround calls to it with a try/catch block. It is perfectly correct &sometimes useful to have a try/finally block without a catch statement. Because the finally clause ‘ll be executed the value returned ‘ll be that within the finally clause.

44 : 1 & 5 is correct. Explanation: The * operator can only work with primitives. The value in a wrapper class object can’t be changed once created. The Character class can only store a char value.(from LanWrights exam)

45 : 3 is correct. Explanation: Only local variables declared final can be used by the inner class. (from LanWrights exam)

46 : 1 & 3 is correct. Explanation: Since wait is a method in Object and Thread is an object; sleep is a static method; start is an instance method of Thread; MAX_PRIORITY is a static final variable of Thread;the daemon boolean variable is a private instance variable. (from LanWrights exam)

47 : 2,5 & 6 is correct. Explanation: transient is used as a modifier for variables; generic is not a keyword; as of Java 1.4, assert is a keyword; the keyword strictfp controls floating point calculations; friend is nota keyword; Java does not use sizeof.

48 : 2 & 4 is correct. Explanation: you can’t cast any primitive array to another primitive array type, no matter how reasonable it seems; any array can be cast to an Object reference; you can’t cast a primitive arrayto an reference type array. The compiler does not care that theInts will have a null value when the program runs. (from LanWrights exam)

49 : 2 is correct. Explanation: the equals method is defined in terms of taking an Object reference so there is no problem; the equals test fails because the objects are of different types, but the charValuereturn is promoted to int and the result is true; the equals test fails because the objects are of different types; only the == comparison will result in true. Note that the compilerpromotes the charValue return to int; promotion of the char value to int is automatic. (Credit: LanWrights)

50 : 1,3 & 4 is correct. Explanation: (1) yes, this is a requirement for hashcodes and the equals method; (2) no, two objects not equal by the == test may still have identical hashcodes (3) Yes, if the equals() testreturns false, x and y can’t possibly refer to the same object (4) Yes, this is a requirement for hashcodes and the equals method. (Credit: LanWrights)

Comments

comments

Related posts:

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

About Us

Javabeat.net is a blog dedicated to Java/J2EE developers. This site focus on beginners and advanced developers. We constantly write fresh content on J2EE topics like Spring, Hibernate and wide variety of J2EE frameworks.