Showing posts with label Mock Question and Answers. Show all posts
Showing posts with label Mock Question and Answers. Show all posts

Friday, May 15, 2009

Java Mock Exam Questions with detailed answers

Q1     Given:

 class J {
  private static int notFinalized;
  public static int notFinalized() {return notFinalized;}
  private K k;
  private int name;
  public int name() {return name;}
  public J(K k, int i) {this.k = k; name = i; notFinalized++;}
  public void finalize() {
    synchronized (k) {
      System.out.print(name);
      notFinalized--;
      k.notify();
    }
  }
}

class K {
  private void m1() {
    J j = null;
    for (int i = 0; i < 5; i++) {
      j = new J(this, i);       // 1
    }
    Runtime.getRuntime().gc();  // 2
    synchronized (this) {
      while (J.notFinalized() > 0) {
        try {wait();} catch (InterruptedException ie) {}
      }
    }
  }
  public static void main(String[] args) {
    new K().m1();
  }
}


When the processing of line 2 begins how many objects of type J that were created at line 1 are eligible for garbage collection?

    (1)     0
    (2)     1
    (3)     4
    (4)     5
    (5)     Can not be determined without more information
    (6)     Compiler error
    (7)     Run time error
    (8)     None of the above
       
    Answer : 3
    Explanation :

 Method K.m1 creates five objects of type J. Each instance has a name represented by an integer between 0 and 4 inclusive.
If the garbage collector does not run then the program will produce no output.If the garbage collector does run then the output of the program could be a series of integers that are the names of four of the five objects.
As each new object is created its reference is assigned to the reference variable j.The previously referenced object then becomes eligible for garbage collection. The last object created, 4, is not available for garbage collection until method m1 runs to completion.
The while loop in the synchronized block will never complete because J.notFinalized will never return zero.
 This program is intended to provide a working example of garbage collecting objects referenced by local variables.
       
Q2    What is the output of the following code when compiled and run? Select two correct answers


1 public class Sample {
2    public static void main(String[] args){
3        int y=0;               
4        int x=z=1;   
5        System.out.println(y+","+x+","+z);
6    }
7 }



    (1)     Prints 0,1,1
    (2)     Error during compilation at line 3
    (3)     Prints 0,0,1
    (4)     Error during compilation at line 4
    (5)     Error during compilation at line 5
       
    Answer : 4,5
    Explanation :

Variable z is not declared, thus, z cannot be resolved on lines 2 and 3. In Java, z cannot be declared that way. In order to get this code to compile, we have to write either:
int z=1,x=z;
Or
int z=1;
int x=z;
Or
int z=1;
int x=1;
       
Q3     What is the output of the following code when compiled and run? Select one correct answer


1 public class Sample {
2    public static void main(String[] args){
3        int j = 017;
4        int i = (byte)j >> 2;
5        System.out.println(Integer.toBinaryString(i));
6    }
7 }



    (1)     Prints 3
    (2)     Error during compilation at line 4
    (3)     Error during compilation at line 5
    (4)     Prints 11
    (5)     Prints 0
       
    Answer : 4
    Explanation :

First off, 017 is an octal integer literal having the decimal value 15. Second, the cast to byte only applies to j and not to j >> 2 as a whole. Thus, j is downcast to byte and then upcast to int again before the shifting.Briefly, the cast has no effect here. Then, the binary sequence of 15 is 00000000 00000000 00000000 00001111, which, shifted 2 bits to the right, yields 00000000 00000000 00000000 00000011. Finally, the binary sequence, 11, is printed. Note that the prefixed 0's are dismissed.
       
Q4    Select three correct statements:

    (1)     The garbage collection thread cannot outlive the last user thread
    (2)     The garbage collection can be forced by invoking System.gc().
    (3)     The garbage collection thread is a non-deamon thread
    (4)     The finalize() method is invoked at most once by the JVM for any given object
    (5)     The finalize() method may resurrect the object upon which it has been invoked
       
    Answer : 1,4,5
    Explanation :

The garbage collection thread is a deamon thread. The latter die when there are no more users threads running. The garbage collection cannot be forced.
       
Q5     What is the output of the following code when compiled and run? Select one correct answer.

