AmbitionBox

Discover Best Places to work in India

Java Programming

Java Programming Questions and Answers

Showing 11 - 20 of 150 questions

11

The width in bits of double primitive type in Java is --.

 

a

The width of double is platform dependent

b

128

c

64

d

8

correct answer b,d
12
public void test(int x) 
{ 
    int odd = 1; 
    if(odd) /* Line 4 */
    {
        System.out.println("odd"); 
    } 
    else 
    {
        System.out.println("even"); 
    } 
}
a

"even" will always be output.

b

"odd" will be output for odd values of x, and "even" for even values.

c

Compilation fails.

d

"odd" will always be output.

correct answer c,d

The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer

13
public class While 
{
    public void loop() 
    {
        int x= 0;
        while ( 1 ) /* Line 6 */
        {
            System.out.print("x plus one is " + (x + 1)); /* Line 8 */
        }
    }
}
a

There is a syntax error on line 1.

b

There are syntax errors on lines 1 and 6.

c

There are syntax errors on lines 1,6 and 8

d

There is a syntax error on line 6

correct answer d

Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java.

A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown.

14
switch(x) 
{ 
    default:  
        System.out.println("Hello"); 
}

Which two are acceptable types for x?
1.byte
2.long
3.char
4.float
5.short
6.long
a

3 and 5

b

4 and 6

c

1 and 3

d

2 and 4

correct answer c,d

Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to an integer, these can also be used. Also shorts can be used.Short and Long are wrapper classes and reference types can not be used as variables.

15
public void foo( boolean a, boolean b)
{ 
    if( a ) 
    {
        System.out.println("A"); /* Line 5 */
    } 
    else if(a && b) /* Line 7 */
    { 
        System.out.println( "A && B"); 
    } 
    else /* Line 11 */
    { 
        if ( !b ) 
        {
            System.out.println( "notB") ;
        } 
        else 
        {
            System.out.println( "ELSE" ) ; 
        } 
    } 
}
a

If a is true and b is true then the output is "A && B"

b

If a is true and b is false then the output is "notB"

c

If a is false and b is true then the output is "ELSE"

d

If a is false and b is false then the output is "ELSE"

correct answer c
16
public void foo( boolean a, boolean b)
{ 
    if( a ) 
    {
        System.out.println("A"); /* Line 5 */
    } 
    else if(a && b) /* Line 7 */
    { 
        System.out.println( "A && B"); 
    } 
    else /* Line 11 */
    { 
        if ( !b ) 
        {
            System.out.println( "notB") ;
        } 
        else 
        {
            System.out.println( "ELSE" ) ; 
        } 
    } 
}
a

If a is true and b is true then the output is "A && B"

b

If a is true and b is false then the output is "notB"

c

If a is false and b is true then the output is "ELSE"

d

If a is false and b is false then the output is "ELSE"

correct answer c

Option C is correct. The output is "ELSE". Only when a is false do the output lines after 11 get some chance of executing.

Option A is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. The condition at line 7 will never be evaluated (when a is true it will always be trapped by the line 12 condition) therefore the output will never be "A && B".

Option B is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed.

Option D is wrong. The output is "notB".

17
publicabstractclassAbstractTest 
{publicint getNum() 
    {
        return45;
    }
    publicabstractclassBar 
    {publicint getNum() 
        {
            return38;
        }
    }
    publicstaticvoid main (String [] args) 
    {
        AbstractTest t = new AbstractTest() 
        {
            publicint getNum() 
            {
                return22;
            }
        };
        AbstractTest.Bar f = t.new Bar() 
        {
            publicint getNum() 
            {
                return57;
            }
        };
        
        System.out.println(f.getNum() + " " + t.getNum());
    }
}

what wll be the output?
a

57 22

b

45 38

c

45 57

d

An exception occurs at runtime.

correct answer a
18
publicclassHorseTest 
{publicstaticvoid main (String [] args) 
    {
        classHorse 
        {public String name; /* Line 7 */public Horse(String s) 
            {
                name = s;
            }
        } /* class Horse ends */
        
        Object obj = new Horse("Zippo"); /* Line 13 */
        Horse h = (Horse) obj; /* Line 14 */
        System.out.println(h.name);
    }
} /* class HorseTest ends */

what will be the output?
a

An exception occurs at runtime at line 10

b

It prints "Zippo"

c

Compilation fails because of an error on line 7.

d

Compilation fails because of an error on line 13.

correct answer b
19
Which constructs an anonymous inner class instance?
a

Runnable r = new Runnable() { };

b

Runnable r = new Runnable(public void run() { });

c

Runnable r = new Runnable { public void run(){}};

d

System.out.println(new Runnable() {public void run() { }});

correct answer d
20

Which is true about a method-local inner class?

a

It must be marked final.

b

It must be marked abstract.

c

It must be marked public.

d

It must be marked static.

correct answer b
Select a company to compare with

vs

Similar Companies