NEW
Font size
WorksheetsOPP_DA_midTerm v2
Total questions: 50
Worksheet time: 27mins
Which of the following statements is a valid declaration of an object that belongs to 'MyClass'?
new MyClass obj;
obj = new MyClass();
MyClass obj = new MyClass;
MyClass obj = new MyClass();
<class name><Object name>=new <class name>();
This is syntax of creating ______.
class
object
method
function
The insertion order is not retained in a Set because:
Sets are unordered collections.
Sets automatically sort elements.
Sets use a hash table for storage.
Sets do not allow duplicate elements.
Create an ArrayList with a size of 100. Use the code snippet provided as a reference.
ArrayList
ArrayList
ArrayList
ArrayList
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
Tom
Mary
Peter
raises Exception
A list is a sequential collection in which each element has a(n) ________.
integer index.
unique identifier.
random value.
fixed size.
The purpose of the equals(Object o) method in Java is to:
compare two objects for equality
compare two objects for memory address
convert an object to a string
initialize an object
The isEmpty() method in Java:
Checks if a string is empty or not
Removes all elements from a collection
Returns the size of a collection
Checks if a file is empty
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
[10, 20, 30, 1, 2]
[10, 30, 1, 2]
[10, 1, 2]
[10, 2]
Which of the following is the correct way to add an element to an ArrayList in Java?
Array_List.add(1);
Array_List.insert(1);
Array_List.put(1);
Array_List.append(1);
Which keyword is used to declare an interface in Java?
interface
class
abstract
implements
How can a class in Java realize an interface?
By using the extends keyword.
By using the implements keyword followed by the interface name.
By directly inheriting from the interface class.
By using the implements keyword followed by the class name.
Which of the following is true about interfaces in Java?
A class can implement multiple interfaces.
An interface can implement another interface.
An interface can extend a class.
How many interfaces can a class implement in Java?
Only one
Two
Three
Unlimited
What is the primary purpose of using interfaces in Java?
To enforce multiple inheritance.
To provide a way for classes to share common method signatures without sharing implementation details.
To restrict access to certain methods and variables.
To allow classes to have multiple constructors.
What keyword tells Java that a class uses an interface?
interface
implements
extends
protected
Can an interface in Java have method implementations?
No, interfaces are only for method signatures.
No, interfaces cannot have method implementations.
Yes, but only if the interface is abstract.
Yes, using default methods.
What happens if a class does not implement all methods of an interface it implements?
The class will throw a runtime exception.
The class must be declared abstract.
The class will compile successfully.
The class will automatically implement default methods.
Which of the following is a valid way to declare an interface in Java?
public interface MyInterface { void myMethod(); }
public implements MyInterface { void myMethod(); }
public abstract interface MyInterface { void myMethod(); }
public class MyInterface { void myMethod(); }
What is the default access level of methods in an interface?
public
private
package-private
protected
3. Which of these keywords must be used to inherit a class?
super
this
void
extends
In Java, which keyword is used to refer to the parent class of a class?
parent
super
this
base
5. Which of these syntaxes is the correct way of how the class Dog inherit from the class Animal?
class Dog + class Animal {}
class Dog inherits class Animal {}
class Dog extends Animal {}
class Dog extends class Animal {}
A class that is inherited is called a ______ .
superclass
Subclass
subsetclass
Relativeclass
3. Which of these keywords must be used to inherit a class?
super
this
void
extends
Output of following Java Program?
10
20
Compilation Error
Run Time Error
What will be the output of the following program?
ONE
TWO
THREE
COMPILE ERROR
When the type of a method is defined as void, this indicates...
the method returns zero
the method returns nothing
the method returns void
the method returns 100
What will the output of this code be?
11
24
Error
Product
In the following code, what is the name of the method?
public void squared(int num1)
public
void
squared
num1
What goes in the parentheses after a method name?
input
integers
parameters
consoles
Which is a properly written method with four parameters?
public void math(int a, int b)
public void math(double a, double b, double c, double d)
public void math(a, b, c, d)
public void math(4, 3, 2, 1)
What is the output of this code?
3 4 5 6 7
7 6 5 4 3
Nothing
Error
what is the name of the method?
AgeCalc
year
return
age
List all parameters that exist in this program segment:-
double point, String student
point, student
name, mark
name[i], mark[i]
Which keyword is used to indicate that a method should not return a value?
double
void
int
String
Why use methods
define the code once, and use it once
so our program looks clean
it is the only way to program
define the code once,and use it many times
What is a lambda expression in Java?
A method that takes no parameters and returns no value.
A concise way to represent an anonymous function.
A class that implements an interface.
A variable that holds a constant value.
Which of the following is the correct syntax for a lambda expression in Java?
`(int a, int b) -> a + b`
`int a, int b -> a + b`
`(int a, int b) { return a + b; }`
`a, b -> a + b`
What is the primary use of lambda expressions in Java?
To define a new class.
To implement interfaces with multiple methods.
To enable functional programming by passing behavior as parameters.
To create a new package.
Which of the following interfaces is commonly used with lambda expressions in Java?
Serializable
Runnable
Comparable
FunctionalInterface
Consider the following lambda expression: `(x) -> x * x`. What is the return type if `x` is an integer?
void
int
double
String
Which of the following is NOT a characteristic of lambda expressions in Java?
They can have zero or more parameters.
They can have a body consisting of a single expression or a block of code.
They can be used to create new classes.
They can return a value.
What is the output of the following code snippet?
```java
List
abc
ABC
a b c
A B C
Which of the following lambda expressions correctly implements the `Comparator` interface for sorting integers in ascending order?
`(a, b) -> a.compareTo(b)`
`(a, b) -> Integer.compare(a, b)`
`(a, b) -> a - b`
`(a, b) -> b - a`
In the context of lambda expressions, what does the term "effectively final" mean?
A variable that is declared as final.
A variable that is not modified after its initial assignment.
A variable that is used only within a single method.
A variable that is declared within a lambda expression.
Which of the following is a benefit of using lambda expressions in Java?
They increase the verbosity of the code.
They allow for the creation of new data types.
They enable more readable and maintainable code.
They require more memory to execute.
