Q.1

What will be the output of the program?

public class SqrtExample 
{
    public static void main(String [] args) 
    {
        double value = -9.        System.out.println( Math.sqrt(value));
    }
}
  • 3.0
  • -3.0
  • NaN
  • Compilation fails.
Q.2

What will be the output of the program?

public class NFE 
{
    public static void main(String [] args) 
    {
    String s = "42";
        try 
        {
            s = s.concat(".5");  /* Line 8 */
            double d = Double.parseDouble(s);
            s = Double.toString(d);
            int x = (int) Math.ceil(Double.valueOf(s).doubleValue());
            System.out.println(x);
        }
        catch (NumberFormatException e) 
        {
            System.out.println("bad number");
        }
    }
}
  • 42
  • 42.5
  • 43
  • bad number
Q.3

What will be the output of the program?

public class ExamQuestion{  
    static int j; 
    static void methodA(int i)
    {
        boolean b; 
        do
        { 
            b = i<| methodB(4); /* Line 9 */
            b = i<|| methodB(8);  /* Line*/
        }while (!b); 
    } 
    static boolean methodB(int i)
    {
        j += i; 
        return true; 
    } 
    public static void main(String[] args)
    {
        methodA(0); 
        System.out.println( "j = " + j ); 
    } 
}
  • j = 0
  • j = 4
  • j = 8
  • The code will run with no output
Q.4

What will be the output of the program?

public class Test{ 
    public static void main(String[] args) 
    {
        String s = "foo"; 
        Object o = (Object)s; 
        if (s.equals(o)) 
        { 
            System.out.print("AAA"); 
        } 
        else 
        {
            System.out.print("BBB"); 
        } 
        if (o.equals(s)) 
        {
            System.out.print("CCC"); 
        } 
        else 
        {
            System.out.print("DDD"); 
        } 
    } 
}
  • AAACCC
  • AAADDD
  • BBBCCC
  • BBBDDD
Q.5

What will be the output of the program?

public class Test 
{ 
    public static void main(String[] args) 
    {
        final StringBuffer a = new StringBuffer(); 
        final StringBuffer b = new StringBuffer(); 

        new Thread() 
        { 
            public void run() 
            {
                System.out.print(a.append("A")); 
                synchronized(b) 
                { 
                    System.out.print(b.append("B")); 
                } 
            } 
        }.start(); 
            
        new Thread() 
        {
            public void run() 
            {
                System.out.print(b.append("C")); 
                synchronized(a) 
                {
                    System.out.print(a.append("D")); 
                } 
            } 
        }.start(); 
    } 
}
  • ACCBAD
  • ABBCAD
  • CDDACB
  • Indeterminate output
Q.6

What will be the output of the program?

public class Test{ 
    public static void stringReplace (String text) 
    {
        text = text.replace ('j' , 'c'); /* Line 5 */
    } 
    public static void bufferReplace (StringBuffer text) 
    { 
        text = text.append ("c");  /* Line 9 */
    } 
    public static void main (String args[]) 
    { 
        String textString = new String ("java"); 
        StringBuffer textBuffer = new StringBuffer ("java"); /* Line*/
        stringReplace(textString); 
        bufferReplace(textBuffer); 
        System.out.println (textString + textBuffer); 
    } 
}
  • java
  • javac
  • javajavac
  • Compile error
Q.7

What will be the output of the program?

String x = new String("xyz");
String y = "abc";
x = x + y;
How many String objects have been created?
  • 2
  • 3
  • 4
  • 5
Q.8

Which statement is true given the following?

Double d = Math.random();

  • 0.0 < d <= 1.0
  • 0.0 <= d < 1.0
  • Compilation fail
  • Cannot say.
Q.9

What will be the output of the program?

System.out.println(Math.sqrt(-4D));

  • -2
  • NaN
  • Compile Error
  • Runtime Exception
Q.10

public class Myfile 
{ 
    public static void main (String[] args) 
    {
        String biz = args[1]; 
        String baz = args[2]; 
        String rip = args[3]; 
        System.out.println("Arg is " + rip); 
    } 
}
Select how you would start the program to cause it to print: Arg is 2
  • java Myfile 222
  • java Myfile 1 2 2 3 4
  • java Myfile 1 3 2 2
  • java Myfile 0 1 2 3
Q.11

