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: 1Read the following code snippet and answer
class Rectangle
{
private int length;
private int breadth;
public int area=0;
public Rectangle()
{
this.length=0;
this.breadth=0;
this.area=0;
}
public Rectangle(int l, int b)
{
this.length=l;
this.breadth=b;
this.area=l*b;
}
public static void main(String a[])
{
Rectangle A = new Rectangle(20,5);
System.out.println(A.area);
}
}
Option B
In this program, the class Rectangle contains:
There are two constructors:
Default Constructor : Sets all values to 0.
Parameterized constructor : Assigns length=l, breadth=b, and calculates area=l*b.
When the statement Rectangle A=new Rectangle(20,5); is executed, the parameterized constructor is called because two arguments are passed. Inside this constructor, length is assigned the value 20, breadth is assigned 5, and area is calculated as l*b, which becomes 20*5 = 100.
After object creation, System.out.println(A.area); prints the value stored in the instance variable area, i.e., 100.
There is no compilation or runtime error because the variable area is properly declared as public int area and initialized.
Q: 2What does the following code do?
public class Example {
private int var;
public Example(int var) { this.var = var; }
}
Option C
In this code, the class Example has a private instance variable named var. The constructor Example(int var) receives a parameter with the same name. Inside the constructor, the statement this.var = var; is used to distinguish between the instance variable and the constructor parameter.
Here, this.var refers to the instance variable of the current object, while var refers to the parameter. Therefore, the code simply assigns the value passed into the constructor to the object's own variable.
Q: 3Choose the incorrect statement—
Option D
In Java, the final keyword is used to restrict modification and ensure immutability where needed. A method declared as final cannot be overridden in subclasses, which helps in maintaining consistent behavior.
class Parent {
final void show() {
System.out.println("This is a final method");
}
}
class Child extends Parent {
void show() {} // Compile-Time Error.
}A class declared as final cannot be inherited, which is useful for creating immutable classes or preventing extension.
final class MyClass {
int value = 100;
}
class NewClass extends MyClass { } //Compile-Time Error.If a variable is declared final, its value cannot be changed, regardless of whether it is inside a final method.
final int p = 33;
final void modifyValue() {
p = 20; //Compile-Time Error.
}The finalize() method in Java is a special method of the Object class that is called by the garbage collector before an object is removed from memory. It can be used to perform cleanup tasks, such as closing file streams, releasing network connections, or freeing other system resources held by the object.
class MyClass {
protected void finalize()throws Throwable {
System.out.println("Object is being Garbage Collected");
}
}
Q: 4What is Encapsulation in Java?
Option C
Encapsulation in Java is one of the fundamental principles of Object-Oriented Programming (OOP). An act of combining data (variables) and the methods that operate on that data into a single unit called a class.
Through encapsulation, the internal details of an object are hidden from the outside world, allowing controlled access via getter and setter methods. This helps maintain data security, ensures better control over the data, and prevents unintended modification.
| CONCEPT | DESCRIPTION | KEY FEATURES |
|---|---|---|
| Encapsulation | The OOP principle of combining data (variables) and methods (functions) into a single unit and restricting direct access to it. |
|
| Class | A blueprint or template from which objects are created. It defines properties (variables) and behaviors (methods). |
|
| Object | An instance of a class that has its own identity, state, and behavior. It occupies memory. |
|
Q: 5A class that exhibits a high degree of primitiveness encapsulates only ______________ operations.
Option B
In object-oriented programming, a class is a blueprint for creating objects and can encapsulate (combine) data (variable) and operations (methods). The degree of primitiveness of a class refers to how basic or elementary its responsibilities are.
Q: 6Which of the following is a correct statement about constructors in Java?
Option C
In Java, a constructor is a special block of code that is automatically executed when an object of a class is created. Its main purpose is to initialize the object, that is, to assign initial values to variables or run setup code.
class MyClass
{
MyClass() //This is a constructor.
{
System.out.println("Constructor called.");
}
void MyClass() //Not a constructor, this is a method.
{
System.out.println("This is a method.");
}
}
Q: 7What is the purpose of the ‘new’ keyword in Java?
Option C
In Java, the new keyword is used to create new objects at runtime. When a class is defined, it serves as a blueprint, but no memory is allocated until an object of that class is created. The new keyword allocates memory for the object on the heap and calls the constructor of the class to initialize it.
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.