import java.io.*;
public class Mohit{
    public static void main(String[] args) {
        MohitSub myref = new MohitSub();
        try{
            myref.test();
        }catch(IOException ioe){}
    }
    void test() throws IOException{
        System.out.println("In Mohit");
        throw new IOException();
    }
}
class MohitSub extends Mohit {
    void test() {
        System.out.println("In MohitSub");
    }
}



    (1)     Prints:

    In MohitSub

    (2)     Prints:

    In Mohit

    (3)     Prints:

    In Mohit
        In MohitSub

    (4)     Prints:

    In MohitSub
        In Mohit

    (5)     The code does not compile
       
    Answer : 5
    Explanation :

The code does not compile because no IOException is thrown when invoking myref.test(). Note that myref's declared and runtime types are MohitSub and thus no dynamic lookup will be performed. However, if you change the declared type to Mohit, the code will compile and the correct answer would be A because method test() isoverridden in MohitSub
       
Q6    What is the output of the following code when compiled and run with the following command line: java Friends two three? Select two correct answers.

public class Friends {
    public static void main(String[] args) throws Exception {
        int i=2;
        boolean b = true;
        throw new Exception("Values are:"+(b!=b)+","+(i=args.length)+","+(b=i==2));
    }
}



    (1)     The exception message is Values are:false,3,true
    (2)     The exception message is Values are:true,2,false
    (3)     The exception message is Values are:false,2,true
    (4)     The final value of b is false
    (5)     An exception is thrown at runtime
       
    Answer : 3,5
    Explanation :

Do not mix b!=b and b=!b. In the former, we check if b's value is different from b's value (?!) which is clearly false. In the latter, we assign b's opposite value to itself, that is, if b is true, then after b=!b, b ends up being false.Moreover, be aware that b=i==2 is evaluated as b=(i==2) because operator = has the lowest precedence. Finally, note that the arguments to the Exception constructor are evaluatedfrom the left to the right. First, b!=b is evaluated, then i=args.length (args.length is 2, so i keeps its value), and finally, b=i==2.
       
    Q7    Select two correct statements about the code given below?


class A{}
class B extends A implements E{}    //line 1
class C extends A{}
class D extends B{}
interface E{}
public class Question07 {
    public static void main(String[] args) {
        A a = new D();    //line 2
        C c = new C();    //line 3
        E e = (E)a;    //line 4
        B b = (B)e;    //line 5
    }
}



    (1)     The code compiles without error and runs fine
    (2)     Compilation error on line 1 because interface E is not yet declared (forward-referencing)
    (3)     Compilation error on line 4 because class A does not implement interface E
    (4)     The cast on line 4 is mandatory
    (5)     The cast on line 5 is not mandatory
       
    Answer : 1,4
    Explanation :

First, pay attention to the class hierarchy (B and C are sibling classes!!) Then, there is no such thing as forward-referencing issues when using interfaces declared later in the compilation unit.On line 4, we are dealing with an object whose runtime type is D which implements interface E. The cast is mandatory, though, since the reference type (A) is not assignmentcompatible with the reference type E. The cast on line 5 is mandatory for the same reasons.
       
Q8     How many objects are eligible for garbage collection immediately after line 1? Select one correct answer.


public class HomeGC {
    public static void main(String[] args) {
        HomeGC tGC = new HomeGC();
        tGC.doSomething();    //line 1
        Thread.sleep(20000);
    }

    public void doSomething(){
        Object[] objArray = new Object[2];
        for(int i = 0 ; i < objArray.length ; i++) {
            objArray[i] = new Object();
        }
    }
}



    (1)     0
    (2)     1
    (3)     2
    (4)     3
    (5)     4
       
    Answer : 4
    Explanation :

We declare an array of Object of length two. We then initialize each element to a new Object. We have 2 objects in the array and the array itself (which is an object, too!), that makes 3.
       
Q9     What is the output of the following code when compiled and run? Select one correct answer.


public class ABC {
    public static void main(String[] args) {
        try {
            int i = (int)(Math.random()*10);
            if(i<=5)
                System.out.println("i = "+i);
            else
                throw new Exception("i > 5");
        } catch (Exception e){
            System.err.println(e.getMessage()+" (i="+i+")");
        }
    }
}



    (1)     The output cannot be determined
    (2)     Compilation error
    (3)     An exception is thrown at runtime
    (4)     Output is i = 2
    (5)     Output is i > 5 (i=6)
       
    Answer : 2
    Explanation :

