wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

OPP_DA_midTerm v2

Total questions: 50

Worksheet time: 27mins

Name
Class
Date
1.

Which of the following statements is a valid declaration of an object that belongs to 'MyClass'?

a)

new MyClass obj;

b)

obj = new MyClass();

c)

MyClass obj = new MyClass;

d)

MyClass obj = new MyClass();

2.

<class name><Object name>=new <class name>();

This is syntax of creating ______.

a)

class

b)

object

c)

method

d)

function

3.

The insertion order is not retained in a Set because:

a)

Sets are unordered collections.

b)

Sets automatically sort elements.

c)

Sets use a hash table for storage.

d)

Sets do not allow duplicate elements.

4.

Create an ArrayList with a size of 100. Use the code snippet provided as a reference.

a)

ArrayList list = new ArrayList<>(100);

b)

ArrayList list = new ArrayList<>();

c)

ArrayList list = new ArrayList<>(10);

d)

ArrayList list = new ArrayList<>(1000);

5.

List listNames = new ArrayList();

listNames.add("Tom");

listNames.add("Mary");

listNames.add("Peter");


String name2 = (String) listNames.get(1);

System.out.println(name2);

What is the output of the above code

a)

Tom

b)

Mary

c)

Peter

d)

raises Exception

6.

A list is a sequential collection in which each element has a(n) ________.

a)

integer index.

b)

unique identifier.

c)

random value.

d)

fixed size.

7.

The purpose of the equals(Object o) method in Java is to:

a)

compare two objects for equality

b)

compare two objects for memory address

c)

convert an object to a string

d)

initialize an object

8.

The isEmpty() method in Java:

a)

Checks if a string is empty or not

b)

Removes all elements from a collection

c)

Returns the size of a collection

d)

Checks if a file is empty

9.

What will be the output of the following code snippet? import java.util.List; import java.util.ArrayList; public class GFG { public static void main(String[] args) { List al = new ArrayList<>(); al.add(10); al.add(20); al.add(30); al.add(1); al.add(2); al.remove(1); al.remove(1); System.out.println(al); } }

a)

[10, 20, 30, 1, 2]

b)

[10, 30, 1, 2]

c)

[10, 1, 2]

d)

[10, 2]

10.

Which of the following is the correct way to add an element to an ArrayList in Java?

a)

Array_List.add(1);

b)

Array_List.insert(1);

c)

Array_List.put(1);

d)

Array_List.append(1);

11.

Which keyword is used to declare an interface in Java?

a)

interface

b)

class

c)

abstract

d)

implements

12.

How can a class in Java realize an interface?

a)

By using the extends keyword.

b)

By using the implements keyword followed by the interface name.

c)

By directly inheriting from the interface class.

d)

By using the implements keyword followed by the class name.

13.

Which of the following is true about interfaces in Java?

a)

A class can implement multiple interfaces.

b)

An interface can implement another interface.

c)

An interface can extend a class.

14.

How many interfaces can a class implement in Java?

a)

Only one

b)

Two

c)

Three

d)

Unlimited

15.

What is the primary purpose of using interfaces in Java?

a)

To enforce multiple inheritance.

b)

To provide a way for classes to share common method signatures without sharing implementation details.

c)

To restrict access to certain methods and variables.

d)

To allow classes to have multiple constructors.

16.

What keyword tells Java that a class uses an interface?

a)

interface

b)

implements

c)

extends

d)

protected

17.

Can an interface in Java have method implementations?

a)

No, interfaces are only for method signatures.

b)

No, interfaces cannot have method implementations.

c)

Yes, but only if the interface is abstract.

d)

Yes, using default methods.

18.

What happens if a class does not implement all methods of an interface it implements?

a)

The class will throw a runtime exception.

b)

The class must be declared abstract.

c)

The class will compile successfully.

d)

The class will automatically implement default methods.

19.

Which of the following is a valid way to declare an interface in Java?

a)

public interface MyInterface { void myMethod(); }

b)

public implements MyInterface { void myMethod(); }

c)

public abstract interface MyInterface { void myMethod(); }

d)

public class MyInterface { void myMethod(); }

20.

What is the default access level of methods in an interface?

a)

public

b)

private

c)

package-private

d)

protected

21.

3. Which of these keywords must be used to inherit a class?

a)

super

b)

this

c)

void

d)

extends

22.

In Java, which keyword is used to refer to the parent class of a class?

a)

parent

b)

super

c)

this

d)

base

23.

5. Which of these syntaxes is the correct way of how the class Dog  inherit from the class Animal?

a)

