wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

JAVA FINAL QUIZ FOR ASSESSMENT

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

Predict the output of the following code.

abstract class A

{

public void disp()

{

System.out.println("Concrete method of parent class");

}

abstract public void disp2();

}

class B extends A

{

public void disp2()

{

System.out.println("Overriding abstract method");

}

public static void main(String args[])

{

B obj = new B();

obj.disp();

}

}

a)

Overriding abstract method

b)

Concrete method of parent class

c)

Runtime error: non abstract method of parent cannot be invoked using child object

d)

Compilation error: abstract class cannot have non abstract methods

2.

Which of the following class creation prevents creating a child class of the Example class?

 

a)

class Example {}

b)

abstract public class Example {}

c)

public final class Example {}

3.

Which of the following is/are true for classes and interfaces?

a)

"Account extends SavingsAccount" is correct if and only if "Account is a class" and "SavingsAccount is an interface"

b)

"Account extends SavingsAccount" is correct if and only if "Account is an interface" and "SavingsAccount is a class"

c)

"SavingsAccount implements Account" is correct if and only if "Account is an interface" and "SavingsAccount is a class"

4.

Which of the following is not part of the Collection interface?

a)

Deque

b)

List

c)

HashMap

d)

LinkedList

5.

Ana is having a requirement, where she needs a method that processes the input from a file. The method has to read all the phone numbers from the file and store it in a collection, by eliminating the duplicates. Which collection suits to her requirement?

a)

LIST

b)

SET

c)

STACK

d)

HASHMAP

6.

Consider the code snippet given below. How many threads are active at line numbers 1, 2, and 3 respectively?

public class MyThreadDemo implements Runnable

{

public void run()

{

System.out.println("Thread is running");

}

public static void main(String[] args)

{

MyThreadDemo obj = new MyThreadDemo();

Thread threadObj = new Thread(obj); // 1

threadObj.start(); // 2

threadObj.run();// 3

}

}

 

a)

Code will provide a run time error

b)

Code will provide an error on compilation

c)

main thread,2, 3 respectively

d)

main thread,2 respectively

7.

What will be expected when the following snippet executes?

public class TestOne

{

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

{

Thread.sleep(3000);

System.out.println("sleep");

}

}

a)

Compilation fails

b)

An exception is thrown at runtime

c)

The code executes normally and prints sleep

d)

The code executes normally, but nothing is printed

8.

Consider the following code snippet:

import java.util.List;

import java.util.ArrayList;

public class ListTester

{

public static void main(String[] args)

{

List<String> testList = new ArrayList<>();

testList.add(new String("hello"));

testList.add(new Integer(12));

}

}

 

a)

Compilation Error check

b)

Program will get executed successfully

c)

Runtime Exception

d)

None of the above

9.

import java.util.List;

import java.util.ArrayList;

public class ListTester

{

public static void main(String[] args)

{

List<String> nameList = new ArrayList<>();

nameList.add(1,"One");

nameList.add(2,"Two");

for(String no:nameList)

{

System.out.println(no);

}

}

}

 

a)

Compilation Error

b)

One Two

c)

Runtime Exception

d)

None of the above

10.

What is the output of this code?

class MyGen<T>

{

private T obj;

public void add(T obj)

{

this.obj = obj;

}

public T get()

{

return obj;

}

}

public class TestGenerics

{

public static void main(String args[])

{

MyGen<Integer> m = new MyGen<Integer>();

m.add(2); // line 7

m.add("5"); //line 8

System.out.println(m.get());

}

}

 

a)

2

b)

5

c)

Compile time error at line 8

d)

Compile time error at line 7

11.

In which of the following package Exception class exist?

a)

java.util

b)

java.file

c)

java.io

d)

java.lang

12.

In Java, when we implement an interface method, it must be declared as:

a)

Private

b)

Protected

c)

Public

d)

Friend

13.

Which of these is not a Thread state?

a)

New

b)

Runnable

c)

sleep

d)

terminated

14.

A thread can acquire a lock by using which reserved keyword?

a)

volatile

b)

synchronized

c)

locked

d)

none

15.

What is difference between starting thread with run() and start() method?

a)

There is no difference

b)

When you call start() method, main thread internally calls run() method to start newly created Thread

c)

run() calls start() method internally

d)

None