1 | Given:
|
(1) | public Demo(int a, int b) |
(2) | public Demo(int c) |
(3) | public Demo() |
Answer : ————————- | |
2 | Which of the following class declarations for a normal top level class are incorrect? |
(1) | public synchronized class Base extends Thread |
(2) | private protected class Base |
(3) | public abstract class Base |
(4) | class Base extends Thread |
Answer : ————————- | |
3 | The GenericFruit class declares the following method: public void setCalorieContent(float f)
|
(1) | protected float setCalorieContent(String s) |
(2) | protected void setCalorieContent(float x) |
(3) | public void setCalorieContent(double d) |
(4) | public void setCalorieContent(String s) throws NumberFormatException |
Answer : ————————- | |
4 | Here is the hierarchy of Exceptions related to array index errors:
|
(1) | The declaration for X must include “throws ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException” |
(2) | If a method calling X catches IndexOutOfBoundsException, both array and string index exceptions will be caught |
(3) | If the declaration for X includes “throws IndexOutOfBoundsException”, any calling method must use a try-catch block |
(4) | The declaration for X does not have to mention exceptions |
Answer : ————————- | |
5 | The following method is designed to convert an input string to a floating point number, while detecting a bad format. Assume that “factor” is correctly defined elsewhere.
|
(1) | Input = “0.1234”. Result: factor = 0.1234, “Finally” is printed, true is returned |
(2) | Input = “0.1234”. Result: factor = 0.1234, “Finally” is printed, false is returned |
(3) | Input = null. Result: factor = NaN, “Finally” is printed, false is returned |
(4) | Input = null. Result: factor unchanged, “Finally” is printed, NullPointerException is thrown |
Answer : ————————- | |
6 | Given the following code fragment:
|
(1) | byte x = 100; |
(2) | short x = 200; |
(3) | int x = 300; |
(4) | long x = 400; |
Answer : ————————- | |
7 | In the following code for a class in which methodA has an inner class.
|
(1) | int ID (line 2) |
(2) | String name (line 3) |
(3) | int nn (line 4) |
(4) | int serialN (line 5) |
Answer : ————————- | |
8 | What happens when we attempt to compile and run the following code?
|
(1) | The program will compile and run, producing the output “N = -1” |
(2) | The program will compile and run, producing the output “N = 1” |
(3) | A run time ArithmeticException will be thrown |
(4) | The program will compile and run, producing the output “N = 0” |
Answer : ————————- | |
9 | What will happen when you attempt to compile and run the following code? (Assume that the code is compiled and run with assertions enabled.)
|
(1) | It will print -10 |
(2) | It will result in Assertion Error showing the message -“The value must not be negative” |
(3) | The code will not compile |
(4) | None of these |
Answer : ————————- | |
10 | What will happen when you attempt to compile and run the following code?
|
(1) | Compile-time error |
(2) | prints 1 |
(3) | prints 2 |
(4) | prints 3 |
(5) | prints 7 |
(6) | prints 8 |
Answer : ————————- | |
11 | What will happen when you attempt to compile and run the following code?
|
(1) | 300 |
(2) | 240 |
(3) | 120 |
(4) | 180 |
(5) | Compilation error |
(6) | None of the above |
Answer : ————————- | |
12 | What will be the result of executing the following code?
|
(1) | The code won’t compile |
(2) | “Some things are true in this world” will be printed |
(3) | “Hey this won’t compile” will be printed |
(4) | None of these |
Answer : ————————- | |
13 | What will happen when you attempt to compile and run the following code?
|
(1) | Compile-time error |
(2) | Runtime error |
(3) | Prints : “I am true interface” followed by ” I am true String” |
(4) | Prints : “I am false interface” followed by ” I am false String” |
(5) | Prints : “I am true interface” followed by ” I am false String” |
(6) | Prints : “I am false interface” followed by ” I am true String” |
Answer : ————————- | |
14 | What results from attempting to compile and run the following code?
|
(1) | prints: Value is – 9 |
(2) | prints: Value is – 5 |
(3) | Compilation error |
(4) | None of these |
Answer : ————————- | |
15 | In the following pieces of code, A and D will compile without any error. True/False?
|
(1) | True |
(2) | False |
Answer : ————————- | |
16 | Which of the following collection classes from java.util package are Thread safe? |
(1) | Vector |
(2) | ArrayList |
(3) | HashMap |
(4) | Hashtable |
Answer : ————————- | |
17 | What will happen when you attempt to compile and run the following code?
|
(1) | Prints : MyThread: start() followed by MyRunnable:run() |
(2) | Prints : MyThread: run() followed by MyRunnable:start() |
(3) | Prints : MyThread: start() followed by MyRunnable:start() |
(4) | Prints : MyThread: run() followed by MyRunnable:run() |
(5) | Compile time error |
(6) | None of the above |
Answer : ————————- | |
18 | What will be the result of executing the following code?
|
(1) | Compilation error at line 5 |
(2) | Compilation error at line 9 |
(3) | Runtime exception at line 11 |
(4) | None of these |
Answer : ————————- | |
19 | What can cause a Thread to stop executing? |
(1) | Calling its own yield method |
(2) | Calling the yield method of another thread |
(3) | A call to the halt method of the Thread class |
(4) | Another thread is given higher priority |
Answer : ————————- | |
20 | Which of the following statements are true? |
(1) | The keys of HashSet are not ordered |
(2) | The keys of LinkedHashSet are ordered |
(3) | The keys of LinkedHashSet are ordered but not sorted |
(4) | The keys of LinkedHashMap are sorted |
Answer : ————————- | |
21 | Given:
|
(1) | 1 |
(2) | 2 |
(3) | 3 |
(4) | 4 |
Answer : ————————- | |
22 | What, if anything, is wrong with the following code?
|
(1) | The class TestClass cannot be declared abstract |
(2) | The variable j cannot be declared transient |
(3) | The variable k cannot be declared synchronized |
(4) | The constructor TestClass( ) cannot be declared final |
(5) | The method f( ) cannot be declared static |
Answer : ————————- | |
23 | Which of these classes have a comparator() method? Select 2 correct options. |
(1) | TreeSet |
(2) | HashMap |
(3) | TreeMap |
(4) | HashSet |
(5) | ArrayList |
Answer : ————————- | |
24 | Assume that Thread 1 currently holds the lock for an object (obj) for which 4 other threads, Thread 2 to 5, are waiting. Now, Thread 1 want to release the lock but as the same time, it want Thread 3 to get the lock. How will you accomplish this? |
(1) | Call t3.resume() after releasing the lock |
(2) | Call t3.release() after releasing the lock |
(3) | Instead of releasing the lock, call t3.accuire(obj); |
(4) | Instead of releasing the lock, call t3.notify(obj); |
(5) | None of these |
Answer : ————————- | |
25 | What will be output by the following code?
|
(1) | Value for i=1 value for j=1 |
(2) | Value for i=2 value for j=1 |
(3) | Value for i=2 value for j=2 |
(4) | Value for i=3 value for j=1 |
Answer : ————————- | |
26 | What will be the result of attempting to compile and run the following program?
|
(1) | The progarm will fail to compile |
(2) | ClassCastException at runtime |
(3) | It will print 30, 20 |
(4) | It will print 30, 30 |
(5) | It will print 20, 20 |
Answer : ————————- | |
27 | Consider the following lines of code: |
(1) | None of the 3 lines will compile |
(2) | All the 3 line will compile and print nulltrue, truenull and nullnull respectively |
(3) | Line 1 and 2 won’t compile but line 3 will print nullnull |
(4) | Line 3 won’t compile but line 1 and 2 will print nulltrue and truenull respectively |
(5) | None of the above |
Answer : ————————- | |
28 | Which of the following are true about the “default” constructor? Select 2 correct options |
(1) | It is provided by the compiler only if the class does not define any constructor |
(2) | It initializes the instance members of the class |
(3) | It calls the default ‘no-args’ constructor of the super class |
(4) | It initializes instance as well as class fields of the class |
(5) | It is provided by the compiler if the class does not define a ‘no- args’ constructor |
Answer : ————————- | |
29 | Which of these methods from the Collection interface return the value true if the collection object was actually modified by the call? Select 3 correct options |
(1) | add( ) |
(2) | retainAll( ) |
(3) | containsAll( ) |
(4) | contains( ) |
(5) | remove() |
Answer : ————————- | |
30 | Following is not a valid comment:
|
(1) | True |
(2) | False |
Answer : ————————- | |
31 | Which statements regarding the following code are correct?
|
(1) | This code won’t compile |
(2) | Constructor for Outer is public |
(3) | Constructor for Outer is private |
(4) | Constructor for Inner is public |
(5) | Constructor for Inner is protected |
Answer : ————————- | |
32 | Consider the following method:
|
(1) | It won’t even compile |
(2) | It will return 10.0 |
(3) | It will return Float.Nan |
(4) | It will return 0.0 |
(5) | None of the above |
Answer : ————————- | |
33 | The following code snippet will not compile.
|
(1) | True |
(2) | False |
Answer : ————————- | |
34 | Giri has written the following class to prevent garbage collection of the objects of this class. Is he mistaken?
|
(1) | True |
(2) | False |
Answer : ————————- | |
35 | What happens when you try to compile and run the following class:
|
(1) | It throws an OverFlowException |
(2) | It will print two same -ive numbers |
(3) | It will print two different -ive numbers |
(4) | It will print one -ive and one +ive number of same magnitude |
(5) | It will print one -ive and one +ive number of different magnitude |
Answer : ————————- | |
36 | Consider the following code snippet:
|
(1) | If IOException gets thrown at line1, then the whole method will end up throwing SQLException |
(2) | If IOException gets thrown at line1, then the whole method will end up throwing CloneNotSupportedException |
(3) | If IOException gets thrown at line1, then the whole method will end up throwing InstantiationException() |
(4) | If no exception is thrown at line1, then the whole method will end up throwing CloneNotSupportedException |
(5) | If SQLException gets thrown at line1, then the whole method will end up throwing InstantiationException() |
Answer : ————————- | |
37 | Consider the following class hierarchy:
|
(1) | objectOfC1.m1(); will cause a compilation error |
(2) | objectOfC2.m1(); will cause A’s m1() to be called |
(3) | objectOfC1.m1(); will cause A’s m1() to be called |
(4) | objectOfB1.m1(); will cause an expection at runtime |
(5) | objectOfB2.m1(); will cause an expection at runtime |
Answer : ————————- | |
38 | What classes can an inner class extend? (Provided that the class is visible and is not final) Select 1 correct option |
(1) | Only the encapsulating class |
(2) | Any top level class |
(3) | Any class |
(4) | It depends on whether the inner class is defined in a method or not |
(5) | None of the above |
Answer : ————————- | |
39 | Which of the following statements are true? |
(1) | System.out.println( -1 >>> 2);will output a result larger than 10 |
(2) | System.out.println( -1 >>> 2); will output a positive number |
(3) | System.out.println( 2 >> 1); Will output the number 1 |
(4) | System.out.println( 1 <<< 2); will output the number 4 |
Answer : ————————- | |
40 | Given the following class definition, which of the following statements would be legal after the comment //Here?
|
(1) | System.out.println(s); |
(2) | System.out.println(iOther); |
(3) | System.out.println(iam); |
(4) | System.out.println(iArgs); |
Answer : ————————- | |
41 | Which statments regarding the following program are correct?
|
(1) | It’ll not compile as run method cannot be overridden |
(2) | It’ll print both “Hello” and “World” 5 times each |
(3) | It’ll print both “Hello” and “World” 5 times each but they may be interspersed |
(4) | Total 5 words will be printed |
(5) | Either 5 “Hello” or 5 “world” will be printed |
Answer : ————————- | |
42 | Consider following two classes:
|
(1) | It will print 10 |
(2) | It will print 20 |
(3) | It will not compile |
(4) | It will throw an exception at Run time |
(5) | None of the above |
Answer : ————————- | |
43 | What will happen when you attempt to compile and run the following code with the command line “java hello there”
|
(1) | Compile time error |
(2) | Compilation and output of “hello” |
(3) | Compilation and output of “there” |
(4) | None of the above |
Answer : ————————- | |
44 | Which of the following statements are true [Check all correct answers] |
(1) | Checked exceptions are derived directly from Exception |
(2) | Checked exceptions are derived directly from RuntimeException |
(3) | Unchecked exceptions are derived directly from Exception |
(4) | Unchecked exceptions are derived direclty from RuntimeException |
(5) | Exception and RuntimeException are both subclasses of Throwable |
Answer : ————————- | |
45 | Which of the following keywords are valid when declaring a top level class? [Check all correct answers] |
(1) | private |
(2) | native |
(3) | final |
(4) | transient |
(5) | abstract |
Answer : ————————- | |
46 | After the execution of the code-fragment below, what will be the values of a, b and c?
|
(1) | a: 2 b: 18 c: 8 |
(2) | a: 7 b: 12 c: 8 |
(3) | a: 7 b: 18 c: 8 |
(4) | a: 7 b: 18 c: 4 |
(5) | None of the above |
Answer : ————————- | |
47 | What, if anything, is wrong with the following code?
|
(1) | The class TestClass cannot be declared abstract |
(2) | The variable j cannot be declared transient |
(3) | The variable k cannot be declared synchronized |
(4) | The constructor TestClass( ) cannot be declared final |
(5) | The method f( ) cannot be declared static |
Answer : ————————- | |
48 | Which of the following statements are true? |
(1) | An object will be garbage collected when it becomes unreachable |
(2) | An object will be garbage collected if it has null assigned to it |
(3) | The finalize method will be run before an object is garbage collected |
(4) | Garbage collection assures that a program will never run out of memory |
Answer : ————————- | |
49 | What will the following program print?
Select 1 correct option |
(1) | 3, 4, 3 |
(2) | 3, 3, 3 |
(3) | 4, 3, 4 |
(4) | 4, 3, 3 |
(5) | It will not compile |
Answer : ————————- | |
50 | Consider following two classes:
|
(1) | It will print 10 |
(2) | It will print 20 |
(3) | It will not compile |
(4) | It will throw an exception at Run time |
(5) | None of the above |
Answer : ————————- | |
1] 2,3
Explanation:
Option
2 is required because it is called in line 7. Option 3 is required
because a default (no arguments) constructor is needed to compile the
constructor starting in line 3.(Credit: www.lanw.com)
* * * *
2] 1,2
Explanation:
Both options 1 and 2 are incorrect, the synchronized, protected and private keywords can not be applied to classes. (Credit: www.lanw.com)
* * * *
3] 1,3,4
Explanation:
Options
1,3 and 4 are valid overloading method declarations, because the
parameter list differs from the method in GenericFruit. (Credit: www.lanw.com)
* * * *
4] 2,4
Explanation:
(1)
No, the significant word here is “must”. Because these exceptions
descend from RuntimeException, they do not have to be declared. (2)
Yes, exceptions obey a hierarchy just like other objects. (3) see (1)
and they do not have to be caughteven if declared by method X. (4) Yes,
because these exceptions descend from RuntimeException, they do not
have to be declared. (Credit: www.lanw.com)
* * * *
5] 1,4
Explanation:
Option
(1) is correct and (2) is wrong because the return value in line 4 is
used. (3) is wrong and (4) is correct, because a NullPointerException
is thrown in line 3 and is not caught in the method. (Credit: www.lanw.com)
* * * *
6] 2,3
Explanation:
(1)
No, x can’t be a byte type ’cause the value 300 is not compatible. The
type used in the switch statement must accommodate all of the values in
the case statements.(2) & (3) yes, x can be a short or an int since
all of the cases can be accommodated. (4) No, switch statements cannot
use long values. To use long, you need a specific cast:
switch((int)x) {
. (Credit: www.lanw.com)
* * * *
7] 1,2,3
Explanation:
(1)
& (2) Correct – because inner classes can access any static or
member variable in the enclosing class. (3) Correct – although it is a
local variable, it is declared final. (4) Wrong – because the local
variable is not declaredfinal. (Credit: www.lanw.com)
* * * *
8] 1
Explanation:
(1)
Correct – the >> operator extends the sign as the shift operation
is performed. (2) No – the >> operator extends the sign as the
shift operation is performed, it is the >>> operator which
does not extend the sign. (3) No – anArithmeticException is typically
thrown due to integer division by zero. (4) No – the sign is extended
while shifting. (Credit: www.lanw.com)
* * * *
9] 3
Explanation:
An assert statement can take any one of these two forms –
assert Expression1;
assert Expression1 : Expression2;
Note
that, in the second form; the second part of the statement must be an
expression – Expression2. Inthis code, the methodB() returns void,
which is not an expression and hence it results in a compile time
error. The code will compile if methodB() returns any value such as
int, String etc. Also, in both forms of the assert
statement,Expression1 must have type boolean or a compile-time error
occurs.
* * * *
10] 4
Explanation:
The code will not give any compilation error. Note that “Static” is a valid class name. Thus (1) is incorrect.
In
the code, on execution, first the static variables (x and y) will be
initialized to 0. Then static block will becalled and finally main()
method will be called. The execution of static block will have no
effect on the output as it declares a new variable (int x).
The
first statement inside main (x–) will result in x to be -1. Afterthat
myMethod() will be executed. The statement “y = x++ + ++x;” will be
evaluated to y = -1 + 1 and x will become 1. In case the statement be
“y =++x + ++x”, it would be evaluated to y = 0 + 1 and x would become
1. Finally when System.out isexecuted “x + y + ++x” will be evaluated
to “1 + 0 + 2” which result in 3 as the output. Thus (4) is correct.
* * * *
11] 1
Explanation:
MyChild
class overrides the addMe(int x, int y) method of the MyParent class.
And in both the MyChild and MyParent class, addMe() method is
overloaded. There is no compilation error anywhere in the code.
On
execution, first, the objectof MyChild class will be constructed.
Please note that there is a super() call from the constructor of
MyChild class, which will call the constructor of MyParent class. This
will cause the value of z var. of MyChild class to be 30 & x, yvars
of MyParent class will become 10 & 20 resply. The next stmt ‘ll
again call the constructor of MyParent class with same x & y
values. This is followed by execution of addMe() method of MyChild
class with x as 10, y as 20 & z as 30. Alsox and y are inherited by
MyChild class from the MyParent class. Thus in the addMe() method of
the MyChild class, the value of this.x will be 10, this.y will be 20
and this.z will be 30. The return val of this method will be “10 + 10 +
20 …
* * * *
12] 4
Explanation:
The
rule for attaching else statements with if conditions is the same as
attaching close brackets with open brackets. A close bracket attaches
with the closest open bracket, which is not already closed. Similarly
an else statement attacheswith the closest if statement, which doesn’t
have an else statement already, attached to it. So the else statement
at line 8 attaches to the if statement @ln 6. The else stmt @ln 10
attaches to the if stmt @ln 5. The else statement @ln 12attaches to the
if stmt @ln 10.
At ln 4 since a is equal to true the execution
falls to ln 5. At ln 5 since b is not true the exec goes to the
corresponding else statement @ln 10. Now it evaluates the condition
inside the if stmt.Note that an assignment stmt also has a val equal to
the val being assigned, hence (b = c) evaluates to true&
subsequently a && (b = c) evaluates to true and “It’s too
confusing to tell what is true and what is false” will be printed.
* * * *
13] 5
Explanation:
(5)
is the correct choice. The “instanceof” operator tests the class of an
object at runtime. It returns true if the class of the left-hand
argument is the same as, or is some subclass of, the class specified by
the right-hand operand.The right-hand operand may equally well be an
interface. In such a case, the test determines if the object at
left-hand argument implements the specified interface.
In this
case there will not be any compiletime or runtime error. Theresult of
“t instance of MyInterface” will be true as “t” is the object of
MyInstanceTest class which implements the MyInstance interface. But the
result of “s instanceof String” will be false as “s” refers to null.
Thus the output of theprogram will be : “I am true interface” followed
by ” I am false String”. Thus choice (5) is correct and others are
incorrect. (Credit: Whizlabs)
* * * *
14] 4
Explanation:
The
code compiles successfully. In this code the optional value for the
ternary operator, 9.0(a double) and 9(an int) are of different types.
The result of a ternary operator must be determined at the compile
time, and here the type chosenusing the rules of promotion for binary
operands, is double. Since the result is a double, the output value is
printed in a floating point format. The choice of which value to be
printed is made on the basis of the result of thecomparison “a < 5”
which results in false, hence the variable “a” takes the second of the
two possible values, which is 9, but because the result type is
promoted to double, the output value is actually written as 9.0,
ratherthan the more obvious 9, hence (4) is correct.
* * * *
15] 2
Explanation:
Choice
(2) is correct. The code segments (2) & (4) will compile without
any error. (1) is not a valid way to construct a StringBuffer, you need
to creat a StringBuffer object using “new”. (2) is a valid construction
of a Boolean(any string other than “true” or “false” to the Boolean
constructor ‘ll result in a Boolean with a value of “false”). (3) will
fail to compile because the valid range for a byte is -128 to +127 (ie,
8 bits,signed). (4) is correct, 0x1234is the hexadecimal representation
in java. (5) fails to compile because the compiler interprets 1.2 as a
double being assigned to a float (down-casting), which is not valid.
You either need an explicit cast (as in “(float)1.2”) or “1.2f”,to
indicate a float.
* * * *
16] 1,4
Explanation:
(1)
and (4) are correct. Vector and Hashtable are two collection classes
that are inherently thread safe or synchronized; whereas, the classes
ArrayList and HashMap are unsynchronized and must be “wrapped”
viaCollections.SynchronizedList or Collections.synchronizedMap if
synchronization is desired.
* * * *
17] 1
Explanation:
In
the code there is not any compilation error. Thus choice (5) is
incorrect. Inside main() method, objects of MyThread and MyRunnable
class are created followed by creation of Thread with object of
MyRunnable class.
Note thatMyThread class extends Thread class
and overrides the start() method of the Thread class. Thus on exec of
“myThread.start()” statement, the start() method of the MyThread class
will be executed and as a result “MyThread:start()” will beprinted. Had
the start() method not there in MyThread class, the start() method of
the Thread class would be called which in turn would call the run()
method of the MyThread class.
On execution of “thread.start();”,
the start()method of the Thread class would be called which in turn
will call the run() method of the class which is passed to Thread
constructor (i.e. MyRunnable class). Thus “MyRunnable:run()” will be
printed out. Thus choice (1) is correct.
* * * *
18] 4
Explanation:
(4)
is correct. When no access modifier is specified for a member, it is
only accessible by another class in the package where its class is
defined. Even if its class is visible in another package, the member is
not accessible there. In thequestion the variable superclassVarX has no
access modifier specified and hence it cannot be accessed in the
packageY even though the class SuperclassX is visible and the protected
method superclassMethodX() can be accessed. Thusthe compiler will raise
an error at line 11.
* * * *
19] 4
Explanation:
Options
1 and 2 are incorrect because the yield method is static and belongs to
the class itself and not any instance of the class. The Thread class
does not have a halt method. (Credit: www.examulator.com)
* * * *
20] 1,2,3
Explanation:
Note
that the word ordered means that the sequence is preserved, whereas
sorted means that the order is according to some kind of comparison.
The LinkedHashSet class was introduced with JDK1.4
* * * *
21] 3
Explanation:
Answer:
3 objects. Just remember that operator = is right associate. The
equivalent statement is d=(c=(b=a)). (1) After b=a, the object original
referenced by b is eligible for GC.(2) After c=(b=a), the object
original referenced by c is eligible for GC (3) After d=(c=(b=a)), the
object original referenced by d is eligible for GC (4) After d=null,
nothingnew is eligible for GC (5) The object original referenced by a
is not eligible for GC, since it is still referred by references a, b,
c (6) Make sure you understand the differences between physical object
and object reference(pointer to object). (Credit: Roseanne Zhang)
* * * *
22] 3
Explanation:
(1)
Any class can be declared abstract (3) Variables cannot be declared
synchronized. Only methods can be declared synchronized (4) It is not a
constructor, it is a simplemethod. Notice void return type. (Credit: www.jdiscuss.com)
* * * *
23] 1,3
Explanation:
Please see Java 1.4 API documentation
* * * *
24] 5
Explanation:
It
is simple not possible to do so. Thread a can only release the lock and
it has no control over who gets the lock next. All the waiting threads
contend to get the lock and any one of them can get it. (credit: www.jdiscuss.com)
* * * *
25] 1,2
Explanation:
The statement continue outer causes the code to jump to the label outer and the for loop increments to the next number (credit: www.examulator.com)
* * * *
26] 3
Explanation:
Remember : variables are SHADOWED and methods are OVERRIDDEN.
Which variable will be used depends on the class that the variable is declared of.
Which
method will be used depends on the actual class of the object that is
referencedby the variable. So, in line o1.m1(), the actual class of the
object is C, so C’s m1() will be used. So it retruns 30. In line o2.i,
o2 is declared to be of class B, so B’s i is used. So it returns 20. (credit: www.jdiscuss.com)
* * * *
27] 1
Explanation:
Note
that none of the parameters is a String so conversion to String will
not happen. It will try to convert every thing to an int and you will
get : Incompatible type for +. Can’t convert null to int.
System.out.println( null + null);
* * * *
28] 1,3
Explanation:
The default constructor is provided by the compiler only when a class does not define ANY constructor explicitly.
* * * *
29] 1,2,5
Explanation:
The
methods add() and retainAll() return the value true if the collection
object was modified during the operation.
The contains() and containsAll() methods return a boolean value, but
these operations never modify the collection, andthe return value is
the result of the test.
* * * *
30] 2
Explanation:
Every thing after /* is ignored till */ is reached. Here, ‘this comment /* // /** is not vaid’ is ignored.
* * * *
31] 5
Explanation:
1. Putting a return type makes private void Outer() { } a method and not a constructor.
2.
When a programmer does not define ANY constructor, the compiler inserts
one on it’s own whose access modifier is same as that of the class.
* * * *
32] 2
Explanation:
finally
block will always execute(except for System.exit() in try). And inside
the finally block it is setting f to 10.0. So no matter what, this
method will always return 10.0 (credit: www.jdiscuss.com)
* * * *
33] 1
Explanation:
Note
that it is not permitted for either the second or the third operand
expression to be an invocation of a void method. In fact, it is not
permitted for a conditional expression to appear in any context where
an invocation of avoid method could appear. The first expression must
be of type boolean, or a compile-time error occurs.
The
conditional operator may be used to choose between second and third
operands of numeric type, or second and third operandsof type boolean,
or second and third operands that are each of either reference type or
the null type. All other cases result in a compile-time error. (credit: www.jdiscuss.com)
* * * *
34] 1
Explanation:
Yes,
he is definitely mistake. Because, all he is creating is a circular
reference, but such references do not prevent an object from garbage
collection. Basically, if B is only refereded to by A and A is eligible
for GC, then B iseligible for GC to. So, if A refers to B and B refers
back to A, this arrangement does not prevent them from being garbage
collected. (credit: www.jdiscuss.com)
* * * *
35] 2
Explanation:
It prints: -2147483648 -2147483648
For
integer values, negation is the same as subtraction from zero. For
floating-point values, negation is not the same as subtraction from
zero, because if x is +0.0, then 0.0-xequals +0.0, but -x equals -0.0.
Unary minus merely inverts the sign of a floating-point number. If the
operand is NaN, the result is NaN (recall that NaN has no sign). If the
operand is an infinity, the result is the infinity ofopposite sign. If
the operand is a zero, the result is the zero of opposite sign. (credit: www.jdiscuss.com)
* * * *
36] 2,4
Explanation:
1.
The Exception that is thrown in the last, gets thrown by the method.
So, When no exception or any exception is thrown at line 1, the control
goes to finally or some catch block. Now, even if the catch blocks
throw some exception, thecontrol goes to finally. The finally block
throws CloneNotSupportedException, so the whole method ends up throwing
CloneNotSupportedException.
2. Exception thrown by a catch
cannot be caught by following catch blocksin the same level. So, if
IOException is thrown at line 1, the control goes to first catch which
again throws SQLException. Now, although there is a catch for
SQLException, it won’t catch the exception because it is atthe same
level. So, the control goes to the finally and same story continues (as
described above). Any exceptions thrown before are forgotten. (credit: www.jdiscuss.com)
* * * *
37] 3
Explanation:
(1)
C1 will inherit B1’s m1() which in turn inherits m1() from A (2) C2 has
m1(), so it’s m1() will override A’s m1() (3) C1 will inherit B1’s m1()
which in turn inherits m1() from A (4) B1 will inherit m1() from A. So
this is valid(5) B2 will inherit m1() from A. So this is valid (credit: www.jdiscuss.com)
* * * *
38] 3
Explanation:
Note
that, in certain situations an Inner class may not be able to extend
some other particular class (for eg. a static inner class cannot extend
another non-static inner class in the same class). But in general there
is no restriction onwhat an inner class may or may not extend. (credit: www.jdiscuss.com)
* * * *
39] 1,2,3
Explanation:
Java
does not have a <<< operator. The operation 1 << 2 would
output 4 Because of the way twos complement number representation works
the unsigned right shift operation means a small shift in a negative
number can return a very largevalue so the output of option 1 will be
much larger than 10.
The unsigned right shift places no significance on the leading bit that
indicates the sign. For this shift the value 1 of the bit sign is
replaced with a zero turning the resultinto a positive number for
option 2.
* * * *
40] 1,4
Explanation:
A
class within a method can only see final variables of the enclosing
method. However with the normal visibility rules apply for variables
outside the enclosing method. (credit: www.examulator.com)
* * * *
41] 4
Explanation:
Notice
that, i is a static variable and there are 2 threads that are accessing
it. None of the threads has synchronized access to the variable. So
there is no guarantee which thread will access it when. But only one
thing is sure, as both ofthe threads are incrementing it and the for
condition ends at i>=5, the total no. of iterations will be 5. So,
in total only 5 words will be printed. But you cannot say how many
“Hello” or “World” will be printed(credit: www.jdiscuss.com)
* * * *
42] 3
Explanation:
Although,
class B extends class A and ‘i’ is a protected member of A, B still
cannot access i , (now this is imp) THROUGH A’s reference because B is
not involved in the implementation of A.
Had the process()
method been defined as process(B b); b.i would have been accessible as
B is involved in the implementation of B. (credit: www.jdiscuss.com)
* * * *
43] 1
Explanation:
You
will get an error saying something like “Cant make a static reference
to a non static variable”. Note that the main method is static. Even if
main was not static the array argv is local to the main method and
would thus not bevisible within amethod. (credit: www.examulator.com)
* * * *
44] 1,4,5
Explanation:
Answer
1 is correct because all classes that descend from Exception are known
as checked exceptions. The compiler insists that program code is
provided to handle checked exceptions. Answer 4 is correct because all
classes thatdescend from RuntimeException are known are unchecked
exceptions. Unchecked exceptions does not require program code for
catching them. Answer 5 is correct because Throwable is the super class
of all exceptions. Answers 2 and 3 areboth incorrect. (credit: www.javacertificate.com)
* * * *
45] 3,5
Explanation:
Answers
3 and 5 are correct. Both final and abstract can be used as class
modifiers. In addition, public and strictfp may also be used. Answers
1, 2, and 4 are incorrect. native can only be used as a method
modifier, transient can only be useda variable modifier and private can
only be used as variable, method, or constructor modifers.
The access modifier private can be used when declaring inner classes. (credit: www.javacertificate.com)
* * * *
46] 3
Explanation:
Answer
3 is correct. In line 4 the value of b(3) is added up to c(4) that
makes the value of a equals to 7, in the same line b is incremented
with 1, that makes b=4. In line 5 the current value of c(4) is added to
the value of b(4), the newvalue of c is 8. Then in line 6 the current
value of b(4) is added to the value of a(7) and multiplied by 2, b is
set to 18. (credit: www.javacertificate.com)
* * * *
47] 3,5
Explanation:
(1)
Any class can be declared abstract even it does not have any abstract
method (2) — (3) Variables cannot be declared synchronized. Only
methods can be declared synchronized (4) It is not a constructor, it is
a simple method. Notice voidreturn type (5) Because it refers to
instance variables j and k (credit: www.jdiscuss.com)
* * * *
48] 3
Explanation:
Assigning
null to an object means it is eligable for garbage collection but you
cannot be certain when, or if that will happen. The same is true for an
unreachable object. Nothing can ensure a program will never run out of
memory, garbagecollection simply recycles memory that is no longer used
(credit: www.examulator.com)
* * * *
49] 4
Explanation:
In
an array creation expression, there may be one or more dimension
expressions, each within brackets. Each dimension expression is fully
evaluated before any part of any dimension expression to its right. The
first dimension iscalculated as 4 before the second dimension
expression sets i to 3. Note that, If evaluation of a dimension
expression completes abruptly, no part of any dimension expression to
its right will appear to have been evaluated. (credit:www.jdiscuss.com)
* * * *
50] 3
Explanation:
Although,
class B extends class A and ‘i’ is a protected member of A, B still
cannot access i , (now this is imp) THROUGH A’s reference because B is
not involved in the implementation of A.
Had the process()
method been defined as process(B b); b.i would have been accessible as
B is involved in the implementation of B.
For more information read Section 6.6.7 of JLS:
JLS section 6.6.7