wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Programming Quiz

Total questions: 55

Worksheet time: 29mins

Name
Class
Date
1.

Which of these can an abstract class contain?

a)

Only concrete methods

b)

Static,private

c)

Constructors

d)

Final

e)

all

2.

What is the purpose of the TreeSet class in Java?

a)

It allows duplicate elements?

b)

It does not allow duplicate elements and maintains a sorted order.

c)

It allows duplicate elements and maintains insertion order

d)

It does not allow duplicate elements and maintains insertion order

3.

Can you declare variables inside an interface?

a)

No, interfaces can only have methods

b)

Yes, but they must be private

c)

Yes, and they are implicitly public and static

d)

Only if they are final and static

4.

Suppose the class Cat extends the class Animal. Which of the following are legal?

a)

Animal x = new Animal();

b)

Animal x = new Animal();

c)

x = new Cat();

d)

x = new Animal();

x = y

e)

y = x

5.

Which of the following collections is thread-safe?

a)

ArrayList

b)

Vector

c)

Мар

d)

LinkedList

e)

TreeSet

6.

What does JDK stand for?

a)

Collection of Java programming language's syntax and semantics

b)

The software for compiling and running Java programs

c)

The Java application program interface

d)

The software for developing Java programs

e)

The program for running Java programs

7.

Which requirements are true to create immutable class?

a)

Class must have only immutable fields

b)

No accessor methods can return a reference to a data field that is mutable

c)

All data fields must be private

d)

There should be User methods for data fields

e)

All answers are correct

8.

Select objects that are stored in stack memory.

a)

heap memory

b)

arraylist

c)

array

d)

int, char, boolean, floa

9.

Which type of method of superclass we cannot override in subclasses?

a)

final

b)

private

c)

Static

d)

Abstract

e)

All

10.

What is the output of the following Java program?

public class Vehicle {

public void move() {

System.out.println("The vehicle moves");

}

}

public class Car extends Vehicle {

public void move() {

System.out.println("The car moves");

}

}

public class Main {

public static void main(String[] args) {

Vehicle vehicle = new Car();

vehicle.move();

}

}

a)

The vehicle moves

b)

The car moves

c)

Vehicle vehicle = new Car()

11.

What is encapsulation?

a)

Sharing field value by all objects of this class

b)

The definition of behavior of the object

c)

Hiding internal data from the outside world

d)

A collection of methods with no implementation

e)

Having several method with the same name and different arguments

12.

Which of the following classes can't be subclassed?

a)

public class Test (.)

b)

public static class Test (,)

c)

public interface Test (.)

d)

public final class Test (.)

e)

public abstract class Test (.)

13.

Which statement is true regarding polymorphism?

a)

An overridden method can be less restricDve than the superclass method.

b)

An overridden method can be more restricDve than the superclass method

c)

All answers

d)

Overtoaded methods cannot change the return typ

e)

Constructors can be overridden

14.

Which of the following best describes the volatile keyword in Java?

a)

It ensures changes to a variable are visible to all threads.

b)

It marks a variable for garbage collection.

c)

It guarantees atomicity for read and write operations.

d)

It allows a variable to be accessed by only one thread at a time.

15.

What will be the output?

public static void main(String[] args) {

List intList = new ArrayList <> ();

intList.add(10);

intList.add(20);

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

}

a)

The list is: [10, 20]

b)

The list is: [20, 20]

c)

The list is: [20, 10]

d)

complition error

16.

List methods to copy one array to another, so that they will point to the separate memory addresses

a)

Use equating (=)

b)

Use the static arraycopy method in the System class.

c)

All answers

d)

Use a loop to copy individual elements one by one.

e)

Use the clone method to copy arrays;

17.

class Animal {

String name = "animal";

String message() {

return "from animal";

}

}

class Child extends Animal {

String name = "child";

@Override

String message() {

return "from child";

}

}

public class Main {

public static void main(String[] args) {

Animal p = new Child();

System.out.println(p.name); // Accessing the 'name' field

}

}

a)

animal

b)

child

c)

child from child

d)

animal from aniimal

18.

Choose collection, which ensures that elements are processed in first-in-first-out (FIFO) order?

a)

PriorityQueue

b)

LinkedList

c)

TreeSet

d)

HashSet

19.

Which keyword is used to prevent a class from being subclassed in Java?

a)

Package

b)

Final

c)

abstract

d)

static

e)

private

20.

What is the primary purpose of using the ArrayList class in Java over regular arrays?

a)

ArrayList can store only unique elements.

b)

ArrayList consumes less memory than arrays.

c)

ArrayList allows you to store primitive types directly.

d)

ArrayList automatically adjusts its size during runtime.

21.

Which of the following are checked exceptions?

a)

IOException

b)

NullPointerException

c)

IndexOutOfBoundsException

22.

