public class Test
{
public int aMethod()
{
static int i = 0;
i++;
return i;
}
public static void main(String args[])
{
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
A.
0
B.
1
C.
2
D.
Compilation fails.
Answer: Option D
Explanation:
Compilation failed because static was an illegal start of expression - method variables do not have a modifier (they are always considered local).
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
A.
12 15
B.
15 15
C.
3 4 5 3 7 5
D.
3 7 5 3 7 5
Answer: Option B
Explanation:
Output: 15 15
The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object.
So Output: 3+7+5+" "3+7+5
Output: 15 15 Because Numeric values will be added
class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
A.
true
B.
false
C.
Compilation fails
D.
An exception is thrown at runtime
Answer: Option C
Explanation:
The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables.
Option A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code, the output would be false.
class Bitwise
{
public static void main(String [] args)
{
int x = 11 & 9;
int y = x ^ 3;
System.out.println( y | 12 );
}
}
A.
0
B.
7
C.
8
D.
14
Answer: Option D
Explanation:
The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.
The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to be true. Hence it prints "dokey".
for (int i = 0; i < 4; i += 2)
{
System.out.print(i + " ");
}
System.out.println(i); /* Line 5 */
A.
0 2 4
B.
0 2 4 5
C.
0 1 2 3 4
D.
Compilation fails.
Answer: Option D
Explanation:
Compilation fails on the line 5 - System.out.println(i); as the variable i has only been declared within the for loop. It is not a recognised variable outside the code block of loop.
int x = 3;
int y = 1;
if (x = y) /* Line 3 */
{
System.out.println("x =" + x);
}
A.
x = 1
B.
x = 3
C.
Compilation fails.
D.
The code runs with no output.
Answer: Option C
Explanation:
Line 3 uses an assignment as opposed to comparison. Because of this, the if statement receives an integer value instead of a boolean. And so the compilation fails.
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (RuntimeException ex) /* Line 10 */
{
System.out.print("B");
}
catch (Exception ex1)
{
System.out.print("C");
}
finally
{
System.out.print("D");
}
System.out.print("E");
}
public static void badMethod()
{
throw new RuntimeException();
}
}
A.
BD
B.
BCD
C.
BDE
D.
BCDE
Answer: Option C
Explanation:
A Run time exception is thrown and caught in the catch statement on line 10. All the code after the finally statement is run because the exception has been caught.
public class ExceptionTest
{
class TestException extends Exception {}
public void runTest() throws TestException {}
public void test() /* Point X */
{
runTest();
}
}
At Point X on line 5, which code is necessary to make the code compile?
A.
No code is necessary.
B.
throws Exception
C.
catch ( Exception e )
D.
throws RuntimeException
Answer: Option B
Explanation:
Option B is correct. This works because it DOES throw an exception if an error occurs.
Option A is wrong. If you compile the code as given the compiler will complain:
"unreported exception must be caught or declared to be thrown" The class extends Exception so we are forced to test for exceptions.
Option C is wrong. The catch statement belongs in a method body not a method specification.
Option D is wrong. TestException is a subclass of Exception therefore the test method, in this example, must throw TestException or some other class further up the Exception tree. Throwing RuntimeException is just not on as this belongs in the java.lang.RuntimeException branch (it is not a superclass of TestException). The compiler complains with the same error as in A above.
You need to store elements in a collection that guarantees that no duplicates are stored. Which one of the following interfaces provide that capability?
A.
Java.util.Map
B.
Java.util.List
C.
Java.util.Collection
D.
None of the above
Answer: Option A
Explanation:
Option A is correct. A Map cannot contain duplicate keys.
Option B is wrong. Lists typically allow duplicate elements.
Option C is wrong. Collection allows duplicate elements.
The char type is integral but unsigned. The range of a variable of type char is from 0 to 216-1 or 0 to 65535. Java characters are Unicode, which is a 16-bit encoding capable of representing a wide range of international characters. If the most significant nine bits of a char are 0, then the encoding is the same as seven-bit ASCII.
The Iterator interface declares only three methods: hasNext, next and remove.
The ListIterator interface extends both the List and Iterator interfaces.
The ListIterator interface provides forward and backward iteration capabilities.
The ListIterator interface provides the ability to modify the List during iteration.
The ListIterator interface provides the ability to determine its position in the List.
A.
2, 3, 4 and 5
B.
1, 3, 4 and 5
C.
3, 4 and 5
D.
1, 2 and 3
Answer: Option B
Explanation:
The ListIterator interface extends the Iterator interface and declares additional methods to provide forward and backward iteration capabilities, List modification capabilities, and the ability to determine the position of the iterator in the List.
Which method must be defined by a class implementing the java.lang.Runnable interface?
A.
void run()
B.
public void run()
C.
public void start()
D.
void run(int priority)
Answer: Option B
Explanation:
Option B is correct because in an interface all methods are abstract by default therefore they must be overridden by the implementing class. The Runnable interface only contains 1 method, the void run() method therefore it must be implemented.
Option A and D are incorrect because they are narrowing the access privileges i.e. package(default) access is narrower than public access.
Option C is not method in the Runnable interface therefore it is incorrect.
Deadlock will not occur if wait()/notify() is used
A thread will resume execution as soon as its sleep duration expires.
Synchronization can prevent two objects from being accessed by the same thread.
The wait() method is overloaded to accept a duration.
The notify() method is overloaded to accept a duration.
Both wait() and notify() must be called from a synchronized context.
A.
1 and 2
B.
3 and 5
C.
4 and 6
D.
1 and 3
Answer: Option C
Explanation:
Statements (4) and (6) are correct. (4) is correct because the wait() method is overloaded to accept a wait duration in milliseconds. If the thread has not been notified by the time the wait duration has elapsed, then the thread will move back to runnable even without having been notified.
(6) is correct because wait()/notify()/notifyAll() must all be called from within a synchronized, context. A thread must own the lock on the object its invoking
wait()/notify()/notifyAll() on.
(1) is incorrect because wait()/notify() will not prevent deadlock.
(2) is incorrect because a sleeping thread will return to runnable when it wakes up, but it might not necessarily resume execution right away. To resume executing, the newly awakened thread must still be moved from runnable to running by the scheduler.
(3) is incorrect because synchronization prevents two or more threads from accessing the same object.
(5) is incorrect because notify() is not overloaded to accept a duration.
If assertions are compiled into a source file, and if no flags are included at runtime, assertions will execute by default.
B.
As of Java version 1.4, assertion statements are compiled by default.
C.
With the proper use of runtime arguments, it is possible to instruct the VM to disable assertions for a certain class, and to enable assertions for a certain package, at the same time.
D.
When evaluating command-line arguments, the VM gives -ea flags precedence over -da flags.
Answer: Option C
Explanation:
Option C is true because multiple VM flags can be used on a single invocation of a Java program.
Option A is incorrect because at runtime assertions are ignored by default.
Option B is incorrect because as of Java 1.4 you must add the argument -source 1.4 to the command line if you want the compiler to compile assertion statements.
Option D is incorrect because the VM evaluates all assertion flags left to right.
try
{
Float f1 = new Float("3.0");
int x = f1.intValue();
byte b = f1.byteValue();
double d = f1.doubleValue();
System.out.println(x + b + d);
}
catch (NumberFormatException e) /* Line 9 */
{
System.out.println("bad number"); /* Line 11 */
}
A.
9.0
B.
bad number
C.
Compilation fails on line 9.
D.
Compilation fails on line 11.
Answer: Option A
Explanation:
The xxxValue() methods convert any numeric wrapper object's value to any primitive type. When narrowing is necessary, significant bits are dropped and the results are difficult to calculate.
public class ExamQuestion6
{
static int x;
boolean catch()
{
x++;
return true;
}
public static void main(String[] args)
{
x=0;
if ((catch() | catch()) || catch())
x++;
System.out.println(x);
}
}
A.
1
B.
2
C.
3
D.
Compilation Fails
Answer: Option D
Explanation:
Initially this looks like a question about the logical and logical shortcut operators "|" and "||" but on closer inspection it should be noticed that the name of the boolean method in this code is "catch". "catch" is a reserved keyword in the Java language and cannot be used as a method name. Hence Compilation will fail.