The code does not compile because i (declared in the try block!) is not in scope when accessed from the catch block.
       
Q10     What is the output of the following code when compiled and run? Select one correct answer.


public class ABCSample {
    public static void main(String[] args) {
        new ABCSample().doSomething();
    }

    public void doSomething(){
        int i=5;
        Thread t = new Thread(new Runnable(){
            public void run(){
                for(int j=0;j<=i;j++){
                    System.out.print(" "+j);
                }
            }
        });
        t.start();
    }
}



    (1)     Prints 0 1 2 3 4
    (2)     Compilation error
    (3)     No output
    (4)     IllegalThreadStateException is thrown at runtime
    (5)     Prints 0 1 2 3 4 5
       
    Answer : 2
    Explanation :

The code does not compile because the anonymous inner class (new Runnable(){...}) tries to access the non-final local variable i.

Tuesday, May 6, 2008

SCJP Questions with Answers

Q 1
What is the output of the following code when compiled and run? Select two correct answers.

public class TechnoSample {
public static void main(String[] args){
for(int i = 0; i <>
System.out.println(getPrimitive(127)); //line 1
}
}
public static int getPrimitive(byte b) { //line 2
return (short)(Math.random()*b); //line 3
}
}


(1) Compilation error on line 1
(2) Compilation error on line 2
(3) Compilation error on line 3
(4) Line 3 compiles fine
(5) Prints 10 random numbers between 0 and 127


Answer : 1,4
Explanation :
Line 1 does not compile because getPrimitive() takes a byte and we pass it an int. In a normal assignment (byte b = 127;) this would work because 127 is in the range for byte values and the compiler implicitely does a norrowing conversion.But this is not the case in method invocations. A quote from JLS 5.3: "The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matchingresolution process". This speaks for itself. Line 3 compiles fine because we have to do with a widening primitive conversion form short to int which is perfectly straightforward.


Q2 Select three correct statements.


(1) A static method may override another static method
(2) A static method cannot override a non-static method
(3) A non-static method cannot override a static method
(4) A non-static method may be overloaded by a static method
(5) A synchronized method cannot be overridden


Answer : 2,3,4
Explanation :
Overriding is for non-static methods and hiding is for static methods. So the following statements are the only true statements about hiding and overriding:

a static method (in a subclass) may hide another static method (in a superclass)
a static method (in a subclass) cannot hide a non-static method (in a superclass)
a non-static method (in a subclass) may override another non-static method (in a superclass)
a non-static method (in a subclass) cannot override a static method (in a superclass)



Q3 Select three correct statements about the following code.
public class TechnoSample {
public static void main(String[] args) {
TechnoSample myref = new TechnoSampleSub();
try{
myref.test();
}
catch(Exception e){}
}
void test() throws Exception{
System.out.println("In TechnoSample");
throw new Exception();
}
}
class TechnoSample Sub extends TechnoSample {
void test() {
System.out.println("In TechnoSampleSub");
}
}



(1) The try-catch block that encloses myref.test(); is mandatory for the code to compile
(2) Prints: In TechnoSample
(3) Prints: In TechnoSampleSub
(4) Method test() in class TechnoSampleSub has no obligation to declare a throws clause
(5) An exception is thrown at runtime


Answer : 1,3,4
Explanation :
myref is an instance of class TechnoSampleSub referenced by a variable of type TechnoSample. Method test() in class TechnoSample is overridden in class TechnoSampleSub, thus the one to be invoked is the one declared in class TechnoSampleSub(Polymorphism!). Moreover, test() has no obligation to declare a throws clause (see overriding rules!). The try-catch block is mandatory because myref could as well reference an instanceof class TechnoSample and in that case the method test() to be invoked would be the one declared in class TechnoSample which throws an exception.


Q4 Given the following code:
import java.util.Date;
public class Example {
public static void main(String args[]) {
Date d1 = new Date (99, 11, 31);
Date d2 = new Date (99, 11, 31);
method(d1, d2);
System.out.println("d1 is " + d1 + "\nd2 is " + d2);
}
public static void method(Date d1, Date d2) {
d2.setYear (100);
d1 = d2;
}
}

Which one or more of the following correctly describe the behavior when this program is compiled and run?