Suppose, we have class Person, with field namo and constructor with arguments for name, what will be the output of the following code: public static void main(Stringl args) ( Person t = new Person("A"): changePerson(t); System.out.printin(t.nam; private static void changePerson(Person t) ( t.name = "B";

a)

The program will throw an exception

b)

Null

c)

B

d)

A

23.

Which requirements are true to create immutable class?

a)

All answers are correct

b)

final

c)

private

d)

static

24.

Choose statements where the literal is presented

a)

Object o = new Object:

b)

String s;

c)

int count = 0:

d)

boolean b = true;

e)

void printo;

25.

True or false?

a. An interface is compiled into a separate bytecode file.

b. An interface can have static methods.

c. An interface can extend one or more interfaces.

d. An interface can extend an abstract class.

e. An interface can have default methods.

a)

a. True

b. False

c. True

d. True

e. True in Java 8

b)

a. True

b. True

c. True

d. False

e. True in Java 8

c)

a. False

b. False

c. True

d. True

e. True in Java 8

d)

a. True

b. True

c. False

d. False

e. Falsein Java 8

26.

What is a thread in Java?

a)

A lightweight process that executes a sequence of instructions.

b)

A data structure to store multiple values.

c)

A Java keyword to define a loop construct.

d)

A memory allocation unit in Java

27.

Which class provides methods to create a client-side socket in Java?

a)

Socket

b)

ServerSocket

c)

ClientSocket

d)

Servlet

28.

What will be the output?

class MyThread extends Thread {

public MyThread(String name) {

this.setName(name);

start();

System.out.println("in constructor " + getName());

}

@Override

public void start() {

System.out.println("in start " + getName());

}

public void run() {

System.out.println("in run " + getName());

}

}

public class ThreadTest {

public static void main(String[] args) {

MyThread th = new MyThread("oops");

}

}

a)

in start oops

b)

in constructor oops

c)

in run oops

d)

in constructor oops in start oops in run oops

e)

in start oops

in constructor oops

29.

Show the output of the following code:

int x = 6;

int y = a++;

System.out.println(x);

System.out.println(y);

x = 6;

y = ++a;

System.out.println(x);

System.out.println(y);

a)

7

6

7

7

b)

6

7

6

7

c)

6

7

5

7

d)

6

7

7

7

30.

Convert for loop to while loopint

sum = 0;

for (int i = 0; i < 4; i++) {

if (i % 3 == 0) continue;

sum += i;

}

a)

int sum = 0;

int i = 0;

while (i < 4) {

if (i % 3 == 0) continue;

sum += i;

i++;

}

b)

int sum = 0;

int i = 0;

while (i < 4) {

if (i % 3 == 0) {

i++;

continue;

}

sum += i;

i++;

}

c)

int sum = 0;

int i = 0;

while (i < 4) {

if (i % 2 == 0) continue;

sum += i;

i++;

}

31.

What is polymorphism?

a)

The ability of objects of different classes to respond to the same method in a way that is specific to their individual class

b)

Restricting access to the declaring classes

c)

Promoting code reusability and establishing an 'is-a' relationship between classes

d)

Hiding the internal implementation details and restricts access to data through defined interfaces

e)

A special method used to initialize objects

32.

Which of the following is a correct interface? Explain each of them why it is correct or not

(a)

interface A {

void print() { }

}

(b)

abstract interface A {

abstract void print() { }

}

(c)

abstract interface A {

print();

}

(d)

interface A {

void print();

}

(e)

interface A {

default void print() {

}

}

(f)

interface A {

static int get() {

return 0;

}

}

a)

a,b,c

b)

a,b,c

c)

a,e,b

d)

c,f,d

e)

a,c,e

33.

What is the purpose of the join method in Java threads?

a)

To wait for a thread to finish its execution.

b)

To notify a thread to stop execution.

c)

To schedule the execution of a thread.

d)

To interrupt the execution of a thread.

34.

Can a class extend multiple classes? (Y/N)

Can a class implement multiple interfaces? (Y/N)

Can an interface extend multiple interfaces? (Y/N)

a)

N

Y

Y

b)

N

Y

N

c)

Y

Y

Y

d)

Y

Y

N

35.

True or falso or true in java 8

a. An interface is compiled into a separate bytecode file.

b. An interface can have static methods.

c. An interface can extend one or more interfaces.

d. An interface can extend an abstract class.

e. An interface can have default methods.

a)

a. True

b. True in Java 8

c. True

d. False

True in Java 8

b)

хз

36.

String s1 = "Welcome to Java";

String s2 = new String("Welcome to Java");

String s3 = "Welcome to Java";

String s4 = new String("Welcome to Java");

a)

s1 == s3 -> will true

b)

s3 == s4-> will true:

c)

s1 == s2-> will true:

d)

s1 == s3 -> will flase

e)

s2 == s4-> will true:

37.

