WorksheetsJava Quiz on Module 1 & 2
Total questions: 21
Worksheet time: 14mins
Can we create an instance of Enum outside of Enum itself?
True
False
Enum is not a class type
None of the above
What will be the output of the following Java code?
enum Season
{
WINTER, SPRING, SUMMER, FALL
};
System.out.println(Season.WINTER.ordinal());
0
1
2
3
What will be the output of the following Java code snippet?
class A
{
}
enum Enums extends A
{
ABC, BCD, CDE, DEF;
}
Runtime Error
Compilation Error
It runs successfully
EnumNotDefined Exception
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?
Compilation error "enum constructor cannot be private
Compilation error" Cannot have variables in enum"
Code will compile sucessfully
Compilation error "enum object should be created with new keyword"
Choose a correct statement about Autoboxing in Java?
Boxing is the process of converting a primitive data to its equivalent Wrapper class type
Unboxing is the process of converting a Wrapper class object to its equivalent primitive data type
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.
All the above
Which does autoboxing or unboxing in Java?
Compiler
Runtime
Third party tools
None
Choose the right statement which does autoboxing in Java?
Integer temperature1 = 100;
int temperature2 = 101;
Integer temperature1 = 100; int temperature2 = 101;
All the above
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); }
}
Reading: 0
Reading: 10
Compiler error
None
Which version of Java introduced annotation?
Java 5
Java 6
Java 7
Java 8
Which of the following is not pre-defined annotation in Java?
@Deprecated
@Overriden
@SafeVarags
@FunctionInterface
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()); }}
Prints hello annotation Prints 10
Prints hello annotation
Prints 10
Compilation Error or Runtime Error
(a) annotation contain no members and data
(a) is an API which is used to examine or modify the behavior of methods, classes, interfaces at runtime.
A (a) is generated when one object is incompatible with another
Which of these packages contain all the collection classes?
java.lang
java.util
java.net
java.awt
Which of these classes is not part of Java’s collection framework?
Array
Stack
Queue
Maps
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]);
}
}
12885
12845
58881
54881
Which implementation of Iterator can traverse a collection back and forth?
Iterator
ListIterator
SetIterator
MapIterator
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);
}
It prints the following: The list is: [10, 20]
It prints the following: The list is: [20, 10]
It results in a compiler error
It results in a runtime exception
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); }
}
[1, 2, 3]
[1, 2]
[2, 1]
An exception occurs at runtime
A (a) is generated when one object is incompatible with another
