wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

JAVA QUIZ

Total questions: 20

Worksheet time: 10mins

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 reserved word in the Java programming language?

a)


method

b)

native

c)

subclasses

d)


reference

3.

Which is a valid keyword in java?

a)

interface

b)


string

c)


Float

d)

unsigned

4.


Which three are legal array declarations?

  1. 1.int [] myScores [];

  2. 2.char [] myChars;

  3. 3.int [6] myScores;

  4. 4.Dog myDogs [];

  5. 5.Dog myDogs [7];


a)

1, 2, 4

b)

2, 4, 5

c)


2, 3, 4

d)

All are correct.

5.

Which is the valid declarations within an interface definition?

a)


public double methoda();

b)

public final double methoda();

c)

static void methoda(double d1);

d)


protected void methoda(double d1);

6.

public class Foo

{

public static void main(String[] args)

{

try

{

return;

}

finally

{

System.out.println( "Finally" );

}

}

}

a)

Finally

b)

Compilation fails.

c)

The code runs with no output.

d)


An exception is thrown at runtime.

7.

What will be the output of the program?

class Equals

{

public static void main(String [] args)

{

int x = 100;

double y = 100.1;

boolean b = (x = y); /* Line 7 */

System.out.println(b);

}

}

a)

true

b)

false

c)


Compilation fails

d)


An exception is thrown at runtime

8.

What will be the output of the program?

class Bitwise

{

public static void main(String [] args)

{

int x = 11 & 9;

int y = x ^ 3;

System.out.println( y | 12 );

}

}

a)

0

b)

7

c)

8

d)

14

9.

What will be the output of the program?

class SSBool

{

public static void main(String [] args)

{

boolean b1 = true;

boolean b2 = false;

boolean b3 = true;

if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 /

System.out.print("ok ");

if ( b1 & b2 | b2 & b3 | b2 | b1 ) /Line 10*/

System.out.println("dokey");

}

}


a)


ok

b)

dokey

c)

ok dokey

d)

No output is produced

10.

What allows the programmer to destroy an object x?

a)


x.delete()

b)

x.finalize()

c)


Runtime.getRuntime().gc()

d)

Only the garbage collection system can destroy an object.

11.

You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?

a)

public

b)


private

c)

protected

d)


transient

12.


Which three form part of correct array declarations?

  1. 1.public int a [ ]

  2. 2.static int [ ] a

  3. 3.public [ ] int a

  4. 4.private int a [3]

  5. 5.private int [3] a [ ]

  6. 6.public final int [ ] a

a)

1, 3, 4

b)


2, 4, 5

c)


1, 2, 6

d)


2, 5, 6

13.


Which three are valid method signatures in an interface?

  1. 1.private int getArea();

  2. 2.public float getVol(float x);

  3. 3.public void main(String [] args);

  4. 4.public static void main(String [] args);

  5. 5.boolean setFlag(Boolean [] test);

a)

1 and 2

b)

2, 3 and 5

c)

3, 4, and 5

d)

2 and 4

14.

Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?

a)

TreeMap

b)

HashMap

c)

LinkedHashMap

d)

The answer depends on the implementation of the existing instance.

15.

Which interface does java.util.Hashtable implement?

b)

Java.util.List

c)

Java.util.HashTable

d)

Java.util.Collection

16.

What will be the output of the program?

public class RTExcept

{

public static void throwit ()

{

System.out.print("throwit ");

throw new RuntimeException();

}

public static void main(String [] args)

{

try

{

System.out.print("hello ");

throwit();

}

catch (Exception re )

{

System.out.print("caught ");

}

finally

{

System.out.print("finally ");

}

System.out.println("after ");

}

}


a)

hello throwit caught


b)

Compilation fails

c)

hello throwitRuntimeException caught after

d)

hello throwit caught finally after

17.


Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?

a)


java.util.HashSet

b)

java.util.LinkedHashSet

c)

java.util.List

d)

java.util.ArrayList

18.

/* Missing Statement ? */

public class foo

{

public static void main(String[]args)throws Exception

{

java.io.PrintWriter out = new java.io.PrintWriter();

new java.io.OutputStreamWriter(System.out,true); out.println("Hello");

}

}

What line of code should replace the missing statement to make this program compile?

a)


No statement required.

b)

import java.io.*;

c)

include java.io.*;

d)

import java.io.PrintWriter;

19.

What is the numerical range of char?

a)

0 to 32767

b)

0 to 65535

c)

-256 to 255

d)

-32768 to 32767

20.


Which of the following are Java reserved words?

  1. 1.run

  2. 2.import

  3. 3.default

  4. 4.implement

a)

1 and 2

b)

2 and 3

c)

3 and 4

d)

2 and 4