wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

JavaDemo1

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

Which one of these lists contains only Java programming language keywords?

a)


class, if, void, long, Int, continue

b)


goto, instanceof, native, finally, default, throws

c)

try, virtual, throw, final, volatile, transient

d)

strictfp, constant, super, implements, do

2.

Which is a valid keyword in java?

a)

interface

b)

string

c)

Float

d)

unsigned

3.

Which is a valid declarations of a String?

a)

String s2 = 'null';

b)

String s1 = null;

c)

String s3 = (String) 'abc';

d)

String s4 = (String) '\ufeed';

4.

What is the output of the following code:
class Test {

    public static void main(String[] args) {

    for(int i = 0; 1; i++) {

             System.out.println("Hello");

            break;

        }

    }

}

a)

Prints Hello once

b)

Prints Hello infinite times

c)

Prints nothing

d)

Compiler error

5.

What is the output of the following code?
class Test {

    public static void main(String[] args) {

        for(int i = 0; 1; i++) {

            System.out.println("Hello");

            break;

        }

    }

}

a)

0

b)

20

c)

Compiler Error

d)

Prints Nothing

6.

What is the output of the following code?
class Main {

public static void main(String args[]) {

try {

throw 10;

}

catch(int e) {

System.out.println("Got the Exception " + e);

}

}

}

a)

Got the Exception 10

b)

Got the Exception 0

c)

Compiler error

d)

None of these

7.
  1. What will be the output of the following?
    class Main

    {public static void main (String[] args) {

    try

    {

    int x = 0;

    int y = 5 / x;

    }

    catch (Exception e)

    {

    System.out.println("Exception");

    }

    catch (ArithmeticException ae)

    {

    System.out.println(" Arithmetic Exception");

    }

    System.out.println("finished");

    }};

a)

finished

b)

Compilation error

c)

Exception

d)

Arithmetic exception

8.
  1. What is the output of the following code?
    class Main

    {public static void main (String[] args) {

    try

    {

    int x = 0;

    int y = 5 / x;

    }

    catch (ArithmeticException ae)

    {

    System.out.println(" Arithmetic Exception");

    }

    catch (Exception e)

    {

    System.out.println("Exception");

    }

    System.out.println("finished");

    }

    };

a)

Exception
finished

b)

Arithmetic Exception
finished

c)

Compiler error

d)

finished

9.

You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?

b)


java.util.Set

c)


java.util.List

d)


java.util.Collection

10.

What package is a part of the wrapper class which is imported by default into all Java programs?

a)
java.util
b)
java.lang
c)
java.io
d)

java.awt

11.

Which of these is not a method in the Collection interface?

a)

add()

b)

get()

c)

remove()

d)

size()

12.

In which collection are elements stored as key-value pairs?

a)

List

b)

Set

c)

Map

d)

Queue

13.

Which collection provides a guaranteed constant time for basic operations like add, remove, and contains?

a)
LinkedList
b)
HashSet
c)
TreeSet
d)
ArrayList
14.

What is the output of the following?
public class Main

{

public static void main(String [] args)

{

String s = "42";

try

{

s = s.concat(".5");

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");

}

}

}

a)

42

b)

43

c)

42.5

d)

bad number

15.

Which of the following is true about interfaces in java?
1.An interface can contain following type of members:public, static, final fields (i.e., constants) ,default and static methods with bodies
2.An instance of interface can be created.
3.A class can implement multiple interfaces.
4.Many classes can implement the same interface.

a)

1,3,4

b)

1,2,4

c)

2,3,4

d)

1,2,3,4