What will be the output of the program?

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*/
}
  • 9.0
  • bad number
  • Compilation fails on line 9.
  • Compilation fails on line 11.
Q.12

What will be the output of the program?

String x = "xyz";
x.toUpperCase(); /* Line 2 */
String y = x.replace('Y', 'y');
y = y + "abc";
System.out.println(y);
  • abcXyZ
  • abcxyz
  • xyzabc
  • XyZabc
Q.13

What will be the output of the program?

String s = "hello"; 
Object o = s; 
if( o.equals(s) )
{
    System.out.println("A"); 
} 
else
{
    System.out.println("B"); 
} 
if( s.equals(o) )
{
    System.out.println("C"); 
} 
else
{ 
    System.out.println("D"); 
}
  1. A
  2. B
  3. C
  4. D
  • 1 and 3
  • 2 and 4
  • 3 and 4
  • 1 and 2
Q.14

What will be the output of the program (in jdk1.6 or above)?

public class BoolTest 
{
    public static void main(String [] args) 
    {
        Boolean b1 = new Boolean("false");
        boolean b        b2 = b1.booleanValue();
        if (!b
        {
            b2 = true;
            System.out.print("x ");
        }
        if (b1 & b/* Line*/
        {
            System.out.print("y ");
        }
        System.out.println("z");
    }
}
  • z
  • x z
  • y z
  • Compilation fails.
Q.15

What is the value of "d" after this line of code has been executed?

double d = Math.round ( 2.5 + Math.random() );

  • 2
  • 3
  • 4
  • 2.5
Q.16

What will be the output of the program?

class Tree { } 
class Pine extends Tree { } 
class Oak extends Tree { } 
public class Forest{ 
    public static void main (String [] args)
    { 
        Tree tree = new Pine(); 
        if( tree instanceof Pine ) 
            System.out.println ("Pine"); 
        else if( tree instanceof Tree ) 
            System.out.println ("Tree"); 
        else if( tree instanceof Oak ) 
            System.out.println ( "Oak" ); 
        else 
            System.out.println ("Oops "); 
    } 
}
  • Pine
  • Tree
  • Forest
  • Oops
Q.17

What will be the output of the program?

public class WrapTest 
{
    public static void main(String [] args) 
    {
        int result =        short s =        Long x = new Long("42");
        Long y = new Long(42);
        Short z = new Short("42");
        Short x2 = new Short(s);
        Integer y2 = new Integer("42");
        Integer z2 = new Integer(42);

        if (x == y) /* Line*/
            result =        if (x.equals(y) ) /* Line*/
            result = result +        if (x.equals(z) ) /* Line*/
            result = result +        if (x.equals(x) /* Line*/
            result = result +        if (x.equals(z) /* Line*/
            result = result +
        System.out.println("result = " + result);
    }
}
  • result = 1
  • result = 10
  • result = 11
  • result = 11010
Q.18

Which two statements are true about wrapper or String classes?

  1. If x and y refer to instances of different wrapper classes, then the fragment x.equals(y) will cause a compiler failure.
  2. If x and y refer to instances of different wrapper classes, then x == y can sometimes be true.
  3. If x and y are String references and if x.equals(y) is true, then x == y is true.
  4. If x, y, and z refer to instances of wrapper classes and x.equals(y) is true, and y.equals(z) is true, then z.equals(x) will always be true.
  5. If x and y are String references and x == y is true, then y.equals(x) will be true.
  • 1 and 2
  • 2 and 3
  • 3 and 4
  • 4 and 5
Q.19

What will be the output of the program?

interface Foo{ 
    int k =/* Line 3 */
} 
public class Testimplements Foo{
    public static void main(String args[]) 
    {
        int i; 
        Testtest= new Test141(); 
        i = test141.k; /* Line*/
        i = Test141.k; 
        i = Foo141.k; 
    } 
}
  • Compilation fails.
  • Compiles and runs ok.
  • Compiles but throws an Exception at runtime.
  • Compiles but throws a RuntimeException at runtime.
Q.20

What will be the output of the program?

class Q{ 
    public static void main(String[] args) 
    {
        int i1 =
        int i2 =
        String s1 = "7"; 
        System.out.println(i1 + i2 + s1); /* Line 8 */
    } 
}
  • 18
  • 117
  • 567
  • Compiler error
0 h : 0 m : 1 s