wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Quiz on Module 1 & 2

Total questions: 21

Worksheet time: 14mins

Name
Class
Date
1.

Can we create an instance of Enum outside of Enum itself?

a)

True

b)

False

c)

Enum is not a class type

d)

None of the above

2.

What will be the output of the following Java code?

enum Season

{

WINTER, SPRING, SUMMER, FALL

};

System.out.println(Season.WINTER.ordinal());

a)

0

b)

1

c)

2

d)

3

3.

What will be the output of the following Java code snippet?

class A

{

 

}

 

enum Enums extends A

{

ABC, BCD, CDE, DEF;

}

a)

Runtime Error

b)

Compilation Error

c)

It runs successfully

d)

EnumNotDefined Exception

4.

Consider the following code inside TestEnum.java file

enum Branches {

Delhi(2),Chennai(3),Mumbai(3),Bangalore(5);

private int noofOffice;

private Branches(int noofOffice){

this.noofOffice=noofOffice;

}

public int getNoofOffice(){

return noofOffice;

}

}

class TestEnum{

public static void main(String[] args) {

System.out.println(Branches.Bangalore.getNoofOffice());

}

}

What will happen after compilation of the code?

a)

Compilation error "enum constructor cannot be private

b)

Compilation error" Cannot have variables in enum"

c)

Code will compile sucessfully

d)

Compilation error "enum object should be created with new keyword"

5.

Choose a correct statement about Autoboxing in Java?

a)

Boxing is the process of converting a primitive data to its equivalent Wrapper class type

b)

Unboxing is the process of converting a Wrapper class object to its equivalent primitive data type

c)

Autoboxing is the process of converting Primitive data type to Wrapper Object type automatically. Auto-Unboxing is the process of converting Wrapper type object to Primitive type automatically.

d)

All the above

6.

Which does autoboxing or unboxing in Java?

a)

Compiler

b)

Runtime

c)

Third party tools

d)

None

7.

Choose the right statement which does autoboxing in Java?

a)

Integer temperature1 = 100;

b)

int temperature2 = 101;

c)

Integer temperature1 = 100; int temperature2 = 101;

d)

All the above

8.

What is the output of the below java code with autoboxing?

public class AutoBoxingTest2 {

static void show(int reading)

{

System.out.println("Reading: " + reading);

}

public static void main(String[] args) {

Integer a = Integer.valueOf(10);

show(a); }

}

a)

Reading: 0

b)

Reading: 10

c)

Compiler error

d)

None

9.

Which version of Java introduced annotation?

a)

Java 5

b)

Java 6

c)

Java 7

d)

Java 8

10.

Which of the following is not pre-defined annotation in Java?

a)

@Deprecated

b)

@Overriden

c)

@SafeVarags

d)

@FunctionInterface

11.

What will be the output of the following program?

import java.lang.annotation.*;

import java.lang.reflect.*;

@Retention(RetentionPolicy.RUNTIME)

@interface MyAnnotation {    

int value();

}

class Hello {    

@MyAnnotation(value = 10)    

public void goodEveng() {        

System.out.println("hello annotation");    

}}

class HelloAnnotation {    

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

 Hello h = new Hello();        

Method m = h.getClass().getMethod("goodEveng");        

MyAnnotation myAnn = m.getAnnotation(MyAnnotation.class);       

 System.out.println("Prints " + myAnn.value());    }}

a)

Prints hello annotation Prints 10

b)

Prints hello annotation

c)

Prints 10

d)

Compilation Error or Runtime Error

12.

(a)   annotation contain no members and data

13.

(a)   is an API which is used to examine or modify the behavior of methods, classes, interfaces at runtime.

14.

A (a)   is generated when one object is incompatible with another

15.

Which of these packages contain all the collection classes?

a)

java.lang

b)

java.util

c)

java.net

d)

java.awt

16.

Which of these classes is not part of Java’s collection framework?

a)

Array

b)

Stack

c)

Queue

d)

Maps

17.

What will be the output of the following Java program?

import java.util.*;

class Array

{

public static void main(String args[])

{

int array[] = new int [5];

for (int i = 5; i > 0; i--)

array[5-i] = i;

Arrays.fill(array, 1, 4, 8);

for (int i = 0; i < 5 ; i++)

System.out.print(array[i]);

}

}

a)

12885

b)

12845

c)

58881

d)

54881

18.

Which implementation of Iterator can traverse a collection back and forth?

a)

Iterator

b)

ListIterator

c)

SetIterator

d)

MapIterator

19.

Choose the correct option based on this program:

import java.util.*;

class UtilitiesTest {

public static void main(String[] args) {

List < int > intList = new ArrayList < > ();

intList.add(10);

intList.add(20);

System.out.println("The list is: " + intList);

}

a)

It prints the following: The list is: [10, 20] 

b)

It prints the following: The list is: [20, 10] 

c)

It results in a compiler error 

d)

It results in a runtime exception

20.

What is the output of the following program?

public class Question_7_1 {

public static void main(String[] args) {

ArrayDeque<Integer> deque = new ArrayDeque<Integer>();

deque.push(1);

deque.push(2);

deque.push(3);

deque.poll();

System.out.println(deque); }

}

a)

[1, 2, 3]

b)

[1, 2]

c)

[2, 1]

d)

An exception occurs at runtime

21.

A (a)   is generated when one object is incompatible with another