Which of the following best describes the volatile keyword in Java?

a)

It ensures changes to a variable are visible to all threads.

b)

It guarantees atomicity for read and write operations.

c)

It marks a variable for garbage collection.

d)

It allows a variable to be accessed by only one thread at a time.

38.

public static void main(String[] args) {

int i = 1;

do {

int num = 1;

for (int j = 1; j <= i; j++) {

System.out.print(num + "A");

num += 2;

}

System.out.println();

i++;

} while (i <= 3);

}

a)

1A

1A3A

1A3A5A

b)

1A

1A2A

1A2A3A

c)

1A

1A3

1A2A5A

d)

1A

1A2A

1A3A5A

39.

Which statement is true regarding polymorphism?

a)

Polymorphism allows objects of different classes to be treated as objects of a common superclass.

b)

Polymorphism allows a method to have multiple implementations within the same class.

c)

Polymorphism is achieved only through method overriding.

d)

Polymorphism allows an object to change its type during runtime.

40.

In polymorphism, a reference variable of the superclass can refer to the object of which classes?

a)

Only the superclass

b)

The superclass or any of its subclasses

c)

Only the subclass

d)

Any class

41.

In which collecDon you cannot have duplicate elements?

a)

Set

b)

Array

c)

Map

42.

What is purpose of the join() method in java threads?

a)

To notify a thread to stop execution

b)

To interrupt the execution of a thread

c)

To schedule the execution of a thread

d)

To wait for a thread to finish its execution

43.

Can we override a static method?

a)

It depends on the access modifier of the static method.

b)

No, static methods cannot be overridden because they belong to the class, not the object.

c)

It depends on whether the static method is declared as final or not.

d)

Yes, we can override a static method just like we do with instance methods.

e)

It depends on the modifier (static or abstract) of class.

44.

Which of these can an abstract class contain?

a)

Both abstract and concrete methods

b)

Only concrete methods

c)

Only abstract methods

d)

None of the above

45.

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

a)

ArrayList

b)

HashSet

c)

TreeSet

d)

LinkedList

46.

Show the output of the following code:

float f = 12.5F;

int i = (int)f;

System.out.println("f is " + f);

System.out.println("i is " + i);

a)

program will throw type cast exception

b)

12.5

12

c)

12.5f

12.5

d)

12.5f

12.5f

47.

Suppose you need to write a program that stores nonduplicate elements in increasing order of the element values, what data structure should you use?

a)

Arraylist

b)

Linkedhashset

c)

Treeset

d)

hashset

e)

array

48.

Фото какое то

Static method in parent

Static method in parent

Про это крч

a)

It will print "Static method in Child".

b)

It will print "Static method in Parent".

49.

Show the output of the following code:

ArrayList list = new Arraylist<>);

list-add ("New York");

ArrayList listl = list;

ArrayList list2 = (ArrayList) (list.clone());

list.add("Atlanta");

System.out. printin(list = listi);

System.out-printin(list == list2);

System.out-printin("list is " + list);

System.out println("listl is " + listl);

System. out printin("list2.get(0) is " + list2-get (0));

System.out-println("list2.size() is " + list2.size());

true

false

a)

list is [New York, Atlanta]

list1 is [New York, Atlanta]

list2.get(0) is New York

list2.size() is 1

b)

хз

c)

хз

d)

хз

e)

хз

50.

Which interface in Java Collections Framework does not extend the Collection interface?

a)

Queue

b)

List

c)

Map

d)

Deque

e)

Set

51.

Which interface must a Java class implement to create a servlet?

a)

HttpServlet

b)

ServletRequest

c)

ServletConfig

d)

ServerSocket

e)

Servlet

52.

public class Main {

public static void main(String[] args) {

int[] numbers = {1, 2, 3, 4, 5}; // Use curly braces {} for array initialization

int sum = 0; // Correct the initialization of sum (no 03 in Java)

for (int i = 0; i < numbers.length; i++) { // Replace ‹ with <, and ( with {

if (numbers[i] % 2 == 0) {

continue; // Skip even numbers

}

sum += numbers[i]; // Add odd numbers to sum

}

System.out.println("Sum: " + sum);

}

}

a)

Sum: 6

b)

Sum: 10

c)

Sum: 9

d)

Sum: 15

53.

Which command is used to compile HelloWorld.java file?

a)

java HelloWorld.java

b)

javac HelloWorld.java

c)

run java HelloWorld.java

54.

Given following code. What will be the output?

public static void main(String[] args) {

Set setTest = Collections.singleton("Test");

setTest.add("Test2");

System.out.println(setTest);

}

a)

UnsupportedOperationException

b)

хз

c)

хз

d)

хз

55.

What is the JVM used for?

a)

All answers

b)

To develop java program

c)

To interpret java bytecode

d)

To compile java file to class file

e)

To compile and run java program