class Dog + class Animal {}

b)

class Dog inherits class Animal {}

c)

class Dog extends Animal {}

d)

class Dog extends class Animal {}

24.
Inheritance is used when the relationship between two classes is a(n) _______ relationship.
a)
"HAS-A"
b)
"PART-OF-A"
c)
"IS-A"
d)
inverse
25.

A class that is inherited is called a ______ .

a)

superclass

b)

Subclass

c)

subsetclass

d)

Relativeclass

26.

3. Which of these keywords must be used to inherit a class?

a)

super

b)

this

c)

void

d)

extends

27.
When related classes have the same ____, they can be used interchangeably in a program.
a)
value
b)
definition
c)
interface
d)
hierarchy
28.
A(n) ____________________ method cannot be overridden by a subclass.
a)
final
b)
private
c)
public
d)
static
29.

Output of following Java Program?

a)

10

b)

20

c)

Compilation Error

d)

Run Time Error

30.

What will be the output of the following program?

a)

ONE

b)

TWO

c)

THREE

d)

COMPILE ERROR

31.

When the type of a method is defined as void, this indicates...

a)

the method returns zero

b)

the method returns nothing

c)

the method returns void

d)

the method returns 100

32.

What will the output of this code be?

a)

11

b)

24

c)

Error

d)

Product

33.

In the following code, what is the name of the method?


public void squared(int num1)

a)

public

b)

void

c)

squared

d)

num1

34.

What goes in the parentheses after a method name?

a)

input

b)

integers

c)

parameters

d)

consoles

35.

Which is a properly written method with four parameters?

a)

public void math(int a, int b)

b)

public void math(double a, double b, double c, double d)

c)

public void math(a, b, c, d)

d)

public void math(4, 3, 2, 1)

36.

What is the output of this code?

a)

3 4 5 6 7

b)

7 6 5 4 3

c)

Nothing

d)

Error

37.

what is the name of the method?

a)

AgeCalc

b)

year

c)

return

d)

age

38.

List all parameters that exist in this program segment:-

a)

double point, String student

b)

point, student

c)

name, mark

d)

name[i], mark[i]

39.

Which keyword is used to indicate that a method should not return a value?

a)

double

b)

void

c)

int

d)

String

40.

Why use methods

a)

define the code once, and use it once

b)

so our program looks clean

c)

it is the only way to program

d)

define the code once,and use it many times

41.

What is a lambda expression in Java?

a)

A method that takes no parameters and returns no value.

b)

A concise way to represent an anonymous function.

c)

A class that implements an interface.

d)

A variable that holds a constant value.

42.

Which of the following is the correct syntax for a lambda expression in Java?

a)

`(int a, int b) -> a + b`

b)

`int a, int b -> a + b`

c)

`(int a, int b) { return a + b; }`

d)

`a, b -> a + b`

43.

What is the primary use of lambda expressions in Java?

a)

To define a new class.

b)

To implement interfaces with multiple methods.

c)

To enable functional programming by passing behavior as parameters.

d)

To create a new package.

44.

Which of the following interfaces is commonly used with lambda expressions in Java?

a)

Serializable

b)

Runnable

c)

Comparable

d)

FunctionalInterface

45.

Consider the following lambda expression: `(x) -> x * x`. What is the return type if `x` is an integer?

a)

void

b)

int

c)

double

d)

String

46.

Which of the following is NOT a characteristic of lambda expressions in Java?

a)

They can have zero or more parameters.

b)

They can have a body consisting of a single expression or a block of code.

c)

They can be used to create new classes.

d)

They can return a value.

47.

What is the output of the following code snippet? ```java List list = Arrays.asList("a", "b", "c"); list.forEach(s -> System.out.print(s.toUpperCase())); ```

a)

abc

b)

ABC

c)

a b c

d)

A B C

48.

Which of the following lambda expressions correctly implements the `Comparator` interface for sorting integers in ascending order?

a)

`(a, b) -> a.compareTo(b)`

b)

`(a, b) -> Integer.compare(a, b)`

c)

`(a, b) -> a - b`

d)

`(a, b) -> b - a`

49.

In the context of lambda expressions, what does the term "effectively final" mean?

a)

A variable that is declared as final.

b)

A variable that is not modified after its initial assignment.

c)

A variable that is used only within a single method.

d)

A variable that is declared within a lambda expression.

50.

Which of the following is a benefit of using lambda expressions in Java?

a)

They increase the verbosity of the code.

b)

They allow for the creation of new data types.

c)

They enable more readable and maintainable code.

d)

They require more memory to execute.