This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Which statement about Java annotations is correct?
Option B
Annotations in Java are special forms of metadata that provide additional information about program elements such as classes, methods, fields, parameters, and constructors.
They begin with the @ symbol and can be used by the compiler, development tools, frameworks, or the JVM, depending on the annotation and its retention policy.
The @Override annotation is one of the most commonly used built-in annotations. It is placed before a method that is intended to override a method inherited from a superclass or implement a method declared in an interface.
The compiler checks this intention and reports a compilation error if the method does not actually override an inherited method.
E.g.:
class Parent
{
void display()
{
System.out.println("Parent");
}
}
class Child extends Parent
{
@Override
void display()
{
System.out.println("Child");
}
}
Here, Child overrides the display() method of Parent. The @Override annotation tells the compiler that this is intentional. If the method name were accidentally written incorrectly as:
@Override
void disply()
{
}
The compiler would report an error because disply() does not override a method from the parent class.
Annotation Retention:
Annotation retention defines how long an annotation is retained and where it is available during the Java program's lifecycle.
Java provides the @Retention annotation to specify the retention policy of an annotation. There are three retention policies in Java: SOURCE, CLASS, and RUNTIME.
| Retention Policy | Description |
|---|---|
| SOURCE | The annotation is available only in the source code. It is discarded by the compiler and is not stored in the compiled .class file. |
| CLASS | The annotation is stored in the compiled .class file, but it is generally not available to the program at runtime through reflection. This is the default retention policy when no retention policy is explicitly specified. |
| RUNTIME | The annotation is stored in the .class file and remains available at runtime. It can be accessed using Java reflection API. |
Q: 2Assertion (A) : The garbage collector can not free objects holding other non-object resources.
Reason (R) : In Java, finalizer method is used to free object holding other non-object resources.
Option B
The Garbage Collector (GC) in Java automatically identifies objects that are no longer reachable and reclaims the memory occupied by those objects.
However, an object may be holding resources that are outside the Java memory system, such as a file, database connection, socket, or operating-system resource. The garbage collector does not directly release such external resources.
Therefore, Assertion (A) is true in the intended sense: GC can reclaim the Java object, but it cannot itself properly manage or release the external/non-object resource held by that object.
The Reason (R) is also true in the traditional Java context. The finalize() method was historically provided as a mechanism through which cleanup of resources associated with an object could be attempted before the object was reclaimed.
However, R does not correctly explain A. The reason GC cannot directly free non-object resources is that garbage collection manages Java object memory, not external resources. The finalize() method was a separate cleanup mechanism, it was not the reason why GC itself cannot manage those resources.
Q: 3In Java, which one of the following is used to perform clean up processing just before garbage collection?
Option B
In Java, Garbage Collection is the process that automatically reclaims memory used by objects that are no longer reachable. Before actually freeing the memory of an object, the Java runtime may give the object a chance to clean up resources such as files, network connections, or database handles.
This cleanup is done by a special method, the finalize() method, which is defined in the Object class. When an object becomes eligible for garbage collection, the garbage collector calls finalize() on that object so it can perform cleanup just before being destroyed.
Q: 4What is the role of content Providers in Android?
Option C
In Android, Content Providers are mainly used to share data between applications in a secure and structured way. They provide a standard interface for accessing and managing data like contacts, images, videos, etc., and apps can query or modify this data using URIs.
Q: 5What is the output of following Java code?
List<Integer> list = List.of(1, 2, 3, 4);
long count = list.stream().filter(x -> x%2==0).count();
Option B
In Java, the Stream API provides a convenient way to process elements of a collection using operations such as filtering, sorting, mapping, and counting. In the given code, the list contains four integers: 1, 2, 3, and 4.
List<Integer> list = List.of(1, 2, 3, 4);
long count = list.stream().filter(x -> x % 2 == 0).count();
The stream() method creates a stream from the list. The filter() method then checks each element against the condition x%2==0. This condition is true only for even numbers because an even number leaves a remainder of 0 when divided by 2.
Therefore, 1 and 3 are removed because they are odd, while 2 and 4 satisfy the condition and remain in the stream. The stream therefore contains only 2 and 4.
Finally, the count() method counts the elements remaining after filtering. Since there are two even numbers, the value of count is 2.
The count() method returns a value of type long, which is why the variable is declared as long count.
Q: 6Which statement about Java generics is correct?
Option C
Generics in Java allow classes, interfaces, and methods to work with a specified type while providing type checking at compile time. They help prevent inappropriate types from being inserted into a collection and reduce the need for explicit type casting.
E.g.:
List<String> names = new ArrayList<>();
names.add("Suresh");
// names.add(101); // Compile-Time Error
Here, List<String> specifies that the list should contain String objects. The compiler checks this restriction, so attempting to add an Integer produces a compile-time error.
Java implements generics using Type Erasure. In general, generic type arguments such as String and Integer are not retained as concrete generic type information in the runtime representation of the class.
Therefore, List<String> and List<Integer> are not separate runtime classes, both use the List type at runtime.
Primitive types such as int, char, and double cannot be used directly as generic type arguments. Their corresponding wrapper classes must be used instead.
Note:
Type Erasure means that the compiler uses generic type information to perform type checking during compilation, and the generic type information is generally erased from the runtime representation.
Q: 7What is garbage collection in the context of Java?
Option C
Garbage collection in Java is an automatic memory management process performed by the Java Virtual Machine (JVM).
When an object is no longer reachable by a running program, its memory becomes eligible for reclamation by the garbage collector. This helps manage heap memory and reduces the need for programmers to release object memory manually.
However, garbage collection does not guarantee that memory will be reclaimed immediately.
Q: 8What is the primary purpose of using Intents in Android?
Option C
In Android, Intents are used as a messaging object that allows communication between different components of an app such as Activities, Services, and Broadcast Receivers, or even between different apps.
Intents can be used to start a new activity, launch a service, deliver a broadcast, or request an action like opening a web page or sharing content.
Q: 9Which statement about a Java record is correct?
Option B
A record in Java is a special kind of class designed mainly for representing simple data-carrying objects. Records reduce the amount of boilerplate code that normally has to be written for classes whose primary purpose is to store data.
E.g.:
record Student(int rollNo, String name)
{
}
The record automatically provides important members related to its components, including accessor methods, equals(), hashCode(), and toString().
You have reached the end of this topic. Continue learning with the next topic below.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.