All Questions


#251Consider following classes:

//In File Other.java
package other;
public class Other
{
public static String hello = "Hello";
}


//In File Test.java
package testPackage;
import other.*;
class Test
{
public static void main(String[] args)
{
String hello = "Hello", lo = "lo";
System.out.print((testPackage.Other.hello == hello) + " "); //line 1
System.out.print((other.Other.hello == hello) + " "); //line 2
System.out.print((hello == ("Hel"+"lo")) + " "); //line 3
System.out.print((hello == ("Hel"+lo)) + " "); //line 4
System.out.println(hello == ("Hel"+lo).intern()); //line 5
}
}


class Other
{
static String hello = "Hello";
}


What will be the output of running class Test?

Select 1 correct option


(1)false false true false true
(2)false true true false true
(3)true true true true true
(4)true true true false true
(5)None of the above

Answer : -------------------------

#252Any class may be unloaded when none of it's instances and class objects that represent this class are reachable. True Or False?

(1)True
(2)False

Answer : -------------------------

#253The following program will print java.lang.ArithmeticException: / by zero.

class Test
{
public static void main(String[] args)
{
int d = 0;
try
{
int i = 1 / (d* doIt());
} catch (Exception e)
{
System.out.println(e);
}
}

public static int doIt() throws Exception
{
throw new Exception("Forget It");
}
}

True Or False?



(1)True
(2)False

Answer : -------------------------

#254What will be the result of attempting to compile and run the following program?

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;
}
}

Select 1 correct option


(1)The progarm will fail to compile
(2)Class cast exception at runtime
(3)It will print 30, 20
(4)It will print 30, 30
(5)It will print 20, 20

Answer : -------------------------

#255The following code snippet will print true.

String str1 = "one";
String str2 = "two";
System.out.println( str1.equals(str1=str2) );




(1)True
(2)False

Answer : -------------------------

#256Which of the following statements are true?

(1)An anonymous class cannot be inherited
(2)An anonymous class may extend another class
(3)An anonymous class may not create and start a Thread
(4)An anoymous class may not declare a constructor

Answer : -------------------------

#257Given:

class TestClass
{
int i;
public TestClass(int i) { this.i = i; }
public String toString()
{
if(i == 0) return null;
else return ""+i;
}

public static void main(String[ ] args)
{
TestClass t1 = new TestClass(0);
TestClass t2 = new TestClass(2);
System.out.println(t2);
System.out.println(""+t1);
}
}

What will be the output of the following program?

Select 1 correct option.



(1)It will throw NullPointerException when run
(2)It will not compile
(3)It will print 2 and then will throw NullPointerException
(4)It will print 2 and null
(5)None of the above

Answer : -------------------------

#258Which of the following statements about NaNs are true ?

(1)Float.NaN == Float.NaN
(2)Float.NaN == Double.NaN
(3)Float.NaN >= 0
(4)Float.NaN < 0
(5)None of the above

Answer : -------------------------

#259What is the result of executing the following fragment of code:


boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = !b2)
{
System.out.println("true");
}
else
{
System.out.println("false");
}


Select 1 correct option


(1)Compile time error
(2)It will print true
(3)It will print false
(4)Runtime error
(5)It will print nothing

Answer : -------------------------

#260Given two collection objects referenced by c1 and c2, which of these statements are true?

Select 2 correct options


(1)c1.retainAll(c2) will not modify c1
(2)c1.removeAll(c2) will not modify c1
(3)c1.addAll(c2) will return a new collection object, containing elements from both c1 and c2
(4)For: c2.retainAll(c1); c1.containsAll(c2); 2nd statement will return true
(5)For: c2.addAll(c1); c1.retainAll(c2); 2nd statement will have no practical effect on c1

Answer : -------------------------

#261What happens when the following code gets executed:

class TechnoSample {
public static void main(String[] args) {
double d1 = 1.0;
double d2 = 0.0;
byte b =1;

d1 = d1/d2;
b = (byte) d1;
System.out.print(b);
}
}



(1)It results in the throwing of an ArithmeticExcepiton
(2)It results in the throwing of a DivedeByZeroException
(3)It displays the value 1.5
(4)It displays the value –1

Answer : -------------------------

#262

Class finalization can be done by implementing the following method:
static void classFinalize() throws Throwable;

True Or False?



(1)True
(2)False

Answer : -------------------------

#263Consider the following method:

public void getLocks(Object a, Object b)
{
synchronized(a)
{
synchronized(b)
{
//do something
}
}
}

and the following instantiations:


Object obj1 = new Object();
Object obj2 = new Object();


obj1 and obj2 are accesible to two different threads and the threads are about to call the getLocks() method.

Assume the first thread calls the method getLocks(obj1, obj2).


Which of the following is true?


Options

Select 1 correct option



(1)The second thread should call getLocks(obj2, obj1)
(2)The second thread should call getLocks(obj1, obj2)
(3)The second thread should call getLocks() only after first thread exits out of it
(4)The second thread may call getLocks() any time and passing parameters in any order
(5)None of the above

Answer : -------------------------

#264Which of these statements concerning nested classes and interfaces are true?

Select 3 correct options

(1)An instance of a top-level nested class has an inherent outer instance
(2)A top-level nested class can contain non-static member variables
(3)A top-level nested interface can contain static member variables
(4)A top-level nested interface has an inherent outer instance associated with it
(5)For each instance of the outer class, there can exist many instances of a non-static inner class

Answer : -------------------------

#265Given the following definition of class, which member variables are accessible from OUTSIDE the package com.technopark?

package com.technopark;

public class TestClass
{
int i;
public int j;
protected int k;
private int l;
}

Select 2 correct options


(1)Member variable i
(2)Member variable j
(3)Member variable k
(4)Member variable k, but only for subclasses
(5)Member variable l

Answer : -------------------------

#266Which of the following are wrapper classes for primitive types?

Select 1 correct option

(1)java.lang.String
(2)java.lang.Void
(3)java.lang.Null
(4)java.lang.Object
(5)None of the above

Answer : -------------------------

#267Which of the following statements are true? Select 2 correct options.


(1)Private methods cannot be overriden in subclasses
(2)A subclass can override any method in a non-final superclass
(3)An overriding method can declare that it throws a wider spectrum of exceptions than the method it is overriding
(4)The parameter list of an overriding method must be a subset of the parameter list of the method that it is overriding
(5)The over riding method may opt not to declare any throws clause even if the original method has a throws clause

Answer : -------------------------

#268
Will the following code compile ? If yes , what is the output ?

class sample {

sample(int i){
System.out.println(i);
this.i = i;

}

public static void main(String args[]){
sample object = new sample(10);
}
}


(1)0
(2)10
(3)null
(4)Compile error "sample.java:10: No variable i defined in class sample"

Answer : -------------------------


2001-2003 Technopark. Developed by: Giri Mandalika