(1) compilation is successful and the output is:
d1 is Fri December 31 00:00:00 GMT 1999 d2 is Fri December 31 00:00:00 GMT 1999

(2) compilation is successful and the output is:
d1 is Fri December 31 00:00:00 GMT 1999 d2 is Sun December 31 00:00:00 GMT 2000

(3) compilation is successful and the output is:
d1 is Sun December 31 00:00:00 GMT 2000 d2 is Sun December 31 00:00:00 GMT 2000

(4) the assignment 'd1 = d2' is rejected by the compiler because the Date class cannot overload the operator '='
(5) the expression (d1 is " + d1 + "\nd2 is " + d2) is rejected by the compiler because the Date class cannot overload the operator '+'


Answer : 2
Explanation :
1) is false because we know that the data in d2 was changed. 3) is false because we know that the data in d1 was not changed. The names d1 and d2 are used in both main and method to be confusing. They are different and stored on the stack in different place. All the interesting stuff that happen in the Example class is in method. main simply initializes some data and prints the results. In method, the following happens:
1.d2 has its year set to 100 (really 2000, as 2.Object d1 is set to be the same as d2. This is a change of the actual reference, not in the data at d1.
Both of these line are perfectly legal, and do not result in a compilation error, so d) is false. I will also point out here that e) is String context. toString() is defined by the Object class and so it is available on all classes in Java. Most non-trivial classes override toString() to return more explicit information about themselves.



Monday, March 3, 2008

SCJP 1.5 Dump questions

Q1
class test
{
public static void main(String[] args)
{
test inst_test = new test();
String pig[][] = { {"one little piggy"}, {"two little piggies"}, {"three little piggies"} };
for ( Object []oink : pig )
{
for ( Object piggy : oink )
{
System.out.println(piggy);
}
}
}
}

a. one little piggy two little piggies three little piggies
b. Compile Error incompatible types.
c. java.lang.String;@187c6c7 java.lang.String;@187c6c8 java.lang.String;@187c6c9
( or something like that )
d. Runtime Null Pointer Exception
e. Prints nothing

Answer1:
a. oink refers to every object reference in a one dimensional row of pig[][].
piggy refers to every object within that row.

--------------------------------------------------------------------------
Q2
class test
{
public static void main(String[] args)
{
test inst_test = new test();
int i1 = 2000;
int i2 = 2000;
int i3 = 2;
int i4 = 2;
Integer Ithree = new Integer(2); // 1
Integer Ifour = new Integer(2); // 2
System.out.println( Ithree == Ifour );
inst_test.method( i3 , i4 );
inst_test.method( i1 , i2 );

}
public void method( Integer i , Integer eye )
{
System.out.println(i == eye );
}
}

a. true false true
b. false true false
c. false false false
d. true true false
e. Compile error

Answer 2:
b: false true false. lthree and lfour are two seperate objects. if the lines 1 and 2 were
lthree = 2 and lfour = 2 the result would have been true. This is when the objects are created in the pool. When the references I and eye in the pool are compared 2==2 results in true and 2000==2000 is false since it exceeds 127.

-------------------------------------------------------------------------
Q3
enum cafe {
BIG ( 10 ) ,
SMALL ( 1 ),
MED ( 5 )
int mySize = 0;
cafe ( int size )
{
mySize = size;

}

}

What happens when this enum is in the code outside a class ?

a. Compiles fine
b. Compiler error
c. Runtime Exception occurs if mySize is accessed.

Answer 3:
a: Compile error: semicolon missing after MED ( 5 ). Watch out for that semicolon when an enum has variables and functions.

---------------------------------------------------------------------

Wednesday, January 23, 2008

Some Questions on Objects for SCJP 5

Q1 What 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 : 1
Explanation : Note that, boolean operators have more precedence than =. (In fact, = has least precedenace) so, in (b2 != b1 = !b2) first b2 != b1 is evaluated which returns a value 'false'. So the expression becomes false = !b2. And this is illegalbecause false is a value and not a variable!Had it been something like (b2 = b1 != b2) then its valid because it will boil down to : b2 = false. Because all an if() needs is a boolean, now b1 != b2 returns false which is a boolean andas b2 = false is an expression and every expression has a return value (which is actually the LHS of the erpression). Here it returns true which is again a boolean.Note, return value of expression (i is int) : i = 10 , is 10 (int).


