1 . Given:
public class Derived extends Demo { int L, M, N; public Derived(int x, int y) { M = x; N = y; } public Derived(int x) { super(x); } }
Which of the following constructor signatures MUST exist in the Demo class for Derived to compile correctly?
- public Demo(int a, int b)
- public Demo(int c)
- public Demo()
2 . Which of the following class declarations for a normal top level class are incorrect?
- public synchronized class Base extends Thread
- private protected class Base
- public abstract class Base
- class Base extends Thread
3 . The GenericFruit class declares the following method:
public void setCalorieContent(float f)
You are writing a class Apple to extend GenericFruit and wish to add methods which overload the method in GenericFruit.
Choose the one below:
- protected float setCalorieContent(String s)
- protected void setCalorieContent(float x)
- public void setCalorieContent(double d)
- public void setCalorieContent(String s) throws NumberFormatException
4 . Here is the hierarchy of Exceptions related to array index errors?
Exception +-- RuntimeException +-- IndexOutOfBoundsException +-- ArrayIndexOutOfBoundsException +-- StringIndexOutOfBoundsException
Suppose you had a method X which could throw both array index and string index exceptions.
Assuming that X does not have any try-catch statements, which of the following statements are correct?
Choose the one below:
- The declaration for X must include “throws ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException”
- If a method calling X catches IndexOutOfBoundsException, both array and string index exceptions will be caught
- If the declaration for X includes “throws IndexOutOfBoundsException”, any calling method must use a try-catch block
- The declaration for X does not have to mention exceptions
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?
public boolean strCvrt(String s){ try { factor = Double.valueOf(s).doubleValue(); return (true); } catch(NumberFormatException e) { System.out.println("Bad Number " + s); } finally { System.out.println("Finally"); } return (false); }
Which of the following descriptions of the results of various inputs to the method are correct?
- Input = “0.1234”. Result: factor = 0.1234, “Finally” is printed, true is returned
- Input = “0.1234”. Result: factor = 0.1234, “Finally” is printed, false is returned
- Input = null. Result: factor = NaN, “Finally” is printed, false is returned
- Input = null. Result: factor unchanged, “Finally” is printed, NullPointerException is thrown
6 . What will be the output?
XXXX x; // variable x is declared and initialized here switch(x) { case 100: System.out.println("One Hundred"); break; case 200: System.out.println("Two Hundred"); break; case 300: System.out.println("Three Hundred"); break; }
Choose the one below:
- byte x = 100;
- short x = 200;
- int x = 300;
- long x = 400;
7 . In the following code for a class in which methodA has an inner class?
public class Base { private static final int ID = 3; private String name; public void methodA(final int nn) { int serialN = 11; class inner { void showResult() { System.out.println("Result = " + XX); } } // inner new inner(); } // methodA }
Which variables would the statement in line 8 be able to use in place of XX?
- int ID (line 2)
- String name (line 3)
- int nn (line 4)
- int serialN (line 5)
8 . What will be the output?
public class Logic { static int minusOne = -1; static public void main(String[] arguments) { int N = minusOne >> 31; System.out.println("N = " + N); } }
Choose the one below:
- The program will compile and run, producing the output “N = -1”
- The program will compile and run, producing the output “N = 1”
- A run time ArithmeticException will be thrown
- The program will compile and run, producing the output “N = 0”
9 . What will be the output?
(Assume that the code is compiled and run with assertions enabled.)
public class AssertTest { public void methodA(int i) { assert i >= 0 : methodB(); System.out.println(i); } public void methodB() { System.out.println("The value must not be negative"); } public static void main(String args[]) { AssertTest test = new AssertTest(); test.methodA(-10); } }
Choose the one below:
- It will print -10
- It will result in Assertion Error showing the message -“The value must not be negative”
- The code will not compile
- None of these
10 . What will be the output?
public class Static { static { int x = 5; } static int x,y; public static void main(String args[]) { x--; myMethod(); System.out.println(x + y + ++x); } public static void myMethod() { y = x++ + ++x; } }
Choose the one below:
- Compile-time error
- prints 1
- prints 2
- prints 3
- prints 7
- prints 8
11 . What will be the output?
class MyParent { int x, y; MyParent(int x, int y) { this.x = x; this.y = y; } public int addMe(int x, int y) { return this.x + x + y + this.y; } public int addMe(MyParent myPar) { return addMe(myPar.x, myPar.y); } } class MyChild extends MyParent { int z; MyChild (int x, int y, int z) { super(x,y); this.z = z; } public int addMe(int x, int y, int z) { return this.x + x + this.y + y + this.z + z; } public int addMe(MyChild myChi) { return addMe(myChi.x, myChi.y, myChi.z); } public int addMe(int x, int y) { return this.x + x + this.y + y; } } public class MySomeOne { public static void main(String args[]) { MyChild myChi = new MyChild(10, 20, 30); MyParent myPar = new MyParent(10, 20); int x = myChi.addMe(10, 20, 30); int y = myChi.addMe(myChi); int z = myPar.addMe(myPar); System.out.println(x + y + z); } }
Choose the one below:
- 300
- 240
- 120
- 180
- Compilation error
- None of the above
12 . What will be the output?
boolean a = true; boolean b = false; boolean c = true; if (a == true) if (b == true) if (c == true) System.out.println("Some things are true in this world"); else System.out.println("Nothing is true in this world!"); else if (a && (b = c)) System.out.println("It's too confusing to tell what is true and what is false"); else System.out.println("Hey this won't compile");
Choose the one below:
- The code won’t compile
- “Some things are true in this world” will be printed
- “Hey this won’t compile” will be printed
- None of these
13 . What will be the output?
interface MyInterface { } public class MyInstanceTest implements MyInterface { static String s; public static void main(String args[]) { MyInstanceTest t = new MyInstanceTest(); if(t instanceof MyInterface) { System.out.println("I am true interface"); } else { System.out.println("I am false interface"); } if(s instanceof String) { System.out.println("I am true String"); } else { System.out.println("I am false String"); } } }
Choose the one below:
- Compile-time error
- Runtime error
- Prints : “I am true interface” followed by ” I am true String”
- Prints : “I am false interface” followed by ” I am false String”
- Prints : “I am true interface” followed by ” I am false String”
- Prints : “I am false interface” followed by ” I am true String”
14 . What will be the output?
public class Ternary { public static void main(String args[]) { int a = 5; System.out.println("Value is - " + ((a < 5) ? 9.9 : 9)); } }
Choose the one below:
- prints: Value is – 9
- prints: Value is – 5
- Compilation error
- None of these
15 . In the following pieces of code, A and D will compile without any error. True/False?
A: StringBuffer sb1 = "abcd"; B: Boolean b = new Boolean("abcd"); C: byte b = 255; D: int x = 0x1234; E: float fl = 1.2;
Choose the one below:
- True
- False
16 . Which of the following collection classes from java.util package are Thread safe?
- Vector
- ArrayList
- HashMap
- Hashtable
17 . What will be the output?
class MyThread extends Thread { public void run() { System.out.println("MyThread: run()"); } public void start() { System.out.println("MyThread: start()"); } } class MyRunnable implements Runnable { public void run() { System.out.println("MyRunnable: run()"); } public void start() { System.out.println("MyRunnable: start()"); } } public class MyTest { public static void main(String args[]) { MyThread myThread = new MyThread(); MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable); myThread.start(); thread.start(); } }
Choose the one below:
- Prints : MyThread: start() followed by MyRunnable:run()
- Prints : MyThread: run() followed by MyRunnable:start()
- Prints : MyThread: start() followed by MyRunnable:start()
- Prints : MyThread: run() followed by MyRunnable:run()
- Compile time error
- None of the above
18 . What will be the output?
// Filename; SuperclassX.java package packageX; public class SuperclassX { protected void superclassMethodX() { } int superclassVarX; } // Filename SubclassY.java package packageX.packageY; public class SubclassY extends SuperclassX { SuperclassX objX = new SubclassY(); SubclassY objY = new SubclassY(); void subclassMethodY() { objY.superclassMethodX(); int i; i = objY.superclassVarX; } }
Choose the one below:
- Compilation error at line 5
- Compilation error at line 9
- Runtime exception at line 11
- None of these
19 . What can cause a Thread to stop executing?
- Calling its own yield method
- Calling the yield method of another thread
- A call to the halt method of the Thread class
- Another thread is given higher priority
20 . Which of the following statements are true?
- The keys of HashSet are not ordered
- The keys of LinkedHashSet are ordered
- The keys of LinkedHashSet are ordered but not sorted
- The keys of LinkedHashMap are sorted
21 . Given:
public class Test{ public static void main(String[] args) { Object a = new Object(); // the object original referenced by object reference a Object b = new Object(); Object c = new Object(); Object d = new Object(); d=c=b=a; d=null; } }
How many objects are eligible for GC in the following code after d = null?
- 1
- 2
- 3
- 4
22 . What is wrong with the following code?
abstract class TestClass { transient int j; synchronized int k; final void TestClass(){} static void f(){} }
Choose the one below:
- The class TestClass cannot be declared abstract
- The variable j cannot be declared transient
- The variable k cannot be declared synchronized
- The constructor TestClass( ) cannot be declared final
- The method f( ) cannot be declared static
23 . Which of these classes have a comparator() method?
- TreeSet
- HashMap
- TreeMap
- HashSet
- ArrayList
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?
- Call t3.resume() after releasing the lock
- Call t3.release() after releasing the lock
- Instead of releasing the lock, call t3.accuire(obj);
- Instead of releasing the lock, call t3.notify(obj);
- None of these
25 . What will be the output?
public class MyFor { public static void main(String argv[]){ int i; int j; outer: for (i=1;i <3;i++) inner: for(j=1; j<3; j++) { if (j==2) continue outer; System.out.println("Value for i=" + i + " Value for j=" +j); } } }
Choose the one below:
- Value for i=1 value for j=1
- Value for i=2 value for j=1
- Value for i=2 value for j=2
- Value for i=3 value for j=1
26 . What will be the output?
public class TestClass { public static void main(String args[ ] ) { A o1 = new C( ); B o2 = (B) o1; System.out.println(o1.m1( ) ); System.out.println(o2.i ); } } class A { int i = 10; int m1( ) { return i; } } class B extends A { int i = 20; int m1() { return i; } } class C extends B { int i = 30; int m1() { return i; } }
Choose the one below:
- The progarm will fail to compile
- ClassCastException at runtime
- It will print 30, 20
- It will print 30, 30
- It will print 20, 20
27 . What will be the output?
System.out.println(null + true); //1 System.out.println(true + null); //2 System.out.println(null + null); //3
Choose the one below:
- None of the 3 lines will compile
- All the 3 line will compile and print nulltrue, truenull and nullnull respectively
- Line 1 and 2 won’t compile but line 3 will print nullnull
- Line 3 won’t compile but line 1 and 2 will print nulltrue and truenull respectively
- None of the above
28 . Which of the following are true about the “default” constructor?
- It is provided by the compiler only if the class does not define any constructor
- It initializes the instance members of the class
- It calls the default ‘no-args’ constructor of the super class
- It initializes instance as well as class fields of the class
- It is provided by the compiler if the class does not define a ‘no- args’ constructor
29 . Which of these methods from the Collection interface return the value true if the collection object was actually modified by the call?
- add( )
- retainAll( )
- containsAll( )
- contains( )
- remove()
30 . Following is not a valid comment?
/* this comment /* // /** is not valid */
Choose the one below:
- True
- False
31 . Which statements regarding the following code are correct?
class Outer { private void Outer() { } protected class Inner { } }
Choose the one below:
- This code won’t compile
- Constructor for Outer is public
- Constructor for Outer is private
- Constructor for Inner is public
- Constructor for Inner is protected
32 . Consider the following method:
public float parseFloat( String s ) { float f = 0.0f; try { f = Float.valueOf( s ).floatValue(); return f ; } catch(NumberFormatException nfe) { f = Float.NaN ; return f; } finally { f = 10.0f; return f; } }
What will it return if the method is called with the input “0.0” ?
Choose the one below:
- It won’t even compile
- It will return 10.0
- It will return Float.Nan
- It will return 0.0
- None of the above
33 . The following code snippet will not compile?
int i = 10; System.out.println( i<20 ? out1() : out2() );
Assume that out1 and out2 have method signature: public void out1(); and public void out2();
- True
- False
34 . Giri has written the following class to prevent garbage collection of the objects of this class. Is he mistaken?
class SelfReferencingClassToPreventGC { private SelfReferencingClassToPreventGC a; Object obj = new Vector(); public SelfReferencingClassToPreventGC() { a = this; //reference itself to make sure that this is not garbage collected. } public void finalize() { System.out.println("Object GCed"); } }
Choose the one below:
- True
- False
35 . What will be the output?
public class TestClass { public static void main(String[] args) throws Exception { int a = Integer.MIN_VALUE; int b = -a; System.out.println( a+ " "+b); } }
Choose the one below:
- It throws an OverFlowException
- It will print two same -ive numbers
- It will print two different -ive numbers
- It will print one -ive and one +ive number of same magnitude
- It will print one -ive and one +ive number of different magnitude
36 . What will be the output?
void m1() throws Exception { try { // line1 } catch (IOException e) { throw new SQLException(); } catch(SQLException e) { throw new InstantiationException(); } finally { throw new CloneNotSupportedException() // this is not a RuntimeException. } }
Choose the two below:
- If IOException gets thrown at line1, then the whole method will end up throwing SQLException
- If IOException gets thrown at line1, then the whole method will end up throwing CloneNotSupportedException
- If IOException gets thrown at line1, then the whole method will end up throwing InstantiationException()
- If no exception is thrown at line1, then the whole method will end up throwing CloneNotSupportedException
- If SQLException gets thrown at line1, then the whole method will end up throwing InstantiationException()
37 . Consider the following class hierarchy?
A | +----B1, B2 | +----C1, C2
(B1 and B2 are subclasses of A and C1, C2 are subclasses of B1)
Assume that method public void m1(){ … } is defined in all of these classes EXCEPT B1 and C1?
Choose the one below:
- objectOfC1.m1(); will cause a compilation error
- objectOfC2.m1(); will cause A’s m1() to be called
- objectOfC1.m1(); will cause A’s m1() to be called
- objectOfB1.m1(); will cause an expection at runtime
- objectOfB2.m1(); will cause an expection at runtime
38 . What classes can an inner class extend? (Provided that the class is visible and is not final)
- Only the encapsulating class
- Any top level class
- Any class
- It depends on whether the inner class is defined in a method or not
- None of the above
39 . Which of the following statements are true?
- System.out.println( -1 >>> 2);will output a result larger than 10
- System.out.println( -1 >>> 2); will output a positive number
- System.out.println( 2 >> 1); Will output the number 1
- System.out.println( 1 <<< 2); will output the number 4
40 . Given the following class definition, which of the following statements would be legal after the comment //Here?
class InOut{ String s= new String("Between"); public void amethod(final int iArgs){ int iam; class Bicycle{ public void sayHello(){ //Here }//End of bicycle class } }//End of amethod public void another(){ int iOther; } }
Choose the one below:
- System.out.println(s);
- System.out.println(iOther);
- System.out.println(iam);
- System.out.println(iArgs);
41 . Which statments regarding the following program are correct?
class A extends Thread { static protected int i = 0; public void run() { for(; i<5; i++) System.out.println("Hello"); } } public class TestClass extends A { public void run() { for(; i<5; i++) System.out.println("World"); } public static void main(String args []) { Thread t1 = new A(); Thread t2 = new TestClass(); t2.start(); t1.start(); } }
Choose the one below:
- It’ll not compile as run method cannot be overridden
- It’ll print both “Hello” and “World” 5 times each
- It’ll print both “Hello” and “World” 5 times each but they may be interspersed
- Total 5 words will be printed
- Either 5 “Hello” or 5 “world” will be printed
42 . What will be the output?
// file A.java package p1; public class A { protected int i = 10; public int getI() { return i; } }
// file B.java package p2; import p1.*; public class B extends p1.A { public void process(A a) { a.i = a.i * 2; } public static void main(String[] args) { A a = new B(); B b = new B(); b.process(a); System.out.println( a.getI() ); } }
Choose the one below:
- It will print 10
- It will print 20
- It will not compile
- It will throw an exception at Run time
- None of the above
43 . What will happen when you attempt to compile and run the following code with the command line?
"java hello there"
public class Arg{ String[] MyArg; public static void main(String argv[]){ MyArg=argv; } public void amethod(){ System.out.println(argv[1]); } }
Choose the one below:
- Compile time error
- Compilation and output of “hello”
- Compilation and output of “there”
- None of the above
44 . Which of the following statements are true?
- Checked exceptions are derived directly from Exception
- Checked exceptions are derived directly from RuntimeException
- Unchecked exceptions are derived directly from Exception
- Unchecked exceptions are derived direclty from RuntimeException
- Exception and RuntimeException are both subclasses of Throwable
45 . Which of the following keywords are valid when declaring a top level class?
- private
- native
- final
- transient
- abstract
46 . What will be the output?
public class TechnoSample { public static void main(String[] args) { int a = 2, b = 3, c = 4; a = b++ + c; c += b; b += a * 2; System.out.println("a: " + a + " b: " + b + " c: " + c); } }
Choose the one below:
- a: 2 b: 18 c: 8
- a: 7 b: 12 c: 8
- a: 7 b: 18 c: 8
- a: 7 b: 18 c: 4
- None of the above
47 . What is wrong with the following code?
abstract class TestClass { transient int j; synchronized int k; final void TestClass(){} static void f() { k = j++; } }
Choose the one below:
- The class TestClass cannot be declared abstract
- The variable j cannot be declared transient
- The variable k cannot be declared synchronized
- The constructor TestClass( ) cannot be declared final
- The method f( ) cannot be declared static
48 . Which of the following statements are true?
- An object will be garbage collected when it becomes unreachable
- An object will be garbage collected if it has null assigned to it
- The finalize method will be run before an object is garbage collected
- Garbage collection assures that a program will never run out of memory
49 . What will be the output?
class TechnoSample { public static void main(String[] args) { int i = 4; int ia[][][] = new int[i][i = 3][i]; System.out.println(ia.length + ", " + ia[0].length+", "+ ia[0][0].length); } }
Choose the one below:
- 3, 4, 3
- 3, 3, 3
- 4, 3, 4
- 4, 3, 3
- It will not compile
50 . What will be the output?
//in file A.java package p1; public class A { protected int i = 10; public int getI() { return i; } }
//in file B.java package p2; import p1.*; public class B extends p1.A { public void process(A a) { a.i = a.i*2; } public static void main(String[] args) { A a = new B(); B b = new B(); b.process(a); System.out.println(a.getI()); } }
Choose the one below:
- It will print 10
- It will print 20
- It will not compile
- It will throw an exception at Run time
- None of the above
Answers
1 : 2 & 3 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
Explanation:
Please see Java 1.4 API documentation
24 : 5 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
Explanation:
The default constructor is provided by the compiler only when a class does not define ANY constructor explicitly.
29 : 1,2 & 5 is correct.
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 is correct.
Explanation:
Every thing after /* is ignored till */ is reached. Here, ‘this comment /* // /** is not vaid’ is ignored.
31 : 5 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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 is correct.
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.