------------------------------------------------------------------------------------------------------------------------------

Q2
Given 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 : 4,5
Explanation : public boolean retainAll(Collection c) retains only the elts in this collection that are contained in the specified collection. In other words, removes from this collection all of its elts that are not contained in the specified collectionpublic boolean removeAll(Collection c) removes all this collection's elts that are also contained in the specified collection. After this call returns, this collection will contain no elts in common with the specifiedcollectionpublic boolean containsAll(Collection c) returns true if this collection contains all of the elts in the specified collectionpublic boolean addAll(Collection c) adds all the elts in the specified collectionto this collection. The behavior of this opern is undefined if the specified collection is modified while the opern is in progress(ie., the behavior of this call is undefined if the specified collection is this collection, and is nonempty)

---------------------------------------------------------------------------------------------------------------------

Q 3
What happens when the following code gets executed:

class Sample {
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 : 4
Explanation : 1.0/0.0 results in Double.POSITIVE_INFINITY. Double.POSITIVE_INFINITY is converted to Integer.MAX_VALUE ('0' followed by 31 '1's). Integer.MAX_VALUE is then cast to byte value, which simply takes the last 8 bits(11111111) and is -1.


---------------------------------------------------------------------------------------------------------

Q4
Class finalization can be done by implementing the following method:static void classFinalize() throws Throwable;True Or False?
(1)True
(2)False
Answer : 2
Explanation : PREVIOUSLY: If a class declares a class method classFinalize that takes no arguments and returns no result: static void classFinalize() throws Throwable { . . . } then this method will be invoked before the class is unloaded . Like thefinalize method for objects, this method will be automatically invoked only once. This method may optionally be declared private, protected, or public. NOW: Class finalization has been removed from the Java language. Thefunctionality of JLS 12.7 is subsumed by instance finalization (JLS 12.6).Here is a rationale for this decision. http://java.sun.com/docs/books/jls/class-finalization-rationale.htmlSimilar thing has happend toclass unloading: A class or interface may be unloaded if and only if its class loader is unreachable (the definition of unreachable is given in JLS 12.6.1). Classes loaded by the bootstrap loader may not be unloaded.

---------------------------------------------------------------------------------------------------------------------------

Q5
Consider 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 : 2
Explanation : (1) This may result in a deadlock (3) The is not necessary. Option 2 works just fine.


------------------------------------------------------------------------------------------------------------

Sunday, January 13, 2008

SCJP Mock Questions and Answers

Ques1
Given:
1. public class MyThread implements Runnable {
2. private String holdA = "This is ";
3. private int[] holdB = {1,2,3,4,5,6,7,8,9,10};
4.
5. public static void main(String args[]) {
6. MyThread z = new MyThread();
7. (new Thread(z)).start();
8. (new Thread(z)).start();
9. }
10.
11. public synchronized void run() {
12. for(int w = 0;w <>
13. System.out.println(holdA + holdB[w] + ".");
14. }
15. }
16. }

What is the result?


(1) Compilation fails because of an error on line 6
(2) Compilation fails because of an error on line 11
(3) Compilation fails because of errors on lines 7 and 8
(4) Compilation succeeds and the program prints each value in the holdB array at the end of the "This is " line. Each value is printed two times before the program ends, and the values are not printed in sequential order
(5) Compilation succeeds & the prog. prints each val in the holdB array at the end of the "This is " line. Each val is printed in order from 1-10 & after the val 10 prints, it starts printing the vals 1-10 in order again


Answer : 5
Explanation :
Option 5 is correct because the Runnable interface is implemented by declaring a synchronized run() method. The method is declared as synchronized to signify that the object lock must be obtained

Options 1, 2, and 3 are incorrect because compilation succeeds. Option 4 is incorrect, but would be correct if the run() method were not declared as synchronized.
----------------------------------------------------------------------------------------------------------------------------

Ques 2 :Which statement about the Map interface is true?


(1) Entries are placed in a Map using the values() method
(2) Entries are placed in a Map using the entrySet() method
(3) A key/value association is added to a Map using the put() method
(4) A key/value association is added to a Map using the putAll() method


Answer : 3
Explanation :
Option 3 is correct because the put() method is used to add a key/value association to a Map.

Option 1 is incorrect because the values() method returns a Collection of all values in a Map.Option 2 is incorrect because the entrySet() method returns a Set of all mappings in a Map. Option 4 is incorrect because the pubAll() method copies all mappings from one Map to another.
---------------------------------------------------------------------------------------------------------------------------

Ques 3 :
Consider the following class definition:
1. public class Test extends Base {
2. public Test(int j) {
3. }
4. public Test(int j, int k) {
5. super(j, k);
6. }
7. }

Which of the following forms of constructor must exist explicitly in the definition of the Base class?


(1) Base() { }
(2) Base(int j) { }
(3) Base(int j, int k) { }
(4) Base(int j, int k, int l) { }


Answer : 1,3
Explanation :
1 and 3 are correct. In the constructor at lines 2 and 3, there is no explicit call to either this() or super(), which means that the compiler will generate a call to the zero argument superclass constructor, as in 1. The explicit call to super() at line 5 requires that the Base class must have a 7.constructor as in 3. This has two consequences. First, 3 must be one of the required constructors and therefore one of the answers.Second, the Base class must have at least that constructor defined explicitly, so the default constructor is not generated, but must be added explicitly. Therefore the constructor of 1 is also required and must be a correct answer.At no point in the Test class is there a call to either a superclass constructor with one or three arguments, so 2 and 4 need not explicitly exist.

-----------------------------------------------------------------------------------------------------------------------------

Saturday, December 22, 2007

Mock Questions for Test Preparation

1) Will this work?

public static void main(String args[])
{
RuntimeException re;
throw re;
}

Ans -> No , compile time exception :: variable re may not have been initialized
-------------------------------------------------------------------------------------------------------------


2) NumberFormatException is a subclass of IOException

-------------------------------------------------------------------------------------------------------------

3) if ("string".toUpperCase() =="STRING")
{
System.out.println("Yes");
}
else System.out.println("No");

Ans -> No // if we use .equals() here , then we get “yes” . This is because , “string” and “STRING” are two separate object references pointing to two separate objects. == is used to compare whether two object references point to same object or not. .equals() compares the objects for their contents.
-------------------------------------------------------------------------------------------------------------------

3) when we extend a class , the static class variables are also passed on to the child class. Final class variables are not passed. Transient class variables are passes( they are implicitly initialized ).

--------------------------------------------------------------------------------------------------------------------

4) We cannot extend a math class ( its final) . all its constants and methods are static.

---------------------------------------------------------------------------------------------------------------------

5) We cannot instantiate a math class .. its constructors are private.

---------------------------------------------------------------------------------------------------------------------


5) class XTC {

public static void main ( String [ ] ka ) {
int s = 64 / 9 ;
float f = 64 / 9 ;
double d = 64 / 9 ;
System . out . println ( s + " & " + f + " & " + d ) ;
}
};

Ans -> 7 , 7.0 , 7.0

---------------------------------------------------------------------------------------------------------------------

6) What is the output of the following ? Will it compile at all ?

class XTC {

public static void main ( String [ ] ka ) {
Integer b = new Integer(3) ;
System . out . println ( b instanceof Integer ) ;

}
};

Ans -> true

-----------------------------------------------------------------------------------------------------------------------

7) What is the output of the following ? Will it compile at all ?

class XTC {

public static void main ( String [ ] ka ) {
Integer b = null;
System . out . println ( b instanceof Integer ) ;

}
};

Ans -> false

--------------------------------------------------------------------------------------------------------------------


8) What is the output of the following ? Will it compile at all ?

class XTC {

public static void main ( String [ ] ka ) {
Integer b = null;
System . out . println ( b instanceof Object ) ;

}
};

Ans -> false

------------------------------------------------------------------------------------------------------------

9) What is the output of the following ? Will it compile at all ?

class XTC {

public static void main ( String [ ] ka ) {
Integer b = new Integer(3);
System . out . println ( b instanceof Object ) ;

}
};

Ans -> true

------------------------------------------------------------------------------------------------------------------------


10) if (Double . POSITIVE_INFINITY == Double . POSITIVE_INFINITY )
{
System . out . println ( “Yes”) ;
}

Ans -> Yes

--------------------------------------------**********************---------------------------------------------

Search Amazon for Best Books on Java J2EE

Blogarama

blogarama - the blog directory

Search your favourite topics

Google